namespace.c 3.9 KB

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