Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3798967pxb; Mon, 4 Oct 2021 09:50:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUsbyhyE1ztICtvdoOyMY62wXvYOGnr5IniSLjt3IEJpU8mk0M9+wF8ZIUGVDXAt4sF4L+ X-Received: by 2002:a17:902:b205:b0:13d:b0a1:da90 with SMTP id t5-20020a170902b20500b0013db0a1da90mr680358plr.26.1633366230190; Mon, 04 Oct 2021 09:50:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633366230; cv=none; d=google.com; s=arc-20160816; b=GB1Cjf3XxQCvZyofwYq5WQKzIgM27Zpdy+Z5s63yenxyjGPKK2EAcvtN9MTMVlnON7 eRYem6ioK2I9wvhQwnwlAdRmtb0oipnKyveDAex7EH5NE62u/2uCytp+lnUMpc2FQLmt z0zxNx6kYo0auJDU71nMyTSA3+EZOf703bbQ0ouLIKhBRAx/Qaep1O5ssL+5ekSo2Axs ePsf33f7FJAc0OwqMMFQCj6tENcDIT0TcGrBqLmdF7eWkRJ2jkMiRyWR4M1tiVuDu1ig 1q/Utm/2WwLga5mQBA4eJ22mZil3beAxo5ZEbxIl+HOBZ8naVp+FjgNd7ixRKRL6I0Ng 6h6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xlX9wGS18FmySNJTysKTMkNew/3g5zDSjSrIX/kP0T0=; b=g6c9nqx0L1XHUIfAaeLQGvScmIzc6GAnhF+ncr+g/4YtKPzejytdP/ATFCWD7q4y2P FPielhIhe4KX3of8ty+Oh6WABBHeZdirn8D9yPoQlt0TwSp/ZJHK8aLd3nrQ3k940n8W FFkQYDSYSnNSNRivNyVTD31JxT9jVrJP7OT4Mk3XR6G6iP+NlU2Xj+hvjNrwXkrjX2Wh HAYaKYOY/dj50IMcn7ZwA2CLKWb64XTY7rEf/W5pAPsFDL37pu5D+WafQwofL9GXCPh+ WpL2jIy+EusBbuw8ZDieW0bWjUQo0O2PnNDNAdXFv+/rK8C4ocUYiNbVPai6B3fOfQDh KU6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tGao5fDQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lb18si25112782pjb.165.2021.10.04.09.50.15; Mon, 04 Oct 2021 09:50:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tGao5fDQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238115AbhJDNjv (ORCPT + 99 others); Mon, 4 Oct 2021 09:39:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:51966 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238153AbhJDNhI (ORCPT ); Mon, 4 Oct 2021 09:37:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1D3CB6124B; Mon, 4 Oct 2021 13:16:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633353405; bh=KMlCW3ihomlYIXeJo//98hTRu56y/+zQROHmuNdSJxs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tGao5fDQOLiNRM2qS1FbavaTBSS4P6myoWir72up5/k4Qcwsjb4Ftf7iDC9iNbFcn 2MqOvfFIZsNLTzaUeqaYHh2YpMt3gaHI5I2gwnHUCGqMGEcQJKRU85x3ifqnwMDgeK v4luCo4+8OGFd2acvymW5gqiX5C+s9JQgl8eA2ag= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kumar Kartikeya Dwivedi , Andrii Nakryiko , Daniel Borkmann , Sasha Levin Subject: [PATCH 5.14 115/172] libbpf: Fix segfault in static linker for objects without BTF Date: Mon, 4 Oct 2021 14:52:45 +0200 Message-Id: <20211004125048.693992509@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211004125044.945314266@linuxfoundation.org> References: <20211004125044.945314266@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kumar Kartikeya Dwivedi [ Upstream commit bcfd367c2839f2126c048fe59700ec1b538e2b06 ] When a BPF object is compiled without BTF info (without -g), trying to link such objects using bpftool causes a SIGSEGV due to btf__get_nr_types accessing obj->btf which is NULL. Fix this by checking for the NULL pointer, and return error. Reproducer: $ cat a.bpf.c extern int foo(void); int bar(void) { return foo(); } $ cat b.bpf.c int foo(void) { return 0; } $ clang -O2 -target bpf -c a.bpf.c $ clang -O2 -target bpf -c b.bpf.c $ bpftool gen obj out a.bpf.o b.bpf.o Segmentation fault (core dumped) After fix: $ bpftool gen obj out a.bpf.o b.bpf.o libbpf: failed to find BTF info for object 'a.bpf.o' Error: failed to link 'a.bpf.o': Unknown error -22 (-22) Fixes: a46349227cd8 (libbpf: Add linker extern resolution support for functions and global variables) Signed-off-by: Kumar Kartikeya Dwivedi Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Link: https://lore.kernel.org/bpf/20210924023725.70228-1-memxor@gmail.com Signed-off-by: Sasha Levin --- tools/lib/bpf/linker.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 10911a8cad0f..2df880cefdae 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -1649,11 +1649,17 @@ static bool btf_is_non_static(const struct btf_type *t) static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name, int *out_btf_sec_id, int *out_btf_id) { - int i, j, n = btf__get_nr_types(obj->btf), m, btf_id = 0; + int i, j, n, m, btf_id = 0; const struct btf_type *t; const struct btf_var_secinfo *vi; const char *name; + if (!obj->btf) { + pr_warn("failed to find BTF info for object '%s'\n", obj->filename); + return -EINVAL; + } + + n = btf__get_nr_types(obj->btf); for (i = 1; i <= n; i++) { t = btf__type_by_id(obj->btf, i); -- 2.33.0