namespaces.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <linux/proc_fs.h>
  2. #include <linux/nsproxy.h>
  3. #include <linux/ptrace.h>
  4. #include <linux/namei.h>
  5. #include <linux/file.h>
  6. #include <linux/utsname.h>
  7. #include <net/net_namespace.h>
  8. #include <linux/ipc_namespace.h>
  9. #include <linux/pid_namespace.h>
  10. #include <linux/user_namespace.h>
  11. #include "internal.h"
  12. static const struct proc_ns_operations *ns_entries[] = {
  13. #ifdef CONFIG_NET_NS
  14. &netns_operations,
  15. #endif
  16. #ifdef CONFIG_UTS_NS
  17. &utsns_operations,
  18. #endif
  19. #ifdef CONFIG_IPC_NS
  20. &ipcns_operations,
  21. #endif
  22. #ifdef CONFIG_PID_NS
  23. &pidns_operations,
  24. #endif
  25. #ifdef CONFIG_USER_NS
  26. &userns_operations,
  27. #endif
  28. &mntns_operations,
  29. #ifdef CONFIG_CGROUPS
  30. &cgroupns_operations,
  31. #endif
  32. };
  33. static const char *proc_ns_get_link(struct dentry *dentry,
  34. struct inode *inode,
  35. struct delayed_call *done)
  36. {
  37. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  38. struct task_struct *task;
  39. struct path ns_path;
  40. void *error = ERR_PTR(-EACCES);
  41. if (!dentry)
  42. return ERR_PTR(-ECHILD);
  43. task = get_proc_task(inode);
  44. if (!task)
  45. return error;
  46. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  47. error = ns_get_path(&ns_path, task, ns_ops);
  48. if (!error)
  49. nd_jump_link(&ns_path);
  50. }
  51. put_task_struct(task);
  52. return error;
  53. }
  54. static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  55. {
  56. struct inode *inode = d_inode(dentry);
  57. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  58. struct task_struct *task;
  59. char name[50];
  60. int res = -EACCES;
  61. task = get_proc_task(inode);
  62. if (!task)
  63. return res;
  64. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  65. res = ns_get_name(name, sizeof(name), task, ns_ops);
  66. if (res >= 0)
  67. res = readlink_copy(buffer, buflen, name);
  68. }
  69. put_task_struct(task);
  70. return res;
  71. }
  72. static const struct inode_operations proc_ns_link_inode_operations = {
  73. .readlink = proc_ns_readlink,
  74. .get_link = proc_ns_get_link,
  75. .setattr = proc_setattr,
  76. };
  77. static int proc_ns_instantiate(struct inode *dir,
  78. struct dentry *dentry, struct task_struct *task, const void *ptr)
  79. {
  80. const struct proc_ns_operations *ns_ops = ptr;
  81. struct inode *inode;
  82. struct proc_inode *ei;
  83. inode = proc_pid_make_inode(dir->i_sb, task);
  84. if (!inode)
  85. goto out;
  86. ei = PROC_I(inode);
  87. inode->i_mode = S_IFLNK|S_IRWXUGO;
  88. inode->i_op = &proc_ns_link_inode_operations;
  89. ei->ns_ops = ns_ops;
  90. d_set_d_op(dentry, &pid_dentry_operations);
  91. d_add(dentry, inode);
  92. /* Close the race of the process dying before we return the dentry */
  93. if (pid_revalidate(dentry, 0))
  94. return 0;
  95. out:
  96. return -ENOENT;
  97. }
  98. static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
  99. {
  100. struct task_struct *task = get_proc_task(file_inode(file));
  101. const struct proc_ns_operations **entry, **last;
  102. if (!task)
  103. return -ENOENT;
  104. if (!dir_emit_dots(file, ctx))
  105. goto out;
  106. if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
  107. goto out;
  108. entry = ns_entries + (ctx->pos - 2);
  109. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  110. while (entry <= last) {
  111. const struct proc_ns_operations *ops = *entry;
  112. if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
  113. proc_ns_instantiate, task, ops))
  114. break;
  115. ctx->pos++;
  116. entry++;
  117. }
  118. out:
  119. put_task_struct(task);
  120. return 0;
  121. }
  122. const struct file_operations proc_ns_dir_operations = {
  123. .read = generic_read_dir,
  124. .iterate_shared = proc_ns_dir_readdir,
  125. .llseek = generic_file_llseek,
  126. };
  127. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  128. struct dentry *dentry, unsigned int flags)
  129. {
  130. int 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 = -ENOENT;
  135. if (!task)
  136. goto out_no_task;
  137. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  138. for (entry = ns_entries; entry < last; entry++) {
  139. if (strlen((*entry)->name) != len)
  140. continue;
  141. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  142. break;
  143. }
  144. if (entry == last)
  145. goto out;
  146. error = proc_ns_instantiate(dir, dentry, task, *entry);
  147. out:
  148. put_task_struct(task);
  149. out_no_task:
  150. return ERR_PTR(error);
  151. }
  152. const struct inode_operations proc_ns_dir_inode_operations = {
  153. .lookup = proc_ns_dir_lookup,
  154. .getattr = pid_getattr,
  155. .setattr = proc_setattr,
  156. };