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