nsfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 = 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 = mntget(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. return ERR_PTR(-ENOMEM);
  70. }
  71. inode->i_ino = ns->inum;
  72. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  73. inode->i_flags |= S_IMMUTABLE;
  74. inode->i_mode = S_IFREG | S_IRUGO;
  75. inode->i_fop = &ns_file_operations;
  76. inode->i_private = ns;
  77. dentry = d_alloc_pseudo(mnt->mnt_sb, &qname);
  78. if (!dentry) {
  79. iput(inode);
  80. return ERR_PTR(-ENOMEM);
  81. }
  82. d_instantiate(dentry, inode);
  83. dentry->d_flags |= DCACHE_RCUACCESS;
  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(struct path *path, struct task_struct *task,
  95. const struct proc_ns_operations *ns_ops)
  96. {
  97. struct ns_common *ns;
  98. void *ret;
  99. again:
  100. ns = ns_ops->get(task);
  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. static int open_related_ns(struct ns_common *ns,
  109. struct ns_common *(*get_ns)(struct ns_common *ns))
  110. {
  111. struct path path = {};
  112. struct file *f;
  113. void *err;
  114. int fd;
  115. fd = get_unused_fd_flags(O_CLOEXEC);
  116. if (fd < 0)
  117. return fd;
  118. while (1) {
  119. struct ns_common *relative;
  120. relative = get_ns(ns);
  121. if (IS_ERR(relative)) {
  122. put_unused_fd(fd);
  123. return PTR_ERR(relative);
  124. }
  125. err = __ns_get_path(&path, relative);
  126. if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
  127. continue;
  128. break;
  129. }
  130. if (IS_ERR(err)) {
  131. put_unused_fd(fd);
  132. return PTR_ERR(err);
  133. }
  134. f = dentry_open(&path, O_RDONLY, current_cred());
  135. path_put(&path);
  136. if (IS_ERR(f)) {
  137. put_unused_fd(fd);
  138. fd = PTR_ERR(f);
  139. } else
  140. fd_install(fd, f);
  141. return fd;
  142. }
  143. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  144. unsigned long arg)
  145. {
  146. struct ns_common *ns = get_proc_ns(file_inode(filp));
  147. switch (ioctl) {
  148. case NS_GET_USERNS:
  149. return open_related_ns(ns, ns_get_owner);
  150. case NS_GET_PARENT:
  151. if (!ns->ops->get_parent)
  152. return -EINVAL;
  153. return open_related_ns(ns, ns->ops->get_parent);
  154. default:
  155. return -ENOTTY;
  156. }
  157. }
  158. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  159. const struct proc_ns_operations *ns_ops)
  160. {
  161. struct ns_common *ns;
  162. int res = -ENOENT;
  163. ns = ns_ops->get(task);
  164. if (ns) {
  165. res = snprintf(buf, size, "%s:[%u]", ns_ops->name, ns->inum);
  166. ns_ops->put(ns);
  167. }
  168. return res;
  169. }
  170. struct file *proc_ns_fget(int fd)
  171. {
  172. struct file *file;
  173. file = fget(fd);
  174. if (!file)
  175. return ERR_PTR(-EBADF);
  176. if (file->f_op != &ns_file_operations)
  177. goto out_invalid;
  178. return file;
  179. out_invalid:
  180. fput(file);
  181. return ERR_PTR(-EINVAL);
  182. }
  183. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  184. {
  185. struct inode *inode = d_inode(dentry);
  186. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  187. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  188. return 0;
  189. }
  190. static const struct super_operations nsfs_ops = {
  191. .statfs = simple_statfs,
  192. .evict_inode = nsfs_evict,
  193. .show_path = nsfs_show_path,
  194. };
  195. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  196. int flags, const char *dev_name, void *data)
  197. {
  198. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  199. &ns_dentry_operations, NSFS_MAGIC);
  200. }
  201. static struct file_system_type nsfs = {
  202. .name = "nsfs",
  203. .mount = nsfs_mount,
  204. .kill_sb = kill_anon_super,
  205. };
  206. void __init nsfs_init(void)
  207. {
  208. nsfs_mnt = kern_mount(&nsfs);
  209. if (IS_ERR(nsfs_mnt))
  210. panic("can't set nsfs up\n");
  211. nsfs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
  212. }