nsfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include <linux/mount.h>
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/proc_ns.h>
  5. #include <linux/magic.h>
  6. #include <linux/ktime.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/user_namespace.h>
  9. #include <linux/nsfs.h>
  10. static struct vfsmount *nsfs_mnt;
  11. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  12. unsigned long arg);
  13. static const struct file_operations ns_file_operations = {
  14. .llseek = no_llseek,
  15. .unlocked_ioctl = ns_ioctl,
  16. };
  17. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  18. {
  19. struct inode *inode = d_inode(dentry);
  20. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  21. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  22. ns_ops->name, inode->i_ino);
  23. }
  24. static void ns_prune_dentry(struct dentry *dentry)
  25. {
  26. struct inode *inode = d_inode(dentry);
  27. if (inode) {
  28. struct ns_common *ns = inode->i_private;
  29. atomic_long_set(&ns->stashed, 0);
  30. }
  31. }
  32. const struct dentry_operations ns_dentry_operations =
  33. {
  34. .d_prune = ns_prune_dentry,
  35. .d_delete = always_delete_dentry,
  36. .d_dname = ns_dname,
  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. static void *__ns_get_path(struct path *path, struct ns_common *ns)
  45. {
  46. struct vfsmount *mnt = mntget(nsfs_mnt);
  47. struct qstr qname = { .name = "", };
  48. struct dentry *dentry;
  49. struct inode *inode;
  50. unsigned long d;
  51. rcu_read_lock();
  52. d = atomic_long_read(&ns->stashed);
  53. if (!d)
  54. goto slow;
  55. dentry = (struct dentry *)d;
  56. if (!lockref_get_not_dead(&dentry->d_lockref))
  57. goto slow;
  58. rcu_read_unlock();
  59. ns->ops->put(ns);
  60. got_it:
  61. path->mnt = mnt;
  62. path->dentry = dentry;
  63. return NULL;
  64. slow:
  65. rcu_read_unlock();
  66. inode = new_inode_pseudo(mnt->mnt_sb);
  67. if (!inode) {
  68. ns->ops->put(ns);
  69. mntput(mnt);
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. inode->i_ino = ns->inum;
  73. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  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, &qname);
  79. if (!dentry) {
  80. iput(inode);
  81. mntput(mnt);
  82. return ERR_PTR(-ENOMEM);
  83. }
  84. d_instantiate(dentry, inode);
  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. mntput(mnt);
  91. cpu_relax();
  92. return ERR_PTR(-EAGAIN);
  93. }
  94. goto got_it;
  95. }
  96. void *ns_get_path(struct path *path, struct task_struct *task,
  97. const struct proc_ns_operations *ns_ops)
  98. {
  99. struct ns_common *ns;
  100. void *ret;
  101. again:
  102. ns = ns_ops->get(task);
  103. if (!ns)
  104. return ERR_PTR(-ENOENT);
  105. ret = __ns_get_path(path, ns);
  106. if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
  107. goto again;
  108. return ret;
  109. }
  110. static int open_related_ns(struct ns_common *ns,
  111. struct ns_common *(*get_ns)(struct ns_common *ns))
  112. {
  113. struct path path = {};
  114. struct file *f;
  115. void *err;
  116. int fd;
  117. fd = get_unused_fd_flags(O_CLOEXEC);
  118. if (fd < 0)
  119. return fd;
  120. while (1) {
  121. struct ns_common *relative;
  122. relative = get_ns(ns);
  123. if (IS_ERR(relative)) {
  124. put_unused_fd(fd);
  125. return PTR_ERR(relative);
  126. }
  127. err = __ns_get_path(&path, relative);
  128. if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
  129. continue;
  130. break;
  131. }
  132. if (IS_ERR(err)) {
  133. put_unused_fd(fd);
  134. return PTR_ERR(err);
  135. }
  136. f = dentry_open(&path, O_RDONLY, current_cred());
  137. path_put(&path);
  138. if (IS_ERR(f)) {
  139. put_unused_fd(fd);
  140. fd = PTR_ERR(f);
  141. } else
  142. fd_install(fd, f);
  143. return fd;
  144. }
  145. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  146. unsigned long arg)
  147. {
  148. struct ns_common *ns = get_proc_ns(file_inode(filp));
  149. switch (ioctl) {
  150. case NS_GET_USERNS:
  151. return open_related_ns(ns, ns_get_owner);
  152. case NS_GET_PARENT:
  153. if (!ns->ops->get_parent)
  154. return -EINVAL;
  155. return open_related_ns(ns, ns->ops->get_parent);
  156. default:
  157. return -ENOTTY;
  158. }
  159. }
  160. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  161. const struct proc_ns_operations *ns_ops)
  162. {
  163. struct ns_common *ns;
  164. int res = -ENOENT;
  165. ns = ns_ops->get(task);
  166. if (ns) {
  167. res = snprintf(buf, size, "%s:[%u]", ns_ops->name, ns->inum);
  168. ns_ops->put(ns);
  169. }
  170. return res;
  171. }
  172. struct file *proc_ns_fget(int fd)
  173. {
  174. struct file *file;
  175. file = fget(fd);
  176. if (!file)
  177. return ERR_PTR(-EBADF);
  178. if (file->f_op != &ns_file_operations)
  179. goto out_invalid;
  180. return file;
  181. out_invalid:
  182. fput(file);
  183. return ERR_PTR(-EINVAL);
  184. }
  185. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  186. {
  187. struct inode *inode = d_inode(dentry);
  188. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  189. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  190. return 0;
  191. }
  192. static const struct super_operations nsfs_ops = {
  193. .statfs = simple_statfs,
  194. .evict_inode = nsfs_evict,
  195. .show_path = nsfs_show_path,
  196. };
  197. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  198. int flags, const char *dev_name, void *data)
  199. {
  200. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  201. &ns_dentry_operations, NSFS_MAGIC);
  202. }
  203. static struct file_system_type nsfs = {
  204. .name = "nsfs",
  205. .mount = nsfs_mount,
  206. .kill_sb = kill_anon_super,
  207. };
  208. void __init nsfs_init(void)
  209. {
  210. nsfs_mnt = kern_mount(&nsfs);
  211. if (IS_ERR(nsfs_mnt))
  212. panic("can't set nsfs up\n");
  213. nsfs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
  214. }