namespace.c 4.6 KB

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