Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3628361pxb; Mon, 24 Jan 2022 13:54:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJyy+uADlOey6navbPx2U/sn7SYGSS1W0u7diU74MYsDSiVnXe+sC7Esi3+JwYHnp3BImDpD X-Received: by 2002:a62:164d:0:b0:4c6:5f10:6aae with SMTP id 74-20020a62164d000000b004c65f106aaemr15564166pfw.86.1643061251605; Mon, 24 Jan 2022 13:54:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643061251; cv=none; d=google.com; s=arc-20160816; b=lAcvrhfNzImPDLU7B56daUMFV5KTFWMDa3viAf3UhCsMbjhpnJQNXuQoRDQ2FgrjC0 HDZWuXSnLckFDeOjQfPHoXKdps4IEKmKNe8jstidmwRocfCyXiZH+/vVA+wJ7Pl0wvQb UWuCly1eUDUkbFbGiZfkGX6u2Ew6ftCo52BJZPdxIB1e1WsDWlqp8KM8n2W9bvpgIkxB pjLIfyoVaOBscS3/JmWkeg3bl2D8JiQXlvoDnakDhxymhGv3IVxx1XYO9tEm8w7aJ5+H gN1jHEljDBQ0oPNYu/P4Y7O4yN24ddzhZAw6xYdrBxh3bU32ov58dDW//67do8m2etXA sr8Q== 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=Yt78g6l+4HfIqgMt2vJfx30Ijktmpj1TUlLvMBuRqfk=; b=nrNAZHmIb4MGkEAXkydsTrQkmVURc2Mx6ODJqNJWYZUmhx0ebFishR5e4JcMCfvOIX dlp7r4E3wE0sN60ZOVg8/fwrse1YhFYNcTEDm3aX6H9blLeh29+1Agc8vZC+xk32aMEz NuQr9Pr21K8iVJ82D900hY7LdGY3biufqq0Tz9yt3U4lkia5ha3yefJSTpkAe3GJy66o KMUfj6OUohcbgCvuPieSJrrq8lAZw3FaVwmXFP6jUl751toeg2x/h3S6RUZCJPSpNJUy ieRpY5e1p9ZCLn7zVLYO7Fzs8COud3F78i/E8ubBdxvZoFeghRi65N6EYNl/VCZI8D7P mVSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ybugiiHT; 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 b4si13676116plz.351.2022.01.24.13.54.00; Mon, 24 Jan 2022 13:54:11 -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=ybugiiHT; 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 S1456697AbiAXVjs (ORCPT + 99 others); Mon, 24 Jan 2022 16:39:48 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:53848 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1444634AbiAXVBO (ORCPT ); Mon, 24 Jan 2022 16:01:14 -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 A5DC061368; Mon, 24 Jan 2022 21:01:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4E10C340E5; Mon, 24 Jan 2022 21:01:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058073; bh=WYGVexxiQzWsmwuFARY5sLnWD4LDm3nwZavgR065GQ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ybugiiHT5Hbw073FMy7P+nlWmCucYzdSzD/sxbU+BPEbVl1y2feW2J6eOc4ZsIHJ5 vJKHw6mgXS37G46UW6K1D3tbKLmlD5nxNgTyW3R/E0aetga0HKdJ1A1B2XF04hv5dT yD+8toHP6A/+wQMu2HgwWqsDTTlay3TpRV2AzOho= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Daniel Borkmann , Sasha Levin Subject: [PATCH 5.16 0173/1039] libbpf: Fix using invalidated memory in bpf_linker Date: Mon, 24 Jan 2022 19:32:42 +0100 Message-Id: <20220124184131.070872399@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 593835377f24ca1bb98008ec1dc3baefe491ad6e ] add_dst_sec() can invalidate bpf_linker's section index making dst_symtab pointer pointing into unallocated memory. Reinitialize dst_symtab pointer on each iteration to make sure it's always valid. Fixes: faf6ed321cf6 ("libbpf: Add BPF static linker APIs") Signed-off-by: Andrii Nakryiko Signed-off-by: Daniel Borkmann Link: https://lore.kernel.org/bpf/20211124002325.1737739-7-andrii@kernel.org Signed-off-by: Sasha Levin --- tools/lib/bpf/linker.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c index 94bdd97859285..6923a0ab3b127 100644 --- a/tools/lib/bpf/linker.c +++ b/tools/lib/bpf/linker.c @@ -2000,7 +2000,7 @@ add_sym: static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj) { struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx]; - struct dst_sec *dst_symtab = &linker->secs[linker->symtab_sec_idx]; + struct dst_sec *dst_symtab; int i, err; for (i = 1; i < obj->sec_cnt; i++) { @@ -2033,6 +2033,9 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob return -1; } + /* add_dst_sec() above could have invalidated linker->secs */ + dst_symtab = &linker->secs[linker->symtab_sec_idx]; + /* shdr->sh_link points to SYMTAB */ dst_sec->shdr->sh_link = linker->symtab_sec_idx; -- 2.34.1