namespaces.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include <linux/proc_fs.h>
  2. #include <linux/nsproxy.h>
  3. #include <linux/sched.h>
  4. #include <linux/ptrace.h>
  5. #include <linux/fs_struct.h>
  6. #include <linux/mount.h>
  7. #include <linux/path.h>
  8. #include <linux/namei.h>
  9. #include <linux/file.h>
  10. #include <linux/utsname.h>
  11. #include <net/net_namespace.h>
  12. #include <linux/ipc_namespace.h>
  13. #include <linux/pid_namespace.h>
  14. #include <linux/user_namespace.h>
  15. #include "internal.h"
  16. static const struct proc_ns_operations *ns_entries[] = {
  17. #ifdef CONFIG_NET_NS
  18. &netns_operations,
  19. #endif
  20. #ifdef CONFIG_UTS_NS
  21. &utsns_operations,
  22. #endif
  23. #ifdef CONFIG_IPC_NS
  24. &ipcns_operations,
  25. #endif
  26. #ifdef CONFIG_PID_NS
  27. &pidns_operations,
  28. #endif
  29. #ifdef CONFIG_USER_NS
  30. &userns_operations,
  31. #endif
  32. &mntns_operations,
  33. };
  34. static const struct file_operations ns_file_operations = {
  35. .llseek = no_llseek,
  36. };
  37. static const struct inode_operations ns_inode_operations = {
  38. .setattr = proc_setattr,
  39. };
  40. static int ns_delete_dentry(const struct dentry *dentry)
  41. {
  42. /* Don't cache namespace inodes when not in use */
  43. return 1;
  44. }
  45. static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
  46. {
  47. struct inode *inode = dentry->d_inode;
  48. const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
  49. return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
  50. ns_ops->name, inode->i_ino);
  51. }
  52. const struct dentry_operations ns_dentry_operations =
  53. {
  54. .d_delete = ns_delete_dentry,
  55. .d_dname = ns_dname,
  56. };
  57. static struct dentry *proc_ns_get_dentry(struct super_block *sb,
  58. struct task_struct *task, const struct proc_ns_operations *ns_ops)
  59. {
  60. struct dentry *dentry, *result;
  61. struct inode *inode;
  62. struct proc_inode *ei;
  63. struct qstr qname = { .name = "", };
  64. void *ns;
  65. ns = ns_ops->get(task);
  66. if (!ns)
  67. return ERR_PTR(-ENOENT);
  68. dentry = d_alloc_pseudo(sb, &qname);
  69. if (!dentry) {
  70. ns_ops->put(ns);
  71. return ERR_PTR(-ENOMEM);
  72. }
  73. inode = iget_locked(sb, ns_ops->inum(ns));
  74. if (!inode) {
  75. dput(dentry);
  76. ns_ops->put(ns);
  77. return ERR_PTR(-ENOMEM);
  78. }
  79. ei = PROC_I(inode);
  80. if (inode->i_state & I_NEW) {
  81. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  82. inode->i_op = &ns_inode_operations;
  83. inode->i_mode = S_IFREG | S_IRUGO;
  84. inode->i_fop = &ns_file_operations;
  85. ei->ns_ops = ns_ops;
  86. ei->ns = ns;
  87. unlock_new_inode(inode);
  88. } else {
  89. ns_ops->put(ns);
  90. }
  91. d_set_d_op(dentry, &ns_dentry_operations);
  92. result = d_instantiate_unique(dentry, inode);
  93. if (result) {
  94. dput(dentry);
  95. dentry = result;
  96. }
  97. return dentry;
  98. }
  99. static void *proc_ns_follow_link(struct dentry *dentry, struct nameidata *nd)
  100. {
  101. struct inode *inode = dentry->d_inode;
  102. struct super_block *sb = inode->i_sb;
  103. struct proc_inode *ei = PROC_I(inode);
  104. struct task_struct *task;
  105. struct path ns_path;
  106. void *error = ERR_PTR(-EACCES);
  107. task = get_proc_task(inode);
  108. if (!task)
  109. goto out;
  110. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  111. goto out_put_task;
  112. ns_path.dentry = proc_ns_get_dentry(sb, task, ei->ns_ops);
  113. if (IS_ERR(ns_path.dentry)) {
  114. error = ERR_CAST(ns_path.dentry);
  115. goto out_put_task;
  116. }
  117. ns_path.mnt = mntget(nd->path.mnt);
  118. nd_jump_link(nd, &ns_path);
  119. error = NULL;
  120. out_put_task:
  121. put_task_struct(task);
  122. out:
  123. return error;
  124. }
  125. static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  126. {
  127. struct inode *inode = dentry->d_inode;
  128. struct proc_inode *ei = PROC_I(inode);
  129. const struct proc_ns_operations *ns_ops = ei->ns_ops;
  130. struct task_struct *task;
  131. void *ns;
  132. char name[50];
  133. int len = -EACCES;
  134. task = get_proc_task(inode);
  135. if (!task)
  136. goto out;
  137. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  138. goto out_put_task;
  139. len = -ENOENT;
  140. ns = ns_ops->get(task);
  141. if (!ns)
  142. goto out_put_task;
  143. snprintf(name, sizeof(name), "%s:[%u]", ns_ops->name, ns_ops->inum(ns));
  144. len = strlen(name);
  145. if (len > buflen)
  146. len = buflen;
  147. if (copy_to_user(buffer, name, len))
  148. len = -EFAULT;
  149. ns_ops->put(ns);
  150. out_put_task:
  151. put_task_struct(task);
  152. out:
  153. return len;
  154. }
  155. static const struct inode_operations proc_ns_link_inode_operations = {
  156. .readlink = proc_ns_readlink,
  157. .follow_link = proc_ns_follow_link,
  158. .setattr = proc_setattr,
  159. };
  160. static struct dentry *proc_ns_instantiate(struct inode *dir,
  161. struct dentry *dentry, struct task_struct *task, const void *ptr)
  162. {
  163. const struct proc_ns_operations *ns_ops = ptr;
  164. struct inode *inode;
  165. struct proc_inode *ei;
  166. struct dentry *error = ERR_PTR(-ENOENT);
  167. inode = proc_pid_make_inode(dir->i_sb, task);
  168. if (!inode)
  169. goto out;
  170. ei = PROC_I(inode);
  171. inode->i_mode = S_IFLNK|S_IRWXUGO;
  172. inode->i_op = &proc_ns_link_inode_operations;
  173. ei->ns_ops = ns_ops;
  174. d_set_d_op(dentry, &pid_dentry_operations);
  175. d_add(dentry, inode);
  176. /* Close the race of the process dying before we return the dentry */
  177. if (pid_revalidate(dentry, 0))
  178. error = NULL;
  179. out:
  180. return error;
  181. }
  182. static int proc_ns_fill_cache(struct file *filp, void *dirent,
  183. filldir_t filldir, struct task_struct *task,
  184. const struct proc_ns_operations *ops)
  185. {
  186. return proc_fill_cache(filp, dirent, filldir,
  187. ops->name, strlen(ops->name),
  188. proc_ns_instantiate, task, ops);
  189. }
  190. static int proc_ns_dir_readdir(struct file *filp, void *dirent,
  191. filldir_t filldir)
  192. {
  193. int i;
  194. struct dentry *dentry = filp->f_path.dentry;
  195. struct inode *inode = dentry->d_inode;
  196. struct task_struct *task = get_proc_task(inode);
  197. const struct proc_ns_operations **entry, **last;
  198. ino_t ino;
  199. int ret;
  200. ret = -ENOENT;
  201. if (!task)
  202. goto out_no_task;
  203. ret = 0;
  204. i = filp->f_pos;
  205. switch (i) {
  206. case 0:
  207. ino = inode->i_ino;
  208. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  209. goto out;
  210. i++;
  211. filp->f_pos++;
  212. /* fall through */
  213. case 1:
  214. ino = parent_ino(dentry);
  215. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  216. goto out;
  217. i++;
  218. filp->f_pos++;
  219. /* fall through */
  220. default:
  221. i -= 2;
  222. if (i >= ARRAY_SIZE(ns_entries)) {
  223. ret = 1;
  224. goto out;
  225. }
  226. entry = ns_entries + i;
  227. last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
  228. while (entry <= last) {
  229. if (proc_ns_fill_cache(filp, dirent, filldir,
  230. task, *entry) < 0)
  231. goto out;
  232. filp->f_pos++;
  233. entry++;
  234. }
  235. }
  236. ret = 1;
  237. out:
  238. put_task_struct(task);
  239. out_no_task:
  240. return ret;
  241. }
  242. const struct file_operations proc_ns_dir_operations = {
  243. .read = generic_read_dir,
  244. .readdir = proc_ns_dir_readdir,
  245. };
  246. static struct dentry *proc_ns_dir_lookup(struct inode *dir,
  247. struct dentry *dentry, unsigned int flags)
  248. {
  249. struct dentry *error;
  250. struct task_struct *task = get_proc_task(dir);
  251. const struct proc_ns_operations **entry, **last;
  252. unsigned int len = dentry->d_name.len;
  253. error = ERR_PTR(-ENOENT);
  254. if (!task)
  255. goto out_no_task;
  256. last = &ns_entries[ARRAY_SIZE(ns_entries)];
  257. for (entry = ns_entries; entry < last; entry++) {
  258. if (strlen((*entry)->name) != len)
  259. continue;
  260. if (!memcmp(dentry->d_name.name, (*entry)->name, len))
  261. break;
  262. }
  263. if (entry == last)
  264. goto out;
  265. error = proc_ns_instantiate(dir, dentry, task, *entry);
  266. out:
  267. put_task_struct(task);
  268. out_no_task:
  269. return error;
  270. }
  271. const struct inode_operations proc_ns_dir_inode_operations = {
  272. .lookup = proc_ns_dir_lookup,
  273. .getattr = pid_getattr,
  274. .setattr = proc_setattr,
  275. };
  276. struct file *proc_ns_fget(int fd)
  277. {
  278. struct file *file;
  279. file = fget(fd);
  280. if (!file)
  281. return ERR_PTR(-EBADF);
  282. if (file->f_op != &ns_file_operations)
  283. goto out_invalid;
  284. return file;
  285. out_invalid:
  286. fput(file);
  287. return ERR_PTR(-EINVAL);
  288. }
  289. bool proc_ns_inode(struct inode *inode)
  290. {
  291. return inode->i_fop == &ns_file_operations;
  292. }