namespace.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 = proc_alloc_inum(&ns->proc_inum);
  25. if (err) {
  26. kfree(ns);
  27. return ERR_PTR(err);
  28. }
  29. atomic_set(&ns->count, 1);
  30. err = mq_init_ns(ns);
  31. if (err) {
  32. proc_free_inum(ns->proc_inum);
  33. kfree(ns);
  34. return ERR_PTR(err);
  35. }
  36. atomic_inc(&nr_ipc_ns);
  37. sem_init_ns(ns);
  38. msg_init_ns(ns);
  39. shm_init_ns(ns);
  40. ns->user_ns = get_user_ns(user_ns);
  41. return ns;
  42. }
  43. struct ipc_namespace *copy_ipcs(unsigned long flags,
  44. struct user_namespace *user_ns, struct ipc_namespace *ns)
  45. {
  46. if (!(flags & CLONE_NEWIPC))
  47. return get_ipc_ns(ns);
  48. return create_ipc_ns(user_ns, ns);
  49. }
  50. /*
  51. * free_ipcs - free all ipcs of one type
  52. * @ns: the namespace to remove the ipcs from
  53. * @ids: the table of ipcs to free
  54. * @free: the function called to free each individual ipc
  55. *
  56. * Called for each kind of ipc when an ipc_namespace exits.
  57. */
  58. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  59. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
  60. {
  61. struct kern_ipc_perm *perm;
  62. int next_id;
  63. int total, in_use;
  64. down_write(&ids->rwsem);
  65. in_use = ids->in_use;
  66. for (total = 0, next_id = 0; total < in_use; next_id++) {
  67. perm = idr_find(&ids->ipcs_idr, next_id);
  68. if (perm == NULL)
  69. continue;
  70. rcu_read_lock();
  71. ipc_lock_object(perm);
  72. free(ns, perm);
  73. total++;
  74. }
  75. up_write(&ids->rwsem);
  76. }
  77. static void free_ipc_ns(struct ipc_namespace *ns)
  78. {
  79. sem_exit_ns(ns);
  80. msg_exit_ns(ns);
  81. shm_exit_ns(ns);
  82. atomic_dec(&nr_ipc_ns);
  83. put_user_ns(ns->user_ns);
  84. proc_free_inum(ns->proc_inum);
  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 void *ipcns_get(struct task_struct *task)
  113. {
  114. struct ipc_namespace *ns = NULL;
  115. struct nsproxy *nsproxy;
  116. task_lock(task);
  117. nsproxy = task->nsproxy;
  118. if (nsproxy)
  119. ns = get_ipc_ns(nsproxy->ipc_ns);
  120. task_unlock(task);
  121. return ns;
  122. }
  123. static void ipcns_put(void *ns)
  124. {
  125. return put_ipc_ns(ns);
  126. }
  127. static int ipcns_install(struct nsproxy *nsproxy, void *new)
  128. {
  129. struct ipc_namespace *ns = new;
  130. if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN) ||
  131. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  132. return -EPERM;
  133. /* Ditch state from the old ipc namespace */
  134. exit_sem(current);
  135. put_ipc_ns(nsproxy->ipc_ns);
  136. nsproxy->ipc_ns = get_ipc_ns(ns);
  137. return 0;
  138. }
  139. static unsigned int ipcns_inum(void *vp)
  140. {
  141. struct ipc_namespace *ns = vp;
  142. return ns->proc_inum;
  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. .inum = ipcns_inum,
  151. };