namespace.c 3.6 KB

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