namespaces.c 4.1 KB

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