Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3624907pxb; Mon, 24 Jan 2022 13:48:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpZnbHK6sz+/d7RM/xl3xIUXdJRVn+4vFNvbGVx78LsCO18intE+FAV9ZY7tOuRH/wngA0 X-Received: by 2002:a17:902:e888:b0:14b:1ff1:b66a with SMTP id w8-20020a170902e88800b0014b1ff1b66amr14000306plg.159.1643060896936; Mon, 24 Jan 2022 13:48:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643060896; cv=none; d=google.com; s=arc-20160816; b=U/VTdZrCpS9K880eHTIXz2PbO9NfQQtQP91zi4a9T9fQfu9RYRg6c2v2MH3JFcTzsA HvGHIdVs43JYjz0FSgimoaXhke/XQSBthBio44rHoi5A1Wuzjxqrr1Xo7zcY6dvym59q YP2CPPg5L4XfFpQKlx9HsrMWXX5HCfgxYD/3fny+4ohnFpXdoq/u5rCbGiMwamxOlWH5 zbaKmIQZS5TVUz388CyVz9y8qMQB35oo7qm/zCP4e3V5pBUNPYQ4VEU9xaeMX4ElhCPC 2CKq1O0DLBTr1eBKhg6OKs2bSvhTsRhf5OmF/RCahox7muITRyIoiIE6ODdm5sBUUlwK DwCw== 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=Rkvv0PRiHTYF4s2fgif1ACQZ5LmgLWYYue1UzybIAIw=; b=W6CjKRoU8cbH9LAZ9BDqoc1kjd5Fyb2hS/ZM+7jaaPptAiy3NyQp2vCaY0E6hkyiCW glyeSEMyxoyF2qL0JJ1u1K+ts2PqXin9uX/lu2275lpRTJpKqFhmrlR53aYJ2dv8ghw8 veHp9FxopZtRwiZyxnHIWzdpU8WkuFNyR8X5IlGM/oRZgrQvi9Fivk1pCN2eIaGnBqSk Atv6xdOzaH/7ItESYN6lVf8eAnGLbDI1OX3bCRbI6fbOK8r0Csz0SUVP4imMj82bIF54 BnT35QsB5LDOLLrUXV+hR3r1IqOFB3LlQjJpXFj6R8j0HLdMAFhZmyTIoVtwKiQRfIpS cdMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YAOpRqgZ; 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 v23si12440282pgl.345.2022.01.24.13.48.05; Mon, 24 Jan 2022 13:48:16 -0800 (PST) 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=YAOpRqgZ; 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 S1455049AbiAXVek (ORCPT + 99 others); Mon, 24 Jan 2022 16:34:40 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:49266 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443639AbiAXU5z (ORCPT ); Mon, 24 Jan 2022 15:57:55 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5E440611DA; Mon, 24 Jan 2022 20:57:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 47923C340E5; Mon, 24 Jan 2022 20:57:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643057874; bh=KhvIkvCRQAidrDbmtKYuotaLjKwnMHLsfkWCzUzzhO0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YAOpRqgZ21eYE9LPlbvZcVp8ArLkdWRZBLAB+x2CSyJFjgRmgjrJwJ6wmIRxvs9EX CPRLC0Ns0/K/NNANlzkfoOfKdpEB9Ompi4HaVkLHI9j2L26KNiVerji1JN0VSrKemE OLPyeUw/tQ6cmpybsOLp8aCDlLedc4zwU4tGLCNc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , Yonghong Song , Sasha Levin Subject: [PATCH 5.16 0107/1039] libbpf: Fix section counting logic Date: Mon, 24 Jan 2022 19:31:36 +0100 Message-Id: <20220124184128.743283043@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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: Andrii Nakryiko [ Upstream commit 0d6988e16a12ebd41d3e268992211b0ceba44ed7 ] e_shnum does include section #0 and as such is exactly the number of ELF sections that we need to allocate memory for to use section indices as array indices. Fix the off-by-one error. This is purely accounting fix, previously we were overallocating one too many array items. But no correctness errors otherwise. Fixes: 25bbbd7a444b ("libbpf: Remove assumptions about uniqueness of .rodata/.data/.bss maps") Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Acked-by: Yonghong Song Link: https://lore.kernel.org/bpf/20211103173213.1376990-5-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/libbpf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 7c74342bb6680..5367bc8e52073 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3191,11 +3191,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj) Elf_Scn *scn; Elf64_Shdr *sh; - /* ELF section indices are 1-based, so allocate +1 element to keep - * indexing simple. Also include 0th invalid section into sec_cnt for - * simpler and more traditional iteration logic. + /* ELF section indices are 0-based, but sec #0 is special "invalid" + * section. e_shnum does include sec #0, so e_shnum is the necessary + * size of an array to keep all the sections. */ - obj->efile.sec_cnt = 1 + obj->efile.ehdr->e_shnum; + obj->efile.sec_cnt = obj->efile.ehdr->e_shnum; obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs)); if (!obj->efile.secs) return -ENOMEM; -- 2.34.1