From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A98F749A for ; Thu, 12 Jan 2023 14:06:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CDD5BC433EF; Thu, 12 Jan 2023 14:06:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1673532370; bh=JG2fjaTuM+C/WNcZeB7C7R3k7QSjeF1fQ2L85pdVXBs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qUEQzELz1q1Rh6CxOnQ5snLPWwncCAgf6+6omZtkl7YpSpLSdV6Gj44JAYzm5bOc5 bGH5H5wG7pKPywR49++J/T/Lgdm7N2ddpnkqCQbrUKf3J+qSrymJhZMmKL9sPzKY4j ZuiciqsObKDmHwz3BZoUjAf+2Q/6lLEgg/1QSILI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Shung-Hsi Yu , Andrii Nakryiko , Sasha Levin Subject: [PATCH 5.10 108/783] libbpf: Fix null-pointer dereference in find_prog_by_sec_insn() Date: Thu, 12 Jan 2023 14:47:04 +0100 Message-Id: <20230112135529.207303327@linuxfoundation.org> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230112135524.143670746@linuxfoundation.org> References: <20230112135524.143670746@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Shung-Hsi Yu [ Upstream commit d0d382f95a9270dcf803539d6781d6bd67e3f5b2 ] When there are no program sections, obj->programs is left unallocated, and find_prog_by_sec_insn()'s search lands on &obj->programs[0] == NULL, and will cause null-pointer dereference in the following access to prog->sec_idx. Guard the search with obj->nr_programs similar to what's being done in __bpf_program__iter() to prevent null-pointer access from happening. Fixes: db2b8b06423c ("libbpf: Support CO-RE relocations for multi-prog sections") Signed-off-by: Shung-Hsi Yu Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/bpf/20221012022353.7350-4-shung-hsi.yu@suse.com Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 66d7f8d494de..015ed8253f73 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3479,6 +3479,9 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj, int l = 0, r = obj->nr_programs - 1, m; struct bpf_program *prog; + if (!obj->nr_programs) + return NULL; + while (l < r) { m = l + (r - l + 1) / 2; prog = &obj->programs[m]; -- 2.35.1