namespaces.c 4.1 KB

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