namespaces.c 7.1 KB

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