From patchwork Thu Jun 27 17:08:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13714877 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 4D782C3064D for ; Thu, 27 Jun 2024 17:09:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DC0FF6B00A2; Thu, 27 Jun 2024 13:09:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCD446B00A3; Thu, 27 Jun 2024 13:09:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A846E6B00A4; Thu, 27 Jun 2024 13:09:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 84A636B00A2 for ; Thu, 27 Jun 2024 13:09:16 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3EF48A3E54 for ; Thu, 27 Jun 2024 17:09:16 +0000 (UTC) X-FDA: 82277304312.14.55DB18B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf15.hostedemail.com (Postfix) with ESMTP id 89552A0013 for ; Thu, 27 Jun 2024 17:09:14 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pifzznZr; spf=pass (imf15.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=1719508131; 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:in-reply-to:references:references:dkim-signature; bh=1mSz6NYBEtVlykLHYOpq7Y8Z0zh+MMwyc2nZ6QivHEo=; b=TCGAaJtCBi5LUEiQZnt31m8YWsAy6HCE/VVmSrKTaMeYgGl2QdSl3as/v/BOdkoFJT0ZcJ 8v0AaO/L6z3wTIhZdyaWVi+bGDgmbUT39gfPcSoi4sX9XUP1ICDWDbTORf0oLPVxc3a+Xx T+Bpo64as4SOOic3SyEJa67Mtdv2VFo= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=pifzznZr; spf=pass (imf15.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=1719508131; a=rsa-sha256; cv=none; b=uM3ZeUJF5cz+GbWLPrRNPrE3rlL69E6ZJ9H/YqrlbHKmzQW8iOnek2NnB91xtgvoVxKv2v 4L9DV/9ErU0kB2bpoKETSOoOa5GshaP5ZVmb2oZot3jJCVbS2Xq4wAXZ9lbo1yncpNAckE 1FCELnGWTuJrFRJ6uwhwfWExRsntg9w= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C667F61F6B; Thu, 27 Jun 2024 17:09:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6313FC2BD10; Thu, 27 Jun 2024 17:09:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719508153; bh=TfcaDQQTdRLothd0FRVJK6mCUHkge2r3AFoLq6I1TMA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pifzznZrR7d2mHil0ZsDNUjdyAtDUfNT8iHlRgb5WpOzvxiiIN5gEWe/sLAsNeS9Q 3JlLJEEUQFeOiKu2UeHqkSwO228T5OvWAIdya2IbVVt6DqAAbr4cCrsfmC+5vUrMnt /+pWc2mJx4YLQ1Y4jjiG7HuspbUxhOLX4yejWUtYCxWErZIjSTtRB5w/3y0bEcVoSh ELHXf2qD+MSJSXRuu0dYiKJGjG9HgMXC991GBaClgKL8a4xT1yto/86DFAe0hSwtqt 3ldeJiQtjfRR+FOI4/3DebO392IATQ+yXMKEtaw7eJ+6I6f+QPT+HCEYuli2kLEwcS fe5N7keYk1IUQ== From: Andrii Nakryiko To: linux-fsdevel@vger.kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, gregkh@linuxfoundation.org, linux-mm@kvack.org, liam.howlett@oracle.com, surenb@google.com, rppt@kernel.org, adobriyan@gmail.com, Andrii Nakryiko Subject: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API Date: Thu, 27 Jun 2024 10:08:55 -0700 Message-ID: <20240627170900.1672542-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240627170900.1672542-1-andrii@kernel.org> References: <20240627170900.1672542-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 89552A0013 X-Stat-Signature: 1d7bgtr5fsgwza39goznajb566mui1wp X-Rspam-User: X-HE-Tag: 1719508154-539433 X-HE-Meta: U2FsdGVkX18YFVvJ/5RcQmtL4UicYBH6wStRq3h25wR1+EhZg2b5EuznA6ZseqpV8Irz5O5bBo5S8wbxsK0V9RyPNkgHq7FK8r04tts7yArtS68muUNmNQqexNArwYHUCEBIRsbws0EqjO/7fQz3bM/rMOVPoNDOTDiCVds0wIJftLJxzroQ94R1l7d47HADGYzx0UJxWK3wspbFcjsQoh6W9F4GwwWV3QfhVoB5Q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Os5PeQmRLQ42VNOaZd1StaiS3r1m7C4NoduQa2rKZc= 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 need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc//maps textual representation doesn't help with this. To get backing file's ELF build ID, application has to first resolve VMA, then use it's start/end address range to follow a special /proc//map_files/- symlink to open the ELF file (this is necessary because backing file might have been removed from the disk or was already replaced with another binary in the same file path. Such approach, beyond just adding complexity of having to do a bunch of extra work, has extra security implications. Because application opens underlying ELF file and needs read access to its entire contents (as far as kernel is concerned), kernel puts additional capable() checks on following /proc//map_files/- symlink. And that makes sense in general. But in the case of build ID, profiler/symbolizer doesn't need the contents of ELF file, per se. It's only build ID that is of interest, and ELF build ID itself doesn't provide any sensitive information. So this patch adds a way to request backing file's ELF build ID along the rest of VMA information in the same API. User has control over whether this piece of information is requested or not by either setting build_id_size field to zero or non-zero maximum buffer size they provided through build_id_addr field (which encodes user pointer as __u64 field). This is a completely optional piece of information, and so has no performance implications for user cases that don't care about build ID, while improving performance and simplifying the setup for those application that do need it. Kernel already implements build ID fetching, which is used from BPF subsystem. We are reusing this code here, but plan a follow up changes to make it work better under more relaxed assumption (compared to what existing code assumes) of being called from user process context, in which page faults are allowed. BPF-specific implementation currently bails out if necessary part of ELF file is not paged in, all due to extra BPF-specific restrictions (like the need to fetch build ID in restrictive contexts such as NMI handler). Signed-off-by: Andrii Nakryiko --- fs/proc/task_mmu.c | 25 ++++++++++++++++++++++++- include/uapi/linux/fs.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 674405b99d0d..32bef3eeab7f 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -445,6 +446,7 @@ static struct vm_area_struct *query_matching_vma(struct mm_struct *mm, addr = vma->vm_end; if (flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA) goto next_vma; + no_vma: return ERR_PTR(-ENOENT); } @@ -455,7 +457,7 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) struct vm_area_struct *vma; struct mm_struct *mm; const char *name = NULL; - char *name_buf = NULL; + char build_id_buf[BUILD_ID_SIZE_MAX], *name_buf = NULL; __u64 usize; int err; @@ -477,6 +479,8 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) /* either both buffer address and size are set, or both should be zero */ if (!!karg.vma_name_size != !!karg.vma_name_addr) return -EINVAL; + if (!!karg.build_id_size != !!karg.build_id_addr) + return -EINVAL; mm = priv->mm; if (!mm || !mmget_not_zero(mm)) @@ -539,6 +543,21 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) } } + if (karg.build_id_size) { + __u32 build_id_sz; + + err = build_id_parse(vma, build_id_buf, &build_id_sz); + if (err) { + karg.build_id_size = 0; + } else { + if (karg.build_id_size < build_id_sz) { + err = -ENAMETOOLONG; + goto out; + } + karg.build_id_size = build_id_sz; + } + } + if (karg.vma_name_size) { size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size); const struct path *path; @@ -583,6 +602,10 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) } kfree(name_buf); + if (karg.build_id_size && copy_to_user((void __user *)karg.build_id_addr, + build_id_buf, karg.build_id_size)) + return -EFAULT; + if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize))) return -EFAULT; diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 5d440f9b5d92..2a4a5f50c98e 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -511,6 +511,26 @@ struct procmap_query { * If set to zero, vma_name_addr should be set to zero as well */ __u32 vma_name_size; /* in/out */ + /* + * If set to non-zero value, signals the request to extract and return + * VMA's backing file's build ID, if the backing file is an ELF file + * and it contains embedded build ID. + * + * Kernel will set this field to zero, if VMA has no backing file, + * backing file is not an ELF file, or ELF file has no build ID + * embedded. + * + * Build ID is a binary value (not a string). Kernel will set + * build_id_size field to exact number of bytes used for build ID. + * If build ID is requested and present, but needs more bytes than + * user-supplied maximum buffer size (see build_id_addr field below), + * -E2BIG error will be returned. + * + * If this field is set to non-zero value, build_id_addr should point + * to valid user space memory buffer of at least build_id_size bytes. + * If set to zero, build_id_addr should be set to zero as well + */ + __u32 build_id_size; /* in/out */ /* * User-supplied address of a buffer of at least vma_name_size bytes * for kernel to fill with matched VMA's name (see vma_name_size field @@ -519,6 +539,14 @@ struct procmap_query { * Should be set to zero if VMA name should not be returned. */ __u64 vma_name_addr; /* in */ + /* + * User-supplied address of a buffer of at least build_id_size bytes + * for kernel to fill with matched VMA's ELF build ID, if available + * (see build_id_size field description above for details). + * + * Should be set to zero if build ID should not be returned. + */ + __u64 build_id_addr; /* in */ }; #endif /* _UAPI_LINUX_FS_H */