nsfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(struct path *path, struct task_struct *task,
  96. const struct proc_ns_operations *ns_ops)
  97. {
  98. struct ns_common *ns;
  99. void *ret;
  100. again:
  101. ns = ns_ops->get(task);
  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. int open_related_ns(struct ns_common *ns,
  110. struct ns_common *(*get_ns)(struct ns_common *ns))
  111. {
  112. struct path path = {};
  113. struct file *f;
  114. void *err;
  115. int fd;
  116. fd = get_unused_fd_flags(O_CLOEXEC);
  117. if (fd < 0)
  118. return fd;
  119. while (1) {
  120. struct ns_common *relative;
  121. relative = get_ns(ns);
  122. if (IS_ERR(relative)) {
  123. put_unused_fd(fd);
  124. return PTR_ERR(relative);
  125. }
  126. err = __ns_get_path(&path, relative);
  127. if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
  128. continue;
  129. break;
  130. }
  131. if (IS_ERR(err)) {
  132. put_unused_fd(fd);
  133. return PTR_ERR(err);
  134. }
  135. f = dentry_open(&path, O_RDONLY, current_cred());
  136. path_put(&path);
  137. if (IS_ERR(f)) {
  138. put_unused_fd(fd);
  139. fd = PTR_ERR(f);
  140. } else
  141. fd_install(fd, f);
  142. return fd;
  143. }
  144. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  145. unsigned long arg)
  146. {
  147. struct user_namespace *user_ns;
  148. struct ns_common *ns = get_proc_ns(file_inode(filp));
  149. uid_t __user *argp;
  150. uid_t uid;
  151. switch (ioctl) {
  152. case NS_GET_USERNS:
  153. return open_related_ns(ns, ns_get_owner);
  154. case NS_GET_PARENT:
  155. if (!ns->ops->get_parent)
  156. return -EINVAL;
  157. return open_related_ns(ns, ns->ops->get_parent);
  158. case NS_GET_NSTYPE:
  159. return ns->ops->type;
  160. case NS_GET_OWNER_UID:
  161. if (ns->ops->type != CLONE_NEWUSER)
  162. return -EINVAL;
  163. user_ns = container_of(ns, struct user_namespace, ns);
  164. argp = (uid_t __user *) arg;
  165. uid = from_kuid_munged(current_user_ns(), user_ns->owner);
  166. return put_user(uid, argp);
  167. default:
  168. return -ENOTTY;
  169. }
  170. }
  171. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  172. const struct proc_ns_operations *ns_ops)
  173. {
  174. struct ns_common *ns;
  175. int res = -ENOENT;
  176. const char *name;
  177. ns = ns_ops->get(task);
  178. if (ns) {
  179. name = ns_ops->real_ns_name ? : ns_ops->name;
  180. res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
  181. ns_ops->put(ns);
  182. }
  183. return res;
  184. }
  185. struct file *proc_ns_fget(int fd)
  186. {
  187. struct file *file;
  188. file = fget(fd);
  189. if (!file)
  190. return ERR_PTR(-EBADF);
  191. if (file->f_op != &ns_file_operations)
  192. goto out_invalid;
  193. return file;
  194. out_invalid:
  195. fput(file);
  196. return ERR_PTR(-EINVAL);
  197. }
  198. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  199. {
  200. struct inode *inode = d_inode(dentry);
  201. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  202. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  203. return 0;
  204. }
  205. static const struct super_operations nsfs_ops = {
  206. .statfs = simple_statfs,
  207. .evict_inode = nsfs_evict,
  208. .show_path = nsfs_show_path,
  209. };
  210. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  211. int flags, const char *dev_name, void *data)
  212. {
  213. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  214. &ns_dentry_operations, NSFS_MAGIC);
  215. }
  216. static struct file_system_type nsfs = {
  217. .name = "nsfs",
  218. .mount = nsfs_mount,
  219. .kill_sb = kill_anon_super,
  220. };
  221. void __init nsfs_init(void)
  222. {
  223. nsfs_mnt = kern_mount(&nsfs);
  224. if (IS_ERR(nsfs_mnt))
  225. panic("can't set nsfs up\n");
  226. nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
  227. }