Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1 | #include <linux/proc_fs.h> |
| 2 | #include <linux/nsproxy.h> |
| 3 | #include <linux/sched.h> |
| 4 | #include <linux/ptrace.h> |
| 5 | #include <linux/fs_struct.h> |
| 6 | #include <linux/mount.h> |
| 7 | #include <linux/path.h> |
| 8 | #include <linux/namei.h> |
| 9 | #include <linux/file.h> |
| 10 | #include <linux/utsname.h> |
| 11 | #include <net/net_namespace.h> |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 12 | #include <linux/ipc_namespace.h> |
| 13 | #include <linux/pid_namespace.h> |
| 14 | #include "internal.h" |
| 15 | |
| 16 | |
| 17 | static const struct proc_ns_operations *ns_entries[] = { |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 18 | #ifdef CONFIG_NET_NS |
| 19 | &netns_operations, |
| 20 | #endif |
Eric W. Biederman | 34482e8 | 2010-03-07 18:43:27 -0800 | [diff] [blame] | 21 | #ifdef CONFIG_UTS_NS |
| 22 | &utsns_operations, |
| 23 | #endif |
Eric W. Biederman | a00eaf1 | 2010-03-07 18:48:39 -0800 | [diff] [blame] | 24 | #ifdef CONFIG_IPC_NS |
| 25 | &ipcns_operations, |
| 26 | #endif |
Eric W. Biederman | 57e8391 | 2010-03-07 18:17:03 -0800 | [diff] [blame^] | 27 | #ifdef CONFIG_PID_NS |
| 28 | &pidns_operations, |
| 29 | #endif |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static const struct file_operations ns_file_operations = { |
| 33 | .llseek = no_llseek, |
| 34 | }; |
| 35 | |
| 36 | static struct dentry *proc_ns_instantiate(struct inode *dir, |
| 37 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
| 38 | { |
| 39 | const struct proc_ns_operations *ns_ops = ptr; |
| 40 | struct inode *inode; |
| 41 | struct proc_inode *ei; |
| 42 | struct dentry *error = ERR_PTR(-ENOENT); |
Eric W. Biederman | 7939253 | 2011-06-15 12:47:04 -0700 | [diff] [blame] | 43 | void *ns; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 44 | |
| 45 | inode = proc_pid_make_inode(dir->i_sb, task); |
| 46 | if (!inode) |
| 47 | goto out; |
| 48 | |
Eric W. Biederman | 7939253 | 2011-06-15 12:47:04 -0700 | [diff] [blame] | 49 | ns = ns_ops->get(task); |
| 50 | if (!ns) |
| 51 | goto out_iput; |
| 52 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 53 | ei = PROC_I(inode); |
| 54 | inode->i_mode = S_IFREG|S_IRUSR; |
| 55 | inode->i_fop = &ns_file_operations; |
| 56 | ei->ns_ops = ns_ops; |
Eric W. Biederman | 7939253 | 2011-06-15 12:47:04 -0700 | [diff] [blame] | 57 | ei->ns = ns; |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 58 | |
Pravin B Shelar | 1b26c9b | 2012-03-23 15:02:55 -0700 | [diff] [blame] | 59 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 60 | d_add(dentry, inode); |
| 61 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 62 | if (pid_revalidate(dentry, 0)) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 63 | error = NULL; |
| 64 | out: |
| 65 | return error; |
| 66 | out_iput: |
| 67 | iput(inode); |
| 68 | goto out; |
| 69 | } |
| 70 | |
| 71 | static int proc_ns_fill_cache(struct file *filp, void *dirent, |
| 72 | filldir_t filldir, struct task_struct *task, |
| 73 | const struct proc_ns_operations *ops) |
| 74 | { |
| 75 | return proc_fill_cache(filp, dirent, filldir, |
| 76 | ops->name, strlen(ops->name), |
| 77 | proc_ns_instantiate, task, ops); |
| 78 | } |
| 79 | |
| 80 | static int proc_ns_dir_readdir(struct file *filp, void *dirent, |
| 81 | filldir_t filldir) |
| 82 | { |
| 83 | int i; |
| 84 | struct dentry *dentry = filp->f_path.dentry; |
| 85 | struct inode *inode = dentry->d_inode; |
| 86 | struct task_struct *task = get_proc_task(inode); |
| 87 | const struct proc_ns_operations **entry, **last; |
| 88 | ino_t ino; |
| 89 | int ret; |
| 90 | |
| 91 | ret = -ENOENT; |
| 92 | if (!task) |
| 93 | goto out_no_task; |
| 94 | |
| 95 | ret = -EPERM; |
| 96 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 97 | goto out; |
| 98 | |
| 99 | ret = 0; |
| 100 | i = filp->f_pos; |
| 101 | switch (i) { |
| 102 | case 0: |
| 103 | ino = inode->i_ino; |
| 104 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
| 105 | goto out; |
| 106 | i++; |
| 107 | filp->f_pos++; |
| 108 | /* fall through */ |
| 109 | case 1: |
| 110 | ino = parent_ino(dentry); |
| 111 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
| 112 | goto out; |
| 113 | i++; |
| 114 | filp->f_pos++; |
| 115 | /* fall through */ |
| 116 | default: |
| 117 | i -= 2; |
| 118 | if (i >= ARRAY_SIZE(ns_entries)) { |
| 119 | ret = 1; |
| 120 | goto out; |
| 121 | } |
| 122 | entry = ns_entries + i; |
| 123 | last = &ns_entries[ARRAY_SIZE(ns_entries) - 1]; |
| 124 | while (entry <= last) { |
| 125 | if (proc_ns_fill_cache(filp, dirent, filldir, |
| 126 | task, *entry) < 0) |
| 127 | goto out; |
| 128 | filp->f_pos++; |
| 129 | entry++; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | ret = 1; |
| 134 | out: |
| 135 | put_task_struct(task); |
| 136 | out_no_task: |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | const struct file_operations proc_ns_dir_operations = { |
| 141 | .read = generic_read_dir, |
| 142 | .readdir = proc_ns_dir_readdir, |
| 143 | }; |
| 144 | |
| 145 | static struct dentry *proc_ns_dir_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 146 | struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 147 | { |
| 148 | struct dentry *error; |
| 149 | struct task_struct *task = get_proc_task(dir); |
| 150 | const struct proc_ns_operations **entry, **last; |
| 151 | unsigned int len = dentry->d_name.len; |
| 152 | |
| 153 | error = ERR_PTR(-ENOENT); |
| 154 | |
| 155 | if (!task) |
| 156 | goto out_no_task; |
| 157 | |
| 158 | error = ERR_PTR(-EPERM); |
| 159 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 160 | goto out; |
| 161 | |
Andrew Morton | 4c619aa | 2012-03-28 14:42:52 -0700 | [diff] [blame] | 162 | last = &ns_entries[ARRAY_SIZE(ns_entries)]; |
| 163 | for (entry = ns_entries; entry < last; entry++) { |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 164 | if (strlen((*entry)->name) != len) |
| 165 | continue; |
| 166 | if (!memcmp(dentry->d_name.name, (*entry)->name, len)) |
| 167 | break; |
| 168 | } |
Eric W. Biederman | 62ca24b | 2011-05-11 15:42:08 -0700 | [diff] [blame] | 169 | error = ERR_PTR(-ENOENT); |
Andrew Morton | 4c619aa | 2012-03-28 14:42:52 -0700 | [diff] [blame] | 170 | if (entry == last) |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 171 | goto out; |
| 172 | |
| 173 | error = proc_ns_instantiate(dir, dentry, task, *entry); |
| 174 | out: |
| 175 | put_task_struct(task); |
| 176 | out_no_task: |
| 177 | return error; |
| 178 | } |
| 179 | |
| 180 | const struct inode_operations proc_ns_dir_inode_operations = { |
| 181 | .lookup = proc_ns_dir_lookup, |
| 182 | .getattr = pid_getattr, |
| 183 | .setattr = proc_setattr, |
| 184 | }; |
| 185 | |
| 186 | struct file *proc_ns_fget(int fd) |
| 187 | { |
| 188 | struct file *file; |
| 189 | |
| 190 | file = fget(fd); |
| 191 | if (!file) |
| 192 | return ERR_PTR(-EBADF); |
| 193 | |
| 194 | if (file->f_op != &ns_file_operations) |
| 195 | goto out_invalid; |
| 196 | |
| 197 | return file; |
| 198 | |
| 199 | out_invalid: |
| 200 | fput(file); |
| 201 | return ERR_PTR(-EINVAL); |
| 202 | } |
| 203 | |