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>
12 #include <linux/ipc_namespace.h>
13 #include <linux/pid_namespace.h>
17 static const struct proc_ns_operations
*ns_entries
[] = {
29 static const struct file_operations ns_file_operations
= {
33 static struct dentry
*proc_ns_instantiate(struct inode
*dir
,
34 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
36 const struct proc_ns_operations
*ns_ops
= ptr
;
38 struct proc_inode
*ei
;
39 struct dentry
*error
= ERR_PTR(-ENOENT
);
42 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
46 ns
= ns_ops
->get(task
);
51 inode
->i_mode
= S_IFREG
|S_IRUSR
;
52 inode
->i_fop
= &ns_file_operations
;
56 d_set_d_op(dentry
, &pid_dentry_operations
);
58 /* Close the race of the process dying before we return the dentry */
59 if (pid_revalidate(dentry
, 0))
68 static int proc_ns_fill_cache(struct file
*filp
, void *dirent
,
69 filldir_t filldir
, struct task_struct
*task
,
70 const struct proc_ns_operations
*ops
)
72 return proc_fill_cache(filp
, dirent
, filldir
,
73 ops
->name
, strlen(ops
->name
),
74 proc_ns_instantiate
, task
, ops
);
77 static int proc_ns_dir_readdir(struct file
*filp
, void *dirent
,
81 struct dentry
*dentry
= filp
->f_path
.dentry
;
82 struct inode
*inode
= dentry
->d_inode
;
83 struct task_struct
*task
= get_proc_task(inode
);
84 const struct proc_ns_operations
**entry
, **last
;
93 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
101 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
107 ino
= parent_ino(dentry
);
108 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
115 if (i
>= ARRAY_SIZE(ns_entries
)) {
119 entry
= ns_entries
+ i
;
120 last
= &ns_entries
[ARRAY_SIZE(ns_entries
) - 1];
121 while (entry
<= last
) {
122 if (proc_ns_fill_cache(filp
, dirent
, filldir
,
132 put_task_struct(task
);
137 const struct file_operations proc_ns_dir_operations
= {
138 .read
= generic_read_dir
,
139 .readdir
= proc_ns_dir_readdir
,
142 static struct dentry
*proc_ns_dir_lookup(struct inode
*dir
,
143 struct dentry
*dentry
, unsigned int flags
)
145 struct dentry
*error
;
146 struct task_struct
*task
= get_proc_task(dir
);
147 const struct proc_ns_operations
**entry
, **last
;
148 unsigned int len
= dentry
->d_name
.len
;
150 error
= ERR_PTR(-ENOENT
);
155 error
= ERR_PTR(-EPERM
);
156 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
159 last
= &ns_entries
[ARRAY_SIZE(ns_entries
)];
160 for (entry
= ns_entries
; entry
< last
; entry
++) {
161 if (strlen((*entry
)->name
) != len
)
163 if (!memcmp(dentry
->d_name
.name
, (*entry
)->name
, len
))
166 error
= ERR_PTR(-ENOENT
);
170 error
= proc_ns_instantiate(dir
, dentry
, task
, *entry
);
172 put_task_struct(task
);
177 const struct inode_operations proc_ns_dir_inode_operations
= {
178 .lookup
= proc_ns_dir_lookup
,
179 .getattr
= pid_getattr
,
180 .setattr
= proc_setattr
,
183 struct file
*proc_ns_fget(int fd
)
189 return ERR_PTR(-EBADF
);
191 if (file
->f_op
!= &ns_file_operations
)
198 return ERR_PTR(-EINVAL
);