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