/proc/mounts and /proc/net are links into /proc/self. Like /proc/self itself they resolve differently for every process. A mount on one of the links redirects the lookup for all processes in that mount namespace. Add proc_self_symlink() for links that point into /proc/self. It creates the link with PROC_ENTRY_CANT_MOUNT and proc_lookup_de() splices such an entry with proc_splice_unmountable(). Use it for /proc/mounts and /proc/net. Mounting through the links keeps working. Such a mount lands on the caller's own /proc//mounts or /proc//net, not on the link. Signed-off-by: Christian Brauner (Amutable) --- fs/proc/generic.c | 26 ++++++++++++++++++++++++-- fs/proc/proc_net.c | 2 +- fs/proc/root.c | 2 +- include/linux/proc_fs.h | 5 +++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/fs/proc/generic.c b/fs/proc/generic.c index 4c3fcaf68ef4..484d7dc123eb 100644 --- a/fs/proc/generic.c +++ b/fs/proc/generic.c @@ -260,6 +260,10 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, if (de->flags & PROC_ENTRY_FORCE_LOOKUP) return proc_splice_unmountable(inode, dentry, &proc_net_dentry_ops); + /* a mount on a link into /proc/self redirects every process */ + if (de->flags & PROC_ENTRY_CANT_MOUNT) + return proc_splice_unmountable(inode, dentry, + &proc_misc_dentry_ops); return d_splice_alias_ops(inode, dentry, &proc_misc_dentry_ops); } @@ -487,8 +491,8 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, return ent; } -struct proc_dir_entry *proc_symlink(const char *name, - struct proc_dir_entry *parent, const char *dest) +static struct proc_dir_entry *__proc_symlink(const char *name, + struct proc_dir_entry *parent, const char *dest, u8 flags) { struct proc_dir_entry *ent; @@ -496,6 +500,7 @@ struct proc_dir_entry *proc_symlink(const char *name, (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); if (ent) { + ent->flags |= flags; ent->size = strlen(dest); ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL); if (ent->data) { @@ -508,8 +513,25 @@ struct proc_dir_entry *proc_symlink(const char *name, } return ent; } + +struct proc_dir_entry *proc_symlink(const char *name, + struct proc_dir_entry *parent, const char *dest) +{ + return __proc_symlink(name, parent, dest, 0); +} EXPORT_SYMBOL(proc_symlink); +/* + * A link into /proc/self resolves differently for every process, so a + * mount on the link itself would redirect all of them. + */ +struct proc_dir_entry *proc_self_symlink(const char *name, + struct proc_dir_entry *parent, const char *dest) +{ + return __proc_symlink(name, parent, dest, PROC_ENTRY_CANT_MOUNT); +} +EXPORT_SYMBOL_GPL(proc_self_symlink); + struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode, struct proc_dir_entry *parent, void *data, bool force_lookup) { diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c index b1f5eafb069a..578b1eb081bf 100644 --- a/fs/proc/proc_net.c +++ b/fs/proc/proc_net.c @@ -420,7 +420,7 @@ static struct pernet_operations __net_initdata proc_net_ns_ops = { int __init proc_net_init(void) { - proc_symlink("net", NULL, "self/net"); + proc_self_symlink("net", NULL, "self/net"); return register_pernet_subsys(&proc_net_ns_ops); } diff --git a/fs/proc/root.c b/fs/proc/root.c index 7fbbe92bf73a..42fdc6ec50a3 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -380,7 +380,7 @@ void __init proc_root_init(void) set_proc_pid_nlink(); proc_self_init(); proc_thread_self_init(); - proc_symlink("mounts", NULL, "self/mounts"); + proc_self_symlink("mounts", NULL, "self/mounts"); proc_net_init(); proc_mkdir("fs", NULL); diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 47d7deaeed8f..903fc811efd4 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -29,6 +29,7 @@ enum { PROC_ENTRY_proc_compat_ioctl = 1U << 2, PROC_ENTRY_proc_lseek = 1U << 3, + PROC_ENTRY_CANT_MOUNT = 1U << 6, PROC_ENTRY_FORCE_LOOKUP = 1U << 7, }; @@ -87,6 +88,8 @@ extern void proc_flush_pid(struct pid *); extern struct proc_dir_entry *proc_symlink(const char *, struct proc_dir_entry *, const char *); +struct proc_dir_entry *proc_self_symlink(const char *, struct proc_dir_entry *, + const char *); struct proc_dir_entry *_proc_mkdir(const char *, umode_t, struct proc_dir_entry *, void *, bool); extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *); extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t, @@ -179,6 +182,8 @@ static inline void proc_flush_pid(struct pid *pid) static inline struct proc_dir_entry *proc_symlink(const char *name, struct proc_dir_entry *parent,const char *dest) { return NULL;} +static inline struct proc_dir_entry *proc_self_symlink(const char *name, + struct proc_dir_entry *parent, const char *dest) { return NULL; } static inline struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent) {return NULL;} static inline struct proc_dir_entry *proc_create_mount_point(const char *name) { return NULL; } -- 2.53.0