namespaces.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 *const 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, strlen(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. return d_splice_alias_ops(inode, dentry, &pid_dentry_operations);
  99. }
  100. static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
  101. {
  102. struct task_struct *task = get_proc_task(file_inode(file));
  103. const struct proc_ns_operations *const *entry, *const *last;
  104. if (!task)
  105. return -ENOENT;
  106. if (!dir_emit_dots(file, ctx))
  107. goto out;
  108. if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
  109. goto out;
  110. entry = ns_entries + (ctx->pos - 2);
  111. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  112. while (entry <= last) {
  113. const struct proc_ns_operations *ops = *entry;
  114. if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
  115. proc_ns_instantiate, task, ops))
  116. break;
  117. ctx->pos++;
  118. entry++;
  119. }
  120. out:
  121. put_task_struct(task);
  122. return 0;
  123. }
  124. const struct file_operations proc_ns_dir_operations = {
  125. .read = generic_read_dir,
  126. .iterate_shared = proc_ns_dir_readdir,
  127. .llseek = generic_file_llseek,
  128. };
  129. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  130. struct dentry *dentry, unsigned int flags)
  131. {
  132. struct task_struct *task = get_proc_task(dir);
  133. const struct proc_ns_operations *const *entry, *const *last;
  134. unsigned int len = dentry->d_name.len;
  135. struct dentry *res = ERR_PTR(-ENOENT);
  136. if (!task)
  137. goto out_no_task;
  138. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  139. for (entry = ns_entries; entry < last; entry++) {
  140. if (strlen((*entry)->name) != len)
  141. continue;
  142. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  143. break;
  144. }
  145. if (entry == last)
  146. goto out;
  147. res = proc_ns_instantiate(dentry, task, *entry);
  148. out:
  149. put_task_struct(task);
  150. out_no_task:
  151. return res;
  152. }
  153. const struct inode_operations proc_ns_dir_inode_operations = {
  154. .lookup = proc_ns_dir_lookup,
  155. .getattr = pid_getattr,
  156. .setattr = proc_setattr,
  157. };