License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 07:07:57 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-02-08 10:51:30 -07:00
|
|
|
#include <linux/sched/signal.h>
|
2012-08-23 03:43:24 -07:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/dcache.h>
|
|
|
|
#include <linux/path.h>
|
|
|
|
#include <linux/fdtable.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/pid.h>
|
2021-06-30 18:54:44 -07:00
|
|
|
#include <linux/ptrace.h>
|
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the
only way to get this number is to enumerate the files in /proc/pid/fd.
The problem with the current approach is that it does many things people
generally don't care about when they need one number for a metric. In our
tests for cadvisor, which reports open file counts per cgroup, we observed
that reading the number of open files is slow. Out of 35.23% of CPU time
spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`,
which is responsible for filling dentry info. Some of this extra time is
spinlock contention, but it's a contention for the lock we don't want to
take to begin with.
We considered putting the number of open files in /proc/pid/status.
Unfortunately, counting the number of fds involves iterating the
open_files bitmap, which has a linear complexity in proportion with the
number of open files (bitmap slots really, but it's close). We don't want
to make /proc/pid/status any slower, so instead we put this info in
/proc/pid/fd as a size member of the stat syscall result. Previously the
reported number was zero, so there's very little risk of breaking
anything, while still providing a somewhat logical way to count the open
files with a fallback if it's zero.
RFC for this patch included iterating open fds under RCU. Thanks to Frank
Hofmann for the suggestion to use the bitmap instead.
Previously:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 0 Blocks: 0 IO Block: 1024 directory
```
With this patch:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 65 Blocks: 0 IO Block: 1024 directory
```
Correctness check:
```
$ sudo ls /proc/1/fd | wc -l
65
```
I added the docs for /proc/<pid>/fd while I'm at it.
[ivan@cloudflare.com: use bitmap_weight() to count the bits]
Link: https://lkml.kernel.org/r/20221018045844.37697-1-ivan@cloudflare.com
[akpm@linux-foundation.org: include linux/bitmap.h for bitmap_weight()]
[ivan@cloudflare.com: return errno from proc_fd_getattr() instead of setting negative size]
Link: https://lkml.kernel.org/r/20221024173140.30673-1-ivan@cloudflare.com
Link: https://lkml.kernel.org/r/20220922224027.59266-1-ivan@cloudflare.com
Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-09-22 15:40:26 -07:00
|
|
|
#include <linux/bitmap.h>
|
2012-08-23 03:43:24 -07:00
|
|
|
#include <linux/security.h>
|
2012-08-26 07:28:20 -07:00
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/seq_file.h>
|
2015-04-16 12:49:38 -07:00
|
|
|
#include <linux/fs.h>
|
2022-11-20 07:15:34 -07:00
|
|
|
#include <linux/filelock.h>
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
|
2014-04-07 15:38:34 -07:00
|
|
|
#include "../mount.h"
|
2012-08-23 03:43:24 -07:00
|
|
|
#include "internal.h"
|
|
|
|
#include "fd.h"
|
|
|
|
|
2012-08-26 07:28:20 -07:00
|
|
|
static int seq_show(struct seq_file *m, void *v)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
|
|
|
struct files_struct *files = NULL;
|
2012-08-26 07:28:20 -07:00
|
|
|
int f_flags = 0, ret = -ENOENT;
|
|
|
|
struct file *file = NULL;
|
|
|
|
struct task_struct *task;
|
|
|
|
|
|
|
|
task = get_proc_task(m->private);
|
|
|
|
if (!task)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2020-11-20 16:14:34 -07:00
|
|
|
task_lock(task);
|
|
|
|
files = task->files;
|
2012-08-23 03:43:24 -07:00
|
|
|
if (files) {
|
2016-09-01 14:42:02 -07:00
|
|
|
unsigned int fd = proc_fd(m->private);
|
2012-08-26 07:28:20 -07:00
|
|
|
|
2012-08-23 03:43:24 -07:00
|
|
|
spin_lock(&files->file_lock);
|
2020-11-20 16:14:25 -07:00
|
|
|
file = files_lookup_fd_locked(files, fd);
|
2012-08-23 03:43:24 -07:00
|
|
|
if (file) {
|
2012-08-26 08:01:04 -07:00
|
|
|
f_flags = file->f_flags;
|
2024-01-04 19:35:38 -07:00
|
|
|
if (close_on_exec(fd, files))
|
2012-08-23 03:43:24 -07:00
|
|
|
f_flags |= O_CLOEXEC;
|
|
|
|
|
2012-08-26 07:28:20 -07:00
|
|
|
get_file(file);
|
|
|
|
ret = 0;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
spin_unlock(&files->file_lock);
|
|
|
|
}
|
2020-11-20 16:14:34 -07:00
|
|
|
task_unlock(task);
|
|
|
|
put_task_struct(task);
|
2012-08-26 07:28:20 -07:00
|
|
|
|
2015-04-16 12:49:38 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2021-06-30 18:54:49 -07:00
|
|
|
seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n",
|
2015-04-16 12:49:38 -07:00
|
|
|
(long long)file->f_pos, f_flags,
|
2021-06-30 18:54:49 -07:00
|
|
|
real_mount(file->f_path.mnt)->mnt_id,
|
|
|
|
file_inode(file)->i_ino);
|
2015-04-16 12:49:38 -07:00
|
|
|
|
2024-09-08 23:33:53 -07:00
|
|
|
/* show_fd_locks() never dereferences files, so a stale value is safe */
|
2015-04-16 12:49:38 -07:00
|
|
|
show_fd_locks(m, file, files);
|
|
|
|
if (seq_has_overflowed(m))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (file->f_op->show_fdinfo)
|
|
|
|
file->f_op->show_fdinfo(m, file);
|
2012-08-26 07:28:20 -07:00
|
|
|
|
2015-04-16 12:49:38 -07:00
|
|
|
out:
|
|
|
|
fput(file);
|
|
|
|
return 0;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
static int seq_fdinfo_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, seq_show, inode);
|
|
|
|
}
|
|
|
|
|
2024-10-18 03:27:03 -07:00
|
|
|
/*
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
* Shared /proc/pid/fdinfo and /proc/pid/fdinfo/fd permission helper to ensure
|
|
|
|
* that the current task has PTRACE_MODE_READ in addition to the normal
|
|
|
|
* POSIX-like checks.
|
|
|
|
*/
|
|
|
|
static int proc_fdinfo_permission(struct mnt_idmap *idmap, struct inode *inode,
|
|
|
|
int mask)
|
2012-08-26 07:28:20 -07:00
|
|
|
{
|
2021-06-30 18:54:44 -07:00
|
|
|
bool allowed = false;
|
|
|
|
struct task_struct *task = get_proc_task(inode);
|
|
|
|
|
|
|
|
if (!task)
|
|
|
|
return -ESRCH;
|
|
|
|
|
|
|
|
allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
|
|
|
|
put_task_struct(task);
|
|
|
|
|
|
|
|
if (!allowed)
|
|
|
|
return -EACCES;
|
|
|
|
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
return generic_permission(idmap, inode, mask);
|
2022-05-09 17:34:28 -07:00
|
|
|
}
|
|
|
|
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
static const struct inode_operations proc_fdinfo_file_inode_operations = {
|
|
|
|
.permission = proc_fdinfo_permission,
|
|
|
|
.setattr = proc_setattr,
|
|
|
|
};
|
2012-08-26 07:28:20 -07:00
|
|
|
|
|
|
|
static const struct file_operations proc_fdinfo_file_operations = {
|
|
|
|
.open = seq_fdinfo_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
2018-05-02 19:42:22 -07:00
|
|
|
static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode)
|
|
|
|
{
|
|
|
|
struct file *file;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2023-09-28 23:45:59 -07:00
|
|
|
file = task_lookup_fdget_rcu(task, fd);
|
2018-05-02 19:42:22 -07:00
|
|
|
rcu_read_unlock();
|
2023-09-28 23:45:59 -07:00
|
|
|
if (file) {
|
|
|
|
*mode = file->f_mode;
|
|
|
|
fput(file);
|
|
|
|
}
|
2018-05-02 19:42:22 -07:00
|
|
|
return !!file;
|
|
|
|
}
|
|
|
|
|
2018-05-03 06:00:33 -07:00
|
|
|
static void tid_fd_update_inode(struct task_struct *task, struct inode *inode,
|
|
|
|
fmode_t f_mode)
|
|
|
|
{
|
|
|
|
task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
|
|
|
|
|
|
|
|
if (S_ISLNK(inode->i_mode)) {
|
|
|
|
unsigned i_mode = S_IFLNK;
|
|
|
|
if (f_mode & FMODE_READ)
|
|
|
|
i_mode |= S_IRUSR | S_IXUSR;
|
|
|
|
if (f_mode & FMODE_WRITE)
|
|
|
|
i_mode |= S_IWUSR | S_IXUSR;
|
|
|
|
inode->i_mode = i_mode;
|
|
|
|
}
|
|
|
|
security_task_to_inode(task, inode);
|
|
|
|
}
|
|
|
|
|
2012-08-23 03:43:24 -07:00
|
|
|
static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
|
|
|
|
{
|
|
|
|
struct task_struct *task;
|
|
|
|
struct inode *inode;
|
2016-09-01 14:42:02 -07:00
|
|
|
unsigned int fd;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
if (flags & LOOKUP_RCU)
|
|
|
|
return -ECHILD;
|
|
|
|
|
2015-03-17 15:25:59 -07:00
|
|
|
inode = d_inode(dentry);
|
2012-08-23 03:43:24 -07:00
|
|
|
task = get_proc_task(inode);
|
|
|
|
fd = proc_fd(inode);
|
|
|
|
|
|
|
|
if (task) {
|
2018-05-03 06:00:33 -07:00
|
|
|
fmode_t f_mode;
|
2018-05-02 19:42:22 -07:00
|
|
|
if (tid_fd_mode(task, fd, &f_mode)) {
|
2018-05-03 06:00:33 -07:00
|
|
|
tid_fd_update_inode(task, inode, f_mode);
|
2018-05-02 19:42:22 -07:00
|
|
|
put_task_struct(task);
|
|
|
|
return 1;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
put_task_struct(task);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dentry_operations tid_fd_dentry_operations = {
|
|
|
|
.d_revalidate = tid_fd_revalidate,
|
|
|
|
.d_delete = pid_delete_dentry,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int proc_fd_link(struct dentry *dentry, struct path *path)
|
|
|
|
{
|
2012-08-26 07:28:20 -07:00
|
|
|
struct task_struct *task;
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2015-03-17 15:25:59 -07:00
|
|
|
task = get_proc_task(d_inode(dentry));
|
2012-08-26 07:28:20 -07:00
|
|
|
if (task) {
|
2016-09-01 14:42:02 -07:00
|
|
|
unsigned int fd = proc_fd(d_inode(dentry));
|
2012-08-26 07:28:20 -07:00
|
|
|
struct file *fd_file;
|
|
|
|
|
2020-11-20 16:14:23 -07:00
|
|
|
fd_file = fget_task(task, fd);
|
2012-08-26 07:28:20 -07:00
|
|
|
if (fd_file) {
|
|
|
|
*path = fd_file->f_path;
|
|
|
|
path_get(&fd_file->f_path);
|
|
|
|
ret = 0;
|
2020-11-20 16:14:23 -07:00
|
|
|
fput(fd_file);
|
2012-08-26 07:28:20 -07:00
|
|
|
}
|
2020-11-20 16:14:23 -07:00
|
|
|
put_task_struct(task);
|
2012-08-26 07:28:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
2018-05-03 06:00:33 -07:00
|
|
|
struct fd_data {
|
|
|
|
fmode_t mode;
|
|
|
|
unsigned fd;
|
|
|
|
};
|
|
|
|
|
2018-05-03 06:21:05 -07:00
|
|
|
static struct dentry *proc_fd_instantiate(struct dentry *dentry,
|
|
|
|
struct task_struct *task, const void *ptr)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2018-05-03 06:00:33 -07:00
|
|
|
const struct fd_data *data = ptr;
|
2012-08-23 03:43:24 -07:00
|
|
|
struct proc_inode *ei;
|
|
|
|
struct inode *inode;
|
|
|
|
|
2018-05-03 06:21:05 -07:00
|
|
|
inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK);
|
2012-08-23 03:43:24 -07:00
|
|
|
if (!inode)
|
2018-05-03 06:21:05 -07:00
|
|
|
return ERR_PTR(-ENOENT);
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
ei = PROC_I(inode);
|
2018-05-03 06:00:33 -07:00
|
|
|
ei->fd = data->fd;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
inode->i_op = &proc_pid_link_inode_operations;
|
|
|
|
inode->i_size = 64;
|
|
|
|
|
|
|
|
ei->op.proc_get_link = proc_fd_link;
|
2018-05-03 06:00:33 -07:00
|
|
|
tid_fd_update_inode(task, inode, data->mode);
|
2012-08-23 03:43:24 -07:00
|
|
|
|
2024-08-06 09:02:31 -07:00
|
|
|
return proc_splice_unmountable(inode, dentry,
|
|
|
|
&tid_fd_dentry_operations);
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *proc_lookupfd_common(struct inode *dir,
|
|
|
|
struct dentry *dentry,
|
|
|
|
instantiate_t instantiate)
|
|
|
|
{
|
|
|
|
struct task_struct *task = get_proc_task(dir);
|
2018-05-03 06:00:33 -07:00
|
|
|
struct fd_data data = {.fd = name_to_int(&dentry->d_name)};
|
2018-05-03 06:21:05 -07:00
|
|
|
struct dentry *result = ERR_PTR(-ENOENT);
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
if (!task)
|
|
|
|
goto out_no_task;
|
2018-05-03 06:00:33 -07:00
|
|
|
if (data.fd == ~0U)
|
2012-08-23 03:43:24 -07:00
|
|
|
goto out;
|
2018-05-03 06:00:33 -07:00
|
|
|
if (!tid_fd_mode(task, data.fd, &data.mode))
|
2018-05-02 19:42:22 -07:00
|
|
|
goto out;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
2018-05-03 06:21:05 -07:00
|
|
|
result = instantiate(dentry, task, &data);
|
2012-08-23 03:43:24 -07:00
|
|
|
out:
|
|
|
|
put_task_struct(task);
|
|
|
|
out_no_task:
|
2018-05-03 06:21:05 -07:00
|
|
|
return result;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
2013-05-16 09:07:31 -07:00
|
|
|
static int proc_readfd_common(struct file *file, struct dir_context *ctx,
|
|
|
|
instantiate_t instantiate)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2013-05-16 09:07:31 -07:00
|
|
|
struct task_struct *p = get_proc_task(file_inode(file));
|
|
|
|
unsigned int fd;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
if (!p)
|
2013-05-16 09:07:31 -07:00
|
|
|
return -ENOENT;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
2013-05-16 09:07:31 -07:00
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2020-11-20 16:14:32 -07:00
|
|
|
for (fd = ctx->pos - 2;; fd++) {
|
2018-05-03 06:00:33 -07:00
|
|
|
struct file *f;
|
|
|
|
struct fd_data data;
|
2018-02-06 16:36:51 -07:00
|
|
|
char name[10 + 1];
|
2018-06-07 17:10:10 -07:00
|
|
|
unsigned int len;
|
2013-05-16 09:07:31 -07:00
|
|
|
|
2023-09-28 23:45:59 -07:00
|
|
|
f = task_lookup_next_fdget_rcu(p, &fd);
|
2020-11-20 16:14:32 -07:00
|
|
|
ctx->pos = fd + 2LL;
|
2018-05-03 06:00:33 -07:00
|
|
|
if (!f)
|
2020-11-20 16:14:32 -07:00
|
|
|
break;
|
2018-05-03 06:00:33 -07:00
|
|
|
data.mode = f->f_mode;
|
2013-05-16 09:07:31 -07:00
|
|
|
rcu_read_unlock();
|
2023-09-28 23:45:59 -07:00
|
|
|
fput(f);
|
2018-05-03 06:00:33 -07:00
|
|
|
data.fd = fd;
|
2013-05-16 09:07:31 -07:00
|
|
|
|
2016-09-01 14:42:02 -07:00
|
|
|
len = snprintf(name, sizeof(name), "%u", fd);
|
2013-05-16 09:07:31 -07:00
|
|
|
if (!proc_fill_cache(file, ctx,
|
|
|
|
name, len, instantiate, p,
|
2018-05-03 06:00:33 -07:00
|
|
|
&data))
|
2020-11-20 16:14:32 -07:00
|
|
|
goto out;
|
2015-12-03 12:12:07 -07:00
|
|
|
cond_resched();
|
2013-05-16 09:07:31 -07:00
|
|
|
rcu_read_lock();
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
2013-05-16 09:07:31 -07:00
|
|
|
rcu_read_unlock();
|
2012-08-23 03:43:24 -07:00
|
|
|
out:
|
|
|
|
put_task_struct(p);
|
2013-05-16 09:07:31 -07:00
|
|
|
return 0;
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the
only way to get this number is to enumerate the files in /proc/pid/fd.
The problem with the current approach is that it does many things people
generally don't care about when they need one number for a metric. In our
tests for cadvisor, which reports open file counts per cgroup, we observed
that reading the number of open files is slow. Out of 35.23% of CPU time
spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`,
which is responsible for filling dentry info. Some of this extra time is
spinlock contention, but it's a contention for the lock we don't want to
take to begin with.
We considered putting the number of open files in /proc/pid/status.
Unfortunately, counting the number of fds involves iterating the
open_files bitmap, which has a linear complexity in proportion with the
number of open files (bitmap slots really, but it's close). We don't want
to make /proc/pid/status any slower, so instead we put this info in
/proc/pid/fd as a size member of the stat syscall result. Previously the
reported number was zero, so there's very little risk of breaking
anything, while still providing a somewhat logical way to count the open
files with a fallback if it's zero.
RFC for this patch included iterating open fds under RCU. Thanks to Frank
Hofmann for the suggestion to use the bitmap instead.
Previously:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 0 Blocks: 0 IO Block: 1024 directory
```
With this patch:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 65 Blocks: 0 IO Block: 1024 directory
```
Correctness check:
```
$ sudo ls /proc/1/fd | wc -l
65
```
I added the docs for /proc/<pid>/fd while I'm at it.
[ivan@cloudflare.com: use bitmap_weight() to count the bits]
Link: https://lkml.kernel.org/r/20221018045844.37697-1-ivan@cloudflare.com
[akpm@linux-foundation.org: include linux/bitmap.h for bitmap_weight()]
[ivan@cloudflare.com: return errno from proc_fd_getattr() instead of setting negative size]
Link: https://lkml.kernel.org/r/20221024173140.30673-1-ivan@cloudflare.com
Link: https://lkml.kernel.org/r/20220922224027.59266-1-ivan@cloudflare.com
Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-09-22 15:40:26 -07:00
|
|
|
static int proc_readfd_count(struct inode *inode, loff_t *count)
|
|
|
|
{
|
|
|
|
struct task_struct *p = get_proc_task(inode);
|
|
|
|
struct fdtable *fdt;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
task_lock(p);
|
|
|
|
if (p->files) {
|
|
|
|
rcu_read_lock();
|
|
|
|
|
|
|
|
fdt = files_fdtable(p->files);
|
|
|
|
*count = bitmap_weight(fdt->open_fds, fdt->max_fds);
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
task_unlock(p);
|
|
|
|
|
|
|
|
put_task_struct(p);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-08-06 09:02:27 -07:00
|
|
|
static int proc_fd_iterate(struct file *file, struct dir_context *ctx)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2013-05-16 09:07:31 -07:00
|
|
|
return proc_readfd_common(file, ctx, proc_fd_instantiate);
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct file_operations proc_fd_operations = {
|
|
|
|
.read = generic_read_dir,
|
2024-08-06 09:02:27 -07:00
|
|
|
.iterate_shared = proc_fd_iterate,
|
2016-04-20 14:13:54 -07:00
|
|
|
.llseek = generic_file_llseek,
|
2012-08-23 03:43:24 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* /proc/pid/fd needs a special permission handler so that a process can still
|
|
|
|
* access /proc/self/fd after it has executed a setuid().
|
|
|
|
*/
|
2023-01-13 04:49:22 -07:00
|
|
|
int proc_fd_permission(struct mnt_idmap *idmap,
|
2021-01-21 06:19:43 -07:00
|
|
|
struct inode *inode, int mask)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2015-11-06 17:30:06 -07:00
|
|
|
struct task_struct *p;
|
|
|
|
int rv;
|
|
|
|
|
2023-01-13 04:49:22 -07:00
|
|
|
rv = generic_permission(&nop_mnt_idmap, inode, mask);
|
2012-08-23 03:43:24 -07:00
|
|
|
if (rv == 0)
|
2015-11-06 17:30:06 -07:00
|
|
|
return rv;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
p = pid_task(proc_pid(inode), PIDTYPE_PID);
|
|
|
|
if (p && same_thread_group(p, current))
|
2012-08-23 03:43:24 -07:00
|
|
|
rv = 0;
|
2015-11-06 17:30:06 -07:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
2012-08-23 03:43:24 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2023-01-13 04:49:12 -07:00
|
|
|
static int proc_fd_getattr(struct mnt_idmap *idmap,
|
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the
only way to get this number is to enumerate the files in /proc/pid/fd.
The problem with the current approach is that it does many things people
generally don't care about when they need one number for a metric. In our
tests for cadvisor, which reports open file counts per cgroup, we observed
that reading the number of open files is slow. Out of 35.23% of CPU time
spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`,
which is responsible for filling dentry info. Some of this extra time is
spinlock contention, but it's a contention for the lock we don't want to
take to begin with.
We considered putting the number of open files in /proc/pid/status.
Unfortunately, counting the number of fds involves iterating the
open_files bitmap, which has a linear complexity in proportion with the
number of open files (bitmap slots really, but it's close). We don't want
to make /proc/pid/status any slower, so instead we put this info in
/proc/pid/fd as a size member of the stat syscall result. Previously the
reported number was zero, so there's very little risk of breaking
anything, while still providing a somewhat logical way to count the open
files with a fallback if it's zero.
RFC for this patch included iterating open fds under RCU. Thanks to Frank
Hofmann for the suggestion to use the bitmap instead.
Previously:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 0 Blocks: 0 IO Block: 1024 directory
```
With this patch:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 65 Blocks: 0 IO Block: 1024 directory
```
Correctness check:
```
$ sudo ls /proc/1/fd | wc -l
65
```
I added the docs for /proc/<pid>/fd while I'm at it.
[ivan@cloudflare.com: use bitmap_weight() to count the bits]
Link: https://lkml.kernel.org/r/20221018045844.37697-1-ivan@cloudflare.com
[akpm@linux-foundation.org: include linux/bitmap.h for bitmap_weight()]
[ivan@cloudflare.com: return errno from proc_fd_getattr() instead of setting negative size]
Link: https://lkml.kernel.org/r/20221024173140.30673-1-ivan@cloudflare.com
Link: https://lkml.kernel.org/r/20220922224027.59266-1-ivan@cloudflare.com
Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-09-22 15:40:26 -07:00
|
|
|
const struct path *path, struct kstat *stat,
|
|
|
|
u32 request_mask, unsigned int query_flags)
|
|
|
|
{
|
|
|
|
struct inode *inode = d_inode(path->dentry);
|
|
|
|
int rv = 0;
|
|
|
|
|
2023-08-07 12:38:33 -07:00
|
|
|
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
|
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the
only way to get this number is to enumerate the files in /proc/pid/fd.
The problem with the current approach is that it does many things people
generally don't care about when they need one number for a metric. In our
tests for cadvisor, which reports open file counts per cgroup, we observed
that reading the number of open files is slow. Out of 35.23% of CPU time
spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`,
which is responsible for filling dentry info. Some of this extra time is
spinlock contention, but it's a contention for the lock we don't want to
take to begin with.
We considered putting the number of open files in /proc/pid/status.
Unfortunately, counting the number of fds involves iterating the
open_files bitmap, which has a linear complexity in proportion with the
number of open files (bitmap slots really, but it's close). We don't want
to make /proc/pid/status any slower, so instead we put this info in
/proc/pid/fd as a size member of the stat syscall result. Previously the
reported number was zero, so there's very little risk of breaking
anything, while still providing a somewhat logical way to count the open
files with a fallback if it's zero.
RFC for this patch included iterating open fds under RCU. Thanks to Frank
Hofmann for the suggestion to use the bitmap instead.
Previously:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 0 Blocks: 0 IO Block: 1024 directory
```
With this patch:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 65 Blocks: 0 IO Block: 1024 directory
```
Correctness check:
```
$ sudo ls /proc/1/fd | wc -l
65
```
I added the docs for /proc/<pid>/fd while I'm at it.
[ivan@cloudflare.com: use bitmap_weight() to count the bits]
Link: https://lkml.kernel.org/r/20221018045844.37697-1-ivan@cloudflare.com
[akpm@linux-foundation.org: include linux/bitmap.h for bitmap_weight()]
[ivan@cloudflare.com: return errno from proc_fd_getattr() instead of setting negative size]
Link: https://lkml.kernel.org/r/20221024173140.30673-1-ivan@cloudflare.com
Link: https://lkml.kernel.org/r/20220922224027.59266-1-ivan@cloudflare.com
Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-09-22 15:40:26 -07:00
|
|
|
|
|
|
|
/* If it's a directory, put the number of open fds there */
|
|
|
|
if (S_ISDIR(inode->i_mode)) {
|
|
|
|
rv = proc_readfd_count(inode, &stat->size);
|
|
|
|
if (rv < 0)
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-23 03:43:24 -07:00
|
|
|
const struct inode_operations proc_fd_inode_operations = {
|
|
|
|
.lookup = proc_lookupfd,
|
|
|
|
.permission = proc_fd_permission,
|
proc: report open files as size in stat() for /proc/pid/fd
Many monitoring tools include open file count as a metric. Currently the
only way to get this number is to enumerate the files in /proc/pid/fd.
The problem with the current approach is that it does many things people
generally don't care about when they need one number for a metric. In our
tests for cadvisor, which reports open file counts per cgroup, we observed
that reading the number of open files is slow. Out of 35.23% of CPU time
spent in `proc_readfd_common`, we see 29.43% spent in `proc_fill_cache`,
which is responsible for filling dentry info. Some of this extra time is
spinlock contention, but it's a contention for the lock we don't want to
take to begin with.
We considered putting the number of open files in /proc/pid/status.
Unfortunately, counting the number of fds involves iterating the
open_files bitmap, which has a linear complexity in proportion with the
number of open files (bitmap slots really, but it's close). We don't want
to make /proc/pid/status any slower, so instead we put this info in
/proc/pid/fd as a size member of the stat syscall result. Previously the
reported number was zero, so there's very little risk of breaking
anything, while still providing a somewhat logical way to count the open
files with a fallback if it's zero.
RFC for this patch included iterating open fds under RCU. Thanks to Frank
Hofmann for the suggestion to use the bitmap instead.
Previously:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 0 Blocks: 0 IO Block: 1024 directory
```
With this patch:
```
$ sudo stat /proc/1/fd | head -n2
File: /proc/1/fd
Size: 65 Blocks: 0 IO Block: 1024 directory
```
Correctness check:
```
$ sudo ls /proc/1/fd | wc -l
65
```
I added the docs for /proc/<pid>/fd while I'm at it.
[ivan@cloudflare.com: use bitmap_weight() to count the bits]
Link: https://lkml.kernel.org/r/20221018045844.37697-1-ivan@cloudflare.com
[akpm@linux-foundation.org: include linux/bitmap.h for bitmap_weight()]
[ivan@cloudflare.com: return errno from proc_fd_getattr() instead of setting negative size]
Link: https://lkml.kernel.org/r/20221024173140.30673-1-ivan@cloudflare.com
Link: https://lkml.kernel.org/r/20220922224027.59266-1-ivan@cloudflare.com
Signed-off-by: Ivan Babrou <ivan@cloudflare.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2022-09-22 15:40:26 -07:00
|
|
|
.getattr = proc_fd_getattr,
|
2012-08-23 03:43:24 -07:00
|
|
|
.setattr = proc_setattr,
|
|
|
|
};
|
|
|
|
|
2018-05-03 06:21:05 -07:00
|
|
|
static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry,
|
|
|
|
struct task_struct *task, const void *ptr)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2018-05-03 06:00:33 -07:00
|
|
|
const struct fd_data *data = ptr;
|
2012-08-23 03:43:24 -07:00
|
|
|
struct proc_inode *ei;
|
|
|
|
struct inode *inode;
|
|
|
|
|
2021-06-30 18:54:44 -07:00
|
|
|
inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO);
|
2012-08-23 03:43:24 -07:00
|
|
|
if (!inode)
|
2018-05-03 06:21:05 -07:00
|
|
|
return ERR_PTR(-ENOENT);
|
2012-08-23 03:43:24 -07:00
|
|
|
|
|
|
|
ei = PROC_I(inode);
|
2018-05-03 06:00:33 -07:00
|
|
|
ei->fd = data->fd;
|
2012-08-23 03:43:24 -07:00
|
|
|
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
inode->i_op = &proc_fdinfo_file_inode_operations;
|
|
|
|
|
2012-08-23 03:43:24 -07:00
|
|
|
inode->i_fop = &proc_fdinfo_file_operations;
|
2018-05-03 06:00:33 -07:00
|
|
|
tid_fd_update_inode(task, inode, 0);
|
2012-08-23 03:43:24 -07:00
|
|
|
|
2024-08-06 09:02:32 -07:00
|
|
|
return proc_splice_unmountable(inode, dentry,
|
|
|
|
&tid_fd_dentry_operations);
|
2012-08-23 03:43:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct dentry *
|
|
|
|
proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags)
|
|
|
|
{
|
|
|
|
return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
|
|
|
|
}
|
|
|
|
|
2024-08-06 09:02:28 -07:00
|
|
|
static int proc_fdinfo_iterate(struct file *file, struct dir_context *ctx)
|
2012-08-23 03:43:24 -07:00
|
|
|
{
|
2013-05-16 09:07:31 -07:00
|
|
|
return proc_readfd_common(file, ctx,
|
2012-08-23 03:43:24 -07:00
|
|
|
proc_fdinfo_instantiate);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct inode_operations proc_fdinfo_inode_operations = {
|
|
|
|
.lookup = proc_lookupfdinfo,
|
proc: Move fdinfo PTRACE_MODE_READ check into the inode .permission operation
The following commits loosened the permissions of /proc/<PID>/fdinfo/
directory, as well as the files within it, from 0500 to 0555 while also
introducing a PTRACE_MODE_READ check between the current task and
<PID>'s task:
- commit 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
- commit 1927e498aee1 ("procfs: prevent unprivileged processes accessing fdinfo dir")
Before those changes, inode based system calls like inotify_add_watch(2)
would fail when the current task didn't have sufficient read permissions:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0500, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
This matches the documented behavior in the inotify_add_watch(2) man
page:
ERRORS
EACCES Read access to the given file is not permitted.
After those changes, inotify_add_watch(2) started succeeding despite the
current task not having PTRACE_MODE_READ privileges on the target task:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 1757
openat(AT_FDCWD, "/proc/1/task/1/fdinfo",
O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 EACCES (Permission denied)
[...]
This change in behavior broke .NET prior to v7. See the github link
below for the v7 commit that inadvertently/quietly (?) fixed .NET after
the kernel changes mentioned above.
Return to the old behavior by moving the PTRACE_MODE_READ check out of
the file .open operation and into the inode .permission operation:
[...]
lstat("/proc/1/task/1/fdinfo", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0
inotify_add_watch(64, "/proc/1/task/1/fdinfo",
IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|
IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = -1 EACCES (Permission denied)
[...]
Reported-by: Kevin Parsons (Microsoft) <parsonskev@gmail.com>
Link: https://github.com/dotnet/runtime/commit/89e5469ac591b82d38510fe7de98346cce74ad4f
Link: https://stackoverflow.com/questions/75379065/start-self-contained-net6-build-exe-as-service-on-raspbian-system-unauthorizeda
Fixes: 7bc3fa0172a4 ("procfs: allow reading fdinfo with PTRACE_MODE_READ")
Cc: stable@vger.kernel.org
Cc: Christian Brauner <brauner@kernel.org>
Cc: Christian König <christian.koenig@amd.com>
Cc: Jann Horn <jannh@google.com>
Cc: Kalesh Singh <kaleshsingh@google.com>
Cc: Hardik Garg <hargar@linux.microsoft.com>
Cc: Allen Pais <apais@linux.microsoft.com>
Signed-off-by: Tyler Hicks (Microsoft) <code@tyhicks.com>
Link: https://lore.kernel.org/r/20240501005646.745089-1-code@tyhicks.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-04-30 17:56:46 -07:00
|
|
|
.permission = proc_fdinfo_permission,
|
2012-08-23 03:43:24 -07:00
|
|
|
.setattr = proc_setattr,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct file_operations proc_fdinfo_operations = {
|
|
|
|
.read = generic_read_dir,
|
2024-08-06 09:02:28 -07:00
|
|
|
.iterate_shared = proc_fdinfo_iterate,
|
2016-04-20 14:13:54 -07:00
|
|
|
.llseek = generic_file_llseek,
|
2012-08-23 03:43:24 -07:00
|
|
|
};
|