namespaces.c 4.1 KB

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