namespace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
  17. struct ipc_namespace *old_ns)
  18. {
  19. struct ipc_namespace *ns;
  20. int err;
  21. ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
  22. if (ns == NULL)
  23. return ERR_PTR(-ENOMEM);
  24. err = proc_alloc_inum(&ns->proc_inum);
  25. if (err) {
  26. kfree(ns);
  27. return ERR_PTR(err);
  28. }
  29. atomic_set(&ns->count, 1);
  30. err = mq_init_ns(ns);
  31. if (err) {
  32. proc_free_inum(ns->proc_inum);
  33. kfree(ns);
  34. return ERR_PTR(err);
  35. }
  36. atomic_inc(&nr_ipc_ns);
  37. sem_init_ns(ns);
  38. msg_init_ns(ns);
  39. shm_init_ns(ns);
  40. /*
  41. * msgmni has already been computed for the new ipc ns.
  42. * Thus, do the ipcns creation notification before registering that
  43. * new ipcns in the chain.
  44. */
  45. ipcns_notify(IPCNS_CREATED);
  46. register_ipcns_notifier(ns);
  47. ns->user_ns = get_user_ns(user_ns);
  48. return ns;
  49. }
  50. struct ipc_namespace *copy_ipcs(unsigned long flags,
  51. struct user_namespace *user_ns, struct ipc_namespace *ns)
  52. {
  53. if (!(flags & CLONE_NEWIPC))
  54. return get_ipc_ns(ns);
  55. return create_ipc_ns(user_ns, ns);
  56. }
  57. /*
  58. * free_ipcs - free all ipcs of one type
  59. * @ns: the namespace to remove the ipcs from
  60. * @ids: the table of ipcs to free
  61. * @free: the function called to free each individual ipc
  62. *
  63. * Called for each kind of ipc when an ipc_namespace exits.
  64. */
  65. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  66. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  67. {
  68. struct kern_ipc_perm *perm;
  69. int next_id;
  70. int total, in_use;
  71. down_write(&ids->rw_mutex);
  72. in_use = ids->in_use;
  73. for (total = 0, next_id = 0; total < in_use; next_id++) {
  74. perm = idr_find(&ids->ipcs_idr, next_id);
  75. if (perm == NULL)
  76. continue;
  77. ipc_lock_by_ptr(perm);
  78. free(ns, perm);
  79. total++;
  80. }
  81. up_write(&ids->rw_mutex);
  82. }
  83. static void free_ipc_ns(struct ipc_namespace *ns)
  84. {
  85. /*
  86. * Unregistering the hotplug notifier at the beginning guarantees
  87. * that the ipc namespace won't be freed while we are inside the
  88. * callback routine. Since the blocking_notifier_chain_XXX routines
  89. * hold a rw lock on the notifier list, unregister_ipcns_notifier()
  90. * won't take the rw lock before blocking_notifier_call_chain() has
  91. * released the rd lock.
  92. */
  93. unregister_ipcns_notifier(ns);
  94. sem_exit_ns(ns);
  95. msg_exit_ns(ns);
  96. shm_exit_ns(ns);
  97. atomic_dec(&nr_ipc_ns);
  98. /*
  99. * Do the ipcns removal notification after decrementing nr_ipc_ns in
  100. * order to have a correct value when recomputing msgmni.
  101. */
  102. ipcns_notify(IPCNS_REMOVED);
  103. put_user_ns(ns->user_ns);
  104. proc_free_inum(ns->proc_inum);
  105. kfree(ns);
  106. }
  107. /*
  108. * put_ipc_ns - drop a reference to an ipc namespace.
  109. * @ns: the namespace to put
  110. *
  111. * If this is the last task in the namespace exiting, and
  112. * it is dropping the refcount to 0, then it can race with
  113. * a task in another ipc namespace but in a mounts namespace
  114. * which has this ipcns's mqueuefs mounted, doing some action
  115. * with one of the mqueuefs files. That can raise the refcount.
  116. * So dropping the refcount, and raising the refcount when
  117. * accessing it through the VFS, are protected with mq_lock.
  118. *
  119. * (Clearly, a task raising the refcount on its own ipc_ns
  120. * needn't take mq_lock since it can't race with the last task
  121. * in the ipcns exiting).
  122. */
  123. void put_ipc_ns(struct ipc_namespace *ns)
  124. {
  125. if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
  126. mq_clear_sbinfo(ns);
  127. spin_unlock(&mq_lock);
  128. mq_put_mnt(ns);
  129. free_ipc_ns(ns);
  130. }
  131. }
  132. static void *ipcns_get(struct task_struct *task)
  133. {
  134. struct ipc_namespace *ns = NULL;
  135. struct nsproxy *nsproxy;
  136. rcu_read_lock();
  137. nsproxy = task_nsproxy(task);
  138. if (nsproxy)
  139. ns = get_ipc_ns(nsproxy->ipc_ns);
  140. rcu_read_unlock();
  141. return ns;
  142. }
  143. static void ipcns_put(void *ns)
  144. {
  145. return put_ipc_ns(ns);
  146. }
  147. static int ipcns_install(struct nsproxy *nsproxy, void *new)
  148. {
  149. struct ipc_namespace *ns = new;
  150. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  151. !nsown_capable(CAP_SYS_ADMIN))
  152. return -EPERM;
  153. /* Ditch state from the old ipc namespace */
  154. exit_sem(current);
  155. put_ipc_ns(nsproxy->ipc_ns);
  156. nsproxy->ipc_ns = get_ipc_ns(ns);
  157. return 0;
  158. }
  159. static unsigned int ipcns_inum(void *vp)
  160. {
  161. struct ipc_namespace *ns = vp;
  162. return ns->proc_inum;
  163. }
  164. const struct proc_ns_operations ipcns_operations = {
  165. .name = "ipc",
  166. .type = CLONE_NEWIPC,
  167. .get = ipcns_get,
  168. .put = ipcns_put,
  169. .install = ipcns_install,
  170. .inum = ipcns_inum,
  171. };