namespaces.c 4.0 KB

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