nsfs.c 17 KB

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