namespace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/ipc/namespace.c
  4. * Copyright (C) 2006 Pavel Emelyanov <[email protected]> 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/sched/task.h>
  18. #include "util.h"
  19. static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
  20. {
  21. return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
  22. }
  23. static void dec_ipc_namespaces(struct ucounts *ucounts)
  24. {
  25. dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
  26. }
  27. static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
  28. struct ipc_namespace *old_ns)
  29. {
  30. struct ipc_namespace *ns;
  31. struct ucounts *ucounts;
  32. int err;
  33. err = -ENOSPC;
  34. ucounts = inc_ipc_namespaces(user_ns);
  35. if (!ucounts)
  36. goto fail;
  37. err = -ENOMEM;
  38. ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
  39. if (ns == NULL)
  40. goto fail_dec;
  41. err = ns_alloc_inum(&ns->ns);
  42. if (err)
  43. goto fail_free;
  44. ns->ns.ops = &ipcns_operations;
  45. refcount_set(&ns->ns.count, 1);
  46. ns->user_ns = get_user_ns(user_ns);
  47. ns->ucounts = ucounts;
  48. err = mq_init_ns(ns);
  49. if (err)
  50. goto fail_put;
  51. err = -ENOMEM;
  52. if (!setup_mq_sysctls(ns))
  53. goto fail_put;
  54. if (!setup_ipc_sysctls(ns))
  55. goto fail_mq;
  56. err = msg_init_ns(ns);
  57. if (err)
  58. goto fail_put;
  59. sem_init_ns(ns);
  60. shm_init_ns(ns);
  61. return ns;
  62. fail_mq:
  63. retire_mq_sysctls(ns);
  64. fail_put:
  65. put_user_ns(ns->user_ns);
  66. ns_free_inum(&ns->ns);
  67. fail_free:
  68. kfree(ns);
  69. fail_dec:
  70. dec_ipc_namespaces(ucounts);
  71. fail:
  72. return ERR_PTR(err);
  73. }
  74. struct ipc_namespace *copy_ipcs(unsigned long flags,
  75. struct user_namespace *user_ns, struct ipc_namespace *ns)
  76. {
  77. if (!(flags & CLONE_NEWIPC))
  78. return get_ipc_ns(ns);
  79. return create_ipc_ns(user_ns, ns);
  80. }
  81. /*
  82. * free_ipcs - free all ipcs of one type
  83. * @ns: the namespace to remove the ipcs from
  84. * @ids: the table of ipcs to free
  85. * @free: the function called to free each individual ipc
  86. *
  87. * Called for each kind of ipc when an ipc_namespace exits.
  88. */
  89. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  90. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  91. {
  92. struct kern_ipc_perm *perm;
  93. int next_id;
  94. int total, in_use;
  95. down_write(&ids->rwsem);
  96. in_use = ids->in_use;
  97. for (total = 0, next_id = 0; total < in_use; next_id++) {
  98. perm = idr_find(&ids->ipcs_idr, next_id);
  99. if (perm == NULL)
  100. continue;
  101. rcu_read_lock();
  102. ipc_lock_object(perm);
  103. free(ns, perm);
  104. total++;
  105. }
  106. up_write(&ids->rwsem);
  107. }
  108. static void free_ipc_ns(struct ipc_namespace *ns)
  109. {
  110. /* mq_put_mnt() waits for a grace period as kern_unmount()
  111. * uses synchronize_rcu().
  112. */
  113. mq_put_mnt(ns);
  114. sem_exit_ns(ns);
  115. msg_exit_ns(ns);
  116. shm_exit_ns(ns);
  117. retire_mq_sysctls(ns);
  118. retire_ipc_sysctls(ns);
  119. dec_ipc_namespaces(ns->ucounts);
  120. put_user_ns(ns->user_ns);
  121. ns_free_inum(&ns->ns);
  122. kfree(ns);
  123. }
  124. static LLIST_HEAD(free_ipc_list);
  125. static void free_ipc(struct work_struct *unused)
  126. {
  127. struct llist_node *node = llist_del_all(&free_ipc_list);
  128. struct ipc_namespace *n, *t;
  129. llist_for_each_entry_safe(n, t, node, mnt_llist)
  130. free_ipc_ns(n);
  131. }
  132. /*
  133. * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount.
  134. */
  135. static DECLARE_WORK(free_ipc_work, free_ipc);
  136. /*
  137. * put_ipc_ns - drop a reference to an ipc namespace.
  138. * @ns: the namespace to put
  139. *
  140. * If this is the last task in the namespace exiting, and
  141. * it is dropping the refcount to 0, then it can race with
  142. * a task in another ipc namespace but in a mounts namespace
  143. * which has this ipcns's mqueuefs mounted, doing some action
  144. * with one of the mqueuefs files. That can raise the refcount.
  145. * So dropping the refcount, and raising the refcount when
  146. * accessing it through the VFS, are protected with mq_lock.
  147. *
  148. * (Clearly, a task raising the refcount on its own ipc_ns
  149. * needn't take mq_lock since it can't race with the last task
  150. * in the ipcns exiting).
  151. */
  152. void put_ipc_ns(struct ipc_namespace *ns)
  153. {
  154. if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) {
  155. mq_clear_sbinfo(ns);
  156. spin_unlock(&mq_lock);
  157. if (llist_add(&ns->mnt_llist, &free_ipc_list))
  158. schedule_work(&free_ipc_work);
  159. }
  160. }
  161. static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
  162. {
  163. return container_of(ns, struct ipc_namespace, ns);
  164. }
  165. static struct ns_common *ipcns_get(struct task_struct *task)
  166. {
  167. struct ipc_namespace *ns = NULL;
  168. struct nsproxy *nsproxy;
  169. task_lock(task);
  170. nsproxy = task->nsproxy;
  171. if (nsproxy)
  172. ns = get_ipc_ns(nsproxy->ipc_ns);
  173. task_unlock(task);
  174. return ns ? &ns->ns : NULL;
  175. }
  176. static void ipcns_put(struct ns_common *ns)
  177. {
  178. return put_ipc_ns(to_ipc_ns(ns));
  179. }
  180. static int ipcns_install(struct nsset *nsset, struct ns_common *new)
  181. {
  182. struct nsproxy *nsproxy = nsset->nsproxy;
  183. struct ipc_namespace *ns = to_ipc_ns(new);
  184. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  185. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  186. return -EPERM;
  187. put_ipc_ns(nsproxy->ipc_ns);
  188. nsproxy->ipc_ns = get_ipc_ns(ns);
  189. return 0;
  190. }
  191. static struct user_namespace *ipcns_owner(struct ns_common *ns)
  192. {
  193. return to_ipc_ns(ns)->user_ns;
  194. }
  195. const struct proc_ns_operations ipcns_operations = {
  196. .name = "ipc",
  197. .type = CLONE_NEWIPC,
  198. .get = ipcns_get,
  199. .put = ipcns_put,
  200. .install = ipcns_install,
  201. .owner = ipcns_owner,
  202. };