nsfs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_fsdata = (void *)ns->ops;
  84. d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
  85. if (d) {
  86. d_delete(dentry); /* make sure ->d_prune() does nothing */
  87. dput(dentry);
  88. cpu_relax();
  89. return ERR_PTR(-EAGAIN);
  90. }
  91. goto got_it;
  92. }
  93. void *ns_get_path(struct path *path, struct task_struct *task,
  94. const struct proc_ns_operations *ns_ops)
  95. {
  96. struct ns_common *ns;
  97. void *ret;
  98. again:
  99. ns = ns_ops->get(task);
  100. if (!ns)
  101. return ERR_PTR(-ENOENT);
  102. ret = __ns_get_path(path, ns);
  103. if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
  104. goto again;
  105. return ret;
  106. }
  107. static int open_related_ns(struct ns_common *ns,
  108. struct ns_common *(*get_ns)(struct ns_common *ns))
  109. {
  110. struct path path = {};
  111. struct file *f;
  112. void *err;
  113. int fd;
  114. fd = get_unused_fd_flags(O_CLOEXEC);
  115. if (fd < 0)
  116. return fd;
  117. while (1) {
  118. struct ns_common *relative;
  119. relative = get_ns(ns);
  120. if (IS_ERR(relative)) {
  121. put_unused_fd(fd);
  122. return PTR_ERR(relative);
  123. }
  124. err = __ns_get_path(&path, relative);
  125. if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
  126. continue;
  127. break;
  128. }
  129. if (IS_ERR(err)) {
  130. put_unused_fd(fd);
  131. return PTR_ERR(err);
  132. }
  133. f = dentry_open(&path, O_RDONLY, current_cred());
  134. path_put(&path);
  135. if (IS_ERR(f)) {
  136. put_unused_fd(fd);
  137. fd = PTR_ERR(f);
  138. } else
  139. fd_install(fd, f);
  140. return fd;
  141. }
  142. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  143. unsigned long arg)
  144. {
  145. struct ns_common *ns = get_proc_ns(file_inode(filp));
  146. switch (ioctl) {
  147. case NS_GET_USERNS:
  148. return open_related_ns(ns, ns_get_owner);
  149. case NS_GET_PARENT:
  150. if (!ns->ops->get_parent)
  151. return -EINVAL;
  152. return open_related_ns(ns, ns->ops->get_parent);
  153. default:
  154. return -ENOTTY;
  155. }
  156. }
  157. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  158. const struct proc_ns_operations *ns_ops)
  159. {
  160. struct ns_common *ns;
  161. int res = -ENOENT;
  162. ns = ns_ops->get(task);
  163. if (ns) {
  164. res = snprintf(buf, size, "%s:[%u]", ns_ops->name, ns->inum);
  165. ns_ops->put(ns);
  166. }
  167. return res;
  168. }
  169. struct file *proc_ns_fget(int fd)
  170. {
  171. struct file *file;
  172. file = fget(fd);
  173. if (!file)
  174. return ERR_PTR(-EBADF);
  175. if (file->f_op != &ns_file_operations)
  176. goto out_invalid;
  177. return file;
  178. out_invalid:
  179. fput(file);
  180. return ERR_PTR(-EINVAL);
  181. }
  182. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  183. {
  184. struct inode *inode = d_inode(dentry);
  185. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  186. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  187. return 0;
  188. }
  189. static const struct super_operations nsfs_ops = {
  190. .statfs = simple_statfs,
  191. .evict_inode = nsfs_evict,
  192. .show_path = nsfs_show_path,
  193. };
  194. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  195. int flags, const char *dev_name, void *data)
  196. {
  197. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  198. &ns_dentry_operations, NSFS_MAGIC);
  199. }
  200. static struct file_system_type nsfs = {
  201. .name = "nsfs",
  202. .mount = nsfs_mount,
  203. .kill_sb = kill_anon_super,
  204. };
  205. void __init nsfs_init(void)
  206. {
  207. nsfs_mnt = kern_mount(&nsfs);
  208. if (IS_ERR(nsfs_mnt))
  209. panic("can't set nsfs up\n");
  210. nsfs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
  211. }