nsfs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mount.h>
  3. #include <linux/file.h>
  4. #include <linux/fs.h>
  5. #include <linux/proc_ns.h>
  6. #include <linux/magic.h>
  7. #include <linux/ktime.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/user_namespace.h>
  10. #include <linux/nsfs.h>
  11. #include <linux/uaccess.h>
  12. static struct vfsmount *nsfs_mnt;
  13. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  14. unsigned long arg);
  15. static const struct file_operations ns_file_operations = {
  16. .llseek = no_llseek,
  17. .unlocked_ioctl = ns_ioctl,
  18. };
  19. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  20. {
  21. struct inode *inode = d_inode(dentry);
  22. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  23. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  24. ns_ops->name, inode->i_ino);
  25. }
  26. static void ns_prune_dentry(struct dentry *dentry)
  27. {
  28. struct inode *inode = d_inode(dentry);
  29. if (inode) {
  30. struct ns_common *ns = inode->i_private;
  31. atomic_long_set(&ns->stashed, 0);
  32. }
  33. }
  34. const struct dentry_operations ns_dentry_operations =
  35. {
  36. .d_prune = ns_prune_dentry,
  37. .d_delete = always_delete_dentry,
  38. .d_dname = ns_dname,
  39. };
  40. static void nsfs_evict(struct inode *inode)
  41. {
  42. struct ns_common *ns = inode->i_private;
  43. clear_inode(inode);
  44. ns->ops->put(ns);
  45. }
  46. static void *__ns_get_path(struct path *path, struct ns_common *ns)
  47. {
  48. struct vfsmount *mnt = nsfs_mnt;
  49. struct dentry *dentry;
  50. struct inode *inode;
  51. unsigned long d;
  52. rcu_read_lock();
  53. d = atomic_long_read(&ns->stashed);
  54. if (!d)
  55. goto slow;
  56. dentry = (struct dentry *)d;
  57. if (!lockref_get_not_dead(&dentry->d_lockref))
  58. goto slow;
  59. rcu_read_unlock();
  60. ns->ops->put(ns);
  61. got_it:
  62. path->mnt = mntget(mnt);
  63. path->dentry = dentry;
  64. return NULL;
  65. slow:
  66. rcu_read_unlock();
  67. inode = new_inode_pseudo(mnt->mnt_sb);
  68. if (!inode) {
  69. ns->ops->put(ns);
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. inode->i_ino = ns->inum;
  73. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  74. inode->i_flags |= S_IMMUTABLE;
  75. inode->i_mode = S_IFREG | S_IRUGO;
  76. inode->i_fop = &ns_file_operations;
  77. inode->i_private = ns;
  78. dentry = d_alloc_pseudo(mnt->mnt_sb, &empty_name);
  79. if (!dentry) {
  80. iput(inode);
  81. return ERR_PTR(-ENOMEM);
  82. }
  83. d_instantiate(dentry, inode);
  84. dentry->d_flags |= DCACHE_RCUACCESS;
  85. dentry->d_fsdata = (void *)ns->ops;
  86. d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
  87. if (d) {
  88. d_delete(dentry); /* make sure ->d_prune() does nothing */
  89. dput(dentry);
  90. cpu_relax();
  91. return ERR_PTR(-EAGAIN);
  92. }
  93. goto got_it;
  94. }
  95. void *ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
  96. void *private_data)
  97. {
  98. struct ns_common *ns;
  99. void *ret;
  100. again:
  101. ns = ns_get_cb(private_data);
  102. if (!ns)
  103. return ERR_PTR(-ENOENT);
  104. ret = __ns_get_path(path, ns);
  105. if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
  106. goto again;
  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. void *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. void *err;
  133. int fd;
  134. fd = get_unused_fd_flags(O_CLOEXEC);
  135. if (fd < 0)
  136. return fd;
  137. while (1) {
  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. if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
  146. continue;
  147. break;
  148. }
  149. if (IS_ERR(err)) {
  150. put_unused_fd(fd);
  151. return PTR_ERR(err);
  152. }
  153. f = dentry_open(&path, O_RDONLY, current_cred());
  154. path_put(&path);
  155. if (IS_ERR(f)) {
  156. put_unused_fd(fd);
  157. fd = PTR_ERR(f);
  158. } else
  159. fd_install(fd, f);
  160. return fd;
  161. }
  162. EXPORT_SYMBOL_GPL(open_related_ns);
  163. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  164. unsigned long arg)
  165. {
  166. struct user_namespace *user_ns;
  167. struct ns_common *ns = get_proc_ns(file_inode(filp));
  168. uid_t __user *argp;
  169. uid_t uid;
  170. switch (ioctl) {
  171. case NS_GET_USERNS:
  172. return open_related_ns(ns, ns_get_owner);
  173. case NS_GET_PARENT:
  174. if (!ns->ops->get_parent)
  175. return -EINVAL;
  176. return open_related_ns(ns, ns->ops->get_parent);
  177. case NS_GET_NSTYPE:
  178. return ns->ops->type;
  179. case NS_GET_OWNER_UID:
  180. if (ns->ops->type != CLONE_NEWUSER)
  181. return -EINVAL;
  182. user_ns = container_of(ns, struct user_namespace, ns);
  183. argp = (uid_t __user *) arg;
  184. uid = from_kuid_munged(current_user_ns(), user_ns->owner);
  185. return put_user(uid, argp);
  186. default:
  187. return -ENOTTY;
  188. }
  189. }
  190. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  191. const struct proc_ns_operations *ns_ops)
  192. {
  193. struct ns_common *ns;
  194. int res = -ENOENT;
  195. const char *name;
  196. ns = ns_ops->get(task);
  197. if (ns) {
  198. name = ns_ops->real_ns_name ? : ns_ops->name;
  199. res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
  200. ns_ops->put(ns);
  201. }
  202. return res;
  203. }
  204. struct file *proc_ns_fget(int fd)
  205. {
  206. struct file *file;
  207. file = fget(fd);
  208. if (!file)
  209. return ERR_PTR(-EBADF);
  210. if (file->f_op != &ns_file_operations)
  211. goto out_invalid;
  212. return file;
  213. out_invalid:
  214. fput(file);
  215. return ERR_PTR(-EINVAL);
  216. }
  217. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  218. {
  219. struct inode *inode = d_inode(dentry);
  220. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  221. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  222. return 0;
  223. }
  224. static const struct super_operations nsfs_ops = {
  225. .statfs = simple_statfs,
  226. .evict_inode = nsfs_evict,
  227. .show_path = nsfs_show_path,
  228. };
  229. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  230. int flags, const char *dev_name, void *data)
  231. {
  232. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  233. &ns_dentry_operations, NSFS_MAGIC);
  234. }
  235. static struct file_system_type nsfs = {
  236. .name = "nsfs",
  237. .mount = nsfs_mount,
  238. .kill_sb = kill_anon_super,
  239. };
  240. void __init nsfs_init(void)
  241. {
  242. nsfs_mnt = kern_mount(&nsfs);
  243. if (IS_ERR(nsfs_mnt))
  244. panic("can't set nsfs up\n");
  245. nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  246. }