From patchwork Tue Jul 9 20:42:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13728505 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 9823FC2BD09 for ; Tue, 9 Jul 2024 20:42:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0403E6B0095; Tue, 9 Jul 2024 16:42:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F32926B0096; Tue, 9 Jul 2024 16:42:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E21146B0098; Tue, 9 Jul 2024 16:42:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BFD806B0095 for ; Tue, 9 Jul 2024 16:42:56 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 25D0641A92 for ; Tue, 9 Jul 2024 20:42:56 +0000 (UTC) X-FDA: 82321388352.09.20E82D7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 8CE12A0002 for ; Tue, 9 Jul 2024 20:42:53 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p0h7Wr0Z; spf=pass (imf25.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1720557757; a=rsa-sha256; cv=none; b=cJrfEgZqKO6twj72ihX2Y3qOE7oLX9BJWOt0bxGjLe8wN29DkqWWiKf4KtFNH+Y4mL1Mxt 7M42jTZE5Dk3e5DFMe4b4mTJN55NvYb2F8sF3/H1dFMujtPFjUpI4kWNkiGJVhIj0dMIdg Ua2AejIMOuAnkogXZ+OYXOR0PD9bbwg= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=p0h7Wr0Z; spf=pass (imf25.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1720557757; 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=b+m1ep57H5z+2cMCuNouoqcV7gbUYnZM5RlraKpIoFI=; b=v/muDzsy8eE0Gx5w/WqWcJRA0mAmPicVNB8hor+HH72th+G9sp6zn/BBJC8aT0Yda5c1Db cncAHyeo2pS6JamIl6xmNSK7E6LRDOSMM2OcDPJkpNyNmFJOoR2Or2wo3CgA3eHZzn9xnS BIAmgybMYLW5WzK40avpiLu9qD9BVis= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 5F1BB616A6; Tue, 9 Jul 2024 20:42:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F0E01C4AF15; Tue, 9 Jul 2024 20:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720557772; bh=vYyrBpzkRZI9HAtJcRaJBlpeRkvBiECfUfa7DYrUBvE=; h=From:To:Cc:Subject:Date:From; b=p0h7Wr0ZspcYf7uyOIqvWIx3+Z1uuIfeRmYyd4jPvOt2Jk7QYKj72j8umuWslLRwm LKFdVBuTHewNZqNuArgMKU8QqsQOgGC+193UifjFOryAyaNx9GFlxla2eAbViqow7P 0MhELdODtpQ+LUXrK7uGmvYI3RdivC1M8f9RDuE0Fw8KwGCQezjg26JyD5CCWI8kEF q7KLgcNWiOb2nA16TO722SSp7C3NHWML9QzfrQTOqj8NVpyIOVRKvx0bRB9OwlEqvg 0rsRhBKNIpZ18bXcQddXBG1PIOBuT8jQihh/9qfQd79ApwH2somxykNyjqy64/woIf svZdV2M0NEP1g== 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, Andrii Nakryiko Subject: [PATCH bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Tue, 9 Jul 2024 13:42:35 -0700 Message-ID: <20240709204245.3847811-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Stat-Signature: fso3gzqk54kos7mii55hkpj98chgio8r X-Rspamd-Queue-Id: 8CE12A0002 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1720557773-718527 X-HE-Meta: U2FsdGVkX1+Ps1pEtXV2uC0TxaperEpgLwW9IYFrFTnr1hMMGfc2aZzoK+WKEmczVW5GvzguzeXvan8eN+/58Pl8Vm1ServG+l6P9c4f8pVaUiuSiQoHCC6hHYQNXLTM4CXyOBNslEFE0womyeG0sHhuqvFPAJLhvHrIKIAnNzHJ9Vx3cTXmNdA0XhI/a7DTl+LDxxBnFS1YNGYPW1oGocMe02hJR/0uURTSAEHJ7gGEJJsgPzOciG5IBy3P0+8Dk/RxiIeax243E3jr9qbtFfywxxntqDCV3GHD+jAExcy5mCzLrrAD5TUqWcDAeIbYiA9wza7cojbakeLM9nBGqKvO76mFCB9t1qUkKVHEJvTAMTSMIZkPpCmCuS2pjjgXDhT8qjseyF4QoD79Lraq9igRuXAN4/3DgPCaEx1v3+m6TD+cRqgkvP8goQkjkYTTWgOL7QbggA60cEGkP8bxbXOnHei15ZZUD9+SLZocxi10jg9RzonAFpDYqPr4ZAOJY5mzioq8J055IQ4rQeZCQ7bWbjgTVJkPvMdApoC6Hp+46J7JYzDh12108DhC/Gi/oD0yTV5q/PCZVp7VQiZd4PyBv/0KpdvP41c1cXK7oBug001baQQwNNHnVKoiRh1gJoCI0b4kZ4PxNZIh8E26PK7QR60DIvwyOl1U+COyykyxvwq1DKTfeEvtCMGrmYj24v3WA9z93PnclDJwvjmVjBpncpCUE71KfszbGzUDiYGoMupmAZBKzrW8ODtJT5rqbv7nXvIHuCOePVa8MBpcfugzFH2BbZPwSgo5fPR0pfzasIWzhWT2ZuozQu/pE9U2BNIIRjg4SSxADpxjxgEuSPDuP7lJKG/oJQVkBY0wtnyv6hHfbEND8jM+67Sz29lN67C+OP7bdsSmUFPvlreCTa9tv3YobzwSt2A0EBnXahTBG+gdjCO7f00NvEdEciiLvq8tyh1PTfnjJIfQCkq x56OK7wW lFrQJIuJYE5sAGrUsxo0E8rDuRc3nWNClhdGIssuOVjN7+SFPaAGgfRj2jLiEE6Am1k5QM2XasLfq2/CUSGmFR6I71ipkMT0bOWd8MX97SzOxMCRcF786TWmXM8bRTW6UdFlo90n77xGDC0HOS408+PNwnrgnCsiHaz7YXKVuTRMvvnt4TSWQum0+vBZht29RLMUWe1UCMfcIyAN5KB7Fx5DpjIYH/l09yCUh7hOmKJFgDfE= 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 problems. 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 page cache page (on demand) and giving 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/ Andrii Nakryiko (10): lib/buildid: add single page-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 lib/buildid: harden build ID parsing logic some more 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 | 370 +++++++++++++----- 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 | 34 ++ tools/testing/selftests/bpf/uprobe_multi.ld | 11 + 11 files changed, 584 insertions(+), 129 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