xref: /linux/fs/nsfs.c (revision 587eb08a5fef911856c16c8bb444b2ab7f4f207f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mount.h>
3 #include <linux/pseudo_fs.h>
4 #include <linux/file.h>
5 #include <linux/fs.h>
6 #include <linux/proc_fs.h>
7 #include <linux/proc_ns.h>
8 #include <linux/magic.h>
9 #include <linux/ktime.h>
10 #include <linux/seq_file.h>
11 #include <linux/pid_namespace.h>
12 #include <linux/user_namespace.h>
13 #include <linux/nsfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/mnt_namespace.h>
16 #include <linux/ipc_namespace.h>
17 #include <linux/time_namespace.h>
18 #include <linux/utsname.h>
19 #include <linux/exportfs.h>
20 #include <linux/nstree.h>
21 #include <net/net_namespace.h>
22 
23 #include "mount.h"
24 #include "internal.h"
25 
26 static struct vfsmount *nsfs_mnt;
27 
28 static struct path nsfs_root_path = {};
29 
nsfs_get_root(struct path * path)30 void nsfs_get_root(struct path *path)
31 {
32 	*path = nsfs_root_path;
33 	path_get(path);
34 }
35 
36 static long ns_ioctl(struct file *filp, unsigned int ioctl,
37 			unsigned long arg);
38 static const struct file_operations ns_file_operations = {
39 	.unlocked_ioctl = ns_ioctl,
40 	.compat_ioctl   = compat_ptr_ioctl,
41 };
42 
ns_dname(struct dentry * dentry,char * buffer,int buflen)43 static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
44 {
45 	struct inode *inode = d_inode(dentry);
46 	struct ns_common *ns = inode->i_private;
47 	const struct proc_ns_operations *ns_ops = ns->ops;
48 
49 	return dynamic_dname(buffer, buflen, "%s:[%lu]",
50 		ns_ops->name, inode->i_ino);
51 }
52 
53 const struct dentry_operations ns_dentry_operations = {
54 	.d_dname	= ns_dname,
55 	.d_prune	= stashed_dentry_prune,
56 };
57 
nsfs_evict(struct inode * inode)58 static void nsfs_evict(struct inode *inode)
59 {
60 	struct ns_common *ns = inode->i_private;
61 
62 	__ns_ref_active_put(ns);
63 	clear_inode(inode);
64 	ns->ops->put(ns);
65 }
66 
ns_get_path_cb(struct path * path,ns_get_path_helper_t * ns_get_cb,void * private_data)67 int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
68 		     void *private_data)
69 {
70 	struct ns_common *ns;
71 
72 	ns = ns_get_cb(private_data);
73 	if (!ns)
74 		return -ENOENT;
75 
76 	return path_from_stashed(&ns->stashed, nsfs_mnt, ns, path);
77 }
78 
79 struct ns_get_path_task_args {
80 	const struct proc_ns_operations *ns_ops;
81 	struct task_struct *task;
82 };
83 
ns_get_path_task(void * private_data)84 static struct ns_common *ns_get_path_task(void *private_data)
85 {
86 	struct ns_get_path_task_args *args = private_data;
87 
88 	return args->ns_ops->get(args->task);
89 }
90 
ns_get_path(struct path * path,struct task_struct * task,const struct proc_ns_operations * ns_ops)91 int ns_get_path(struct path *path, struct task_struct *task,
92 		  const struct proc_ns_operations *ns_ops)
93 {
94 	struct ns_get_path_task_args args = {
95 		.ns_ops	= ns_ops,
96 		.task	= task,
97 	};
98 
99 	return ns_get_path_cb(path, ns_get_path_task, &args);
100 }
101 
102 /**
103  * open_namespace - open a namespace
104  * @ns: the namespace to open
105  *
106  * This will consume a reference to @ns indendent of success or failure.
107  *
108  * Return: A file descriptor on success or a negative error code on failure.
109  */
open_namespace(struct ns_common * ns)110 int open_namespace(struct ns_common *ns)
111 {
112 	struct path path __free(path_put) = {};
113 	int err;
114 
115 	/* call first to consume reference */
116 	err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
117 	if (err < 0)
118 		return err;
119 
120 	return FD_ADD(O_CLOEXEC, dentry_open(&path, O_RDONLY, current_cred()));
121 }
122 
open_related_ns(struct ns_common * ns,struct ns_common * (* get_ns)(struct ns_common * ns))123 int open_related_ns(struct ns_common *ns,
124 		   struct ns_common *(*get_ns)(struct ns_common *ns))
125 {
126 	struct ns_common *relative;
127 
128 	relative = get_ns(ns);
129 	if (IS_ERR(relative))
130 		return PTR_ERR(relative);
131 
132 	return open_namespace(relative);
133 }
134 EXPORT_SYMBOL_GPL(open_related_ns);
135 
copy_ns_info_to_user(const struct mnt_namespace * mnt_ns,struct mnt_ns_info __user * uinfo,size_t usize,struct mnt_ns_info * kinfo)136 static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,
137 				struct mnt_ns_info __user *uinfo, size_t usize,
138 				struct mnt_ns_info *kinfo)
139 {
140 	/*
141 	 * If userspace and the kernel have the same struct size it can just
142 	 * be copied. If userspace provides an older struct, only the bits that
143 	 * userspace knows about will be copied. If userspace provides a new
144 	 * struct, only the bits that the kernel knows aobut will be copied and
145 	 * the size value will be set to the size the kernel knows about.
146 	 */
147 	kinfo->size		= min(usize, sizeof(*kinfo));
148 	kinfo->mnt_ns_id	= mnt_ns->ns.ns_id;
149 	kinfo->nr_mounts	= READ_ONCE(mnt_ns->nr_mounts);
150 	/* Subtract the root mount of the mount namespace. */
151 	if (kinfo->nr_mounts)
152 		kinfo->nr_mounts--;
153 
154 	if (copy_to_user(uinfo, kinfo, kinfo->size))
155 		return -EFAULT;
156 
157 	return 0;
158 }
159 
nsfs_ioctl_valid(unsigned int cmd)160 static bool nsfs_ioctl_valid(unsigned int cmd)
161 {
162 	switch (cmd) {
163 	case NS_GET_USERNS:
164 	case NS_GET_PARENT:
165 	case NS_GET_NSTYPE:
166 	case NS_GET_OWNER_UID:
167 	case NS_GET_MNTNS_ID:
168 	case NS_GET_PID_FROM_PIDNS:
169 	case NS_GET_TGID_FROM_PIDNS:
170 	case NS_GET_PID_IN_PIDNS:
171 	case NS_GET_TGID_IN_PIDNS:
172 	case NS_GET_ID:
173 		return true;
174 	}
175 
176 	/* Extensible ioctls require some extra handling. */
177 	switch (_IOC_NR(cmd)) {
178 	case _IOC_NR(NS_MNT_GET_INFO):
179 		return extensible_ioctl_valid(cmd, NS_MNT_GET_INFO, MNT_NS_INFO_SIZE_VER0);
180 	case _IOC_NR(NS_MNT_GET_NEXT):
181 		return extensible_ioctl_valid(cmd, NS_MNT_GET_NEXT, MNT_NS_INFO_SIZE_VER0);
182 	case _IOC_NR(NS_MNT_GET_PREV):
183 		return extensible_ioctl_valid(cmd, NS_MNT_GET_PREV, MNT_NS_INFO_SIZE_VER0);
184 	}
185 
186 	return false;
187 }
188 
ns_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)189 static long ns_ioctl(struct file *filp, unsigned int ioctl,
190 			unsigned long arg)
191 {
192 	struct user_namespace *user_ns;
193 	struct pid_namespace *pid_ns;
194 	struct task_struct *tsk;
195 	struct ns_common *ns;
196 	struct mnt_namespace *mnt_ns;
197 	bool previous = false;
198 	uid_t __user *argp;
199 	uid_t uid;
200 	int ret;
201 
202 	if (!nsfs_ioctl_valid(ioctl))
203 		return -ENOIOCTLCMD;
204 
205 	ns = get_proc_ns(file_inode(filp));
206 	switch (ioctl) {
207 	case NS_GET_USERNS:
208 		return open_related_ns(ns, ns_get_owner);
209 	case NS_GET_PARENT:
210 		if (!ns->ops->get_parent)
211 			return -EINVAL;
212 		return open_related_ns(ns, ns->ops->get_parent);
213 	case NS_GET_NSTYPE:
214 		return ns->ns_type;
215 	case NS_GET_OWNER_UID:
216 		if (ns->ns_type != CLONE_NEWUSER)
217 			return -EINVAL;
218 		user_ns = container_of(ns, struct user_namespace, ns);
219 		argp = (uid_t __user *) arg;
220 		uid = from_kuid_munged(current_user_ns(), user_ns->owner);
221 		return put_user(uid, argp);
222 	case NS_GET_PID_FROM_PIDNS:
223 		fallthrough;
224 	case NS_GET_TGID_FROM_PIDNS:
225 		fallthrough;
226 	case NS_GET_PID_IN_PIDNS:
227 		fallthrough;
228 	case NS_GET_TGID_IN_PIDNS: {
229 		if (ns->ns_type != CLONE_NEWPID)
230 			return -EINVAL;
231 
232 		ret = -ESRCH;
233 		pid_ns = container_of(ns, struct pid_namespace, ns);
234 
235 		guard(rcu)();
236 
237 		if (ioctl == NS_GET_PID_IN_PIDNS ||
238 		    ioctl == NS_GET_TGID_IN_PIDNS)
239 			tsk = find_task_by_vpid(arg);
240 		else
241 			tsk = find_task_by_pid_ns(arg, pid_ns);
242 		if (!tsk)
243 			break;
244 
245 		switch (ioctl) {
246 		case NS_GET_PID_FROM_PIDNS:
247 			ret = task_pid_vnr(tsk);
248 			break;
249 		case NS_GET_TGID_FROM_PIDNS:
250 			ret = task_tgid_vnr(tsk);
251 			break;
252 		case NS_GET_PID_IN_PIDNS:
253 			ret = task_pid_nr_ns(tsk, pid_ns);
254 			break;
255 		case NS_GET_TGID_IN_PIDNS:
256 			ret = task_tgid_nr_ns(tsk, pid_ns);
257 			break;
258 		default:
259 			ret = 0;
260 			break;
261 		}
262 
263 		if (!ret)
264 			ret = -ESRCH;
265 		return ret;
266 	}
267 	case NS_GET_MNTNS_ID:
268 		if (ns->ns_type != CLONE_NEWNS)
269 			return -EINVAL;
270 		fallthrough;
271 	case NS_GET_ID: {
272 		__u64 __user *idp;
273 		__u64 id;
274 
275 		idp = (__u64 __user *)arg;
276 		id = ns->ns_id;
277 		return put_user(id, idp);
278 	}
279 	}
280 
281 	/* extensible ioctls */
282 	switch (_IOC_NR(ioctl)) {
283 	case _IOC_NR(NS_MNT_GET_INFO): {
284 		struct mnt_ns_info kinfo = {};
285 		struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg;
286 		size_t usize = _IOC_SIZE(ioctl);
287 
288 		if (ns->ns_type != CLONE_NEWNS)
289 			return -EINVAL;
290 
291 		if (!uinfo)
292 			return -EINVAL;
293 
294 		if (usize < MNT_NS_INFO_SIZE_VER0)
295 			return -EINVAL;
296 
297 		return copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
298 	}
299 	case _IOC_NR(NS_MNT_GET_PREV):
300 		previous = true;
301 		fallthrough;
302 	case _IOC_NR(NS_MNT_GET_NEXT): {
303 		struct mnt_ns_info kinfo = {};
304 		struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg;
305 		struct path path __free(path_put) = {};
306 		size_t usize = _IOC_SIZE(ioctl);
307 
308 		if (ns->ns_type != CLONE_NEWNS)
309 			return -EINVAL;
310 
311 		if (usize < MNT_NS_INFO_SIZE_VER0)
312 			return -EINVAL;
313 
314 		mnt_ns = get_sequential_mnt_ns(to_mnt_ns(ns), previous);
315 		if (IS_ERR(mnt_ns))
316 			return PTR_ERR(mnt_ns);
317 
318 		ns = to_ns_common(mnt_ns);
319 		/* Transfer ownership of @mnt_ns reference to @path. */
320 		ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
321 		if (ret)
322 			return ret;
323 
324 		FD_PREPARE(fdf, O_CLOEXEC, dentry_open(&path, O_RDONLY, current_cred()));
325 		if (fdf.err)
326 			return fdf.err;
327 		/*
328 		 * If @uinfo is passed return all information about the
329 		 * mount namespace as well.
330 		 */
331 		ret = copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
332 		if (ret)
333 			return ret;
334 		ret = fd_publish(fdf);
335 		break;
336 	}
337 	default:
338 		ret = -ENOTTY;
339 	}
340 
341 	return ret;
342 }
343 
ns_get_name(char * buf,size_t size,struct task_struct * task,const struct proc_ns_operations * ns_ops)344 int ns_get_name(char *buf, size_t size, struct task_struct *task,
345 			const struct proc_ns_operations *ns_ops)
346 {
347 	struct ns_common *ns;
348 	int res = -ENOENT;
349 	const char *name;
350 	ns = ns_ops->get(task);
351 	if (ns) {
352 		name = ns_ops->real_ns_name ? : ns_ops->name;
353 		res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
354 		ns_ops->put(ns);
355 	}
356 	return res;
357 }
358 
proc_ns_file(const struct file * file)359 bool proc_ns_file(const struct file *file)
360 {
361 	return file->f_op == &ns_file_operations;
362 }
363 
364 /**
365  * ns_match() - Returns true if current namespace matches dev/ino provided.
366  * @ns: current namespace
367  * @dev: dev_t from nsfs that will be matched against current nsfs
368  * @ino: ino_t from nsfs that will be matched against current nsfs
369  *
370  * Return: true if dev and ino matches the current nsfs.
371  */
ns_match(const struct ns_common * ns,dev_t dev,ino_t ino)372 bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino)
373 {
374 	return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev);
375 }
376 
377 
nsfs_show_path(struct seq_file * seq,struct dentry * dentry)378 static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
379 {
380 	struct inode *inode = d_inode(dentry);
381 	const struct ns_common *ns = inode->i_private;
382 	const struct proc_ns_operations *ns_ops = ns->ops;
383 
384 	seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
385 	return 0;
386 }
387 
388 static const struct super_operations nsfs_ops = {
389 	.statfs = simple_statfs,
390 	.evict_inode = nsfs_evict,
391 	.show_path = nsfs_show_path,
392 	.drop_inode = inode_just_drop,
393 };
394 
nsfs_init_inode(struct inode * inode,void * data)395 static int nsfs_init_inode(struct inode *inode, void *data)
396 {
397 	struct ns_common *ns = data;
398 
399 	inode->i_private = data;
400 	inode->i_mode |= S_IRUGO;
401 	inode->i_fop = &ns_file_operations;
402 	inode->i_ino = ns->inum;
403 
404 	/*
405 	 * Bring the namespace subtree back to life if we have to. This
406 	 * can happen when e.g., all processes using a network namespace
407 	 * and all namespace files or namespace file bind-mounts have
408 	 * died but there are still sockets pinning it. The SIOCGSKNS
409 	 * ioctl on such a socket will resurrect the relevant namespace
410 	 * subtree.
411 	 */
412 	__ns_ref_active_get(ns);
413 	return 0;
414 }
415 
nsfs_put_data(void * data)416 static void nsfs_put_data(void *data)
417 {
418 	struct ns_common *ns = data;
419 	ns->ops->put(ns);
420 }
421 
422 static const struct stashed_operations nsfs_stashed_ops = {
423 	.init_inode = nsfs_init_inode,
424 	.put_data = nsfs_put_data,
425 };
426 
427 #define NSFS_FID_SIZE_U32_VER0 (NSFS_FILE_HANDLE_SIZE_VER0 / sizeof(u32))
428 #define NSFS_FID_SIZE_U32_LATEST (NSFS_FILE_HANDLE_SIZE_LATEST / sizeof(u32))
429 
nsfs_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)430 static int nsfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
431 			  struct inode *parent)
432 {
433 	struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
434 	struct ns_common *ns = inode->i_private;
435 	int len = *max_len;
436 
437 	if (parent)
438 		return FILEID_INVALID;
439 
440 	if (len < NSFS_FID_SIZE_U32_VER0) {
441 		*max_len = NSFS_FID_SIZE_U32_LATEST;
442 		return FILEID_INVALID;
443 	} else if (len > NSFS_FID_SIZE_U32_LATEST) {
444 		*max_len = NSFS_FID_SIZE_U32_LATEST;
445 	}
446 
447 	fid->ns_id	= ns->ns_id;
448 	fid->ns_type	= ns->ns_type;
449 	fid->ns_inum	= inode->i_ino;
450 	return FILEID_NSFS;
451 }
452 
is_current_namespace(struct ns_common * ns)453 bool is_current_namespace(struct ns_common *ns)
454 {
455 	switch (ns->ns_type) {
456 #ifdef CONFIG_CGROUPS
457 	case CLONE_NEWCGROUP:
458 		return current_in_namespace(to_cg_ns(ns));
459 #endif
460 #ifdef CONFIG_IPC_NS
461 	case CLONE_NEWIPC:
462 		return current_in_namespace(to_ipc_ns(ns));
463 #endif
464 	case CLONE_NEWNS:
465 		return current_in_namespace(to_mnt_ns(ns));
466 #ifdef CONFIG_NET_NS
467 	case CLONE_NEWNET:
468 		return current_in_namespace(to_net_ns(ns));
469 #endif
470 #ifdef CONFIG_PID_NS
471 	case CLONE_NEWPID:
472 		return current_in_namespace(to_pid_ns(ns));
473 #endif
474 #ifdef CONFIG_TIME_NS
475 	case CLONE_NEWTIME:
476 		return current_in_namespace(to_time_ns(ns));
477 #endif
478 #ifdef CONFIG_USER_NS
479 	case CLONE_NEWUSER:
480 		return current_in_namespace(to_user_ns(ns));
481 #endif
482 #ifdef CONFIG_UTS_NS
483 	case CLONE_NEWUTS:
484 		return current_in_namespace(to_uts_ns(ns));
485 #endif
486 	default:
487 		VFS_WARN_ON_ONCE(true);
488 		return false;
489 	}
490 }
491 
nsfs_fh_to_dentry(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)492 static struct dentry *nsfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
493 					int fh_len, int fh_type)
494 {
495 	struct path path __free(path_put) = {};
496 	struct nsfs_file_handle *fid = (struct nsfs_file_handle *)fh;
497 	struct user_namespace *owning_ns = NULL;
498 	struct ns_common *ns;
499 	int ret;
500 
501 	if (fh_len < NSFS_FID_SIZE_U32_VER0)
502 		return NULL;
503 
504 	/* Check that any trailing bytes are zero. */
505 	if ((fh_len > NSFS_FID_SIZE_U32_LATEST) &&
506 	    memchr_inv((void *)fid + NSFS_FID_SIZE_U32_LATEST, 0,
507 		       fh_len - NSFS_FID_SIZE_U32_LATEST))
508 		return NULL;
509 
510 	switch (fh_type) {
511 	case FILEID_NSFS:
512 		break;
513 	default:
514 		return NULL;
515 	}
516 
517 	if (!fid->ns_id)
518 		return NULL;
519 	/* Either both are set or both are unset. */
520 	if (!fid->ns_inum != !fid->ns_type)
521 		return NULL;
522 
523 	scoped_guard(rcu) {
524 		ns = ns_tree_lookup_rcu(fid->ns_id, fid->ns_type);
525 		if (!ns)
526 			return NULL;
527 
528 		VFS_WARN_ON_ONCE(ns->ns_id != fid->ns_id);
529 
530 		if (fid->ns_inum && (fid->ns_inum != ns->inum))
531 			return NULL;
532 		if (fid->ns_type && (fid->ns_type != ns->ns_type))
533 			return NULL;
534 
535 		/*
536 		 * This is racy because we're not actually taking an
537 		 * active reference. IOW, it could happen that the
538 		 * namespace becomes inactive after this check.
539 		 * We don't care because nsfs_init_inode() will just
540 		 * resurrect the relevant namespace tree for us. If it
541 		 * has been active here we just allow it's resurrection.
542 		 * We could try to take an active reference here and
543 		 * then drop it again. But really, why bother.
544 		 */
545 		if (!ns_get_unless_inactive(ns))
546 			return NULL;
547 	}
548 
549 	switch (ns->ns_type) {
550 #ifdef CONFIG_CGROUPS
551 	case CLONE_NEWCGROUP:
552 		if (!current_in_namespace(to_cg_ns(ns)))
553 			owning_ns = to_cg_ns(ns)->user_ns;
554 		break;
555 #endif
556 #ifdef CONFIG_IPC_NS
557 	case CLONE_NEWIPC:
558 		if (!current_in_namespace(to_ipc_ns(ns)))
559 			owning_ns = to_ipc_ns(ns)->user_ns;
560 		break;
561 #endif
562 	case CLONE_NEWNS:
563 		if (!current_in_namespace(to_mnt_ns(ns)))
564 			owning_ns = to_mnt_ns(ns)->user_ns;
565 		break;
566 #ifdef CONFIG_NET_NS
567 	case CLONE_NEWNET:
568 		if (!current_in_namespace(to_net_ns(ns)))
569 			owning_ns = to_net_ns(ns)->user_ns;
570 		break;
571 #endif
572 #ifdef CONFIG_PID_NS
573 	case CLONE_NEWPID:
574 		if (!current_in_namespace(to_pid_ns(ns))) {
575 			owning_ns = to_pid_ns(ns)->user_ns;
576 		} else if (!READ_ONCE(to_pid_ns(ns)->child_reaper)) {
577 			ns->ops->put(ns);
578 			return ERR_PTR(-EPERM);
579 		}
580 		break;
581 #endif
582 #ifdef CONFIG_TIME_NS
583 	case CLONE_NEWTIME:
584 		if (!current_in_namespace(to_time_ns(ns)))
585 			owning_ns = to_time_ns(ns)->user_ns;
586 		break;
587 #endif
588 #ifdef CONFIG_USER_NS
589 	case CLONE_NEWUSER:
590 		if (!current_in_namespace(to_user_ns(ns)))
591 			owning_ns = to_user_ns(ns);
592 		break;
593 #endif
594 #ifdef CONFIG_UTS_NS
595 	case CLONE_NEWUTS:
596 		if (!current_in_namespace(to_uts_ns(ns)))
597 			owning_ns = to_uts_ns(ns)->user_ns;
598 		break;
599 #endif
600 	default:
601 		return ERR_PTR(-EOPNOTSUPP);
602 	}
603 
604 	if (owning_ns && !ns_capable(owning_ns, CAP_SYS_ADMIN)) {
605 		ns->ops->put(ns);
606 		return ERR_PTR(-EPERM);
607 	}
608 
609 	/* path_from_stashed() unconditionally consumes the reference. */
610 	ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
611 	if (ret)
612 		return ERR_PTR(ret);
613 
614 	return no_free_ptr(path.dentry);
615 }
616 
nsfs_export_permission(struct handle_to_path_ctx * ctx,unsigned int oflags)617 static int nsfs_export_permission(struct handle_to_path_ctx *ctx,
618 				   unsigned int oflags)
619 {
620 	/* nsfs_fh_to_dentry() performs all permission checks. */
621 	return 0;
622 }
623 
nsfs_export_open(const struct path * path,unsigned int oflags)624 static struct file *nsfs_export_open(const struct path *path, unsigned int oflags)
625 {
626 	return file_open_root(path, "", oflags, 0);
627 }
628 
629 static const struct export_operations nsfs_export_operations = {
630 	.encode_fh	= nsfs_encode_fh,
631 	.fh_to_dentry	= nsfs_fh_to_dentry,
632 	.open		= nsfs_export_open,
633 	.permission	= nsfs_export_permission,
634 };
635 
nsfs_init_fs_context(struct fs_context * fc)636 static int nsfs_init_fs_context(struct fs_context *fc)
637 {
638 	struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
639 	if (!ctx)
640 		return -ENOMEM;
641 	fc->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
642 	ctx->s_d_flags |= DCACHE_DONTCACHE;
643 	ctx->ops = &nsfs_ops;
644 	ctx->eops = &nsfs_export_operations;
645 	ctx->dops = &ns_dentry_operations;
646 	fc->s_fs_info = (void *)&nsfs_stashed_ops;
647 	return 0;
648 }
649 
650 static struct file_system_type nsfs = {
651 	.name = "nsfs",
652 	.init_fs_context = nsfs_init_fs_context,
653 	.kill_sb = kill_anon_super,
654 };
655 
nsfs_init(void)656 void __init nsfs_init(void)
657 {
658 	nsfs_mnt = kern_mount(&nsfs);
659 	if (IS_ERR(nsfs_mnt))
660 		panic("can't set nsfs up\n");
661 	nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
662 	nsfs_root_path.mnt = nsfs_mnt;
663 	nsfs_root_path.dentry = nsfs_mnt->mnt_root;
664 }
665 
nsproxy_ns_active_get(struct nsproxy * ns)666 void nsproxy_ns_active_get(struct nsproxy *ns)
667 {
668 	ns_ref_active_get(ns->mnt_ns);
669 	ns_ref_active_get(ns->uts_ns);
670 	ns_ref_active_get(ns->ipc_ns);
671 	ns_ref_active_get(ns->pid_ns_for_children);
672 	ns_ref_active_get(ns->cgroup_ns);
673 	ns_ref_active_get(ns->net_ns);
674 	ns_ref_active_get(ns->time_ns);
675 	ns_ref_active_get(ns->time_ns_for_children);
676 }
677 
nsproxy_ns_active_put(struct nsproxy * ns)678 void nsproxy_ns_active_put(struct nsproxy *ns)
679 {
680 	ns_ref_active_put(ns->mnt_ns);
681 	ns_ref_active_put(ns->uts_ns);
682 	ns_ref_active_put(ns->ipc_ns);
683 	ns_ref_active_put(ns->pid_ns_for_children);
684 	ns_ref_active_put(ns->cgroup_ns);
685 	ns_ref_active_put(ns->net_ns);
686 	ns_ref_active_put(ns->time_ns);
687 	ns_ref_active_put(ns->time_ns_for_children);
688 }
689