namespace.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/ipc/namespace.c
  4. * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
  5. */
  6. #include <linux/ipc.h>
  7. #include <linux/msg.h>
  8. #include <linux/ipc_namespace.h>
  9. #include <linux/rcupdate.h>
  10. #include <linux/nsproxy.h>
  11. #include <linux/slab.h>
  12. #include <linux/cred.h>
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/user_namespace.h>
  16. #include <linux/proc_ns.h>
  17. #include <linux/nstree.h>
  18. #include <linux/sched/task.h>
  19. #include "util.h"
  20. /*
  21. * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount.
  22. */
  23. static void free_ipc(struct work_struct *unused);
  24. static DECLARE_WORK(free_ipc_work, free_ipc);
  25. static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
  26. {
  27. return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
  28. }
  29. static void dec_ipc_namespaces(struct ucounts *ucounts)
  30. {
  31. dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
  32. }
  33. static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
  34. struct ipc_namespace *old_ns)
  35. {
  36. struct ipc_namespace *ns;
  37. struct ucounts *ucounts;
  38. int err;
  39. err = -ENOSPC;
  40. again:
  41. ucounts = inc_ipc_namespaces(user_ns);
  42. if (!ucounts) {
  43. /*
  44. * IPC namespaces are freed asynchronously, by free_ipc_work.
  45. * If frees were pending, flush_work will wait, and
  46. * return true. Fail the allocation if no frees are pending.
  47. */
  48. if (flush_work(&free_ipc_work))
  49. goto again;
  50. goto fail;
  51. }
  52. err = -ENOMEM;
  53. ns = kzalloc_obj(struct ipc_namespace, GFP_KERNEL_ACCOUNT);
  54. if (ns == NULL)
  55. goto fail_dec;
  56. err = ns_common_init(ns);
  57. if (err)
  58. goto fail_free;
  59. ns_tree_gen_id(ns);
  60. ns->user_ns = get_user_ns(user_ns);
  61. ns->ucounts = ucounts;
  62. err = mq_init_ns(ns);
  63. if (err)
  64. goto fail_put;
  65. err = -ENOMEM;
  66. if (!setup_mq_sysctls(ns))
  67. goto fail_mq_mount;
  68. if (!setup_ipc_sysctls(ns))
  69. goto fail_mq_sysctls;
  70. err = msg_init_ns(ns);
  71. if (err)
  72. goto fail_ipc;
  73. sem_init_ns(ns);
  74. shm_init_ns(ns);
  75. ns_tree_add_raw(ns);
  76. return ns;
  77. fail_ipc:
  78. retire_ipc_sysctls(ns);
  79. fail_mq_sysctls:
  80. retire_mq_sysctls(ns);
  81. fail_mq_mount:
  82. mntput(ns->mq_mnt);
  83. fail_put:
  84. put_user_ns(ns->user_ns);
  85. ns_common_free(ns);
  86. fail_free:
  87. kfree(ns);
  88. fail_dec:
  89. dec_ipc_namespaces(ucounts);
  90. fail:
  91. return ERR_PTR(err);
  92. }
  93. struct ipc_namespace *copy_ipcs(u64 flags,
  94. struct user_namespace *user_ns, struct ipc_namespace *ns)
  95. {
  96. if (!(flags & CLONE_NEWIPC))
  97. return get_ipc_ns(ns);
  98. return create_ipc_ns(user_ns, ns);
  99. }
  100. /*
  101. * free_ipcs - free all ipcs of one type
  102. * @ns: the namespace to remove the ipcs from
  103. * @ids: the table of ipcs to free
  104. * @free: the function called to free each individual ipc
  105. *
  106. * Called for each kind of ipc when an ipc_namespace exits.
  107. */
  108. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  109. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  110. {
  111. struct kern_ipc_perm *perm;
  112. int next_id;
  113. int total, in_use;
  114. down_write(&ids->rwsem);
  115. in_use = ids->in_use;
  116. for (total = 0, next_id = 0; total < in_use; next_id++) {
  117. perm = idr_find(&ids->ipcs_idr, next_id);
  118. if (perm == NULL)
  119. continue;
  120. rcu_read_lock();
  121. ipc_lock_object(perm);
  122. free(ns, perm);
  123. total++;
  124. }
  125. up_write(&ids->rwsem);
  126. }
  127. static void free_ipc_ns(struct ipc_namespace *ns)
  128. {
  129. /*
  130. * Caller needs to wait for an RCU grace period to have passed
  131. * after making the mount point inaccessible to new accesses.
  132. */
  133. mntput(ns->mq_mnt);
  134. sem_exit_ns(ns);
  135. msg_exit_ns(ns);
  136. shm_exit_ns(ns);
  137. retire_mq_sysctls(ns);
  138. retire_ipc_sysctls(ns);
  139. dec_ipc_namespaces(ns->ucounts);
  140. put_user_ns(ns->user_ns);
  141. ns_common_free(ns);
  142. kfree(ns);
  143. }
  144. static LLIST_HEAD(free_ipc_list);
  145. static void free_ipc(struct work_struct *unused)
  146. {
  147. struct llist_node *node = llist_del_all(&free_ipc_list);
  148. struct ipc_namespace *n, *t;
  149. llist_for_each_entry_safe(n, t, node, mnt_llist)
  150. mnt_make_shortterm(n->mq_mnt);
  151. /* Wait for any last users to have gone away. */
  152. synchronize_rcu();
  153. llist_for_each_entry_safe(n, t, node, mnt_llist)
  154. free_ipc_ns(n);
  155. }
  156. /*
  157. * put_ipc_ns - drop a reference to an ipc namespace.
  158. * @ns: the namespace to put
  159. *
  160. * If this is the last task in the namespace exiting, and
  161. * it is dropping the refcount to 0, then it can race with
  162. * a task in another ipc namespace but in a mounts namespace
  163. * which has this ipcns's mqueuefs mounted, doing some action
  164. * with one of the mqueuefs files. That can raise the refcount.
  165. * So dropping the refcount, and raising the refcount when
  166. * accessing it through the VFS, are protected with mq_lock.
  167. *
  168. * (Clearly, a task raising the refcount on its own ipc_ns
  169. * needn't take mq_lock since it can't race with the last task
  170. * in the ipcns exiting).
  171. */
  172. void put_ipc_ns(struct ipc_namespace *ns)
  173. {
  174. if (ns_ref_put_and_lock(ns, &mq_lock)) {
  175. mq_clear_sbinfo(ns);
  176. spin_unlock(&mq_lock);
  177. ns_tree_remove(ns);
  178. if (llist_add(&ns->mnt_llist, &free_ipc_list))
  179. schedule_work(&free_ipc_work);
  180. }
  181. }
  182. static struct ns_common *ipcns_get(struct task_struct *task)
  183. {
  184. struct ipc_namespace *ns = NULL;
  185. struct nsproxy *nsproxy;
  186. task_lock(task);
  187. nsproxy = task->nsproxy;
  188. if (nsproxy)
  189. ns = get_ipc_ns(nsproxy->ipc_ns);
  190. task_unlock(task);
  191. return ns ? &ns->ns : NULL;
  192. }
  193. static void ipcns_put(struct ns_common *ns)
  194. {
  195. return put_ipc_ns(to_ipc_ns(ns));
  196. }
  197. static int ipcns_install(struct nsset *nsset, struct ns_common *new)
  198. {
  199. struct nsproxy *nsproxy = nsset->nsproxy;
  200. struct ipc_namespace *ns = to_ipc_ns(new);
  201. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  202. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  203. return -EPERM;
  204. put_ipc_ns(nsproxy->ipc_ns);
  205. nsproxy->ipc_ns = get_ipc_ns(ns);
  206. return 0;
  207. }
  208. static struct user_namespace *ipcns_owner(struct ns_common *ns)
  209. {
  210. return to_ipc_ns(ns)->user_ns;
  211. }
  212. const struct proc_ns_operations ipcns_operations = {
  213. .name = "ipc",
  214. .get = ipcns_get,
  215. .put = ipcns_put,
  216. .install = ipcns_install,
  217. .owner = ipcns_owner,
  218. };