namespace.c 4.3 KB

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