From patchwork Mon Jan 27 22:21:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13951781 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 87A64C02188 for ; Mon, 27 Jan 2025 22:21:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E238280181; Mon, 27 Jan 2025 17:21:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19528280163; Mon, 27 Jan 2025 17:21:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0819F280181; Mon, 27 Jan 2025 17:21:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DFC6A280163 for ; Mon, 27 Jan 2025 17:21:23 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6B33AB08CD for ; Mon, 27 Jan 2025 22:21:23 +0000 (UTC) X-FDA: 83054654046.20.35F87DB Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf25.hostedemail.com (Postfix) with ESMTP id DA311A000D for ; Mon, 27 Jan 2025 22:21:21 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ss76KQYx; spf=pass (imf25.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=1738016481; 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=zlUSnc5GSH+dWAjP7GntJY6IcbiISqYR6S16pkMimvo=; b=eFz1uGkS4GFjuwsHjB7s/1c6EhoactUjnLIS4q9ta91dCA8VVmy/4dqNTsX1Qx1nDXfoJm CvSGQjVLCNLgzeZdfolbvZVVYROMr4168gNl88m48wkmGrV8NP2i3kFRJi3kaALCgg+/jD NbAdgxKuU6tcPRfHRwUbjL0jLIUPpg8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ss76KQYx; spf=pass (imf25.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738016481; a=rsa-sha256; cv=none; b=Wv8ZMtZkPcFdVRN40KpmqGYRhT3ug9LehKeWzimvnzvBFUrE4TUotbWdax75T3VcF8/eed 9ogozxafOMgj2Rhi5r7JQa2iMM2PggtihzQcnYpz61nsNRJ92gQInNhROpr/iDKpg9TbHQ U6uB14UQuKva7LF6iIEY8oihZIx45xg= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 11E42A40249; Mon, 27 Jan 2025 22:19:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8AF70C4CED2; Mon, 27 Jan 2025 22:21:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1738016480; bh=4xM5GmrbIafh9OA6F7trKBrxUqS4abqgi5jrfUGrN+I=; h=From:To:Cc:Subject:Date:From; b=ss76KQYxKiJDlvPPDR6Fjv7j8LneXb5twX0eVkpvtfMQcn3u8Fk2USj+AWBlxvy40 5fqoq7WNxVCgLaYJcMah0hDgLHk8SsIliRDvkxFKbtesq0AdkaTxGGR8ifwMAY6VgR 6S+mrTCJBJSCEUQcoW6rkupbS5x6cXORTO4uFXXxL77TaaQp2YhU5gUpK0Frdy5sTh XSh3NtAArjkOisx/HnJhRcwod3lqbTPUDvCyxhMXw78m3hI+rNASZjBBSey1PW+MJT kFct/KLU+p5zirffmabFZ3Tm+vHHm+cnfKieCVGqP3ya6pwI4jPt/pDwTFBMYJYK4y zxZZv3UlsmayQ== 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, kees@kernel.org, jannh@google.com, Andrii Nakryiko Subject: [PATCH v2] mm,procfs: allow read-only remote mm access under CAP_PERFMON Date: Mon, 27 Jan 2025 14:21:14 -0800 Message-ID: <20250127222114.1132392-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: DA311A000D X-Stat-Signature: 78anqinhh165pxc8ar3uamhsaf4mnptu X-HE-Tag: 1738016481-242944 X-HE-Meta: U2FsdGVkX1+y5omwxtX/5e6fOUDH5kM84oSyw38DNYFwRWD83z+PclYFMuc3zHBuE9CHTXXNUtoqVGTyQJk6xhvAXthXJP/cVSc/00GhWrpRNNVOadTr2M5Cxj/t9FoBvkq3WF0jlcdUjCSQUoWeW/2Ko8kNyandqdkY0qciMxTzWW4O/u/od3fVTVrt24rlCdLqoLZmZddlRTSNAzgIa2DDWWKONeGSPeHQtwC6BM2aUZMHAlZj/QZzlidSfujmrXwUAC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ack.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. So /proc/PID/mem, which uses PTRACE_MODE_ATTACH, won't be permitted by CAP_PERFMON, but /proc/PID/maps, /proc/PID/environ, and a bunch of other read-only contents will be allowable under 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 Reviewed-by: Shakeel Butt --- v1->v2: - expanded commit message a bit more about PTRACE_MODE_ATTACH vs PTRACE_MODE_READ uses inside procfs; left the generic logic untouched, as it still seems generally meaningful to allow CAP_PERFMON for read-only memory access, given its use within perf and BPF subsystems; - moved perfmon_capable() check after ptrace_may_access() to minimize the worry of extra audit messages where CAP_SYS_PTRACE would be provided (Christian); - s/can/may/_access_mm rename (Kees); kernel/fork.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/kernel/fork.c b/kernel/fork.c index ded49f18cd95..452018f752a1 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1547,6 +1547,17 @@ struct mm_struct *get_task_mm(struct task_struct *task) } EXPORT_SYMBOL_GPL(get_task_mm); +static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode) +{ + if (mm == current->mm) + return true; + if (ptrace_may_access(task, mode)) + return true; + if ((mode & PTRACE_MODE_READ) && perfmon_capable()) + return true; + return false; +} + struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) { struct mm_struct *mm; @@ -1559,7 +1570,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 (!may_access_mm(mm, task, mode)) { mmput(mm); mm = ERR_PTR(-EACCES); }