blob: 51b8b0a8ad91b27d569d65c50e61d8d14aecab83 [file] [log] [blame]
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001#include <linux/proc_fs.h>
2#include <linux/nsproxy.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003#include <linux/ptrace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08004#include <linux/namei.h>
5#include <linux/file.h>
6#include <linux/utsname.h>
7#include <net/net_namespace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08008#include <linux/ipc_namespace.h>
9#include <linux/pid_namespace.h>
Eric W. Biedermancde19752012-07-26 06:24:06 -070010#include <linux/user_namespace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080011#include "internal.h"
12
13
14static const struct proc_ns_operations *ns_entries[] = {
Eric W. Biederman13b6f572010-03-07 18:14:23 -080015#ifdef CONFIG_NET_NS
16 &netns_operations,
17#endif
Eric W. Biederman34482e82010-03-07 18:43:27 -080018#ifdef CONFIG_UTS_NS
19 &utsns_operations,
20#endif
Eric W. Biedermana00eaf12010-03-07 18:48:39 -080021#ifdef CONFIG_IPC_NS
22 &ipcns_operations,
23#endif
Eric W. Biederman57e83912010-03-07 18:17:03 -080024#ifdef CONFIG_PID_NS
25 &pidns_operations,
26#endif
Eric W. Biedermancde19752012-07-26 06:24:06 -070027#ifdef CONFIG_USER_NS
28 &userns_operations,
29#endif
Eric W. Biederman8823c072010-03-07 18:49:36 -080030 &mntns_operations,
Aditya Kalia79a9082016-01-29 02:54:06 -060031#ifdef CONFIG_CGROUPS
32 &cgroupns_operations,
33#endif
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080034};
35
Al Viro6b255392015-11-17 10:20:54 -050036static const char *proc_ns_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -050037 struct inode *inode,
38 struct delayed_call *done)
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070039{
Al Viro3d3d35b2014-11-01 11:10:28 -040040 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070041 struct task_struct *task;
Eric W. Biedermandb04dc62013-03-09 00:14:45 -080042 struct path ns_path;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070043 void *error = ERR_PTR(-EACCES);
44
Al Viro6b255392015-11-17 10:20:54 -050045 if (!dentry)
46 return ERR_PTR(-ECHILD);
47
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070048 task = get_proc_task(inode);
49 if (!task)
Al Viroe149ed22014-11-01 10:57:28 -040050 return error;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070051
Jann Horncaaee622016-01-20 15:00:04 -080052 if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Al Viroe149ed22014-11-01 10:57:28 -040053 error = ns_get_path(&ns_path, task, ns_ops);
54 if (!error)
Al Viro6e771372015-05-02 13:37:52 -040055 nd_jump_link(&ns_path);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070056 }
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070057 put_task_struct(task);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070058 return error;
59}
60
61static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
62{
David Howells2b0143b2015-03-17 22:25:59 +000063 struct inode *inode = d_inode(dentry);
Al Viro3d3d35b2014-11-01 11:10:28 -040064 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070065 struct task_struct *task;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070066 char name[50];
Al Viro5d826c82014-03-14 13:42:45 -040067 int res = -EACCES;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070068
69 task = get_proc_task(inode);
70 if (!task)
Al Viroe149ed22014-11-01 10:57:28 -040071 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070072
Jann Horncaaee622016-01-20 15:00:04 -080073 if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Al Viroe149ed22014-11-01 10:57:28 -040074 res = ns_get_name(name, sizeof(name), task, ns_ops);
75 if (res >= 0)
76 res = readlink_copy(buffer, buflen, name);
77 }
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070078 put_task_struct(task);
Al Viro5d826c82014-03-14 13:42:45 -040079 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070080}
81
82static const struct inode_operations proc_ns_link_inode_operations = {
83 .readlink = proc_ns_readlink,
Al Viro6b255392015-11-17 10:20:54 -050084 .get_link = proc_ns_get_link,
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070085 .setattr = proc_setattr,
86};
87
Al Viroc52a47a2013-06-15 11:15:20 +040088static int proc_ns_instantiate(struct inode *dir,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080089 struct dentry *dentry, struct task_struct *task, const void *ptr)
90{
91 const struct proc_ns_operations *ns_ops = ptr;
92 struct inode *inode;
93 struct proc_inode *ei;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080094
95 inode = proc_pid_make_inode(dir->i_sb, task);
96 if (!inode)
97 goto out;
98
99 ei = PROC_I(inode);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -0700100 inode->i_mode = S_IFLNK|S_IRWXUGO;
101 inode->i_op = &proc_ns_link_inode_operations;
Al Viro3d3d35b2014-11-01 11:10:28 -0400102 ei->ns_ops = ns_ops;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800103
Pravin B Shelar1b26c9b2012-03-23 15:02:55 -0700104 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800105 d_add(dentry, inode);
106 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -0400107 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +0400108 return 0;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800109out:
Al Viroc52a47a2013-06-15 11:15:20 +0400110 return -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800111}
112
Al Virof0c3b502013-05-16 12:07:31 -0400113static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800114{
Al Virof0c3b502013-05-16 12:07:31 -0400115 struct task_struct *task = get_proc_task(file_inode(file));
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800116 const struct proc_ns_operations **entry, **last;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800117
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800118 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -0400119 return -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800120
Al Virof0c3b502013-05-16 12:07:31 -0400121 if (!dir_emit_dots(file, ctx))
122 goto out;
123 if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
124 goto out;
125 entry = ns_entries + (ctx->pos - 2);
126 last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
127 while (entry <= last) {
128 const struct proc_ns_operations *ops = *entry;
129 if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
130 proc_ns_instantiate, task, ops))
131 break;
132 ctx->pos++;
133 entry++;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800134 }
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800135out:
136 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -0400137 return 0;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800138}
139
140const struct file_operations proc_ns_dir_operations = {
141 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400142 .iterate_shared = proc_ns_dir_readdir,
143 .llseek = generic_file_llseek,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800144};
145
146static struct dentry *proc_ns_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -0400147 struct dentry *dentry, unsigned int flags)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800148{
Al Viroc52a47a2013-06-15 11:15:20 +0400149 int error;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800150 struct task_struct *task = get_proc_task(dir);
151 const struct proc_ns_operations **entry, **last;
152 unsigned int len = dentry->d_name.len;
153
Al Viroc52a47a2013-06-15 11:15:20 +0400154 error = -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800155
156 if (!task)
157 goto out_no_task;
158
Andrew Morton4c619aa2012-03-28 14:42:52 -0700159 last = &ns_entries[ARRAY_SIZE(ns_entries)];
160 for (entry = ns_entries; entry < last; entry++) {
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800161 if (strlen((*entry)->name) != len)
162 continue;
163 if (!memcmp(dentry->d_name.name, (*entry)->name, len))
164 break;
165 }
Andrew Morton4c619aa2012-03-28 14:42:52 -0700166 if (entry == last)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800167 goto out;
168
169 error = proc_ns_instantiate(dir, dentry, task, *entry);
170out:
171 put_task_struct(task);
172out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +0400173 return ERR_PTR(error);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800174}
175
176const struct inode_operations proc_ns_dir_inode_operations = {
177 .lookup = proc_ns_dir_lookup,
178 .getattr = pid_getattr,
179 .setattr = proc_setattr,
180};