From patchwork Thu Jan 23 21:43:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13948648 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 5AC27C02182 for ; Thu, 23 Jan 2025 21:44:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D2BD6B00BB; Thu, 23 Jan 2025 16:44:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 75A926B00BC; Thu, 23 Jan 2025 16:44:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FB9B28000C; Thu, 23 Jan 2025 16:44:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3D6B66B00BB for ; Thu, 23 Jan 2025 16:44:12 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B487B46894 for ; Thu, 23 Jan 2025 21:44:11 +0000 (UTC) X-FDA: 83040045102.22.88C4882 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf26.hostedemail.com (Postfix) with ESMTP id 2F79614000D for ; Thu, 23 Jan 2025 21:44:10 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YGRqBL/k"; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737668650; 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=dt+lYUq7SNbt4rgXK/wPfEbO58SVFDGGtovmQkA4i7Q=; b=RxLNc+jYpHoFEf8w7gz1CEwnwLZ4bPjiJFF2pwZmT5yk0HWI8RO3YgM9a/BSKEXc6pxD+s Uis6vEp0Y8CReIKOLsEZ/n/lmJO8lxp8P0jsbnkuPi5WHFpxKkuiDO+lM+hfR1RqbEifmY Z0IGijQfSSJaTfZiGg321JSyFDVl66o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737668650; a=rsa-sha256; cv=none; b=Ht3t7Qi53qV1ba9xvf7EKTaFH0m7YvsPzBfZu1IUhZWQjyOfjoGZN7Wd2ylrMqcFHjphWp uUEHM02zT8+9MyhafeHtY+UaO/PPW2OxZjS0azp8w0CZCTMd+HY6y6IlJbx22fUFi9IpRK PmIv+W7KVCBH5Dl7kJBV+H9RFvly9BY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YGRqBL/k"; spf=pass (imf26.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 03592A411B6; Thu, 23 Jan 2025 21:42:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7964C4CED3; Thu, 23 Jan 2025 21:44:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737668649; bh=R85lw46lWbNKwIBjKcm0nPFbL7eV+3oJagH3JDMENic=; h=From:To:Cc:Subject:Date:From; b=YGRqBL/k5NSeLv2lvBxmXoUTbvMhc5e4DucR0zIIXXpZFHcApqrD7CdjkV+WKfdaf p2RVk4P34gOSLzAF/ndbuuV96NKi4UGLY239QVETaMRmPUNn3U+Uv5dGzWCMbQ5s82 3Z9rPxzLgfzke5hOz6pEYBGcTH5Sl1bZdl/aV+o8gfDyHvcvt2UHD+OmVWd7DnVH+Q fsA4ZqaQd3a14N3G/FLqZOh3XpGZTvxnAi+rMwaGyyVP1Rs1bnghl44oBtyGXU1/N0 wi/KdZ3+ORlGxlaFejSGFmNzobtElNImr+HEhoXeIOdnzNp4QaqnmomxFK2mardbjO NDcTnul24Y0lQ== From: Andrii Nakryiko To: linux-mm@kvack.org, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, kernel-team@meta.com, rostedt@goodmis.org, peterz@infradead.org, mingo@kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, shakeel.butt@linux.dev, rppt@kernel.org, liam.howlett@oracle.com, surenb@google.com, Andrii Nakryiko Subject: [PATCH] mm,procfs: allow read-only remote mm access under CAP_PERFMON Date: Thu, 23 Jan 2025 13:43:42 -0800 Message-ID: <20250123214342.4145818-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Stat-Signature: 9fn9enyku6myohk15qyhsdiaz7x4xi47 X-Rspamd-Queue-Id: 2F79614000D X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737668650-962223 X-HE-Meta: U2FsdGVkX1+r0ZLNkuH6FFVyd6QQfvWj47GQkc1INZlOv039da8pROWCAgEcCH+JKGXRspjCg8jaXpE8sRTOePLF9yYSj6/V+33iYYWSONYoCfkbTBaeRzuoYWxOd8cTbUBLvO/L5xicZVHJXEY9XTTb3igOrhY6vVNEMjkJG7LXQYFFeNPI8g2tSMfHfbIm6465YBQx8YutHV8kQsjk++UyAPSKN6Zy0qpEqWepTYJzELW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: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: It's very common for various tracing and profiling toolis to need to access /proc/PID/maps contents for stack symbolization needs to learn which shared libraries are mapped in memory, at which file offset, etc. Currently, access to /proc/PID/maps requires CAP_SYS_PTRACE (unless we are looking at data for our own process, which is a trivial case not too relevant for profilers use cases). Unfortunately, CAP_SYS_PTRACE implies way more than just ability to discover memory layout of another process: it allows to fully control arbitrary other processes. This is problematic from security POV for applications that only need read-only /proc/PID/maps (and other similar read-only data) access, and in large production settings CAP_SYS_PTRACE is frowned upon even for the system-wide profilers. On the other hand, it's already possible to access similar kind of information (and more) with just CAP_PERFMON capability. E.g., setting up PERF_RECORD_MMAP collection through perf_event_open() would give one similar information to what /proc/PID/maps provides. CAP_PERFMON, together with CAP_BPF, is already a very common combination for system-wide profiling and observability application. As such, it's reasonable and convenient to be able to access /proc/PID/maps with CAP_PERFMON capabilities instead of CAP_SYS_PTRACE. For procfs, these permissions are checked through common mm_access() helper, and so we augment that with cap_perfmon() check *only* if requested mode is PTRACE_MODE_READ. I.e., PTRACE_MODE_ATTACH wouldn't be permitted by CAP_PERFMON. Besides procfs itself, mm_access() is used by process_madvise() and process_vm_{readv,writev}() syscalls. The former one uses PTRACE_MODE_READ to avoid leaking ASLR metadata, and as such CAP_PERFMON seems like a meaningful allowable capability as well. process_vm_{readv,writev} currently assume PTRACE_MODE_ATTACH level of permissions (though for readv PTRACE_MODE_READ seems more reasonable, but that's outside the scope of this change), and as such won't be affected by this patch. Signed-off-by: Andrii Nakryiko --- kernel/fork.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kernel/fork.c b/kernel/fork.c index ded49f18cd95..c57cb3ad9931 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1547,6 +1547,15 @@ struct mm_struct *get_task_mm(struct task_struct *task) } EXPORT_SYMBOL_GPL(get_task_mm); +static bool can_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode) +{ + if (mm == current->mm) + return true; + if ((mode & PTRACE_MODE_READ) && perfmon_capable()) + return true; + return ptrace_may_access(task, mode); +} + struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) { struct mm_struct *mm; @@ -1559,7 +1568,7 @@ struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) mm = get_task_mm(task); if (!mm) { mm = ERR_PTR(-ESRCH); - } else if (mm != current->mm && !ptrace_may_access(task, mode)) { + } else if (!can_access_mm(mm, task, mode)) { mmput(mm); mm = ERR_PTR(-EACCES); }