namespace.c 4.5 KB

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