namespace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 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. 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(user_ns);
  42. return ns;
  43. }
  44. struct ipc_namespace *copy_ipcs(unsigned long flags,
  45. struct user_namespace *user_ns, struct ipc_namespace *ns)
  46. {
  47. if (!(flags & CLONE_NEWIPC))
  48. return get_ipc_ns(ns);
  49. return create_ipc_ns(user_ns, 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. }
  125. static void *ipcns_get(struct task_struct *task)
  126. {
  127. struct ipc_namespace *ns = NULL;
  128. struct nsproxy *nsproxy;
  129. rcu_read_lock();
  130. nsproxy = task_nsproxy(task);
  131. if (nsproxy)
  132. ns = get_ipc_ns(nsproxy->ipc_ns);
  133. rcu_read_unlock();
  134. return ns;
  135. }
  136. static void ipcns_put(void *ns)
  137. {
  138. return put_ipc_ns(ns);
  139. }
  140. static int ipcns_install(struct nsproxy *nsproxy, void *new)
  141. {
  142. struct ipc_namespace *ns = new;
  143. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN))
  144. return -EPERM;
  145. /* Ditch state from the old ipc namespace */
  146. exit_sem(current);
  147. put_ipc_ns(nsproxy->ipc_ns);
  148. nsproxy->ipc_ns = get_ipc_ns(ns);
  149. return 0;
  150. }
  151. const struct proc_ns_operations ipcns_operations = {
  152. .name = "ipc",
  153. .type = CLONE_NEWIPC,
  154. .get = ipcns_get,
  155. .put = ipcns_put,
  156. .install = ipcns_install,
  157. };