namespaces.c 4.0 KB

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