blob: 2a17fd9ae6a9ae75cfbbe9e92f69ac77aa945c5c [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. Biederman8823c072010-03-07 18:49:36 -080030 &mntns_operations,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080031};
32
33static const struct file_operations ns_file_operations = {
34 .llseek = no_llseek,
35};
36
37static struct dentry *proc_ns_instantiate(struct inode *dir,
38 struct dentry *dentry, struct task_struct *task, const void *ptr)
39{
40 const struct proc_ns_operations *ns_ops = ptr;
41 struct inode *inode;
42 struct proc_inode *ei;
43 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman79392532011-06-15 12:47:04 -070044 void *ns;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080045
46 inode = proc_pid_make_inode(dir->i_sb, task);
47 if (!inode)
48 goto out;
49
Eric W. Biederman79392532011-06-15 12:47:04 -070050 ns = ns_ops->get(task);
51 if (!ns)
52 goto out_iput;
53
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080054 ei = PROC_I(inode);
55 inode->i_mode = S_IFREG|S_IRUSR;
56 inode->i_fop = &ns_file_operations;
57 ei->ns_ops = ns_ops;
Eric W. Biederman79392532011-06-15 12:47:04 -070058 ei->ns = ns;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080059
Pravin B Shelar1b26c9b2012-03-23 15:02:55 -070060 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080061 d_add(dentry, inode);
62 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -040063 if (pid_revalidate(dentry, 0))
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080064 error = NULL;
65out:
66 return error;
67out_iput:
68 iput(inode);
69 goto out;
70}
71
72static int proc_ns_fill_cache(struct file *filp, void *dirent,
73 filldir_t filldir, struct task_struct *task,
74 const struct proc_ns_operations *ops)
75{
76 return proc_fill_cache(filp, dirent, filldir,
77 ops->name, strlen(ops->name),
78 proc_ns_instantiate, task, ops);
79}
80
81static int proc_ns_dir_readdir(struct file *filp, void *dirent,
82 filldir_t filldir)
83{
84 int i;
85 struct dentry *dentry = filp->f_path.dentry;
86 struct inode *inode = dentry->d_inode;
87 struct task_struct *task = get_proc_task(inode);
88 const struct proc_ns_operations **entry, **last;
89 ino_t ino;
90 int ret;
91
92 ret = -ENOENT;
93 if (!task)
94 goto out_no_task;
95
96 ret = -EPERM;
97 if (!ptrace_may_access(task, PTRACE_MODE_READ))
98 goto out;
99
100 ret = 0;
101 i = filp->f_pos;
102 switch (i) {
103 case 0:
104 ino = inode->i_ino;
105 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
106 goto out;
107 i++;
108 filp->f_pos++;
109 /* fall through */
110 case 1:
111 ino = parent_ino(dentry);
112 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
113 goto out;
114 i++;
115 filp->f_pos++;
116 /* fall through */
117 default:
118 i -= 2;
119 if (i >= ARRAY_SIZE(ns_entries)) {
120 ret = 1;
121 goto out;
122 }
123 entry = ns_entries + i;
124 last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
125 while (entry <= last) {
126 if (proc_ns_fill_cache(filp, dirent, filldir,
127 task, *entry) < 0)
128 goto out;
129 filp->f_pos++;
130 entry++;
131 }
132 }
133
134 ret = 1;
135out:
136 put_task_struct(task);
137out_no_task:
138 return ret;
139}
140
141const struct file_operations proc_ns_dir_operations = {
142 .read = generic_read_dir,
143 .readdir = proc_ns_dir_readdir,
144};
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{
149 struct dentry *error;
150 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
154 error = ERR_PTR(-ENOENT);
155
156 if (!task)
157 goto out_no_task;
158
159 error = ERR_PTR(-EPERM);
160 if (!ptrace_may_access(task, PTRACE_MODE_READ))
161 goto out;
162
Andrew Morton4c619aa2012-03-28 14:42:52 -0700163 last = &ns_entries[ARRAY_SIZE(ns_entries)];
164 for (entry = ns_entries; entry < last; entry++) {
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800165 if (strlen((*entry)->name) != len)
166 continue;
167 if (!memcmp(dentry->d_name.name, (*entry)->name, len))
168 break;
169 }
Eric W. Biederman62ca24b2011-05-11 15:42:08 -0700170 error = ERR_PTR(-ENOENT);
Andrew Morton4c619aa2012-03-28 14:42:52 -0700171 if (entry == last)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800172 goto out;
173
174 error = proc_ns_instantiate(dir, dentry, task, *entry);
175out:
176 put_task_struct(task);
177out_no_task:
178 return error;
179}
180
181const struct inode_operations proc_ns_dir_inode_operations = {
182 .lookup = proc_ns_dir_lookup,
183 .getattr = pid_getattr,
184 .setattr = proc_setattr,
185};
186
187struct file *proc_ns_fget(int fd)
188{
189 struct file *file;
190
191 file = fget(fd);
192 if (!file)
193 return ERR_PTR(-EBADF);
194
195 if (file->f_op != &ns_file_operations)
196 goto out_invalid;
197
198 return file;
199
200out_invalid:
201 fput(file);
202 return ERR_PTR(-EINVAL);
203}
204
Eric W. Biederman8823c072010-03-07 18:49:36 -0800205bool proc_ns_inode(struct inode *inode)
206{
207 return inode->i_fop == &ns_file_operations;
208}