namespaces.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include <linux/proc_fs.h>
  2. #include <linux/nsproxy.h>
  3. #include <linux/sched.h>
  4. #include <linux/ptrace.h>
  5. #include <linux/fs_struct.h>
  6. #include <linux/mount.h>
  7. #include <linux/path.h>
  8. #include <linux/namei.h>
  9. #include <linux/file.h>
  10. #include <linux/utsname.h>
  11. #include <net/net_namespace.h>
  12. #include <linux/ipc_namespace.h>
  13. #include <linux/pid_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include "internal.h"
  16. static const struct proc_ns_operations *ns_entries[] = {
  17. #ifdef CONFIG_NET_NS
  18. &netns_operations,
  19. #endif
  20. #ifdef CONFIG_UTS_NS
  21. &utsns_operations,
  22. #endif
  23. #ifdef CONFIG_IPC_NS
  24. &ipcns_operations,
  25. #endif
  26. #ifdef CONFIG_PID_NS
  27. &pidns_operations,
  28. #endif
  29. #ifdef CONFIG_USER_NS
  30. &userns_operations,
  31. #endif
  32. &mntns_operations,
  33. };
  34. static const struct file_operations ns_file_operations = {
  35. .llseek = no_llseek,
  36. };
  37. static const struct inode_operations ns_inode_operations = {
  38. .setattr = proc_setattr,
  39. };
  40. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  41. {
  42. struct inode *inode = dentry->d_inode;
  43. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns.ns_ops;
  44. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  45. ns_ops->name, inode->i_ino);
  46. }
  47. const struct dentry_operations ns_dentry_operations =
  48. {
  49. .d_delete = always_delete_dentry,
  50. .d_dname = ns_dname,
  51. };
  52. static struct dentry *proc_ns_get_dentry(struct super_block *sb,
  53. struct task_struct *task, const struct proc_ns_operations *ns_ops)
  54. {
  55. struct dentry *dentry, *result;
  56. struct inode *inode;
  57. struct proc_inode *ei;
  58. struct qstr qname = { .name = "", };
  59. void *ns;
  60. ns = ns_ops->get(task);
  61. if (!ns)
  62. return ERR_PTR(-ENOENT);
  63. dentry = d_alloc_pseudo(sb, &qname);
  64. if (!dentry) {
  65. ns_ops->put(ns);
  66. return ERR_PTR(-ENOMEM);
  67. }
  68. inode = iget_locked(sb, ns_ops->inum(ns));
  69. if (!inode) {
  70. dput(dentry);
  71. ns_ops->put(ns);
  72. return ERR_PTR(-ENOMEM);
  73. }
  74. ei = PROC_I(inode);
  75. if (inode->i_state & I_NEW) {
  76. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  77. inode->i_op = &ns_inode_operations;
  78. inode->i_mode = S_IFREG | S_IRUGO;
  79. inode->i_fop = &ns_file_operations;
  80. ei->ns.ns_ops = ns_ops;
  81. ei->ns.ns = ns;
  82. unlock_new_inode(inode);
  83. } else {
  84. ns_ops->put(ns);
  85. }
  86. d_set_d_op(dentry, &ns_dentry_operations);
  87. result = d_instantiate_unique(dentry, inode);
  88. if (result) {
  89. dput(dentry);
  90. dentry = result;
  91. }
  92. return dentry;
  93. }
  94. static void *proc_ns_follow_link(struct dentry *dentry, struct nameidata *nd)
  95. {
  96. struct inode *inode = dentry->d_inode;
  97. struct super_block *sb = inode->i_sb;
  98. struct proc_inode *ei = PROC_I(inode);
  99. struct task_struct *task;
  100. struct path ns_path;
  101. void *error = ERR_PTR(-EACCES);
  102. task = get_proc_task(inode);
  103. if (!task)
  104. goto out;
  105. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  106. goto out_put_task;
  107. ns_path.dentry = proc_ns_get_dentry(sb, task, ei->ns.ns_ops);
  108. if (IS_ERR(ns_path.dentry)) {
  109. error = ERR_CAST(ns_path.dentry);
  110. goto out_put_task;
  111. }
  112. ns_path.mnt = mntget(nd->path.mnt);
  113. nd_jump_link(nd, &ns_path);
  114. error = NULL;
  115. out_put_task:
  116. put_task_struct(task);
  117. out:
  118. return error;
  119. }
  120. static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  121. {
  122. struct inode *inode = dentry->d_inode;
  123. struct proc_inode *ei = PROC_I(inode);
  124. const struct proc_ns_operations *ns_ops = ei->ns.ns_ops;
  125. struct task_struct *task;
  126. void *ns;
  127. char name[50];
  128. int len = -EACCES;
  129. task = get_proc_task(inode);
  130. if (!task)
  131. goto out;
  132. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  133. goto out_put_task;
  134. len = -ENOENT;
  135. ns = ns_ops->get(task);
  136. if (!ns)
  137. goto out_put_task;
  138. snprintf(name, sizeof(name), "%s:[%u]", ns_ops->name, ns_ops->inum(ns));
  139. len = strlen(name);
  140. if (len > buflen)
  141. len = buflen;
  142. if (copy_to_user(buffer, name, len))
  143. len = -EFAULT;
  144. ns_ops->put(ns);
  145. out_put_task:
  146. put_task_struct(task);
  147. out:
  148. return len;
  149. }
  150. static const struct inode_operations proc_ns_link_inode_operations = {
  151. .readlink = proc_ns_readlink,
  152. .follow_link = proc_ns_follow_link,
  153. .setattr = proc_setattr,
  154. };
  155. static int proc_ns_instantiate(struct inode *dir,
  156. struct dentry *dentry, struct task_struct *task, const void *ptr)
  157. {
  158. const struct proc_ns_operations *ns_ops = ptr;
  159. struct inode *inode;
  160. struct proc_inode *ei;
  161. inode = proc_pid_make_inode(dir->i_sb, task);
  162. if (!inode)
  163. goto out;
  164. ei = PROC_I(inode);
  165. inode->i_mode = S_IFLNK|S_IRWXUGO;
  166. inode->i_op = &proc_ns_link_inode_operations;
  167. ei->ns.ns_ops = ns_ops;
  168. d_set_d_op(dentry, &pid_dentry_operations);
  169. d_add(dentry, inode);
  170. /* Close the race of the process dying before we return the dentry */
  171. if (pid_revalidate(dentry, 0))
  172. return 0;
  173. out:
  174. return -ENOENT;
  175. }
  176. static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
  177. {
  178. struct task_struct *task = get_proc_task(file_inode(file));
  179. const struct proc_ns_operations **entry, **last;
  180. if (!task)
  181. return -ENOENT;
  182. if (!dir_emit_dots(file, ctx))
  183. goto out;
  184. if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
  185. goto out;
  186. entry = ns_entries + (ctx->pos - 2);
  187. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  188. while (entry <= last) {
  189. const struct proc_ns_operations *ops = *entry;
  190. if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
  191. proc_ns_instantiate, task, ops))
  192. break;
  193. ctx->pos++;
  194. entry++;
  195. }
  196. out:
  197. put_task_struct(task);
  198. return 0;
  199. }
  200. const struct file_operations proc_ns_dir_operations = {
  201. .read = generic_read_dir,
  202. .iterate = proc_ns_dir_readdir,
  203. };
  204. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  205. struct dentry *dentry, unsigned int flags)
  206. {
  207. int error;
  208. struct task_struct *task = get_proc_task(dir);
  209. const struct proc_ns_operations **entry, **last;
  210. unsigned int len = dentry->d_name.len;
  211. error = -ENOENT;
  212. if (!task)
  213. goto out_no_task;
  214. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  215. for (entry = ns_entries; entry < last; entry++) {
  216. if (strlen((*entry)->name) != len)
  217. continue;
  218. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  219. break;
  220. }
  221. if (entry == last)
  222. goto out;
  223. error = proc_ns_instantiate(dir, dentry, task, *entry);
  224. out:
  225. put_task_struct(task);
  226. out_no_task:
  227. return ERR_PTR(error);
  228. }
  229. const struct inode_operations proc_ns_dir_inode_operations = {
  230. .lookup = proc_ns_dir_lookup,
  231. .getattr = pid_getattr,
  232. .setattr = proc_setattr,
  233. };
  234. struct file *proc_ns_fget(int fd)
  235. {
  236. struct file *file;
  237. file = fget(fd);
  238. if (!file)
  239. return ERR_PTR(-EBADF);
  240. if (file->f_op != &ns_file_operations)
  241. goto out_invalid;
  242. return file;
  243. out_invalid:
  244. fput(file);
  245. return ERR_PTR(-EINVAL);
  246. }
  247. struct proc_ns *get_proc_ns(struct inode *inode)
  248. {
  249. return &PROC_I(inode)->ns;
  250. }
  251. bool proc_ns_inode(struct inode *inode)
  252. {
  253. return inode->i_fop == &ns_file_operations;
  254. }