FAILED: patch "[PATCH] proc: don't let an entry below /proc/<pid>/net be a mountpoint" failed to apply to 5.10-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x eeecea75ece2ba894bee4d3c53ed2e40abda8f0e
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From eeecea75ece2ba894bee4d3c53ed2e40abda8f0e Mon Sep 17 00:00:00 2001
From: Christian Brauner <[email protected]>
Date: Sat, 24 Oct 2026 12:38:27 +0200
Subject: [PATCH] proc: don't let an entry below /proc/<pid>/net be a
mountpoint
proc_net_d_revalidate() always returns 0 so that a dentry below /proc/<pid>/net is never reused after setns(). So lookup_fast() will end up calling d_invalidate(), which unhashes the dentry and detaches every mount on it, in every mount namespace; including locked mounts.
Then lookup_slow() creates a new dentry for the same entry. So a mount on /proc/<pid>/net/dev succeeds and goes away with the first lookup of the name by anybody with search permission on the directory. Given it's 0555 that's everyone's grandma. A mask on such a name hides nothing as the repro below shows.
Treat those entries like I did fd/, fdinfo/ and map_files/ and refuse to mount on them in the first place. It's nonsensical theater otherwise:
procnet net/dev stat
// SPDX-License-Identifier: GPL-2.0
/*
* procnet: cover an entry of /proc/<pid>/ with a bind mount, lock the mount,
* then look the name up.
*
* The first task stays alive, <pid> is its pid. The "container" task has its
* own mount namespace. There it binds a mask on /proc/<pid>/<name>. It drops to
* uid 1000 and enters a new user namespace with a new mount namespace, so that
* the mask is locked. Then one task looks the name up:
*
* stat the container calls stat() on the name * umount the container calls umount2(MNT_DETACH) on the name * nobody a task with uid 65534 in the first namespaces calls stat()
*
* The container lists its mounts before and after (that does not look the
* name up) and then reads the name. MASK is fine. Anything else means that
* the lookup uncovered the entry.
*
* usage: procnet <name> stat|umount|nobody
* name "status" is the control, "net/dev" is a file below net, "net/stat" is
* a directory below net
*/
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define WORK "/run/pn"
static char target[128];
static void die(const char *what)
{
fprintf(stderr, "FATAL %s: %s\n", what, strerror(errno));
exit(2);
}
static void write_str(const char *path, const char *s, int flags)
{
int fd = open(path, O_WRONLY | flags, 0644);
if (fd < 0 || write(fd, s, strlen(s)) < 0)
die(path);
close(fd);
}
/* list the mounts below /proc without a lookup of the target */
static void mounts(const char *when)
{
char line[512];
FILE *f = fopen("/proc/self/mountinfo", "r");
int n = 0;
if (!f)
die("mountinfo");
while (fgets(line, sizeof(line), f)) {
if (!strstr(line, " /proc/"))
continue;
printf("container: %s: mount: %s", when, line);
n++;
}
fclose(f);
if (!n)
printf("container: %s: nothing is mounted below /proc\n", when);
}
static void show(const char *when, int isdir)
{
char buf[48] = "";
int fd;
if (isdir) {
struct dirent *de;
DIR *d = opendir(target);
int hit = 0;
if (!d) {
printf("container: %s: opendir(%s) = %s\n", when, target, strerror(errno));
return;
}
printf("container: %s: readdir(%s) =", when, target);
while ((de = readdir(d))) {
if (de->d_name[0] == '.')
continue;
printf(" %s", de->d_name);
hit |= strcmp(de->d_name, "MASK");
}
closedir(d);
printf("%s\n", hit ? " <-- HIT" : "");
return;
}
fd = open(target, O_RDONLY);
if (fd < 0) {
printf("container: %s: open(%s) = %s\n", when, target, strerror(errno));
return;
}
if (read(fd, buf, sizeof(buf) - 1) < 0)
die("read");
close(fd);
if (strchr(buf, '\n'))
*strchr(buf, '\n') = 0;
printf("container: %s: read(%s) = \"%s\"%s\n", when, target, buf,
strncmp(buf, "MASK", 4) ? " <-- HIT" : "");
}
int main(int argc, char **argv)
{
const char *name = argc > 1 ? argv[1] : "net/dev";
const char *op = argc > 2 ? argv[2] : "stat";
int isdir = !strcmp(name, "net/stat");
int to_host[2], to_cont[2];
struct stat st;
pid_t pid;
char c;
setvbuf(stdout, NULL, _IOLBF, 0);
snprintf(target, sizeof(target), "/proc/%d/%s", getpid(), name);
if (unshare(CLONE_NEWNS) || mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
die("unshare");
mkdir(WORK, 0755);
if (mount("W", WORK, "tmpfs", 0, NULL))
die("mount W");
write_str(WORK "/mask", "MASK", O_CREAT);
mkdir(WORK "/maskdir", 0755);
write_str(WORK "/maskdir/MASK", "MASK", O_CREAT);
if (pipe(to_host) || pipe(to_cont))
die("pipe");
pid = fork();
if (pid < 0)
die("fork");
if (!pid) {
if (unshare(CLONE_NEWNS))
die("container unshare");
if (mount(isdir ? WORK "/maskdir" : WORK "/mask", target, NULL, MS_BIND, NULL)) {
printf("container: bind the mask on %s: %s\n", target, strerror(errno));
exit(0);
}
printf("container: bind the mask on %s: ok\n", target);
if (setgid(1000) || setuid(1000) || prctl(PR_SET_DUMPABLE, 1))
die("setuid");
/* lock the mount: a new user namespace with a copy of the tree */
if (unshare(CLONE_NEWUSER | CLONE_NEWNS))
die("container userns");
write_str("/proc/self/setgroups", "deny", 0);
write_str("/proc/self/uid_map", "0 1000 1", 0);
write_str("/proc/self/gid_map", "0 1000 1", 0);
mounts("before");
errno = 0;
if (!strcmp(op, "stat")) {
stat(target, &st);
printf("container: stat(%s): %s\n", target, strerror(errno));
} else if (!strcmp(op, "umount")) {
umount2(target, MNT_DETACH);
printf("container: umount2(%s, MNT_DETACH): %s\n", target, strerror(errno));
} else if (write(to_host[1], "g", 1) != 1 || read(to_cont[0], &c, 1) != 1) {
die("sync");
}
mounts("after");
show("after", isdir);
exit(0);
}
close(to_host[1]);
if (read(to_host[0], &c, 1) == 1) {
pid_t p2 = fork();
if (!p2) {
if (setgid(65534) || setuid(65534))
die("setuid");
errno = 0;
stat(target, &st);
printf("nobody: uid %u: stat(%s): %s\n", getuid(), target, strerror(errno));
exit(0);
}
waitpid(p2, NULL, 0);
if (write(to_cont[1], "g", 1) != 1)
die("sync");
}
waitpid(pid, NULL, 0);
return 0;
}--- fs/proc/generic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/generic.c +3 -2
@@ -256,9 +256,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, inode =proc_get_inode (dir->i_sb, de);if (!inode)return ERR_PTR (-ENOMEM ); +/* never reused and so never a mountpoint for long */ if (de->flags &PROC_ENTRY_FORCE_LOOKUP ) -return d_splice_alias_ops (inode, dentry, - &proc_net_dentry_ops); +return proc_splice_unmountable (inode, dentry, + &proc_net_dentry_ops);return d_splice_alias_ops (inode, dentry, &proc_misc_dentry_ops); }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/6 /proc/sys: use d_splice_alias() calling conventions to simplify failure exits
From: Al Viro <[email protected]>
[ Upstream commit 1eae9a47835ed4703055afbb3c52fef6d07a8a4c ]
Stable-dep-of: eeecea75ece2 ("proc: don't let an entry below /proc/<pid>/net be a mountpoint")
--- fs/proc/proc_sysctl.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
fs/proc/proc_sysctl.c +2 -12
@@ -524,13 +524,8 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, goto out; } - inode =proc_sys_make_inode (dir->i_sb, h ? h : head, p); -if (IS_ERR (inode)) { - err =ERR_CAST (inode); - goto out; - } -d_set_d_op (dentry, &proc_sys_dentry_operations); + inode =proc_sys_make_inode (dir->i_sb, h ? h : head, p); err =d_splice_alias (inode, dentry); out:
@@ -688,13 +683,8 @@ static bool proc_sys_fill_cache(struct file *file,return false;if (d_in_lookup (child)) {struct dentry *res; - inode =proc_sys_make_inode (dir->d_sb, head, table); -if (IS_ERR (inode)) { -d_lookup_done (child); -dput (child); -return false; - }d_set_d_op (child, &proc_sys_dentry_operations); + inode =proc_sys_make_inode (dir->d_sb, head, table); res =d_splice_alias (inode, child);d_lookup_done (child);if (unlikely (res)) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/6 proc: add proc_splice_unmountable()
From: Christian Brauner <[email protected]>
[ Upstream commit 32a0a965b8a5b7b505babae2ee10c513ede376d8 ]
Add a tiny procfs helper to splice a dentry that cannot be mounted upon.
Stable-dep-of: eeecea75ece2 ("proc: don't let an entry below /proc/<pid>/net be a mountpoint")
--- fs/proc/internal.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/proc/internal.h +13 -0
@@ -331,3 +331,16 @@ static inline void pde_force_lookup(struct proc_dir_entry *pde)/* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */ pde->proc_dops = &proc_net_dentry_ops; } + +/* +* Add a new procfs dentry that can't serve as a mountpoint. That should +* encompass anything that is ephemeral and can just disappear while the +* process is still around. +*/ +static inline struct dentry *proc_splice_unmountable (struct inode *inode, +struct dentry *dentry,const struct dentry_operations *d_ops) +{ +d_set_d_op (dentry, d_ops); +dont_mount (dentry); +return d_splice_alias (inode, dentry); +}
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/6 procfs: kill ->proc_dops
In reply to Sasha LevinFrom: Al Viro <[email protected]>
[ Upstream commit 5943c611c47c9444e834555c867dec744158b7ad ]
It has two possible values - one for "forced lookup" entries, another for the normal ones. We'd be better off with that as an explicit flag anyway and in addition to that it opens some fun possibilities with ->d_op and ->d_flags handling.
[moved PROC_ENTRY_FORCE_LOOKUP to include/linux/proc_fs.h, switched it to an unused bit - there was a conflict]
Stable-dep-of: eeecea75ece2 ("proc: don't let an entry below /proc/<pid>/net be a mountpoint")
--- fs/proc/generic.c | 8 +++++--- fs/proc/internal.h | 3 +-- include/linux/proc_fs.h | 2 ++ 3 files changed, 8 insertions(+), 5 deletions(-)
fs/proc/generic.c +5 -3
@@ -261,7 +261,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, inode =proc_get_inode (dir->i_sb, de);if (!inode)return ERR_PTR (-ENOMEM ); -d_set_d_op (dentry, de->proc_dops); +if (de->flags &PROC_ENTRY_FORCE_LOOKUP ) +d_set_d_op (dentry, &proc_net_dentry_ops); +else +d_set_d_op (dentry, &proc_misc_dentry_ops);return d_splice_alias (inode, dentry); }read_unlock (&proc_subdir_lock);
@@ -453,9 +456,8 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,INIT_LIST_HEAD (&ent->pde_openers);proc_set_user (ent, (*parent)->uid, (*parent)->gid); - ent->proc_dops = &proc_misc_dentry_ops;/* Revalidate everything under /proc/${pid}/net */ -if ((*parent)->proc_dops == &proc_net_dentry_ops) +if ((*parent)->flags &PROC_ENTRY_FORCE_LOOKUP )pde_force_lookup (ent); out:
fs/proc/internal.h +1 -2
@@ -43,7 +43,6 @@ struct proc_dir_entry {const struct proc_ops *proc_ops;const struct file_operations *proc_dir_ops; }; -const struct dentry_operations *proc_dops;union {const struct seq_operations *seq_ops;int (*single_show)(struct seq_file *,void *);
@@ -329,7 +328,7 @@ extern const struct dentry_operations proc_net_dentry_ops;static inline void pde_force_lookup (struct proc_dir_entry *pde) {/* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */ - pde->proc_dops = &proc_net_dentry_ops; + pde->flags |=PROC_ENTRY_FORCE_LOOKUP ; }/*
include/linux/proc_fs.h +2 -0
@@ -27,6 +27,8 @@ enum { PROC_ENTRY_proc_read_iter =1U <<1 , PROC_ENTRY_proc_compat_ioctl =1U <<2 , + + PROC_ENTRY_FORCE_LOOKUP = 1U << 7, }; struct proc_ops {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/6 new helper: d_splice_alias_ops()
In reply to Sasha LevinFrom: Al Viro <[email protected]>
[ Upstream commit 790fa81b8c43cda9fe25c1b564d0afe3ddeeb370 ]
Uses of d_set_d_op() on live dentry can be very dangerous; it is going to be withdrawn and replaced with saner things.
The best way for a filesystem is to have the default dentry_operations set at mount time and be done with that - __d_alloc() will use that.
Currently there are two cases when d_set_d_op() is used on a live dentry - one is procfs, which has several genuinely different dentry_operations instances (different ->d_revalidate(), etc.) and another is simple_lookup(), where we would be better off without overriding ->d_op.
For procfs we have d_set_d_op() calls followed by d_splice_alias(); provide a new helper (d_splice_alias_ops(inode, dentry, d_ops)) that would combine those two, and do the d_set_d_op() part while under ->d_lock. That eliminates one of the places where ->d_flags had been modified without holding ->d_lock; current behaviour is not racy, but the reasons for that are far too brittle. Better move to uniform locking rules and simpler proof of correctness...
The next commit will convert procfs to use of that helper; it is not exported and won't be until somebody comes up with convincing modular user for it.
Again, the best approach is to have default ->d_op and let __d_alloc() do the right thing; filesystem _may_ need non-uniform ->d_op (procfs does), but there'd better be good reasons for that.
Stable-dep-of: eeecea75ece2 ("proc: don't let an entry below /proc/<pid>/net be a mountpoint")
--- fs/dcache.c | 63 ++++++++++++++++++++++++------------------ include/linux/dcache.h | 3 ++ 2 files changed, 39 insertions(+), 27 deletions(-)
fs/dcache.c +36 -27
@@ -2668,7 +2668,8 @@ EXPORT_SYMBOL(__d_lookup_done);/* inode->i_lock held if inode is non-NULL */ -static inline void __d_add (struct dentry *dentry,struct inode *inode) +static inline void __d_add (struct dentry *dentry,struct inode *inode, +const struct dentry_operations *ops) {struct inode *dir =NULL ;unsigned n;
@@ -2678,6 +2679,8 @@ static inline void __d_add(struct dentry *dentry, struct inode *inode) n =start_dir_add (dir);__d_lookup_done (dentry); } +if (unlikely (ops)) +d_set_d_op (dentry, ops);if (inode) {unsigned add_flags =d_flags_for_inode (inode);hlist_add_head (&dentry->d_u.d_alias, &inode->i_dentry);
@@ -2709,7 +2712,7 @@ void d_add(struct dentry *entry, struct inode *inode)security_d_instantiate (entry, inode);spin_lock (&inode->i_lock); } -__d_add (entry, inode); +__d_add (entry, inode,NULL ); }EXPORT_SYMBOL (d_add);
@@ -3004,30 +3007,8 @@ static int __d_unalias(struct inode *inode,return ret; } -/** -* d_splice_alias - splice a disconnected dentry into the tree if one exists -* @inode: the inode which may have a disconnected dentry -* @dentry: a negative dentry which we want to point to the inode. -* -* If inode is a directory and has an IS_ROOT alias, then d_move that in -* place of the given dentry and return it, else simply d_add the inode -* to the dentry and return NULL. -* -* If a non-IS_ROOT directory is found, the filesystem is corrupt, and -* we should error out: directories can't have multiple aliases. -* -* This is needed in the lookup routine of any filesystem that is exportable -* (via knfsd) so that we can build dcache paths to directories effectively. -* -* If a dentry was found and moved, then it is returned. Otherwise NULL -* is returned. This matches the expected return value of ->lookup. -* -* Cluster filesystems may call this function with a negative, hashed dentry. -* In that case, we know that the inode will be a regular file, and also this -* will only occur during atomic_open. So we need to check for the dentry -* being already hashed only in the final case. -*/ -struct dentry *d_splice_alias (struct inode *inode,struct dentry *dentry) +struct dentry *d_splice_alias_ops (struct inode *inode,struct dentry *dentry, +const struct dentry_operations *ops) {if (IS_ERR (inode))return ERR_CAST (inode);
@@ -3073,9 +3054,37 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry) } } out: -__d_add (dentry, inode); +__d_add (dentry, inode, ops);return NULL ; } + +/** +* d_splice_alias - splice a disconnected dentry into the tree if one exists +* @inode: the inode which may have a disconnected dentry +* @dentry: a negative dentry which we want to point to the inode. +* +* If inode is a directory and has an IS_ROOT alias, then d_move that in +* place of the given dentry and return it, else simply d_add the inode +* to the dentry and return NULL. +* +* If a non-IS_ROOT directory is found, the filesystem is corrupt, and +* we should error out: directories can't have multiple aliases. +* +* This is needed in the lookup routine of any filesystem that is exportable +* (via knfsd) so that we can build dcache paths to directories effectively. +* +* If a dentry was found and moved, then it is returned. Otherwise NULL +* is returned. This matches the expected return value of ->lookup. +* +* Cluster filesystems may call this function with a negative, hashed dentry. +* In that case, we know that the inode will be a regular file, and also this +* will only occur during atomic_open. So we need to check for the dentry +* being already hashed only in the final case. +*/ +struct dentry *d_splice_alias (struct inode *inode,struct dentry *dentry) +{ +return d_splice_alias_ops (inode, dentry,NULL ); +}EXPORT_SYMBOL (d_splice_alias);/*
include/linux/dcache.h +3 -0
@@ -240,6 +240,9 @@ extern struct dentry * d_alloc_anon(struct super_block *);extern struct dentry *d_alloc_parallel (struct dentry *,const struct qstr *,wait_queue_head_t *);extern struct dentry *d_splice_alias (struct inode *,struct dentry *); +/* weird procfs mess; *NOT* exported */ +extern struct dentry *d_splice_alias_ops (struct inode *,struct dentry *, +const struct dentry_operations *);extern struct dentry *d_add_ci (struct dentry *,struct inode *,struct qstr *);extern struct dentry *d_exact_alias (struct dentry *,struct inode *);extern struct dentry *d_find_any_alias (struct inode *inode);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/6 switch procfs from d_set_d_op() to d_splice_alias_ops()
In reply to Sasha LevinFrom: Al Viro <[email protected]>
[ Upstream commit ec169ef86ba45389b353189cf1fc06461163f667 ]
Stable-dep-of: eeecea75ece2 ("proc: don't let an entry below /proc/<pid>/net be a mountpoint")
--- fs/proc/base.c | 9 +++------ fs/proc/generic.c | 8 ++++---- fs/proc/internal.h | 3 +-- fs/proc/namespaces.c | 3 +-- fs/proc/proc_sysctl.c | 7 +++---- 5 files changed, 12 insertions(+), 18 deletions(-)
fs/proc/base.c +3 -6
@@ -2685,8 +2685,7 @@ static struct dentry *proc_pident_instantiate(struct dentry *dentry, inode->i_fop = p->fop; ei->op = p->op;pid_update_inode (task, inode); -d_set_d_op (dentry, &pid_dentry_operations); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, &pid_dentry_operations); } static struct dentry *proc_pident_lookup(struct inode *dir,
@@ -3422,8 +3421,7 @@ static struct dentry *proc_pid_instantiate(struct dentry * dentry,set_nlink (inode, nlink_tgid);pid_update_inode (task, inode); -d_set_d_op (dentry, &pid_dentry_operations); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, &pid_dentry_operations); }struct dentry *proc_pid_lookup (struct dentry *dentry,unsigned int flags)
@@ -3719,8 +3717,7 @@ static struct dentry *proc_task_instantiate(struct dentry *dentry,set_nlink (inode, nlink_tid);pid_update_inode (task, inode); -d_set_d_op (dentry, &pid_dentry_operations); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, &pid_dentry_operations); }static struct dentry *proc_task_lookup (struct inode *dir,struct dentry * dentry,unsigned int flags)
fs/proc/generic.c +4 -4
@@ -262,10 +262,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,if (!inode)return ERR_PTR (-ENOMEM );if (de->flags &PROC_ENTRY_FORCE_LOOKUP ) -d_set_d_op (dentry, &proc_net_dentry_ops); -else -d_set_d_op (dentry, &proc_misc_dentry_ops); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, + &proc_net_dentry_ops); +return d_splice_alias_ops (inode, dentry, + &proc_misc_dentry_ops); }read_unlock (&proc_subdir_lock);return ERR_PTR (-ENOENT );
fs/proc/internal.h +1 -2
@@ -339,7 +339,6 @@ static inline void pde_force_lookup(struct proc_dir_entry *pde)static inline struct dentry *proc_splice_unmountable (struct inode *inode,struct dentry *dentry,const struct dentry_operations *d_ops) { -d_set_d_op (dentry, d_ops);dont_mount (dentry); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, d_ops); }
fs/proc/namespaces.c +1 -2
@@ -125,8 +125,7 @@ static struct dentry *proc_ns_instantiate(struct dentry *dentry, ei->ns_ops = ns_ops;pid_update_inode (task, inode); -d_set_d_op (dentry, &pid_dentry_operations); -return d_splice_alias (inode, dentry); +return d_splice_alias_ops (inode, dentry, &pid_dentry_operations); }static int proc_ns_dir_readdir (struct file *file,struct dir_context *ctx)
fs/proc/proc_sysctl.c +3 -4
@@ -524,9 +524,8 @@ static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, goto out; } -d_set_d_op (dentry, &proc_sys_dentry_operations); inode =proc_sys_make_inode (dir->i_sb, h ? h : head, p); - err =d_splice_alias (inode, dentry); + err =d_splice_alias_ops (inode, dentry, &proc_sys_dentry_operations); out:if (h)
@@ -683,9 +682,9 @@ static bool proc_sys_fill_cache(struct file *file,return false;if (d_in_lookup (child)) {struct dentry *res; -d_set_d_op (child, &proc_sys_dentry_operations); inode =proc_sys_make_inode (dir->d_sb, head, table); - res =d_splice_alias (inode, child); + res =d_splice_alias_ops (inode, child, + &proc_sys_dentry_operations);d_lookup_done (child);if (unlikely (res)) {if (IS_ERR (res)) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 6/6 proc: don't let an entry below /proc/<pid>/net be a mountpoint
In reply to Sasha LevinFrom: Christian Brauner <[email protected]>
[ Upstream commit eeecea75ece2ba894bee4d3c53ed2e40abda8f0e ]
proc_net_d_revalidate() always returns 0 so that a dentry below /proc/<pid>/net is never reused after setns(). So lookup_fast() will end up calling d_invalidate(), which unhashes the dentry and detaches every mount on it, in every mount namespace; including locked mounts.
Then lookup_slow() creates a new dentry for the same entry. So a mount on /proc/<pid>/net/dev succeeds and goes away with the first lookup of the name by anybody with search permission on the directory. Given it's 0555 that's everyone's grandma. A mask on such a name hides nothing as the repro below shows.
Treat those entries like I did fd/, fdinfo/ and map_files/ and refuse to mount on them in the first place. It's nonsensical theater otherwise:
procnet net/dev stat
// SPDX-License-Identifier: GPL-2.0
/*
* procnet: cover an entry of /proc/<pid>/ with a bind mount, lock the mount,
* then look the name up.
*
* The first task stays alive, <pid> is its pid. The "container" task has its
* own mount namespace. There it binds a mask on /proc/<pid>/<name>. It drops to
* uid 1000 and enters a new user namespace with a new mount namespace, so that
* the mask is locked. Then one task looks the name up:
*
* stat the container calls stat() on the name * umount the container calls umount2(MNT_DETACH) on the name * nobody a task with uid 65534 in the first namespaces calls stat()
*
* The container lists its mounts before and after (that does not look the
* name up) and then reads the name. MASK is fine. Anything else means that
* the lookup uncovered the entry.
*
* usage: procnet <name> stat|umount|nobody
* name "status" is the control, "net/dev" is a file below net, "net/stat" is
* a directory below net
*/
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define WORK "/run/pn"
static char target[128];
static void die(const char *what)
{
fprintf(stderr, "FATAL %s: %s\n", what, strerror(errno));
exit(2);
}
static void write_str(const char *path, const char *s, int flags)
{
int fd = open(path, O_WRONLY | flags, 0644);
if (fd < 0 || write(fd, s, strlen(s)) < 0)
die(path);
close(fd);
}
/* list the mounts below /proc without a lookup of the target */
static void mounts(const char *when)
{
char line[512];
FILE *f = fopen("/proc/self/mountinfo", "r");
int n = 0;
if (!f)
die("mountinfo");
while (fgets(line, sizeof(line), f)) {
if (!strstr(line, " /proc/"))
continue;
printf("container: %s: mount: %s", when, line);
n++;
}
fclose(f);
if (!n)
printf("container: %s: nothing is mounted below /proc\n", when);
}
static void show(const char *when, int isdir)
{
char buf[48] = "";
int fd;
if (isdir) {
struct dirent *de;
DIR *d = opendir(target);
int hit = 0;
if (!d) {
printf("container: %s: opendir(%s) = %s\n", when, target, strerror(errno));
return;
}
printf("container: %s: readdir(%s) =", when, target);
while ((de = readdir(d))) {
if (de->d_name[0] == '.')
continue;
printf(" %s", de->d_name);
hit |= strcmp(de->d_name, "MASK");
}
closedir(d);
printf("%s\n", hit ? " <-- HIT" : "");
return;
}
fd = open(target, O_RDONLY);
if (fd < 0) {
printf("container: %s: open(%s) = %s\n", when, target, strerror(errno));
return;
}
if (read(fd, buf, sizeof(buf) - 1) < 0)
die("read");
close(fd);
if (strchr(buf, '\n'))
*strchr(buf, '\n') = 0;
printf("container: %s: read(%s) = \"%s\"%s\n", when, target, buf,
strncmp(buf, "MASK", 4) ? " <-- HIT" : "");
}
int main(int argc, char **argv)
{
const char *name = argc > 1 ? argv[1] : "net/dev";
const char *op = argc > 2 ? argv[2] : "stat";
int isdir = !strcmp(name, "net/stat");
int to_host[2], to_cont[2];
struct stat st;
pid_t pid;
char c;
setvbuf(stdout, NULL, _IOLBF, 0);
snprintf(target, sizeof(target), "/proc/%d/%s", getpid(), name);
if (unshare(CLONE_NEWNS) || mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
die("unshare");
mkdir(WORK, 0755);
if (mount("W", WORK, "tmpfs", 0, NULL))
die("mount W");
write_str(WORK "/mask", "MASK", O_CREAT);
mkdir(WORK "/maskdir", 0755);
write_str(WORK "/maskdir/MASK", "MASK", O_CREAT);
if (pipe(to_host) || pipe(to_cont))
die("pipe");
pid = fork();
if (pid < 0)
die("fork");
if (!pid) {
if (unshare(CLONE_NEWNS))
die("container unshare");
if (mount(isdir ? WORK "/maskdir" : WORK "/mask", target, NULL, MS_BIND, NULL)) {
printf("container: bind the mask on %s: %s\n", target, strerror(errno));
exit(0);
}
printf("container: bind the mask on %s: ok\n", target);
if (setgid(1000) || setuid(1000) || prctl(PR_SET_DUMPABLE, 1))
die("setuid");
/* lock the mount: a new user namespace with a copy of the tree */
if (unshare(CLONE_NEWUSER | CLONE_NEWNS))
die("container userns");
write_str("/proc/self/setgroups", "deny", 0);
write_str("/proc/self/uid_map", "0 1000 1", 0);
write_str("/proc/self/gid_map", "0 1000 1", 0);
mounts("before");
errno = 0;
if (!strcmp(op, "stat")) {
stat(target, &st);
printf("container: stat(%s): %s\n", target, strerror(errno));
} else if (!strcmp(op, "umount")) {
umount2(target, MNT_DETACH);
printf("container: umount2(%s, MNT_DETACH): %s\n", target, strerror(errno));
} else if (write(to_host[1], "g", 1) != 1 || read(to_cont[0], &c, 1) != 1) {
die("sync");
}
mounts("after");
show("after", isdir);
exit(0);
}
close(to_host[1]);
if (read(to_host[0], &c, 1) == 1) {
pid_t p2 = fork();
if (!p2) {
if (setgid(65534) || setuid(65534))
die("setuid");
errno = 0;
stat(target, &st);
printf("nobody: uid %u: stat(%s): %s\n", getuid(), target, strerror(errno));
exit(0);
}
waitpid(p2, NULL, 0);
if (write(to_cont[1], "g", 1) != 1)
die("sync");
}
waitpid(pid, NULL, 0);
return 0;
}--- fs/proc/generic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/proc/generic.c +3 -2
@@ -261,9 +261,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, inode =proc_get_inode (dir->i_sb, de);if (!inode)return ERR_PTR (-ENOMEM ); +/* never reused and so never a mountpoint for long */ if (de->flags &PROC_ENTRY_FORCE_LOOKUP ) -return d_splice_alias_ops (inode, dentry, - &proc_net_dentry_ops); +return proc_splice_unmountable (inode, dentry, + &proc_net_dentry_ops);return d_splice_alias_ops (inode, dentry, &proc_misc_dentry_ops); }
Signature · 1 line
2.53.0