From patchwork Tue Jul 30 20:39:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747863 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 18215C3DA7F for ; Tue, 30 Jul 2024 20:39:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64A8F6B0082; Tue, 30 Jul 2024 16:39:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FB106B0085; Tue, 30 Jul 2024 16:39:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C1796B0089; Tue, 30 Jul 2024 16:39:21 -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 2D3026B0082 for ; Tue, 30 Jul 2024 16:39:21 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A344EC04F6 for ; Tue, 30 Jul 2024 20:39:20 +0000 (UTC) X-FDA: 82397584080.10.BBFC29E Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id 0234910003F for ; Tue, 30 Jul 2024 20:39:18 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=R9GNZdLc; spf=pass (imf14.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=1722371931; a=rsa-sha256; cv=none; b=zEHtGDINVdnzidILvH07FY8rBs2tsXw0aNGuY1VXmNZqhc7tmwYjiVmtuCUvU7XcjCDKIl yhr5uuIb8zbBrhasQUlB8qd6iFdADZdO7pYGXXJxt19rjEIKvgfEymghyqwIcF+YbZxwJ8 Zj65f6yAq7RPBzrR67ZFYO05PCRQMns= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=R9GNZdLc; spf=pass (imf14.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=1722371931; 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=KCoSUVQH886bjyNv92pO5B6ZdKWpLMxb6DrpSp7Y9cU=; b=PUQkcYG4IVBIC77p0QSGlVGHfQd/ibxYRPdFwYqqm5I16qP8T1tncwFc3zxRSTWxWpWCdJ pg/jj3ol39bnN8MvFpWXi/5Nse3mG9IAlubLtzBGWpxQzPJvZ/R7g05vw+PDfYTKU5Dnb8 Y+aTTTdBIceGFdFPlR43uLslyKsYM9E= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0B27361FAD; Tue, 30 Jul 2024 20:39:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87AA3C32782; Tue, 30 Jul 2024 20:39:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371957; bh=mYH2sieZgwgmDgI4KOAA8U1nIcciTpOh3t+Ty3X8b2w=; h=From:To:Cc:Subject:Date:From; b=R9GNZdLcv55TEN8Z6OMDS+kE7DUjIOIRmfflm/RIOO0jsYFB2A6M97kPJXSStthD8 CAjFUSnGyTC24LRj/6a/mLzhaW37GDDUR2IptxUzZ3iMUewI1kHpNCoDPWcOoiforJ k5te+PIW6yYF7yebg0d5yTmr34f+3NApAzBMgIAXmRxWU/WoSA++W/iVnN+XHI4QFa 9/wvfB18NQral7y/ue8Rh8GlUhr981M4MTS2bc4Niu5qm4FuwtQoq9YUL0OdVDNbCH x7yu+qoTAzrdtfCGl9OmuF2YPQYdSLSB37/5lfJvPbTsI9wkKfeeJJuLcqAJIyD3+h qXhlEdHj4E9WA== 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, Andrii Nakryiko Subject: [PATCH v3 bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Tue, 30 Jul 2024 13:39:04 -0700 Message-ID: <20240730203914.1182569-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Stat-Signature: x9bxudefquf6h1n38zoeowd1skfsu331 X-Rspamd-Queue-Id: 0234910003F X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722371958-545482 X-HE-Meta: U2FsdGVkX19WWMtPyu9J8vIstPoU3yT9k0soLMOQRauOm+P+frluQvcJN94RfTS49Ps3GZMsDoKQyqVzAOw7RiavgqVmn4mP6fmOYn8pI/z1Rc0SyWK6KqqlwbWuDHxmoSJdFLcM9x2Hm75Oghc1XgNpc6Zok2U5SVTo6KB57rnYp81mwQsv51PRlzLSF2xLVTaaHCjQlvsk9Co62dIHtrXGU2y1tZ9iPuU/3fdtRAi1Ntr9XpWuqZNIsF6r1o3SLTNvq1PQDQ1ARtHq8rxIpg0ABSGQBEQHonRl1DF7mqqiwQDivEzc+Zk//61mL8iJzipQ12B8GuS5Z2kxrDDmzKu6OGBty1yA8fwlBvkLvdiQweoc30dpMYhkQvHaxke/Yau7aTXZtnmWs/csrJRFn51mX+SfzMervHxzHaCSEEeawB7F7IaZdOnSyD7crWmnhxOpn1VcKdJFkux7NMR6bd/q/qZli6RvoAjyhRp5qaslKFRvs8DlYttkmbptnzNH6AEOwMSaEd5FZuaNh0GrL7NZmWXWblxjmBXT8WYiSndDD5GhhzNyXhH/5UmiKMh1km0E9JwkuCf3OjRkhMGc/LrGJ6nrsixHS+2MdWNAj3xuGPFesuocLWXopfpi/9TaPKj2/NoqDjLRO36TFRW67NJg9icga4hjGBLiPc4yd/8bidLWVKBGfqF/0AFbairwtA/F6rw1PPOmjjWPiqJ2nSNKVdbSmv0Fz6KXpOm3FjLsQ8d5Mnsj4SoHhoBp1QsV3sJpOa1ogYdtUsCcqn5ykobz0ikPw2YD/c5/CJGaCEyOzGCNqG1b+xsJ7eSTCICh/5fXESVKSuU8e3HiAVBsezklUP4yoLcNepy8OwDwVO765LlKtc5UXHPzB/FMHmIgxjKJmhdk2SmJG62sojDLp0cYqyvALsfW7BrDx94c+egSB9B2OMSdZtyh+8T02SqJ09gp0K+fDLK0BQPqMVx NNQt4Cwk aSF2AhCv5F20jbhByslWBiA6zIjg+Rd+8eaugUGj2G58S0YGEVCDoO3NEpx+ZI7DMBx6BIS0ZzYvf6FkdJxYJ+vQShLjURhJuDtuxWKn8B5rhten+4DRFDj+xxqbkAoZNHvlqgUg5y5w6dJnMixykaBGQ0xG+uLVD5TQdyTm1dE3oQ44XTpUetkLnNT49ymk/gAiHng6O7xmUbesIgSjsPfktdKjYFj0A6i8zeDbBI5GkFTI= 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. 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 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/ 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 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 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 | 385 +++++++++++++----- 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, 594 insertions(+), 141 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