Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1 | #include <linux/proc_fs.h> |
| 2 | #include <linux/nsproxy.h> |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 3 | #include <linux/ptrace.h> |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 4 | #include <linux/namei.h> |
| 5 | #include <linux/file.h> |
| 6 | #include <linux/utsname.h> |
| 7 | #include <net/net_namespace.h> |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 8 | #include <linux/ipc_namespace.h> |
| 9 | #include <linux/pid_namespace.h> |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 10 | #include <linux/user_namespace.h> |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 11 | #include "internal.h" |
| 12 | |
| 13 | |
| 14 | static const struct proc_ns_operations *ns_entries[] = { |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 15 | #ifdef CONFIG_NET_NS |
| 16 | &netns_operations, |
| 17 | #endif |
Eric W. Biederman | 34482e8 | 2010-03-07 18:43:27 -0800 | [diff] [blame] | 18 | #ifdef CONFIG_UTS_NS |
| 19 | &utsns_operations, |
| 20 | #endif |
Eric W. Biederman | a00eaf1 | 2010-03-07 18:48:39 -0800 | [diff] [blame] | 21 | #ifdef CONFIG_IPC_NS |
| 22 | &ipcns_operations, |
| 23 | #endif |
Eric W. Biederman | 57e8391 | 2010-03-07 18:17:03 -0800 | [diff] [blame] | 24 | #ifdef CONFIG_PID_NS |
| 25 | &pidns_operations, |
| 26 | #endif |
Eric W. Biederman | cde1975 | 2012-07-26 06:24:06 -0700 | [diff] [blame] | 27 | #ifdef CONFIG_USER_NS |
| 28 | &userns_operations, |
| 29 | #endif |
Eric W. Biederman | 8823c07 | 2010-03-07 18:49:36 -0800 | [diff] [blame] | 30 | &mntns_operations, |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 31 | }; |
| 32 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 33 | static const char *proc_ns_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 34 | struct inode *inode, |
| 35 | struct delayed_call *done) |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 36 | { |
Al Viro | 3d3d35b | 2014-11-01 11:10:28 -0400 | [diff] [blame] | 37 | const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 38 | struct task_struct *task; |
Eric W. Biederman | db04dc6 | 2013-03-09 00:14:45 -0800 | [diff] [blame] | 39 | struct path ns_path; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 40 | void *error = ERR_PTR(-EACCES); |
| 41 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 42 | if (!dentry) |
| 43 | return ERR_PTR(-ECHILD); |
| 44 | |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 45 | task = get_proc_task(inode); |
| 46 | if (!task) |
Al Viro | e149ed2 | 2014-11-01 10:57:28 -0400 | [diff] [blame] | 47 | return error; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 48 | |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame^] | 49 | if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) { |
Al Viro | e149ed2 | 2014-11-01 10:57:28 -0400 | [diff] [blame] | 50 | error = ns_get_path(&ns_path, task, ns_ops); |
| 51 | if (!error) |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 52 | nd_jump_link(&ns_path); |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 53 | } |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 54 | put_task_struct(task); |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 55 | return error; |
| 56 | } |
| 57 | |
| 58 | static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 59 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 60 | struct inode *inode = d_inode(dentry); |
Al Viro | 3d3d35b | 2014-11-01 11:10:28 -0400 | [diff] [blame] | 61 | const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 62 | struct task_struct *task; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 63 | char name[50]; |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 64 | int res = -EACCES; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 65 | |
| 66 | task = get_proc_task(inode); |
| 67 | if (!task) |
Al Viro | e149ed2 | 2014-11-01 10:57:28 -0400 | [diff] [blame] | 68 | return res; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 69 | |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame^] | 70 | if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) { |
Al Viro | e149ed2 | 2014-11-01 10:57:28 -0400 | [diff] [blame] | 71 | res = ns_get_name(name, sizeof(name), task, ns_ops); |
| 72 | if (res >= 0) |
| 73 | res = readlink_copy(buffer, buflen, name); |
| 74 | } |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 75 | put_task_struct(task); |
Al Viro | 5d826c8 | 2014-03-14 13:42:45 -0400 | [diff] [blame] | 76 | return res; |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static const struct inode_operations proc_ns_link_inode_operations = { |
| 80 | .readlink = proc_ns_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 81 | .get_link = proc_ns_get_link, |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 82 | .setattr = proc_setattr, |
| 83 | }; |
| 84 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 85 | static int proc_ns_instantiate(struct inode *dir, |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 86 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
| 87 | { |
| 88 | const struct proc_ns_operations *ns_ops = ptr; |
| 89 | struct inode *inode; |
| 90 | struct proc_inode *ei; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 91 | |
| 92 | inode = proc_pid_make_inode(dir->i_sb, task); |
| 93 | if (!inode) |
| 94 | goto out; |
| 95 | |
| 96 | ei = PROC_I(inode); |
Eric W. Biederman | bf056bf | 2011-06-18 17:48:18 -0700 | [diff] [blame] | 97 | inode->i_mode = S_IFLNK|S_IRWXUGO; |
| 98 | inode->i_op = &proc_ns_link_inode_operations; |
Al Viro | 3d3d35b | 2014-11-01 11:10:28 -0400 | [diff] [blame] | 99 | ei->ns_ops = ns_ops; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 100 | |
Pravin B Shelar | 1b26c9b | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 101 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 102 | d_add(dentry, inode); |
| 103 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 104 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 105 | return 0; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 106 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 107 | return -ENOENT; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 110 | static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 111 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 112 | struct task_struct *task = get_proc_task(file_inode(file)); |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 113 | const struct proc_ns_operations **entry, **last; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 114 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 115 | if (!task) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 116 | return -ENOENT; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 117 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 118 | if (!dir_emit_dots(file, ctx)) |
| 119 | goto out; |
| 120 | if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries)) |
| 121 | goto out; |
| 122 | entry = ns_entries + (ctx->pos - 2); |
| 123 | last = &ns_entries[ARRAY_SIZE(ns_entries) - 1]; |
| 124 | while (entry <= last) { |
| 125 | const struct proc_ns_operations *ops = *entry; |
| 126 | if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name), |
| 127 | proc_ns_instantiate, task, ops)) |
| 128 | break; |
| 129 | ctx->pos++; |
| 130 | entry++; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 131 | } |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 132 | out: |
| 133 | put_task_struct(task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 134 | return 0; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | const struct file_operations proc_ns_dir_operations = { |
| 138 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 139 | .iterate = proc_ns_dir_readdir, |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct dentry *proc_ns_dir_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 143 | struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 144 | { |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 145 | int error; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 146 | struct task_struct *task = get_proc_task(dir); |
| 147 | const struct proc_ns_operations **entry, **last; |
| 148 | unsigned int len = dentry->d_name.len; |
| 149 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 150 | error = -ENOENT; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 151 | |
| 152 | if (!task) |
| 153 | goto out_no_task; |
| 154 | |
Andrew Morton | 4c619aa | 2012-03-28 14:42:52 -0700 | [diff] [blame] | 155 | last = &ns_entries[ARRAY_SIZE(ns_entries)]; |
| 156 | for (entry = ns_entries; entry < last; entry++) { |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 157 | if (strlen((*entry)->name) != len) |
| 158 | continue; |
| 159 | if (!memcmp(dentry->d_name.name, (*entry)->name, len)) |
| 160 | break; |
| 161 | } |
Andrew Morton | 4c619aa | 2012-03-28 14:42:52 -0700 | [diff] [blame] | 162 | if (entry == last) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 163 | goto out; |
| 164 | |
| 165 | error = proc_ns_instantiate(dir, dentry, task, *entry); |
| 166 | out: |
| 167 | put_task_struct(task); |
| 168 | out_no_task: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 169 | return ERR_PTR(error); |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | const struct inode_operations proc_ns_dir_inode_operations = { |
| 173 | .lookup = proc_ns_dir_lookup, |
| 174 | .getattr = pid_getattr, |
| 175 | .setattr = proc_setattr, |
| 176 | }; |