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>
14 #include <linux/user_namespace.h>
18 static const struct proc_ns_operations
*ns_entries
[] = {
37 static const struct file_operations ns_file_operations
= {
41 static const struct inode_operations ns_inode_operations
= {
42 .setattr
= proc_setattr
,
45 static char *ns_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
47 struct inode
*inode
= dentry
->d_inode
;
48 const struct proc_ns_operations
*ns_ops
= dentry
->d_fsdata
;
50 return dynamic_dname(dentry
, buffer
, buflen
, "%s:[%lu]",
51 ns_ops
->name
, inode
->i_ino
);
54 const struct dentry_operations ns_dentry_operations
=
56 .d_delete
= always_delete_dentry
,
60 static struct dentry
*proc_ns_get_dentry(struct super_block
*sb
,
61 struct task_struct
*task
, const struct proc_ns_operations
*ns_ops
)
63 struct dentry
*dentry
, *result
;
65 struct proc_inode
*ei
;
66 struct qstr qname
= { .name
= "", };
69 ns
= ns_ops
->get(task
);
71 return ERR_PTR(-ENOENT
);
73 dentry
= d_alloc_pseudo(sb
, &qname
);
76 return ERR_PTR(-ENOMEM
);
78 dentry
->d_fsdata
= (void *)ns_ops
;
80 inode
= iget_locked(sb
, ns
->inum
);
84 return ERR_PTR(-ENOMEM
);
88 if (inode
->i_state
& I_NEW
) {
89 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
90 inode
->i_op
= &ns_inode_operations
;
91 inode
->i_mode
= S_IFREG
| S_IRUGO
;
92 inode
->i_fop
= &ns_file_operations
;
93 ei
->ns
.ns_ops
= ns_ops
;
95 unlock_new_inode(inode
);
100 d_set_d_op(dentry
, &ns_dentry_operations
);
101 result
= d_instantiate_unique(dentry
, inode
);
110 static void *proc_ns_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
112 struct inode
*inode
= dentry
->d_inode
;
113 struct super_block
*sb
= inode
->i_sb
;
114 struct proc_inode
*ei
= PROC_I(inode
);
115 struct task_struct
*task
;
117 void *error
= ERR_PTR(-EACCES
);
119 task
= get_proc_task(inode
);
123 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
126 ns_path
.dentry
= proc_ns_get_dentry(sb
, task
, ei
->ns
.ns_ops
);
127 if (IS_ERR(ns_path
.dentry
)) {
128 error
= ERR_CAST(ns_path
.dentry
);
132 ns_path
.mnt
= mntget(nd
->path
.mnt
);
133 nd_jump_link(nd
, &ns_path
);
137 put_task_struct(task
);
142 static int proc_ns_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
144 struct inode
*inode
= dentry
->d_inode
;
145 struct proc_inode
*ei
= PROC_I(inode
);
146 const struct proc_ns_operations
*ns_ops
= ei
->ns
.ns_ops
;
147 struct task_struct
*task
;
148 struct ns_common
*ns
;
152 task
= get_proc_task(inode
);
156 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
160 ns
= ns_ops
->get(task
);
164 snprintf(name
, sizeof(name
), "%s:[%u]", ns_ops
->name
, ns
->inum
);
165 res
= readlink_copy(buffer
, buflen
, name
);
168 put_task_struct(task
);
173 static const struct inode_operations proc_ns_link_inode_operations
= {
174 .readlink
= proc_ns_readlink
,
175 .follow_link
= proc_ns_follow_link
,
176 .setattr
= proc_setattr
,
179 static int proc_ns_instantiate(struct inode
*dir
,
180 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
182 const struct proc_ns_operations
*ns_ops
= ptr
;
184 struct proc_inode
*ei
;
186 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
191 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
192 inode
->i_op
= &proc_ns_link_inode_operations
;
193 ei
->ns
.ns_ops
= ns_ops
;
195 d_set_d_op(dentry
, &pid_dentry_operations
);
196 d_add(dentry
, inode
);
197 /* Close the race of the process dying before we return the dentry */
198 if (pid_revalidate(dentry
, 0))
204 static int proc_ns_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
206 struct task_struct
*task
= get_proc_task(file_inode(file
));
207 const struct proc_ns_operations
**entry
, **last
;
212 if (!dir_emit_dots(file
, ctx
))
214 if (ctx
->pos
>= 2 + ARRAY_SIZE(ns_entries
))
216 entry
= ns_entries
+ (ctx
->pos
- 2);
217 last
= &ns_entries
[ARRAY_SIZE(ns_entries
) - 1];
218 while (entry
<= last
) {
219 const struct proc_ns_operations
*ops
= *entry
;
220 if (!proc_fill_cache(file
, ctx
, ops
->name
, strlen(ops
->name
),
221 proc_ns_instantiate
, task
, ops
))
227 put_task_struct(task
);
231 const struct file_operations proc_ns_dir_operations
= {
232 .read
= generic_read_dir
,
233 .iterate
= proc_ns_dir_readdir
,
236 static struct dentry
*proc_ns_dir_lookup(struct inode
*dir
,
237 struct dentry
*dentry
, unsigned int flags
)
240 struct task_struct
*task
= get_proc_task(dir
);
241 const struct proc_ns_operations
**entry
, **last
;
242 unsigned int len
= dentry
->d_name
.len
;
249 last
= &ns_entries
[ARRAY_SIZE(ns_entries
)];
250 for (entry
= ns_entries
; entry
< last
; entry
++) {
251 if (strlen((*entry
)->name
) != len
)
253 if (!memcmp(dentry
->d_name
.name
, (*entry
)->name
, len
))
259 error
= proc_ns_instantiate(dir
, dentry
, task
, *entry
);
261 put_task_struct(task
);
263 return ERR_PTR(error
);
266 const struct inode_operations proc_ns_dir_inode_operations
= {
267 .lookup
= proc_ns_dir_lookup
,
268 .getattr
= pid_getattr
,
269 .setattr
= proc_setattr
,
272 struct file
*proc_ns_fget(int fd
)
278 return ERR_PTR(-EBADF
);
280 if (file
->f_op
!= &ns_file_operations
)
287 return ERR_PTR(-EINVAL
);
290 struct ns_common
*get_proc_ns(struct inode
*inode
)
292 return PROC_I(inode
)->ns
.ns
;
295 bool proc_ns_inode(struct inode
*inode
)
297 return inode
->i_fop
== &ns_file_operations
;