blob: 85ca047e35f135e7b012da336e88c41137591faf [file] [log] [blame]
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001#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. Biederman6b4e3062010-03-07 16:41:34 -080012#include <linux/ipc_namespace.h>
13#include <linux/pid_namespace.h>
14#include "internal.h"
15
16
17static const struct proc_ns_operations *ns_entries[] = {
Eric W. Biederman13b6f572010-03-07 18:14:23 -080018#ifdef CONFIG_NET_NS
19 &netns_operations,
20#endif
Eric W. Biederman34482e82010-03-07 18:43:27 -080021#ifdef CONFIG_UTS_NS
22 &utsns_operations,
23#endif
Eric W. Biedermana00eaf12010-03-07 18:48:39 -080024#ifdef CONFIG_IPC_NS
25 &ipcns_operations,
26#endif
Eric W. Biederman57e83912010-03-07 18:17:03 -080027#ifdef CONFIG_PID_NS
28 &pidns_operations,
29#endif
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080030};
31
32static const struct file_operations ns_file_operations = {
33 .llseek = no_llseek,
34};
35
36static 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. Biederman79392532011-06-15 12:47:04 -070043 void *ns;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080044
45 inode = proc_pid_make_inode(dir->i_sb, task);
46 if (!inode)
47 goto out;
48
Eric W. Biederman79392532011-06-15 12:47:04 -070049 ns = ns_ops->get(task);
50 if (!ns)
51 goto out_iput;
52
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080053 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. Biederman79392532011-06-15 12:47:04 -070057 ei->ns = ns;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080058
Pravin B Shelar1b26c9b2012-03-23 15:02:55 -070059 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080060 d_add(dentry, inode);
61 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -040062 if (pid_revalidate(dentry, 0))
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080063 error = NULL;
64out:
65 return error;
66out_iput:
67 iput(inode);
68 goto out;
69}
70
71static 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
80static 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;
134out:
135 put_task_struct(task);
136out_no_task:
137 return ret;
138}
139
140const struct file_operations proc_ns_dir_operations = {
141 .read = generic_read_dir,
142 .readdir = proc_ns_dir_readdir,
143};
144
145static struct dentry *proc_ns_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -0400146 struct dentry *dentry, unsigned int flags)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800147{
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 Morton4c619aa2012-03-28 14:42:52 -0700162 last = &ns_entries[ARRAY_SIZE(ns_entries)];
163 for (entry = ns_entries; entry < last; entry++) {
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800164 if (strlen((*entry)->name) != len)
165 continue;
166 if (!memcmp(dentry->d_name.name, (*entry)->name, len))
167 break;
168 }
Eric W. Biederman62ca24b2011-05-11 15:42:08 -0700169 error = ERR_PTR(-ENOENT);
Andrew Morton4c619aa2012-03-28 14:42:52 -0700170 if (entry == last)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800171 goto out;
172
173 error = proc_ns_instantiate(dir, dentry, task, *entry);
174out:
175 put_task_struct(task);
176out_no_task:
177 return error;
178}
179
180const struct inode_operations proc_ns_dir_inode_operations = {
181 .lookup = proc_ns_dir_lookup,
182 .getattr = pid_getattr,
183 .setattr = proc_setattr,
184};
185
186struct 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
199out_invalid:
200 fput(file);
201 return ERR_PTR(-EINVAL);
202}
203