nsfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include <linux/uaccess.h>
  11. static struct vfsmount *nsfs_mnt;
  12. static long ns_ioctl(struct file *filp, unsigned int ioctl,
  13. unsigned long arg);
  14. static const struct file_operations ns_file_operations = {
  15. .llseek = no_llseek,
  16. .unlocked_ioctl = ns_ioctl,
  17. };
  18. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  19. {
  20. struct inode *inode = d_inode(dentry);
  21. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  22. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  23. ns_ops->name, inode->i_ino);
  24. }
  25. static void ns_prune_dentry(struct dentry *dentry)
  26. {
  27. struct inode *inode = d_inode(dentry);
  28. if (inode) {
  29. struct ns_common *ns = inode->i_private;
  30. atomic_long_set(&ns->stashed, 0);
  31. }
  32. }
  33. const struct dentry_operations ns_dentry_operations =
  34. {
  35. .d_prune = ns_prune_dentry,
  36. .d_delete = always_delete_dentry,
  37. .d_dname = ns_dname,
  38. };
  39. static void nsfs_evict(struct inode *inode)
  40. {
  41. struct ns_common *ns = inode->i_private;
  42. clear_inode(inode);
  43. ns->ops->put(ns);
  44. }
  45. static void *__ns_get_path(struct path *path, struct ns_common *ns)
  46. {
  47. struct vfsmount *mnt = nsfs_mnt;
  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, &empty_name);
  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. 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 user_namespace *user_ns;
  147. struct ns_common *ns = get_proc_ns(file_inode(filp));
  148. uid_t __user *argp;
  149. uid_t uid;
  150. switch (ioctl) {
  151. case NS_GET_USERNS:
  152. return open_related_ns(ns, ns_get_owner);
  153. case NS_GET_PARENT:
  154. if (!ns->ops->get_parent)
  155. return -EINVAL;
  156. return open_related_ns(ns, ns->ops->get_parent);
  157. case NS_GET_NSTYPE:
  158. return ns->ops->type;
  159. case NS_GET_OWNER_UID:
  160. if (ns->ops->type != CLONE_NEWUSER)
  161. return -EINVAL;
  162. user_ns = container_of(ns, struct user_namespace, ns);
  163. argp = (uid_t __user *) arg;
  164. uid = from_kuid_munged(current_user_ns(), user_ns->owner);
  165. return put_user(uid, argp);
  166. default:
  167. return -ENOTTY;
  168. }
  169. }
  170. int ns_get_name(char *buf, size_t size, struct task_struct *task,
  171. const struct proc_ns_operations *ns_ops)
  172. {
  173. struct ns_common *ns;
  174. int res = -ENOENT;
  175. const char *name;
  176. ns = ns_ops->get(task);
  177. if (ns) {
  178. name = ns_ops->real_ns_name ? : ns_ops->name;
  179. res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
  180. ns_ops->put(ns);
  181. }
  182. return res;
  183. }
  184. struct file *proc_ns_fget(int fd)
  185. {
  186. struct file *file;
  187. file = fget(fd);
  188. if (!file)
  189. return ERR_PTR(-EBADF);
  190. if (file->f_op != &ns_file_operations)
  191. goto out_invalid;
  192. return file;
  193. out_invalid:
  194. fput(file);
  195. return ERR_PTR(-EINVAL);
  196. }
  197. static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
  198. {
  199. struct inode *inode = d_inode(dentry);
  200. const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
  201. seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
  202. return 0;
  203. }
  204. static const struct super_operations nsfs_ops = {
  205. .statfs = simple_statfs,
  206. .evict_inode = nsfs_evict,
  207. .show_path = nsfs_show_path,
  208. };
  209. static struct dentry *nsfs_mount(struct file_system_type *fs_type,
  210. int flags, const char *dev_name, void *data)
  211. {
  212. return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
  213. &ns_dentry_operations, NSFS_MAGIC);
  214. }
  215. static struct file_system_type nsfs = {
  216. .name = "nsfs",
  217. .mount = nsfs_mount,
  218. .kill_sb = kill_anon_super,
  219. };
  220. void __init nsfs_init(void)
  221. {
  222. nsfs_mnt = kern_mount(&nsfs);
  223. if (IS_ERR(nsfs_mnt))
  224. panic("can't set nsfs up\n");
  225. nsfs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
  226. }