From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_RED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27348C11F68 for ; Thu, 1 Jul 2021 01:54:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CE4616141A for ; Thu, 1 Jul 2021 01:54:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CE4616141A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 578418D0260; Wed, 30 Jun 2021 21:54:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 500CD8D024F; Wed, 30 Jun 2021 21:54:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A2258D0260; Wed, 30 Jun 2021 21:54:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 104A88D024F for ; Wed, 30 Jun 2021 21:54:47 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DC09282DBD41 for ; Thu, 1 Jul 2021 01:54:46 +0000 (UTC) X-FDA: 78312350172.01.44AC5C3 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf17.hostedemail.com (Postfix) with ESMTP id 862ECF000123 for ; Thu, 1 Jul 2021 01:54:46 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 26F2B61420; Thu, 1 Jul 2021 01:54:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1625104486; bh=kfGV9OQf9RWeVwp+jTzprYxY+fIDam5q/fW18WdXoZo=; h=Date:From:To:Subject:In-Reply-To:From; b=RRunjc7U6adkbi5Zitxc9mq99UtwuQE7CbJnCjayNhuYM3vzgEq5EjQScQUfb6B8U nbHvVkstuQhAGQF/wZylGGRkBFunworJ0mL6n2g+9HwALflp6i5B3j6DHTxaUhUXlJ sMu1YRpXS1DHRT62ltzIgX3KaAC41PxghdE3f0qo= Date: Wed, 30 Jun 2021 18:54:44 -0700 From: Andrew Morton To: adobriyan@gmail.com, akpm@linux-foundation.org, avagin@gmail.com, bernd.edlinger@hotmail.de, christian.brauner@ubuntu.com, christian.koenig@amd.com, corbet@lwn.net, deller@gmx.de, ebiederm@xmission.com, gladkov.alexey@gmail.com, hridya@google.com, jamorris@linux.microsoft.com, jannh@google.com, jeffv@google.com, kaleshsingh@google.com, keescook@chromium.org, linux-mm@kvack.org, mchehab+huawei@kernel.org, mhocko@suse.com, minchan@kernel.org, mm-commits@vger.kernel.org, rdunlap@infradead.org, surenb@google.com, szabolcs.nagy@arm.com, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, walken@google.com, willy@infradead.org Subject: [patch 142/192] procfs: allow reading fdinfo with PTRACE_MODE_READ Message-ID: <20210701015444.ZOZaFPX0b%akpm@linux-foundation.org> In-Reply-To: <20210630184624.9ca1937310b0dd5ce66b30e7@linux-foundation.org> User-Agent: s-nail v14.8.16 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 862ECF000123 X-Stat-Signature: wfqpb97jiagctz5wmr9equ5izutcd4f7 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=RRunjc7U; dmarc=none; spf=pass (imf17.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-HE-Tag: 1625104486-366496 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: =46rom: Kalesh Singh Subject: procfs: allow reading fdinfo with PTRACE_MODE_READ Android captures per-process system memory state when certain low memory events (e.g a foreground app kill) occur, to identify potential memory hoggers. In order to measure how much memory a process actually consumes, it is necessary to include the DMA buffer sizes for that process in the memory accounting. Since the handle to DMA buffers are raw FDs, it is important to be able to identify which processes have FD references to a DMA buffer. Currently, DMA buffer FDs can be accounted using /proc//fd/* and /proc//fdinfo -- both are only readable by the process owner, as follows: 1. Do a readlink on each FD. 2. If the target path begins with "/dmabuf", then the FD is a dmabuf FD. 3. stat the file to get the dmabuf inode number. 4. Read/ proc//fdinfo/, to get the DMA buffer size. Accessing other processes' fdinfo requires root privileges. This limits the use of the interface to debugging environments and is not suitable for production builds. Granting root privileges even to a system process increases the attack surface and is highly undesirable. Since fdinfo doesn't permit reading process memory and manipulating process state, allow accessing fdinfo under PTRACE_MODE_READ_FSCRED. Link: https://lkml.kernel.org/r/20210308170651.919148-1-kaleshsingh@google.= com Signed-off-by: Kalesh Singh Suggested-by: Jann Horn Acked-by: Christian K=C3=B6nig Cc: Alexander Viro Cc: Alexey Dobriyan Cc: Alexey Gladkov Cc: Andrei Vagin Cc: Bernd Edlinger Cc: Christian Brauner Cc: Eric W. Biederman Cc: Helge Deller Cc: Hridya Valsaraju Cc: James Morris Cc: Jeff Vander Stoep Cc: Jonathan Corbet Cc: Kees Cook Cc: Matthew Wilcox Cc: Mauro Carvalho Chehab Cc: Michal Hocko Cc: Michel Lespinasse Cc: Minchan Kim Cc: Randy Dunlap Cc: Suren Baghdasaryan Cc: Szabolcs Nagy Signed-off-by: Andrew Morton --- fs/proc/base.c | 4 ++-- fs/proc/fd.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) --- a/fs/proc/base.c~procfs-allow-reading-fdinfo-with-ptrace_mode_read +++ a/fs/proc/base.c @@ -3172,7 +3172,7 @@ static const struct pid_entry tgid_base_ DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_= operations), DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_oper= ations), DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_= map_files_operations), - DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdi= nfo_operations), + DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdi= nfo_operations), DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_op= erations), #ifdef CONFIG_NET DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_op= erations), @@ -3517,7 +3517,7 @@ static const struct inode_operations pro */ static const struct pid_entry tid_base_stuff[] =3D { DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_opera= tions), - DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdin= fo_operations), + DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdin= fo_operations), DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_ope= rations), #ifdef CONFIG_NET DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_op= erations), --- a/fs/proc/fd.c~procfs-allow-reading-fdinfo-with-ptrace_mode_read +++ a/fs/proc/fd.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,18 @@ out: =20 static int seq_fdinfo_open(struct inode *inode, struct file *file) { + bool allowed =3D false; + struct task_struct *task =3D get_proc_task(inode); + + if (!task) + return -ESRCH; + + allowed =3D ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); + put_task_struct(task); + + if (!allowed) + return -EACCES; + return single_open(file, seq_show, inode); } =20 @@ -308,7 +321,7 @@ static struct dentry *proc_fdinfo_instan struct proc_inode *ei; struct inode *inode; =20 - inode =3D proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUSR); + inode =3D proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO); if (!inode) return ERR_PTR(-ENOENT); =20 _