namespace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * linux/ipc/namespace.c
  3. * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
  4. */
  5. #include <linux/ipc.h>
  6. #include <linux/msg.h>
  7. #include <linux/ipc_namespace.h>
  8. #include <linux/rcupdate.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/slab.h>
  11. #include <linux/cred.h>
  12. #include <linux/fs.h>
  13. #include <linux/mount.h>
  14. #include <linux/user_namespace.h>
  15. #include <linux/proc_ns.h>
  16. #include <linux/sched/task.h>
  17. #include "util.h"
  18. static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns)
  19. {
  20. return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES);
  21. }
  22. static void dec_ipc_namespaces(struct ucounts *ucounts)
  23. {
  24. dec_ucount(ucounts, UCOUNT_IPC_NAMESPACES);
  25. }
  26. static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
  27. struct ipc_namespace *old_ns)
  28. {
  29. struct ipc_namespace *ns;
  30. struct ucounts *ucounts;
  31. int err;
  32. err = -ENOSPC;
  33. ucounts = inc_ipc_namespaces(user_ns);
  34. if (!ucounts)
  35. goto fail;
  36. err = -ENOMEM;
  37. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  38. if (ns == NULL)
  39. goto fail_dec;
  40. err = ns_alloc_inum(&ns->ns);
  41. if (err)
  42. goto fail_free;
  43. ns->ns.ops = &ipcns_operations;
  44. refcount_set(&ns->count, 1);
  45. ns->user_ns = get_user_ns(user_ns);
  46. ns->ucounts = ucounts;
  47. err = sem_init_ns(ns);
  48. if (err)
  49. goto fail_put;
  50. err = msg_init_ns(ns);
  51. if (err)
  52. goto fail_destroy_sem;
  53. err = shm_init_ns(ns);
  54. if (err)
  55. goto fail_destroy_msg;
  56. err = mq_init_ns(ns);
  57. if (err)
  58. goto fail_destroy_shm;
  59. return ns;
  60. fail_destroy_shm:
  61. shm_exit_ns(ns);
  62. fail_destroy_msg:
  63. msg_exit_ns(ns);
  64. fail_destroy_sem:
  65. sem_exit_ns(ns);
  66. fail_put:
  67. put_user_ns(ns->user_ns);
  68. ns_free_inum(&ns->ns);
  69. fail_free:
  70. kfree(ns);
  71. fail_dec:
  72. dec_ipc_namespaces(ucounts);
  73. fail:
  74. return ERR_PTR(err);
  75. }
  76. struct ipc_namespace *copy_ipcs(unsigned long flags,
  77. struct user_namespace *user_ns, struct ipc_namespace *ns)
  78. {
  79. if (!(flags & CLONE_NEWIPC))
  80. return get_ipc_ns(ns);
  81. return create_ipc_ns(user_ns, ns);
  82. }
  83. /*
  84. * free_ipcs - free all ipcs of one type
  85. * @ns: the namespace to remove the ipcs from
  86. * @ids: the table of ipcs to free
  87. * @free: the function called to free each individual ipc
  88. *
  89. * Called for each kind of ipc when an ipc_namespace exits.
  90. */
  91. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  92. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  93. {
  94. struct kern_ipc_perm *perm;
  95. int next_id;
  96. int total, in_use;
  97. down_write(&ids->rwsem);
  98. in_use = ids->in_use;
  99. for (total = 0, next_id = 0; total < in_use; next_id++) {
  100. perm = idr_find(&ids->ipcs_idr, next_id);
  101. if (perm == NULL)
  102. continue;
  103. rcu_read_lock();
  104. ipc_lock_object(perm);
  105. free(ns, perm);
  106. total++;
  107. }
  108. up_write(&ids->rwsem);
  109. }
  110. static void free_ipc_ns(struct ipc_namespace *ns)
  111. {
  112. sem_exit_ns(ns);
  113. msg_exit_ns(ns);
  114. shm_exit_ns(ns);
  115. dec_ipc_namespaces(ns->ucounts);
  116. put_user_ns(ns->user_ns);
  117. ns_free_inum(&ns->ns);
  118. kfree(ns);
  119. }
  120. /*
  121. * put_ipc_ns - drop a reference to an ipc namespace.
  122. * @ns: the namespace to put
  123. *
  124. * If this is the last task in the namespace exiting, and
  125. * it is dropping the refcount to 0, then it can race with
  126. * a task in another ipc namespace but in a mounts namespace
  127. * which has this ipcns's mqueuefs mounted, doing some action
  128. * with one of the mqueuefs files. That can raise the refcount.
  129. * So dropping the refcount, and raising the refcount when
  130. * accessing it through the VFS, are protected with mq_lock.
  131. *
  132. * (Clearly, a task raising the refcount on its own ipc_ns
  133. * needn't take mq_lock since it can't race with the last task
  134. * in the ipcns exiting).
  135. */
  136. void put_ipc_ns(struct ipc_namespace *ns)
  137. {
  138. if (refcount_dec_and_lock(&ns->count, &mq_lock)) {
  139. mq_clear_sbinfo(ns);
  140. spin_unlock(&mq_lock);
  141. mq_put_mnt(ns);
  142. free_ipc_ns(ns);
  143. }
  144. }
  145. static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
  146. {
  147. return container_of(ns, struct ipc_namespace, ns);
  148. }
  149. static struct ns_common *ipcns_get(struct task_struct *task)
  150. {
  151. struct ipc_namespace *ns = NULL;
  152. struct nsproxy *nsproxy;
  153. task_lock(task);
  154. nsproxy = task->nsproxy;
  155. if (nsproxy)
  156. ns = get_ipc_ns(nsproxy->ipc_ns);
  157. task_unlock(task);
  158. return ns ? &ns->ns : NULL;
  159. }
  160. static void ipcns_put(struct ns_common *ns)
  161. {
  162. return put_ipc_ns(to_ipc_ns(ns));
  163. }
  164. static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
  165. {
  166. struct ipc_namespace *ns = to_ipc_ns(new);
  167. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  168. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  169. return -EPERM;
  170. /* Ditch state from the old ipc namespace */
  171. exit_sem(current);
  172. put_ipc_ns(nsproxy->ipc_ns);
  173. nsproxy->ipc_ns = get_ipc_ns(ns);
  174. return 0;
  175. }
  176. static struct user_namespace *ipcns_owner(struct ns_common *ns)
  177. {
  178. return to_ipc_ns(ns)->user_ns;
  179. }
  180. const struct proc_ns_operations ipcns_operations = {
  181. .name = "ipc",
  182. .type = CLONE_NEWIPC,
  183. .get = ipcns_get,
  184. .put = ipcns_put,
  185. .install = ipcns_install,
  186. .owner = ipcns_owner,
  187. };