namespace.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/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);
  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->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. sem_init_ns(ns);
  52. msg_init_ns(ns);
  53. shm_init_ns(ns);
  54. return ns;
  55. fail_put:
  56. put_user_ns(ns->user_ns);
  57. ns_free_inum(&ns->ns);
  58. fail_free:
  59. kfree(ns);
  60. fail_dec:
  61. dec_ipc_namespaces(ucounts);
  62. fail:
  63. return ERR_PTR(err);
  64. }
  65. struct ipc_namespace *copy_ipcs(unsigned long flags,
  66. struct user_namespace *user_ns, struct ipc_namespace *ns)
  67. {
  68. if (!(flags & CLONE_NEWIPC))
  69. return get_ipc_ns(ns);
  70. return create_ipc_ns(user_ns, ns);
  71. }
  72. /*
  73. * free_ipcs - free all ipcs of one type
  74. * @ns: the namespace to remove the ipcs from
  75. * @ids: the table of ipcs to free
  76. * @free: the function called to free each individual ipc
  77. *
  78. * Called for each kind of ipc when an ipc_namespace exits.
  79. */
  80. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  81. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  82. {
  83. struct kern_ipc_perm *perm;
  84. int next_id;
  85. int total, in_use;
  86. down_write(&ids->rwsem);
  87. in_use = ids->in_use;
  88. for (total = 0, next_id = 0; total < in_use; next_id++) {
  89. perm = idr_find(&ids->ipcs_idr, next_id);
  90. if (perm == NULL)
  91. continue;
  92. rcu_read_lock();
  93. ipc_lock_object(perm);
  94. free(ns, perm);
  95. total++;
  96. }
  97. up_write(&ids->rwsem);
  98. }
  99. static void free_ipc_ns(struct ipc_namespace *ns)
  100. {
  101. /* mq_put_mnt() waits for a grace period as kern_unmount()
  102. * uses synchronize_rcu().
  103. */
  104. mq_put_mnt(ns);
  105. sem_exit_ns(ns);
  106. msg_exit_ns(ns);
  107. shm_exit_ns(ns);
  108. dec_ipc_namespaces(ns->ucounts);
  109. put_user_ns(ns->user_ns);
  110. ns_free_inum(&ns->ns);
  111. kfree(ns);
  112. }
  113. static LLIST_HEAD(free_ipc_list);
  114. static void free_ipc(struct work_struct *unused)
  115. {
  116. struct llist_node *node = llist_del_all(&free_ipc_list);
  117. struct ipc_namespace *n, *t;
  118. llist_for_each_entry_safe(n, t, node, mnt_llist)
  119. free_ipc_ns(n);
  120. }
  121. /*
  122. * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount.
  123. */
  124. static DECLARE_WORK(free_ipc_work, free_ipc);
  125. /*
  126. * put_ipc_ns - drop a reference to an ipc namespace.
  127. * @ns: the namespace to put
  128. *
  129. * If this is the last task in the namespace exiting, and
  130. * it is dropping the refcount to 0, then it can race with
  131. * a task in another ipc namespace but in a mounts namespace
  132. * which has this ipcns's mqueuefs mounted, doing some action
  133. * with one of the mqueuefs files. That can raise the refcount.
  134. * So dropping the refcount, and raising the refcount when
  135. * accessing it through the VFS, are protected with mq_lock.
  136. *
  137. * (Clearly, a task raising the refcount on its own ipc_ns
  138. * needn't take mq_lock since it can't race with the last task
  139. * in the ipcns exiting).
  140. */
  141. void put_ipc_ns(struct ipc_namespace *ns)
  142. {
  143. if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
  144. mq_clear_sbinfo(ns);
  145. spin_unlock(&mq_lock);
  146. if (llist_add(&ns->mnt_llist, &free_ipc_list))
  147. schedule_work(&free_ipc_work);
  148. }
  149. }
  150. static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
  151. {
  152. return container_of(ns, struct ipc_namespace, ns);
  153. }
  154. static struct ns_common *ipcns_get(struct task_struct *task)
  155. {
  156. struct ipc_namespace *ns = NULL;
  157. struct nsproxy *nsproxy;
  158. task_lock(task);
  159. nsproxy = task->nsproxy;
  160. if (nsproxy)
  161. ns = get_ipc_ns(nsproxy->ipc_ns);
  162. task_unlock(task);
  163. return ns ? &ns->ns : NULL;
  164. }
  165. static void ipcns_put(struct ns_common *ns)
  166. {
  167. return put_ipc_ns(to_ipc_ns(ns));
  168. }
  169. static int ipcns_install(struct nsset *nsset, struct ns_common *new)
  170. {
  171. struct nsproxy *nsproxy = nsset->nsproxy;
  172. struct ipc_namespace *ns = to_ipc_ns(new);
  173. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  174. !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
  175. return -EPERM;
  176. put_ipc_ns(nsproxy->ipc_ns);
  177. nsproxy->ipc_ns = get_ipc_ns(ns);
  178. return 0;
  179. }
  180. static struct user_namespace *ipcns_owner(struct ns_common *ns)
  181. {
  182. return to_ipc_ns(ns)->user_ns;
  183. }
  184. const struct proc_ns_operations ipcns_operations = {
  185. .name = "ipc",
  186. .type = CLONE_NEWIPC,
  187. .get = ipcns_get,
  188. .put = ipcns_put,
  189. .install = ipcns_install,
  190. .owner = ipcns_owner,
  191. };