namespace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_fs.h>
  15. #include "util.h"
  16. static struct ipc_namespace *create_ipc_ns(struct task_struct *tsk,
  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(task_cred_xxx(tsk, user_ns));
  48. return ns;
  49. }
  50. struct ipc_namespace *copy_ipcs(unsigned long flags,
  51. struct task_struct *tsk)
  52. {
  53. struct ipc_namespace *ns = tsk->nsproxy->ipc_ns;
  54. if (!(flags & CLONE_NEWIPC))
  55. return get_ipc_ns(ns);
  56. return create_ipc_ns(tsk, ns);
  57. }
  58. /*
  59. * free_ipcs - free all ipcs of one type
  60. * @ns: the namespace to remove the ipcs from
  61. * @ids: the table of ipcs to free
  62. * @free: the function called to free each individual ipc
  63. *
  64. * Called for each kind of ipc when an ipc_namespace exits.
  65. */
  66. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  67. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  68. {
  69. struct kern_ipc_perm *perm;
  70. int next_id;
  71. int total, in_use;
  72. down_write(&ids->rw_mutex);
  73. in_use = ids->in_use;
  74. for (total = 0, next_id = 0; total < in_use; next_id++) {
  75. perm = idr_find(&ids->ipcs_idr, next_id);
  76. if (perm == NULL)
  77. continue;
  78. ipc_lock_by_ptr(perm);
  79. free(ns, perm);
  80. total++;
  81. }
  82. up_write(&ids->rw_mutex);
  83. }
  84. static void free_ipc_ns(struct ipc_namespace *ns)
  85. {
  86. /*
  87. * Unregistering the hotplug notifier at the beginning guarantees
  88. * that the ipc namespace won't be freed while we are inside the
  89. * callback routine. Since the blocking_notifier_chain_XXX routines
  90. * hold a rw lock on the notifier list, unregister_ipcns_notifier()
  91. * won't take the rw lock before blocking_notifier_call_chain() has
  92. * released the rd lock.
  93. */
  94. unregister_ipcns_notifier(ns);
  95. sem_exit_ns(ns);
  96. msg_exit_ns(ns);
  97. shm_exit_ns(ns);
  98. atomic_dec(&nr_ipc_ns);
  99. /*
  100. * Do the ipcns removal notification after decrementing nr_ipc_ns in
  101. * order to have a correct value when recomputing msgmni.
  102. */
  103. ipcns_notify(IPCNS_REMOVED);
  104. put_user_ns(ns->user_ns);
  105. proc_free_inum(ns->proc_inum);
  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 void *ipcns_get(struct task_struct *task)
  134. {
  135. struct ipc_namespace *ns = NULL;
  136. struct nsproxy *nsproxy;
  137. task_lock(task);
  138. nsproxy = task->nsproxy;
  139. if (nsproxy)
  140. ns = get_ipc_ns(nsproxy->ipc_ns);
  141. task_unlock(task);
  142. return ns;
  143. }
  144. static void ipcns_put(void *ns)
  145. {
  146. return put_ipc_ns(ns);
  147. }
  148. static int ipcns_install(struct nsproxy *nsproxy, void *new)
  149. {
  150. struct ipc_namespace *ns = new;
  151. if (!ns_capable(ns->user_ns, 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. };