From patchwork Tue Aug 13 00:29: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: 13761165 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 33ABCC52D7D for ; Tue, 13 Aug 2024 00:29:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 868F76B0092; Mon, 12 Aug 2024 20:29:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 817316B0095; Mon, 12 Aug 2024 20:29:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B8A26B0098; Mon, 12 Aug 2024 20:29:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4EE016B0092 for ; Mon, 12 Aug 2024 20:29:43 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id F0ED91A04CE for ; Tue, 13 Aug 2024 00:29:42 +0000 (UTC) X-FDA: 82445339004.13.F0CE295 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf26.hostedemail.com (Postfix) with ESMTP id C68A3140003 for ; Tue, 13 Aug 2024 00:29:40 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Xp0/J1GR"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 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=1723508903; 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=oUVeuKIsdTeE5371so/2rQ063CRn3yuTKNjh+rW5FjU=; b=UUaM95FQrOWtPWRh8eeJnTAXsTSP+YtWGFUtPZy2aaC1ArKNA7K0Zn8C0ICrrd+NiY/IeJ bky5vVhJXpsDql+Fy08x7XXnygM0wcRZtRz5ts68ngzSzw4MDKUgeswgg98MkFnfYHTAIn dd4nnTwIQnIF2cyyPjTb4xiyv4AusNs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508903; a=rsa-sha256; cv=none; b=SoGkO+1bIqn+2R1nXA1SkCD6V0zmih9OAywJAlrbymX8Pwv+K0QvA7XIaxoNaWIyGLHiyE Wyzg4CwMl+dXWZ7n9VhKVGqpO0Gy7SeOELt3HLYhvhylEdwh7q+xRYSit2hJ7KcQQf9PfV N28MyFFPnRhQ29vPOyYHAiNbWzozyzo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Xp0/J1GR"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 0467FCE10E0; Tue, 13 Aug 2024 00:29:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C707C4AF0E; Tue, 13 Aug 2024 00:29:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508974; bh=Fzk/HK9bIRfF20c/h6DzElGIXTaohG+iN7OWOlC5voc=; h=From:To:Cc:Subject:Date:From; b=Xp0/J1GRdRc6G9QaLBJszLu4DzgYdx/qupcibeHj2PRxLOXIY9QSS4yuJzxl7aDDa axA66jygf6h0jy7ez7Uweaomog6otM66WN2f/tZ72/oAaN6ZNkLhL0t7jpP/o5Zl7Y wjtiK+atzKJOdzydHIOe/ooK4fFLHjAhsXUpf54bROSZbrDHt4bnTP0yjtr5wGX6oY XdPITVCOgoKhUUizX/HFXv1uXRZDVTVUqJwGo68nBauo14WYcef5XY/l91v0rmjrgU fZkZEPEXuO6GTPHfVRMgFf2BcaJeu6saqYJpGggsu9hcl3GK1xJtnSVB9BtTLxRkoD lo5HaIlV5Ccxw== 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 v5 bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Mon, 12 Aug 2024 17:29:22 -0700 Message-ID: <20240813002932.3373935-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C68A3140003 X-Stat-Signature: ii6jermdqn4fd7j4pbadspf3d1dz1g66 X-Rspam-User: X-HE-Tag: 1723508980-549284 X-HE-Meta: U2FsdGVkX1+74upTrXmP8QYm7zd1d9TTBbSNsx4VrzmnYy47bUzdvKf2ybmui2dJTZ5QpGOK8OoHH2yONjTwpoRNEbjFeQrAPgrdHEFqjt52gYrjWHHww4NmU08chlGtXS2Pvxc4QtPbyWx5Cut3fsOEoWm/cjMj3VxgqHmQRxhV9RcRobKV+PCUp3PQWGVRTlJ3UQEZtFNF7FkkGuxUV25f5A4YAvopyZp8kSo7nRa+69bmlilR/JRCLdxEGvpKCW/nEehUaG+vbLXxf2JwckbeFO1zv1Tv/vaZG2jeDNW5BhLc9Z/z2aHCJXqt7+zPVHn2gUXC2IBzFI5P3RgHQcOCeAGZ9jiA23FMGZRYtauujyTOFV3llpvEr0cJQFv10IvOenjnsjaaXyRTQYEY0fDxpS/Y6zPfuKp1qf+3qy4rBdPJ7z5GYVFZ+3hvJv8AqX4GNgfzMUkbKa1Rb0VoNiA9Js/5nQQfUoYAPeWndJzyU2WZudEiMCsshnCD7eX2xTw44W9BbPmyxp2eI3quMb//f+rlbAQSZtt8al6IH21Uq939QDWwBYfIX8eA9oMZpUCRTPtTuNV5APMaBSgUSbTDBZnfezHjhmsi3UmxCgQ49awf/TNQ36cRgcaUBx9oZFFDmt+kRtSNXKQdTvKaJUCRA9zu2Ed+B+vBFypn95/NAM1TxtVMHLxDA4ZsyrsEerni2HpwhAe0U07IYqrFr7moNIo9OiDkuGakDeGBDJvrSE7yILokUWslzf569bZBSa6M5uSWgcboYvLptwrawQF6Z6QS3ygSFoEadJT/Sf1Sn8DzX78LRcADCzq+YPTTUIyAIqhfijQYM06IXQIi51/q1qN2dyI/GPNGbArPkJEdogYD7/dmNFtLMMgyrVP8wGKJFuWNyTsGLt1PCYmgRdz1et+YZsWET2QTbcvhsr3qcngQxwG7JrGCoiiO9tE/ISYvDTMQCLTsls2wSKS AuOpZxva yyesXm1f5wc8hMJTIpXfMh/A0dk2FG0ydIMMybOgXSoYZDFJiIo9bO/yxsn5mqaeTMeSGiN2BN605opZwRISvPYOYJ3ONWZERgAS1ov1s+r/09+XhJpBx/RAdfByuuQTgpaWMPNNkuve23SAhc8aqyFWogfN7AXQ4bJyAuTpZqzuxcedFW/3Srhf9XGV2rbVRdc0SSHlx309TShkN8MmwkceHtyAwBnHZc4kiWEMRPnAKEj4= 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/ 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