namespaces.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. };
  35. static const char *proc_ns_get_link(struct dentry *dentry,
  36. struct inode *inode,
  37. struct delayed_call *done)
  38. {
  39. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  40. struct task_struct *task;
  41. struct path ns_path;
  42. void *error = ERR_PTR(-EACCES);
  43. if (!dentry)
  44. return ERR_PTR(-ECHILD);
  45. task = get_proc_task(inode);
  46. if (!task)
  47. return error;
  48. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  49. error = ns_get_path(&ns_path, task, ns_ops);
  50. if (!error)
  51. nd_jump_link(&ns_path);
  52. }
  53. put_task_struct(task);
  54. return error;
  55. }
  56. static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  57. {
  58. struct inode *inode = d_inode(dentry);
  59. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  60. struct task_struct *task;
  61. char name[50];
  62. int res = -EACCES;
  63. task = get_proc_task(inode);
  64. if (!task)
  65. return res;
  66. if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  67. res = ns_get_name(name, sizeof(name), task, ns_ops);
  68. if (res >= 0)
  69. res = readlink_copy(buffer, buflen, name);
  70. }
  71. put_task_struct(task);
  72. return res;
  73. }
  74. static const struct inode_operations proc_ns_link_inode_operations = {
  75. .readlink = proc_ns_readlink,
  76. .get_link = proc_ns_get_link,
  77. .setattr = proc_setattr,
  78. };
  79. static struct dentry *proc_ns_instantiate(struct dentry *dentry,
  80. struct task_struct *task, const void *ptr)
  81. {
  82. const struct proc_ns_operations *ns_ops = ptr;
  83. struct inode *inode;
  84. struct proc_inode *ei;
  85. inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
  86. if (!inode)
  87. return ERR_PTR(-ENOENT);
  88. ei = PROC_I(inode);
  89. inode->i_op = &proc_ns_link_inode_operations;
  90. ei->ns_ops = ns_ops;
  91. pid_update_inode(task, inode);
  92. d_set_d_op(dentry, &pid_dentry_operations);
  93. return d_splice_alias(inode, dentry);
  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_shared = proc_ns_dir_readdir,
  122. .llseek = generic_file_llseek,
  123. };
  124. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  125. struct dentry *dentry, unsigned int flags)
  126. {
  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. struct dentry *res = ERR_PTR(-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. res = proc_ns_instantiate(dentry, task, *entry);
  143. out:
  144. put_task_struct(task);
  145. out_no_task:
  146. return res;
  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. };