From patchwork Wed Aug 14 18:54:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763835 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 F358EC52D7F for ; Wed, 14 Aug 2024 18:54:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7262B6B007B; Wed, 14 Aug 2024 14:54:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D59D6B0083; Wed, 14 Aug 2024 14:54:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 59CEC6B0085; Wed, 14 Aug 2024 14:54:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3BB0C6B007B for ; Wed, 14 Aug 2024 14:54:28 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C16F81C48B1 for ; Wed, 14 Aug 2024 18:54:27 +0000 (UTC) X-FDA: 82451751774.02.E8412FA Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf08.hostedemail.com (Postfix) with ESMTP id 76326160025 for ; Wed, 14 Aug 2024 18:54:25 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qHodFtsl; spf=pass (imf08.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723661570; 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=ADA64SGQQSrzGMwCFFRPOdwqgbZLd/YT0DUgYXXvWn0=; b=Aq8PATPtp8aMcmMlDc+ilcVLVD4CGCX9pAV4InhyESo1Dek54R7OMGAgSdrrMTnaOG+j1G I+HsOyrgjheLh7B9xtdJ94kvEEOxl/m0xacfRFtra3Nm78EilR57p1goVJGS98mwm5SXCF DCuYJgKBYqBgAmZwdmsfpIAlTs6P9FY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=qHodFtsl; spf=pass (imf08.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723661570; a=rsa-sha256; cv=none; b=DxtMg0my2X5mPrv5axvXwHgNRx40xEAVpYpZ7176tzmzk2EJ1gHT8PaCOjbFohDDnBNjG5 MU/iECyuZ7wbfGSY4GU8yvatN0JvDAZs3Cmp1I6p0LlSMMKw7dpSEcpGkF1SEUoEfSmiMD 4cLH/r4ZnKAcFOnK28jFINxnatYxL20= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 04596CE1ACB; Wed, 14 Aug 2024 18:54:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03BBBC116B1; Wed, 14 Aug 2024 18:54:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661661; bh=mbghPs69mZVoRPsfVF5bAy9PBdak792WWklTSzxXxKY=; h=From:To:Cc:Subject:Date:From; b=qHodFtsl6O+X7wo7pPDlMvNh80RhbXwpXJDheJVSqqICi14kUY01jOFTFN15R1RWO 6OQzMGIuCdVUpZtIY/dL2OJA10uUbKek+LAOqbBTqWA2NNe1wkZ5ezWCAjpSspwGIw ozDUmS4bzU3tjY9OpQAm5vWr60rpDGCUIncKZ29PJHQfWeDQYjpgXEG+A+Yjd2pDzQ +segpEQVfbdFkD6uTO5dv2aXDl7zx905gH3SNkBAQcv+HTYLuz2a5FCR13gR8KDF8b N7sX1NyDYDfspZQ8S1N1uRAvHsLe3VBUoDitr/RWZ8fwtux+olkuaCuEZ2f68Rx2XP r0WBFvcvVbf9A== 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 v6 bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Wed, 14 Aug 2024 11:54:07 -0700 Message-ID: <20240814185417.1171430-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 76326160025 X-Stat-Signature: wrjxrqsb7aenwyji1tawkdhmir7pz8kk X-Rspam-User: X-HE-Tag: 1723661665-178766 X-HE-Meta: U2FsdGVkX18/j6M3/tdmDWfSftKh2tFhL+UewaiFCtu6Rb9pdfN18Ov4rkRXI/1nVcqw7E0UbuzyAlVhEv9d8UmaZC+NX1jBxEavrt2st54bMzzgDCZ5bFgE1cODjOTiCaeP52TcJPMjUvFuKbDRWecAsLmKh02Xtsf81PARfnRSBpQEVKVK5/XB1dBqfiEL7TeqZk339h9toM+WLfOKt4mgMDUT5FZ1Nq6OQbRipEity2zA+NTrO2ATNSX0++PE45aQG/2/rYUcyREIpuc8srYHwkeYYgg+P7Mo+Qn+Ow8XXZrATDSeA9VjsT5F8wO+b2kXu1Fgapk/C1akTlIOmaWyr7w5SPyjBkHaT61C8ln1zxqb4YXTzd+sWpA7C4su7bV6kgwoxEqhqLa1dXUnIvK1p1K/tPTSVet/0eG+7WAV8Bx+bFxio3MHFl2M3yYdRrbCZFLwLxup3qyYWuazbnQsfd3lejq+o4mW+NJY15fJ/EcOypdVJBkMW7t6HLNfZKKzkEREr63V6ins/Eqll0r9koW3gPaM5SiMPp2hkPy0NfZj+tNJo56Xhu1dxdBWKqYm0N57YVoMpFTzzUmjYBG8Xa2iHZZYfGSM7elWkDDkJ0P1V+fOcVPmf910DJiTYii2j2p3yhmw4u2IpLjxdMPXjdisbC3FRRUZnPnMDAFIihJ6akXHXK1WExf0CNahfee6F1zGqrfz6tKm9kahIO8m85pg6T/4PStXV0/O6Ky5eAH1MmwKStmo7LsGWEh+t6ZN1NHqZ5GQ6XyTlzzMbMSamxjRG8lXR0ZQ/e5bptFgOSL9s2sUa/LorTBJM3sXWgcLzIcaOgUphyZc9zgSPWpBA/J7D58rVoTJLRbcgL/+lSoew22q+dY01asyZpatRZbbFE1YDwZnSqvpZul/xA+/GR1NjgXLEKwiljVxgUMJk3oYl1MPlovnc4sTKr51dxehmPrbCEzvQQhasrC IYZABBNc KDBZFhAPJB+/BayDW72AcEw5fTps7rBXyWAtMP5MnTjx6phoca7Zq7tN5D3a6IEBbkEasaGREe+F2r2JuApW+0E4tle+wtC2E1SBwI25OvmNbt9ot8+ki1SxTLGQXjevppAPMDEhdCkVJIHwpca9cWnrKRKn8RMAIH5B96u1WlLqwAn3thzhNHsQrdWwADPUMQouiIZ3ThfftRqClWgqA6fIDEdwuIZr9cUs4kmE18ypg4Zw= 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/ 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 | 395 +++++++++++++----- 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, 603 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 Reviewed-by: Eduard Zingerman