nsfs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/user_namespace.h>
  12. #include <linux/nsfs.h>
  13. #include <linux/uaccess.h>
  14. #include "internal.h"
  15. static struct vfsmount *nsfs_mnt;
  16. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  17. unsigned long arg);
  18. static const struct file_operations ns_file_operations = {
  19. .llseek = no_llseek,
  20. .unlocked_ioctl = ns_ioctl,
  21. };
  22. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  23. {
  24. struct inode *inode = d_inode(dentry);
  25. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  26. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  27. ns_ops->name, inode->i_ino);
  28. }
  29. static void ns_prune_dentry(struct dentry *dentry)
  30. {
  31. struct inode *inode = d_inode(dentry);
  32. if (inode) {
  33. struct ns_common *ns = inode->i_private;
  34. atomic_long_set(&ns->stashed, 0);
  35. }
  36. }
  37. const struct dentry_operations ns_dentry_operations =
  38. {
  39. .d_prune = ns_prune_dentry,
  40. .d_delete = always_delete_dentry,
  41. .d_dname = ns_dname,
  42. };
  43. static void nsfs_evict(struct inode *inode)
  44. {
  45. struct ns_common *ns = inode->i_private;
  46. clear_inode(inode);
  47. ns->ops->put(ns);
  48. }
  49. static int __ns_get_path(struct path *path, struct ns_common *ns)
  50. {
  51. struct vfsmount *mnt = nsfs_mnt;
  52. struct dentry *dentry;
  53. struct inode *inode;
  54. unsigned long d;
  55. rcu_read_lock();
  56. d = atomic_long_read(&ns->stashed);
  57. if (!d)
  58. goto slow;
  59. dentry = (struct dentry *)d;
  60. if (!lockref_get_not_dead(&dentry->d_lockref))
  61. goto slow;
  62. rcu_read_unlock();
  63. ns->ops->put(ns);
  64. got_it:
  65. path->mnt = mntget(mnt);
  66. path->dentry = dentry;
  67. return 0;
  68. slow:
  69. rcu_read_unlock();
  70. inode = new_inode_pseudo(mnt->mnt_sb);
  71. if (!inode) {
  72. ns->ops->put(ns);
  73. return -ENOMEM;
  74. }
  75. inode->i_ino = ns->inum;
  76. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  77. inode->i_flags |= S_IMMUTABLE;
  78. inode->i_mode = S_IFREG | S_IRUGO;
  79. inode->i_fop = &ns_file_operations;
  80. inode->i_private = ns;
  81. dentry = d_alloc_anon(mnt->mnt_sb);
  82. if (!dentry) {
  83. iput(inode);
  84. return -ENOMEM;
  85. }
  86. d_instantiate(dentry, inode);
  87. dentry->d_fsdata = (void *)ns->ops;
  88. d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
  89. if (d) {
  90. d_delete(dentry); /* make sure ->d_prune() does nothing */
  91. dput(dentry);
  92. cpu_relax();
  93. return -EAGAIN;
  94. }
  95. goto got_it;
  96. }
  97. int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
  98. void *private_data)
  99. {
  100. int ret;
  101. do {
  102. struct ns_common *ns = ns_get_cb(private_data);
  103. if (!ns)
  104. return -ENOENT;
  105. ret = __ns_get_path(path, ns);
  106. } while (ret == -EAGAIN);
  107. return ret;
  108. }
  109. struct ns_get_path_task_args {
  110. const struct proc_ns_operations *ns_ops;
  111. struct task_struct *task;
  112. };
  113. static struct ns_common *ns_get_path_task(void *private_data)
  114. {
  115. struct ns_get_path_task_args *args = private_data;
  116. return args->ns_ops->get(args->task);
  117. }
  118. int ns_get_path(struct path *path, struct task_struct *task,
  119. const struct proc_ns_operations *ns_ops)
  120. {
  121. struct ns_get_path_task_args args = {
  122. .ns_ops = ns_ops,
  123. .task = task,
  124. };
  125. return ns_get_path_cb(path, ns_get_path_task, &args);
  126. }
  127. int open_related_ns(struct ns_common *ns,
  128. struct ns_common *(*get_ns)(struct ns_common *ns))
  129. {
  130. struct path path = {};
  131. struct file *f;
  132. int err;
  133. int fd;
  134. fd = get_unused_fd_flags(O_CLOEXEC);
  135. if (fd < 0)
  136. return fd;
  137. do {
  138. struct ns_common *relative;
  139. relative = get_ns(ns);
  140. if (IS_ERR(relative)) {
  141. put_unused_fd(fd);
  142. return PTR_ERR(relative);
  143. }
  144. err = __ns_get_path(&path, relative);
  145. } while (err == -EAGAIN);
  146. if (err) {
  147. put_unused_fd(fd);
  148. return err;
  149. }
  150. f = dentry_open(&path, O_RDONLY, current_cred());
  151. path_put(&path);
  152. if (IS_ERR(f)) {
  153. put_unused_fd(fd);
  154. fd = PTR_ERR(f);
  155. } else
  156. fd_install(fd, f);
  157. return fd;
  158. }
  159. EXPORT_SYMBOL_GPL(open_related_ns);
  160. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  161. unsigned long arg)
  162. {
  163. struct user_namespace *user_ns;
  164. struct ns_common *ns = get_proc_ns(file_inode(filp));
  165. uid_t __user *argp;
  166. uid_t uid;
  167. switch (ioctl) {
  168. case NS_GET_USERNS:
  169. return open_related_ns(ns, ns_get_owner);
  170. case NS_GET_PARENT:
  171. if (!ns->ops->get_parent)
  172. return -EINVAL;
  173. return open_related_ns(ns, ns->ops->get_parent);
  174. case NS_GET_NSTYPE:
  175. return ns->ops->type;
  176. case NS_GET_OWNER_UID:
  177. if (ns->ops->type != CLONE_NEWUSER)
  178. return -EINVAL;
  179. user_ns = container_of(ns, struct user_namespace, ns);
  180. argp = (uid_t __user *) arg;
  181. uid = from_kuid_munged(current_user_ns(), user_ns->owner);
  182. return put_user(uid, argp);
  183. default:
  184. return -ENOTTY;
  185. }
  186. }
  187. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  188. const struct proc_ns_operations *ns_ops)
  189. {
  190. struct ns_common *ns;
  191. int res = -ENOENT;
  192. const char *name;
  193. ns = ns_ops->get(task);
  194. if (ns) {
  195. name = ns_ops->real_ns_name ? : ns_ops->name;
  196. res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
  197. ns_ops->put(ns);
  198. }
  199. return res;
  200. }
  201. bool proc_ns_file(const struct file *file)
  202. {
  203. return file->f_op == &ns_file_operations;
  204. }
  205. struct file *proc_ns_fget(int fd)
  206. {
  207. struct file *file;
  208. file = fget(fd);
  209. if (!file)
  210. return ERR_PTR(-EBADF);
  211. if (file->f_op != &ns_file_operations)
  212. goto out_invalid;
  213. return file;
  214. out_invalid:
  215. fput(file);
  216. return ERR_PTR(-EINVAL);
  217. }
  218. /**
  219. * ns_match() - Returns true if current namespace matches dev/ino provided.
  220. * @ns_common: current ns
  221. * @dev: dev_t from nsfs that will be matched against current nsfs
  222. * @ino: ino_t from nsfs that will be matched against current nsfs
  223. *
  224. * Return: true if dev and ino matches the current nsfs.
  225. */
  226. bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino)
  227. {
  228. return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev);
  229. }
  230. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  231. {
  232. struct inode *inode = d_inode(dentry);
  233. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  234. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  235. return 0;
  236. }
  237. static const struct super_operations nsfs_ops = {
  238. .statfs = simple_statfs,
  239. .evict_inode = nsfs_evict,
  240. .show_path = nsfs_show_path,
  241. };
  242. static int nsfs_init_fs_context(struct fs_context *fc)
  243. {
  244. struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
  245. if (!ctx)
  246. return -ENOMEM;
  247. ctx->ops = &nsfs_ops;
  248. ctx->dops = &ns_dentry_operations;
  249. return 0;
  250. }
  251. static struct file_system_type nsfs = {
  252. .name = "nsfs",
  253. .init_fs_context = nsfs_init_fs_context,
  254. .kill_sb = kill_anon_super,
  255. };
  256. void __init nsfs_init(void)
  257. {
  258. nsfs_mnt = kern_mount(&nsfs);
  259. if (IS_ERR(nsfs_mnt))
  260. panic("can't set nsfs up\n");
  261. nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  262. }