namespaces.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 struct dentry *proc_ns_instantiate(struct inode *dir,
  38. struct dentry *dentry, struct task_struct *task, const void *ptr)
  39. {
  40. const struct proc_ns_operations *ns_ops = ptr;
  41. struct inode *inode;
  42. struct proc_inode *ei;
  43. struct dentry *error = ERR_PTR(-ENOENT);
  44. void *ns;
  45. inode = proc_pid_make_inode(dir->i_sb, task);
  46. if (!inode)
  47. goto out;
  48. ns = ns_ops->get(task);
  49. if (!ns)
  50. goto out_iput;
  51. ei = PROC_I(inode);
  52. inode->i_mode = S_IFREG|S_IRUSR;
  53. inode->i_fop = &ns_file_operations;
  54. ei->ns_ops = ns_ops;
  55. ei->ns = ns;
  56. d_set_d_op(dentry, &pid_dentry_operations);
  57. d_add(dentry, inode);
  58. /* Close the race of the process dying before we return the dentry */
  59. if (pid_revalidate(dentry, 0))
  60. error = NULL;
  61. out:
  62. return error;
  63. out_iput:
  64. iput(inode);
  65. goto out;
  66. }
  67. static int proc_ns_fill_cache(struct file *filp, void *dirent,
  68. filldir_t filldir, struct task_struct *task,
  69. const struct proc_ns_operations *ops)
  70. {
  71. return proc_fill_cache(filp, dirent, filldir,
  72. ops->name, strlen(ops->name),
  73. proc_ns_instantiate, task, ops);
  74. }
  75. static int proc_ns_dir_readdir(struct file *filp, void *dirent,
  76. filldir_t filldir)
  77. {
  78. int i;
  79. struct dentry *dentry = filp->f_path.dentry;
  80. struct inode *inode = dentry->d_inode;
  81. struct task_struct *task = get_proc_task(inode);
  82. const struct proc_ns_operations **entry, **last;
  83. ino_t ino;
  84. int ret;
  85. ret = -ENOENT;
  86. if (!task)
  87. goto out_no_task;
  88. ret = -EPERM;
  89. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  90. goto out;
  91. ret = 0;
  92. i = filp->f_pos;
  93. switch (i) {
  94. case 0:
  95. ino = inode->i_ino;
  96. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  97. goto out;
  98. i++;
  99. filp->f_pos++;
  100. /* fall through */
  101. case 1:
  102. ino = parent_ino(dentry);
  103. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  104. goto out;
  105. i++;
  106. filp->f_pos++;
  107. /* fall through */
  108. default:
  109. i -= 2;
  110. if (i >= ARRAY_SIZE(ns_entries)) {
  111. ret = 1;
  112. goto out;
  113. }
  114. entry = ns_entries + i;
  115. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  116. while (entry <= last) {
  117. if (proc_ns_fill_cache(filp, dirent, filldir,
  118. task, *entry) < 0)
  119. goto out;
  120. filp->f_pos++;
  121. entry++;
  122. }
  123. }
  124. ret = 1;
  125. out:
  126. put_task_struct(task);
  127. out_no_task:
  128. return ret;
  129. }
  130. const struct file_operations proc_ns_dir_operations = {
  131. .read = generic_read_dir,
  132. .readdir = proc_ns_dir_readdir,
  133. };
  134. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  135. struct dentry *dentry, unsigned int flags)
  136. {
  137. struct dentry *error;
  138. struct task_struct *task = get_proc_task(dir);
  139. const struct proc_ns_operations **entry, **last;
  140. unsigned int len = dentry->d_name.len;
  141. error = ERR_PTR(-ENOENT);
  142. if (!task)
  143. goto out_no_task;
  144. error = ERR_PTR(-EPERM);
  145. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  146. goto out;
  147. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  148. for (entry = ns_entries; entry < last; entry++) {
  149. if (strlen((*entry)->name) != len)
  150. continue;
  151. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  152. break;
  153. }
  154. error = ERR_PTR(-ENOENT);
  155. if (entry == last)
  156. goto out;
  157. error = proc_ns_instantiate(dir, dentry, task, *entry);
  158. out:
  159. put_task_struct(task);
  160. out_no_task:
  161. return error;
  162. }
  163. const struct inode_operations proc_ns_dir_inode_operations = {
  164. .lookup = proc_ns_dir_lookup,
  165. .getattr = pid_getattr,
  166. .setattr = proc_setattr,
  167. };
  168. struct file *proc_ns_fget(int fd)
  169. {
  170. struct file *file;
  171. file = fget(fd);
  172. if (!file)
  173. return ERR_PTR(-EBADF);
  174. if (file->f_op != &ns_file_operations)
  175. goto out_invalid;
  176. return file;
  177. out_invalid:
  178. fput(file);
  179. return ERR_PTR(-EINVAL);
  180. }
  181. bool proc_ns_inode(struct inode *inode)
  182. {
  183. return inode->i_fop == &ns_file_operations;
  184. }