ns_cgroup.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * ns_cgroup.c - namespace cgroup subsystem
  3. *
  4. * Copyright 2006, 2007 IBM Corp
  5. */
  6. #include <linux/module.h>
  7. #include <linux/cgroup.h>
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/nsproxy.h>
  11. struct ns_cgroup {
  12. struct cgroup_subsys_state css;
  13. spinlock_t lock;
  14. };
  15. struct cgroup_subsys ns_subsys;
  16. static inline struct ns_cgroup *cgroup_to_ns(
  17. struct cgroup *cgroup)
  18. {
  19. return container_of(cgroup_subsys_state(cgroup, ns_subsys_id),
  20. struct ns_cgroup, css);
  21. }
  22. int ns_cgroup_clone(struct task_struct *task)
  23. {
  24. return cgroup_clone(task, &ns_subsys);
  25. }
  26. /*
  27. * Rules:
  28. * 1. you can only enter a cgroup which is a child of your current
  29. * cgroup
  30. * 2. you can only place another process into a cgroup if
  31. * a. you have CAP_SYS_ADMIN
  32. * b. your cgroup is an ancestor of task's destination cgroup
  33. * (hence either you are in the same cgroup as task, or in an
  34. * ancestor cgroup thereof)
  35. */
  36. static int ns_can_attach(struct cgroup_subsys *ss,
  37. struct cgroup *new_cgroup, struct task_struct *task)
  38. {
  39. struct cgroup *orig;
  40. if (current != task) {
  41. if (!capable(CAP_SYS_ADMIN))
  42. return -EPERM;
  43. if (!cgroup_is_descendant(new_cgroup))
  44. return -EPERM;
  45. }
  46. if (atomic_read(&new_cgroup->count) != 0)
  47. return -EPERM;
  48. orig = task_cgroup(task, ns_subsys_id);
  49. if (orig && orig != new_cgroup->parent)
  50. return -EPERM;
  51. return 0;
  52. }
  53. /*
  54. * Rules: you can only create a cgroup if
  55. * 1. you are capable(CAP_SYS_ADMIN)
  56. * 2. the target cgroup is a descendant of your own cgroup
  57. */
  58. static struct cgroup_subsys_state *ns_create(struct cgroup_subsys *ss,
  59. struct cgroup *cgroup)
  60. {
  61. struct ns_cgroup *ns_cgroup;
  62. if (!capable(CAP_SYS_ADMIN))
  63. return ERR_PTR(-EPERM);
  64. if (!cgroup_is_descendant(cgroup))
  65. return ERR_PTR(-EPERM);
  66. ns_cgroup = kzalloc(sizeof(*ns_cgroup), GFP_KERNEL);
  67. if (!ns_cgroup)
  68. return ERR_PTR(-ENOMEM);
  69. spin_lock_init(&ns_cgroup->lock);
  70. return &ns_cgroup->css;
  71. }
  72. static void ns_destroy(struct cgroup_subsys *ss,
  73. struct cgroup *cgroup)
  74. {
  75. struct ns_cgroup *ns_cgroup;
  76. ns_cgroup = cgroup_to_ns(cgroup);
  77. kfree(ns_cgroup);
  78. }
  79. struct cgroup_subsys ns_subsys = {
  80. .name = "ns",
  81. .can_attach = ns_can_attach,
  82. .create = ns_create,
  83. .destroy = ns_destroy,
  84. .subsys_id = ns_subsys_id,
  85. };