namespace.c 4.8 KB

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