namespaces.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. };
  30. static const char *proc_ns_get_link(struct dentry *dentry,
  31. struct inode *inode,
  32. struct delayed_call *done)
  33. {
  34. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  35. struct task_struct *task;
  36. struct path ns_path;
  37. void *error = ERR_PTR(-EACCES);
  38. if (!dentry)
  39. return ERR_PTR(-ECHILD);
  40. task = get_proc_task(inode);
  41. if (!task)
  42. return error;
  43. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  44. error = ns_get_path(&ns_path, task, ns_ops);
  45. if (!error)
  46. nd_jump_link(&ns_path);
  47. }
  48. put_task_struct(task);
  49. return error;
  50. }
  51. static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  52. {
  53. struct inode *inode = d_inode(dentry);
  54. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  55. struct task_struct *task;
  56. char name[50];
  57. int res = -EACCES;
  58. task = get_proc_task(inode);
  59. if (!task)
  60. return res;
  61. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  62. res = ns_get_name(name, sizeof(name), task, ns_ops);
  63. if (res >= 0)
  64. res = readlink_copy(buffer, buflen, name);
  65. }
  66. put_task_struct(task);
  67. return res;
  68. }
  69. static const struct inode_operations proc_ns_link_inode_operations = {
  70. .readlink = proc_ns_readlink,
  71. .get_link = proc_ns_get_link,
  72. .setattr = proc_setattr,
  73. };
  74. static int proc_ns_instantiate(struct inode *dir,
  75. struct dentry *dentry, struct task_struct *task, const void *ptr)
  76. {
  77. const struct proc_ns_operations *ns_ops = ptr;
  78. struct inode *inode;
  79. struct proc_inode *ei;
  80. inode = proc_pid_make_inode(dir->i_sb, task);
  81. if (!inode)
  82. goto out;
  83. ei = PROC_I(inode);
  84. inode->i_mode = S_IFLNK|S_IRWXUGO;
  85. inode->i_op = &proc_ns_link_inode_operations;
  86. ei->ns_ops = ns_ops;
  87. d_set_d_op(dentry, &pid_dentry_operations);
  88. d_add(dentry, inode);
  89. /* Close the race of the process dying before we return the dentry */
  90. if (pid_revalidate(dentry, 0))
  91. return 0;
  92. out:
  93. return -ENOENT;
  94. }
  95. static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
  96. {
  97. struct task_struct *task = get_proc_task(file_inode(file));
  98. const struct proc_ns_operations **entry, **last;
  99. if (!task)
  100. return -ENOENT;
  101. if (!dir_emit_dots(file, ctx))
  102. goto out;
  103. if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
  104. goto out;
  105. entry = ns_entries + (ctx->pos - 2);
  106. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  107. while (entry <= last) {
  108. const struct proc_ns_operations *ops = *entry;
  109. if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
  110. proc_ns_instantiate, task, ops))
  111. break;
  112. ctx->pos++;
  113. entry++;
  114. }
  115. out:
  116. put_task_struct(task);
  117. return 0;
  118. }
  119. const struct file_operations proc_ns_dir_operations = {
  120. .read = generic_read_dir,
  121. .iterate = proc_ns_dir_readdir,
  122. };
  123. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  124. struct dentry *dentry, unsigned int flags)
  125. {
  126. int error;
  127. struct task_struct *task = get_proc_task(dir);
  128. const struct proc_ns_operations **entry, **last;
  129. unsigned int len = dentry->d_name.len;
  130. error = -ENOENT;
  131. if (!task)
  132. goto out_no_task;
  133. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  134. for (entry = ns_entries; entry < last; entry++) {
  135. if (strlen((*entry)->name) != len)
  136. continue;
  137. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  138. break;
  139. }
  140. if (entry == last)
  141. goto out;
  142. error = proc_ns_instantiate(dir, dentry, task, *entry);
  143. out:
  144. put_task_struct(task);
  145. out_no_task:
  146. return ERR_PTR(error);
  147. }
  148. const struct inode_operations proc_ns_dir_inode_operations = {
  149. .lookup = proc_ns_dir_lookup,
  150. .getattr = pid_getattr,
  151. .setattr = proc_setattr,
  152. };