namespace.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = ns_alloc_inum(&ns->ns);
  25. if (err) {
  26. kfree(ns);
  27. return ERR_PTR(err);
  28. }
  29. ns->ns.ops = &ipcns_operations;
  30. atomic_set(&ns->count, 1);
  31. err = mq_init_ns(ns);
  32. if (err) {
  33. ns_free_inum(&ns->ns);
  34. kfree(ns);
  35. return ERR_PTR(err);
  36. }
  37. atomic_inc(&nr_ipc_ns);
  38. sem_init_ns(ns);
  39. msg_init_ns(ns);
  40. shm_init_ns(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->rwsem);
  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. rcu_read_lock();
  72. ipc_lock_object(perm);
  73. free(ns, perm);
  74. total++;
  75. }
  76. up_write(&ids->rwsem);
  77. }
  78. static void free_ipc_ns(struct ipc_namespace *ns)
  79. {
  80. sem_exit_ns(ns);
  81. msg_exit_ns(ns);
  82. shm_exit_ns(ns);
  83. atomic_dec(&nr_ipc_ns);
  84. put_user_ns(ns->user_ns);
  85. ns_free_inum(&ns->ns);
  86. kfree(ns);
  87. }
  88. /*
  89. * put_ipc_ns - drop a reference to an ipc namespace.
  90. * @ns: the namespace to put
  91. *
  92. * If this is the last task in the namespace exiting, and
  93. * it is dropping the refcount to 0, then it can race with
  94. * a task in another ipc namespace but in a mounts namespace
  95. * which has this ipcns's mqueuefs mounted, doing some action
  96. * with one of the mqueuefs files. That can raise the refcount.
  97. * So dropping the refcount, and raising the refcount when
  98. * accessing it through the VFS, are protected with mq_lock.
  99. *
  100. * (Clearly, a task raising the refcount on its own ipc_ns
  101. * needn't take mq_lock since it can't race with the last task
  102. * in the ipcns exiting).
  103. */
  104. void put_ipc_ns(struct ipc_namespace *ns)
  105. {
  106. if (atomic_dec_and_lock(&ns->count, &mq_lock)) {
  107. mq_clear_sbinfo(ns);
  108. spin_unlock(&mq_lock);
  109. mq_put_mnt(ns);
  110. free_ipc_ns(ns);
  111. }
  112. }
  113. static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
  114. {
  115. return container_of(ns, struct ipc_namespace, ns);
  116. }
  117. static struct ns_common *ipcns_get(struct task_struct *task)
  118. {
  119. struct ipc_namespace *ns = NULL;
  120. struct nsproxy *nsproxy;
  121. task_lock(task);
  122. nsproxy = task->nsproxy;
  123. if (nsproxy)
  124. ns = get_ipc_ns(nsproxy->ipc_ns);
  125. task_unlock(task);
  126. return ns ? &ns->ns : NULL;
  127. }
  128. static void ipcns_put(struct ns_common *ns)
  129. {
  130. return put_ipc_ns(to_ipc_ns(ns));
  131. }
  132. static int ipcns_install(struct nsproxy *nsproxy, struct ns_common *new)
  133. {
  134. struct ipc_namespace *ns = to_ipc_ns(new);
  135. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  136. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  137. return -EPERM;
  138. /* Ditch state from the old ipc namespace */
  139. exit_sem(current);
  140. put_ipc_ns(nsproxy->ipc_ns);
  141. nsproxy->ipc_ns = get_ipc_ns(ns);
  142. return 0;
  143. }
  144. const struct proc_ns_operations ipcns_operations = {
  145. .name = "ipc",
  146. .type = CLONE_NEWIPC,
  147. .get = ipcns_get,
  148. .put = ipcns_put,
  149. .install = ipcns_install,
  150. };