namespaces.c 4.2 KB

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