namespaces.c 4.0 KB

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