namespaces.c 4.2 KB

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