namespace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 = 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. sem_exit_ns(ns);
  102. msg_exit_ns(ns);
  103. shm_exit_ns(ns);
  104. dec_ipc_namespaces(ns->ucounts);
  105. put_user_ns(ns->user_ns);
  106. ns_free_inum(&ns->ns);
  107. kfree(ns);
  108. }
  109. /*
  110. * put_ipc_ns - drop a reference to an ipc namespace.
  111. * @ns: the namespace to put
  112. *
  113. * If this is the last task in the namespace exiting, and
  114. * it is dropping the refcount to 0, then it can race with
  115. * a task in another ipc namespace but in a mounts namespace
  116. * which has this ipcns's mqueuefs mounted, doing some action
  117. * with one of the mqueuefs files. That can raise the refcount.
  118. * So dropping the refcount, and raising the refcount when
  119. * accessing it through the VFS, are protected with mq_lock.
  120. *
  121. * (Clearly, a task raising the refcount on its own ipc_ns
  122. * needn't take mq_lock since it can't race with the last task
  123. * in the ipcns exiting).
  124. */
  125. void put_ipc_ns(struct ipc_namespace *ns)
  126. {
  127. if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
  128. mq_clear_sbinfo(ns);
  129. spin_unlock(&mq_lock);
  130. mq_put_mnt(ns);
  131. free_ipc_ns(ns);
  132. }
  133. }
  134. static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
  135. {
  136. return container_of(ns, struct ipc_namespace, ns);
  137. }
  138. static struct ns_common *ipcns_get(struct task_struct *task)
  139. {
  140. struct ipc_namespace *ns = NULL;
  141. struct nsproxy *nsproxy;
  142. task_lock(task);
  143. nsproxy = task->nsproxy;
  144. if (nsproxy)
  145. ns = get_ipc_ns(nsproxy->ipc_ns);
  146. task_unlock(task);
  147. return ns ? &ns->ns : NULL;
  148. }
  149. static void ipcns_put(struct ns_common *ns)
  150. {
  151. return put_ipc_ns(to_ipc_ns(ns));
  152. }
  153. static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
  154. {
  155. struct ipc_namespace *ns = to_ipc_ns(new);
  156. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  157. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  158. return -EPERM;
  159. /* Ditch state from the old ipc namespace */
  160. exit_sem(current);
  161. put_ipc_ns(nsproxy->ipc_ns);
  162. nsproxy->ipc_ns = get_ipc_ns(ns);
  163. return 0;
  164. }
  165. static struct user_namespace *ipcns_owner(struct ns_common *ns)
  166. {
  167. return to_ipc_ns(ns)->user_ns;
  168. }
  169. const struct proc_ns_operations ipcns_operations = {
  170. .name = "ipc",
  171. .type = CLONE_NEWIPC,
  172. .get = ipcns_get,
  173. .put = ipcns_put,
  174. .install = ipcns_install,
  175. .owner = ipcns_owner,
  176. };