nsfs.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "mount.h"
  17. #include "internal.h"
  18. static struct vfsmount *nsfs_mnt;
  19. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  20. unsigned long arg);
  21. static const struct file_operations ns_file_operations = {
  22. .unlocked_ioctl = ns_ioctl,
  23. .compat_ioctl = compat_ptr_ioctl,
  24. };
  25. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  26. {
  27. struct inode *inode = d_inode(dentry);
  28. struct ns_common *ns = inode->i_private;
  29. const struct proc_ns_operations *ns_ops = ns->ops;
  30. return dynamic_dname(buffer, buflen, "%s:[%lu]",
  31. ns_ops->name, inode->i_ino);
  32. }
  33. const struct dentry_operations ns_dentry_operations = {
  34. .d_delete = always_delete_dentry,
  35. .d_dname = ns_dname,
  36. .d_prune = stashed_dentry_prune,
  37. };
  38. static void nsfs_evict(struct inode *inode)
  39. {
  40. struct ns_common *ns = inode->i_private;
  41. clear_inode(inode);
  42. ns->ops->put(ns);
  43. }
  44. int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
  45. void *private_data)
  46. {
  47. struct ns_common *ns;
  48. ns = ns_get_cb(private_data);
  49. if (!ns)
  50. return -ENOENT;
  51. return path_from_stashed(&ns->stashed, nsfs_mnt, ns, path);
  52. }
  53. struct ns_get_path_task_args {
  54. const struct proc_ns_operations *ns_ops;
  55. struct task_struct *task;
  56. };
  57. static struct ns_common *ns_get_path_task(void *private_data)
  58. {
  59. struct ns_get_path_task_args *args = private_data;
  60. return args->ns_ops->get(args->task);
  61. }
  62. int ns_get_path(struct path *path, struct task_struct *task,
  63. const struct proc_ns_operations *ns_ops)
  64. {
  65. struct ns_get_path_task_args args = {
  66. .ns_ops = ns_ops,
  67. .task = task,
  68. };
  69. return ns_get_path_cb(path, ns_get_path_task, &args);
  70. }
  71. /**
  72. * open_namespace - open a namespace
  73. * @ns: the namespace to open
  74. *
  75. * This will consume a reference to @ns indendent of success or failure.
  76. *
  77. * Return: A file descriptor on success or a negative error code on failure.
  78. */
  79. int open_namespace(struct ns_common *ns)
  80. {
  81. struct path path __free(path_put) = {};
  82. struct file *f;
  83. int err;
  84. /* call first to consume reference */
  85. err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
  86. if (err < 0)
  87. return err;
  88. CLASS(get_unused_fd, fd)(O_CLOEXEC);
  89. if (fd < 0)
  90. return fd;
  91. f = dentry_open(&path, O_RDONLY, current_cred());
  92. if (IS_ERR(f))
  93. return PTR_ERR(f);
  94. fd_install(fd, f);
  95. return take_fd(fd);
  96. }
  97. int open_related_ns(struct ns_common *ns,
  98. struct ns_common *(*get_ns)(struct ns_common *ns))
  99. {
  100. struct ns_common *relative;
  101. relative = get_ns(ns);
  102. if (IS_ERR(relative))
  103. return PTR_ERR(relative);
  104. return open_namespace(relative);
  105. }
  106. EXPORT_SYMBOL_GPL(open_related_ns);
  107. static int copy_ns_info_to_user(const struct mnt_namespace *mnt_ns,
  108. struct mnt_ns_info __user *uinfo, size_t usize,
  109. struct mnt_ns_info *kinfo)
  110. {
  111. /*
  112. * If userspace and the kernel have the same struct size it can just
  113. * be copied. If userspace provides an older struct, only the bits that
  114. * userspace knows about will be copied. If userspace provides a new
  115. * struct, only the bits that the kernel knows aobut will be copied and
  116. * the size value will be set to the size the kernel knows about.
  117. */
  118. kinfo->size = min(usize, sizeof(*kinfo));
  119. kinfo->mnt_ns_id = mnt_ns->seq;
  120. kinfo->nr_mounts = READ_ONCE(mnt_ns->nr_mounts);
  121. /* Subtract the root mount of the mount namespace. */
  122. if (kinfo->nr_mounts)
  123. kinfo->nr_mounts--;
  124. if (copy_to_user(uinfo, kinfo, kinfo->size))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  129. unsigned long arg)
  130. {
  131. struct user_namespace *user_ns;
  132. struct pid_namespace *pid_ns;
  133. struct task_struct *tsk;
  134. struct ns_common *ns = get_proc_ns(file_inode(filp));
  135. struct mnt_namespace *mnt_ns;
  136. bool previous = false;
  137. uid_t __user *argp;
  138. uid_t uid;
  139. int ret;
  140. switch (ioctl) {
  141. case NS_GET_USERNS:
  142. return open_related_ns(ns, ns_get_owner);
  143. case NS_GET_PARENT:
  144. if (!ns->ops->get_parent)
  145. return -EINVAL;
  146. return open_related_ns(ns, ns->ops->get_parent);
  147. case NS_GET_NSTYPE:
  148. return ns->ops->type;
  149. case NS_GET_OWNER_UID:
  150. if (ns->ops->type != CLONE_NEWUSER)
  151. return -EINVAL;
  152. user_ns = container_of(ns, struct user_namespace, ns);
  153. argp = (uid_t __user *) arg;
  154. uid = from_kuid_munged(current_user_ns(), user_ns->owner);
  155. return put_user(uid, argp);
  156. case NS_GET_MNTNS_ID: {
  157. __u64 __user *idp;
  158. __u64 id;
  159. if (ns->ops->type != CLONE_NEWNS)
  160. return -EINVAL;
  161. mnt_ns = container_of(ns, struct mnt_namespace, ns);
  162. idp = (__u64 __user *)arg;
  163. id = mnt_ns->seq;
  164. return put_user(id, idp);
  165. }
  166. case NS_GET_PID_FROM_PIDNS:
  167. fallthrough;
  168. case NS_GET_TGID_FROM_PIDNS:
  169. fallthrough;
  170. case NS_GET_PID_IN_PIDNS:
  171. fallthrough;
  172. case NS_GET_TGID_IN_PIDNS: {
  173. if (ns->ops->type != CLONE_NEWPID)
  174. return -EINVAL;
  175. ret = -ESRCH;
  176. pid_ns = container_of(ns, struct pid_namespace, ns);
  177. guard(rcu)();
  178. if (ioctl == NS_GET_PID_IN_PIDNS ||
  179. ioctl == NS_GET_TGID_IN_PIDNS)
  180. tsk = find_task_by_vpid(arg);
  181. else
  182. tsk = find_task_by_pid_ns(arg, pid_ns);
  183. if (!tsk)
  184. break;
  185. switch (ioctl) {
  186. case NS_GET_PID_FROM_PIDNS:
  187. ret = task_pid_vnr(tsk);
  188. break;
  189. case NS_GET_TGID_FROM_PIDNS:
  190. ret = task_tgid_vnr(tsk);
  191. break;
  192. case NS_GET_PID_IN_PIDNS:
  193. ret = task_pid_nr_ns(tsk, pid_ns);
  194. break;
  195. case NS_GET_TGID_IN_PIDNS:
  196. ret = task_tgid_nr_ns(tsk, pid_ns);
  197. break;
  198. default:
  199. ret = 0;
  200. break;
  201. }
  202. if (!ret)
  203. ret = -ESRCH;
  204. return ret;
  205. }
  206. }
  207. /* extensible ioctls */
  208. switch (_IOC_NR(ioctl)) {
  209. case _IOC_NR(NS_MNT_GET_INFO): {
  210. struct mnt_ns_info kinfo = {};
  211. struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg;
  212. size_t usize = _IOC_SIZE(ioctl);
  213. if (ns->ops->type != CLONE_NEWNS)
  214. return -EINVAL;
  215. if (!uinfo)
  216. return -EINVAL;
  217. if (usize < MNT_NS_INFO_SIZE_VER0)
  218. return -EINVAL;
  219. return copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
  220. }
  221. case _IOC_NR(NS_MNT_GET_PREV):
  222. previous = true;
  223. fallthrough;
  224. case _IOC_NR(NS_MNT_GET_NEXT): {
  225. struct mnt_ns_info kinfo = {};
  226. struct mnt_ns_info __user *uinfo = (struct mnt_ns_info __user *)arg;
  227. struct path path __free(path_put) = {};
  228. struct file *f __free(fput) = NULL;
  229. size_t usize = _IOC_SIZE(ioctl);
  230. if (ns->ops->type != CLONE_NEWNS)
  231. return -EINVAL;
  232. if (usize < MNT_NS_INFO_SIZE_VER0)
  233. return -EINVAL;
  234. if (previous)
  235. mnt_ns = lookup_prev_mnt_ns(to_mnt_ns(ns));
  236. else
  237. mnt_ns = lookup_next_mnt_ns(to_mnt_ns(ns));
  238. if (IS_ERR(mnt_ns))
  239. return PTR_ERR(mnt_ns);
  240. ns = to_ns_common(mnt_ns);
  241. /* Transfer ownership of @mnt_ns reference to @path. */
  242. ret = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
  243. if (ret)
  244. return ret;
  245. CLASS(get_unused_fd, fd)(O_CLOEXEC);
  246. if (fd < 0)
  247. return fd;
  248. f = dentry_open(&path, O_RDONLY, current_cred());
  249. if (IS_ERR(f))
  250. return PTR_ERR(f);
  251. if (uinfo) {
  252. /*
  253. * If @uinfo is passed return all information about the
  254. * mount namespace as well.
  255. */
  256. ret = copy_ns_info_to_user(to_mnt_ns(ns), uinfo, usize, &kinfo);
  257. if (ret)
  258. return ret;
  259. }
  260. /* Transfer reference of @f to caller's fdtable. */
  261. fd_install(fd, no_free_ptr(f));
  262. /* File descriptor is live so hand it off to the caller. */
  263. return take_fd(fd);
  264. }
  265. default:
  266. ret = -ENOTTY;
  267. }
  268. return ret;
  269. }
  270. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  271. const struct proc_ns_operations *ns_ops)
  272. {
  273. struct ns_common *ns;
  274. int res = -ENOENT;
  275. const char *name;
  276. ns = ns_ops->get(task);
  277. if (ns) {
  278. name = ns_ops->real_ns_name ? : ns_ops->name;
  279. res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
  280. ns_ops->put(ns);
  281. }
  282. return res;
  283. }
  284. bool proc_ns_file(const struct file *file)
  285. {
  286. return file->f_op == &ns_file_operations;
  287. }
  288. /**
  289. * ns_match() - Returns true if current namespace matches dev/ino provided.
  290. * @ns: current namespace
  291. * @dev: dev_t from nsfs that will be matched against current nsfs
  292. * @ino: ino_t from nsfs that will be matched against current nsfs
  293. *
  294. * Return: true if dev and ino matches the current nsfs.
  295. */
  296. bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino)
  297. {
  298. return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev);
  299. }
  300. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  301. {
  302. struct inode *inode = d_inode(dentry);
  303. const struct ns_common *ns = inode->i_private;
  304. const struct proc_ns_operations *ns_ops = ns->ops;
  305. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  306. return 0;
  307. }
  308. static const struct super_operations nsfs_ops = {
  309. .statfs = simple_statfs,
  310. .evict_inode = nsfs_evict,
  311. .show_path = nsfs_show_path,
  312. };
  313. static int nsfs_init_inode(struct inode *inode, void *data)
  314. {
  315. struct ns_common *ns = data;
  316. inode->i_private = data;
  317. inode->i_mode |= S_IRUGO;
  318. inode->i_fop = &ns_file_operations;
  319. inode->i_ino = ns->inum;
  320. return 0;
  321. }
  322. static void nsfs_put_data(void *data)
  323. {
  324. struct ns_common *ns = data;
  325. ns->ops->put(ns);
  326. }
  327. static const struct stashed_operations nsfs_stashed_ops = {
  328. .init_inode = nsfs_init_inode,
  329. .put_data = nsfs_put_data,
  330. };
  331. static int nsfs_init_fs_context(struct fs_context *fc)
  332. {
  333. struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
  334. if (!ctx)
  335. return -ENOMEM;
  336. ctx->ops = &nsfs_ops;
  337. ctx->dops = &ns_dentry_operations;
  338. fc->s_fs_info = (void *)&nsfs_stashed_ops;
  339. return 0;
  340. }
  341. static struct file_system_type nsfs = {
  342. .name = "nsfs",
  343. .init_fs_context = nsfs_init_fs_context,
  344. .kill_sb = kill_anon_super,
  345. };
  346. void __init nsfs_init(void)
  347. {
  348. nsfs_mnt = kern_mount(&nsfs);
  349. if (IS_ERR(nsfs_mnt))
  350. panic("can't set nsfs up\n");
  351. nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  352. }