From patchwork Thu Aug 29 17:42:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13783491 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57B4CC8303E for ; Thu, 29 Aug 2024 17:42:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA4676B009E; Thu, 29 Aug 2024 13:42:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D06856B00A4; Thu, 29 Aug 2024 13:42:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B59476B00AB; Thu, 29 Aug 2024 13:42:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 903156B009E for ; Thu, 29 Aug 2024 13:42:40 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0B9F4140C05 for ; Thu, 29 Aug 2024 17:42:40 +0000 (UTC) X-FDA: 82506002880.09.23125B7 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf12.hostedemail.com (Postfix) with ESMTP id 4578040005 for ; Thu, 29 Aug 2024 17:42:36 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sXxDx64F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of andrii@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724953285; a=rsa-sha256; cv=none; b=JBtNzcjOt7FZMMhjLDTzP+YOsUzm/2OHQsNqMVhLh5GROelkbAqp2Tb9Z0i8pYle/bJXiq BwFWyFHcWbRxhv7+uR97P1CyVNl+77fMhjLn+ONv/CuRs3Al/LLn49P2kwuedm0/BWwRaD 9xBcdF82+jJgL0VyavcEVfm2dujAp+s= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=sXxDx64F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of andrii@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724953285; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=KpP2i1a6w0vPorW7LQOdEnmewbG9zAF/Zaba6a731RM=; b=31FetIkUQIM4MYfntUZi1yA9JgoaonnV8df7w/okNsvT0brA8HmG99Ry8MDYyXkoaU+X3f NdJM35Z/cm58ub6rsINDXoBhe94IfXyLYGz3ljO83fbTA08bxrqKoXkmBCx3WLN3QGOw0N RhVbBuk2Hqd7285VtPGt/CsVtbcWrjk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 028CEAE08AC; Thu, 29 Aug 2024 17:42:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CBEDEC4CEC1; Thu, 29 Aug 2024 17:42:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724953355; bh=4nRg3RUz99sC+bTAc3chTieWxnU9HfYtyRRpqpRzGvU=; h=From:To:Cc:Subject:Date:From; b=sXxDx64F2XQO2t8V77kyt59L27JoWD8GMWr61BdbjjrAZZGYGgaqHh6fx2PpwQ3d1 E4t+y1ltBEWWFYrmF91WTaWN38IUlWaKWfMrjVQE3h3oRrWkO3ij4/L6MeJrkKOQzZ fUVNo6HRhxPSrVqEi7+2XRLg6NK/Q+AFMVxzsuVqnyP4qX1d3wsCgSaXV5crNgIRF6 04IyWUz7zEh3E0dXQSbr/N30uIldZNt+01v2zjUfZQ6ZJsCSbPSbNGgLqnLamddWil D8atG92lLn/sfdaunkbjhIwBtDuYrNFs4Tv12hbWy6+zLl91F2stOF9urQBP+Uv6xo /gPwqpmb9F21Q== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, adobriyan@gmail.com, shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com, osandov@osandov.com, song@kernel.org, jannh@google.com, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v7 bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Thu, 29 Aug 2024 10:42:22 -0700 Message-ID: <20240829174232.3133883-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4578040005 X-Stat-Signature: cr13uyou9ecr1x7k53byxizzz3cwiaya X-Rspam-User: X-HE-Tag: 1724953356-19889 X-HE-Meta: U2FsdGVkX19TNKKPvjYrwGsLRfJvW04SL6r6wIW3O290lhFU5gQVzZZF+7dIISbxjKTBBwtW2z5e/A9rAfFn6MIcckcI3ROr2YYH0Y0JFIuu1epsnsrpBUwRiUUTwrxtHHtfvrNhRcozWrlxcr7TeyDr9kKqc3yt27MZ2uC8RSTHdYJvV4TH/rsRo/msS8U/FSxC11vZiHlUlZfAj6MN9OQQ4DWYTvrOT8j+p1KhsP+R0sWzgCra5F1oWp7ghd++skTPo4MB7pFaZcugZwPQ3SXOQAG3QQw4oxSVTX5ERyl5Ub/jeTtVedyHCJLRRsGdU+IQ4cPsVkB6yBEIxmTjdXr+bmYWjmh2u8UP+bl7CUX7HgMq5+4/Quqw5agBwkpv22lCQjUV1fc6lPntsEJjXlQFPhXj5M26mjWvQT8q0VHfCUuDXYzXEXf8WDrtrGl3eux5o66wlVRdGgli2MuSHWgbxeKKTa+aT9vdKGj/Dc9xzsj6/SV/ID7nnQdNeHrWuqsDxdtjuhqVl6LYOSdrN292nByAKM9ExyEhLSgRapzX6GX+rpovgq8xLH8UKTLS0M1ZoAyaUGw76/Nwbkw+Y6VKW1hbmwp25VZ3lLFrqifR6j6m6SMK2IECd2XQrZMZvgJ4Z2bLOGqsHbmJWtvGYGHOvpH0F+0qsp0eH/kwLlLkNmqtgcv/xs7DPju5sF7Pdp2mZDWGi3pMRWs5fQ4wmK0nuA+raYfIoncBumDbi8kV72i31KiN6qhLhOVVWQfp1Tr8QdUNJFH8TcfME4Jo/d0aSEQJjY/BCAbBg89WRslZ3gLFRCS3IM8PSQTQLms8vamU4A5UMfZKnvHFNT1XYhWbvuA76VLXqqJu5FtUGCmGZgV2rSV+GTst5Ym2lm/IK34yZ3CiLDu6GulpW0e/iSos3fB7fYPNIJgL2/TRdlaLqsabTFnkX5EP3sQVyPy2FnaB8+rmrwCarbrzPoX UzzH8T+N bbIfGQDde3S4IinV3a2W+L3aYcsbWJ3SnIMJEdFe12gLTlKsx+lMNAz2KPLSf3Qspotuct5xh2tlV7HrW2Qj7jNU+CjvCuaOmyNJEn0PgvcgjVJdtnWzqfv+AXLMcaUr0YC9L8UWpUnLSeb0iPCRU3jrxqy3U51SjdKdHIADF7FKc3niyhV5tAYj8ReVpcVHcVyY9s4MNw1xEwUfiZ1xBL15A/ZQg9HxZVL+e8w0psxwCYQQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The goal of this patch set is to extend existing ELF build ID parsing logic, currently mostly used by BPF subsystem, with support for working in sleepable mode in which memory faults are allowed and can be relied upon to fetch relevant parts of ELF file to find and fetch .note.gnu.build-id information. This is useful and important for BPF subsystem itself, but also for PROCMAP_QUERY ioctl(), built atop of /proc//maps functionality (see [0]), which makes use of the same build_id_parse() functionality. PROCMAP_QUERY is always called from sleepable user process context, so it doesn't have to suffer from current restrictions of build_id_parse() which are due to the NMI context assumption. Along the way, we harden the logic to avoid TOCTOU, overflow, out-of-bounds access problems. This is the very first patch, which can be backported to older releases, if necessary. We also lift existing limitations of only working as long as ELF program headers and build ID note section is contained strictly within the very first page of ELF file. We achieve all of the above without duplication of logic between sleepable and non-sleepable modes through freader abstraction that manages underlying folio from page cache (on demand) and gives a simple to use direct memory access interface. With that, single page restrictions and adding sleepable mode support is rather straightforward. We also extend existing set of BPF selftests with a few tests targeting build ID logic across sleepable and non-sleepabe contexts (we utilize sleepable and non-sleepable uprobes for that). [0] https://lore.kernel.org/linux-mm/20240627170900.1672542-4-andrii@kernel.org/ v6->v7: - added filemap_invalidate_{lock,unlock}_shared() around read_cache_folio and kept Eduard's Reviewed-by (Eduard); v5->v6: - use local phnum variable in get_build_id_32() (Jann); - switch memcmp() instead of strcmp() in parse_build_id() (Jann); v4->v5: - pass proper file reference to read_cache_folio() (Shakeel); - fix another potential overflow due to two u32 additions (Andi); - add PageUptodate() check to patch #1 (Jann); v3->v4: - fix few more potential overflow and out-of-bounds access issues (Andi); - use purely folio-based implementation for freader (Matthew); v2->v3: - remove unneeded READ_ONCE()s and force phoff to u64 for 32-bit mode (Andi); - moved hardening fixes to the front for easier backporting (Jann); - call freader_cleanup() from build_id_parse_buf() for consistency (Jiri); v1->v2: - ensure MADV_PAGEOUT works reliably by paging data in first (Shakeel); - to fix BPF CI build optionally define MADV_POPULATE_READ in selftest. Andrii Nakryiko (10): lib/buildid: harden build ID parsing logic lib/buildid: add single folio-based file reader abstraction lib/buildid: take into account e_phoff when fetching program headers lib/buildid: remove single-page limit for PHDR search lib/buildid: rename build_id_parse() into build_id_parse_nofault() lib/buildid: implement sleepable build_id_parse() API lib/buildid: don't limit .note.gnu.build-id to the first page in ELF bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers selftests/bpf: add build ID tests include/linux/bpf.h | 2 + include/linux/buildid.h | 4 +- kernel/bpf/stackmap.c | 131 ++++-- kernel/events/core.c | 2 +- kernel/trace/bpf_trace.c | 5 +- lib/buildid.c | 397 +++++++++++++----- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/prog_tests/build_id.c | 118 ++++++ .../selftests/bpf/progs/test_build_id.c | 31 ++ tools/testing/selftests/bpf/uprobe_multi.c | 41 ++ tools/testing/selftests/bpf/uprobe_multi.ld | 11 + 11 files changed, 605 insertions(+), 142 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/build_id.c create mode 100644 tools/testing/selftests/bpf/progs/test_build_id.c create mode 100644 tools/testing/selftests/bpf/uprobe_multi.ld