namespaces.c 4.0 KB

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