^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ipc_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/user_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static const struct proc_ns_operations *ns_entries[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #ifdef CONFIG_NET_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) &netns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #ifdef CONFIG_UTS_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) &utsns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef CONFIG_IPC_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) &ipcns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifdef CONFIG_PID_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) &pidns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) &pidns_for_children_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifdef CONFIG_USER_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) &userns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) &mntns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #ifdef CONFIG_CGROUPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) &cgroupns_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #ifdef CONFIG_TIME_NS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) &timens_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) &timens_for_children_operations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static const char *proc_ns_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct path ns_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int error = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) task = get_proc_task(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) error = ns_get_path(&ns_path, task, ns_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) error = nd_jump_link(&ns_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) char name[50];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int res = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) task = get_proc_task(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) res = ns_get_name(name, sizeof(name), task, ns_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (res >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) res = readlink_copy(buffer, buflen, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static const struct inode_operations proc_ns_link_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .readlink = proc_ns_readlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .get_link = proc_ns_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .setattr = proc_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static struct dentry *proc_ns_instantiate(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct task_struct *task, const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) const struct proc_ns_operations *ns_ops = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct proc_inode *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ei = PROC_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) inode->i_op = &proc_ns_link_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ei->ns_ops = ns_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pid_update_inode(task, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) d_set_d_op(dentry, &pid_dentry_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct task_struct *task = get_proc_task(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const struct proc_ns_operations **entry, **last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) entry = ns_entries + (ctx->pos - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) while (entry <= last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const struct proc_ns_operations *ops = *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) proc_ns_instantiate, task, ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) entry++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const struct file_operations proc_ns_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .iterate_shared = proc_ns_dir_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static struct dentry *proc_ns_dir_lookup(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct task_struct *task = get_proc_task(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) const struct proc_ns_operations **entry, **last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct dentry *res = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto out_no_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) last = &ns_entries[ARRAY_SIZE(ns_entries)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) for (entry = ns_entries; entry < last; entry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (strlen((*entry)->name) != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!memcmp(dentry->d_name.name, (*entry)->name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (entry == last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) res = proc_ns_instantiate(dentry, task, *entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) put_task_struct(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) out_no_task:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) const struct inode_operations proc_ns_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .lookup = proc_ns_dir_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .getattr = pid_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .setattr = proc_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };