test_cpuset.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/limits.h>
  3. #include <signal.h>
  4. #include "kselftest.h"
  5. #include "cgroup_util.h"
  6. static int idle_process_fn(const char *cgroup, void *arg)
  7. {
  8. (void)pause();
  9. return 0;
  10. }
  11. static int do_migration_fn(const char *cgroup, void *arg)
  12. {
  13. int object_pid = (int)(size_t)arg;
  14. if (setuid(TEST_UID))
  15. return EXIT_FAILURE;
  16. // XXX checking /proc/$pid/cgroup would be quicker than wait
  17. if (cg_enter(cgroup, object_pid) ||
  18. cg_wait_for_proc_count(cgroup, 1))
  19. return EXIT_FAILURE;
  20. return EXIT_SUCCESS;
  21. }
  22. static int do_controller_fn(const char *cgroup, void *arg)
  23. {
  24. const char *child = cgroup;
  25. const char *parent = arg;
  26. if (setuid(TEST_UID))
  27. return EXIT_FAILURE;
  28. if (!cg_read_strstr(child, "cgroup.controllers", "cpuset"))
  29. return EXIT_FAILURE;
  30. if (cg_write(parent, "cgroup.subtree_control", "+cpuset"))
  31. return EXIT_FAILURE;
  32. if (cg_read_strstr(child, "cgroup.controllers", "cpuset"))
  33. return EXIT_FAILURE;
  34. if (cg_write(parent, "cgroup.subtree_control", "-cpuset"))
  35. return EXIT_FAILURE;
  36. if (!cg_read_strstr(child, "cgroup.controllers", "cpuset"))
  37. return EXIT_FAILURE;
  38. return EXIT_SUCCESS;
  39. }
  40. /*
  41. * Migrate a process between two sibling cgroups.
  42. * The success should only depend on the parent cgroup permissions and not the
  43. * migrated process itself (cpuset controller is in place because it uses
  44. * security_task_setscheduler() in cgroup v1).
  45. *
  46. * Deliberately don't set cpuset.cpus in children to avoid definining migration
  47. * permissions between two different cpusets.
  48. */
  49. static int test_cpuset_perms_object(const char *root, bool allow)
  50. {
  51. char *parent = NULL, *child_src = NULL, *child_dst = NULL;
  52. char *parent_procs = NULL, *child_src_procs = NULL, *child_dst_procs = NULL;
  53. const uid_t test_euid = TEST_UID;
  54. int object_pid = 0;
  55. int ret = KSFT_FAIL;
  56. parent = cg_name(root, "cpuset_test_0");
  57. if (!parent)
  58. goto cleanup;
  59. parent_procs = cg_name(parent, "cgroup.procs");
  60. if (!parent_procs)
  61. goto cleanup;
  62. if (cg_create(parent))
  63. goto cleanup;
  64. child_src = cg_name(parent, "cpuset_test_1");
  65. if (!child_src)
  66. goto cleanup;
  67. child_src_procs = cg_name(child_src, "cgroup.procs");
  68. if (!child_src_procs)
  69. goto cleanup;
  70. if (cg_create(child_src))
  71. goto cleanup;
  72. child_dst = cg_name(parent, "cpuset_test_2");
  73. if (!child_dst)
  74. goto cleanup;
  75. child_dst_procs = cg_name(child_dst, "cgroup.procs");
  76. if (!child_dst_procs)
  77. goto cleanup;
  78. if (cg_create(child_dst))
  79. goto cleanup;
  80. if (cg_write(parent, "cgroup.subtree_control", "+cpuset"))
  81. goto cleanup;
  82. if (cg_read_strstr(child_src, "cgroup.controllers", "cpuset") ||
  83. cg_read_strstr(child_dst, "cgroup.controllers", "cpuset"))
  84. goto cleanup;
  85. /* Enable permissions along src->dst tree path */
  86. if (chown(child_src_procs, test_euid, -1) ||
  87. chown(child_dst_procs, test_euid, -1))
  88. goto cleanup;
  89. if (allow && chown(parent_procs, test_euid, -1))
  90. goto cleanup;
  91. /* Fork a privileged child as a test object */
  92. object_pid = cg_run_nowait(child_src, idle_process_fn, NULL);
  93. if (object_pid < 0)
  94. goto cleanup;
  95. /* Carry out migration in a child process that can drop all privileges
  96. * (including capabilities), the main process must remain privileged for
  97. * cleanup.
  98. * Child process's cgroup is irrelevant but we place it into child_dst
  99. * as hacky way to pass information about migration target to the child.
  100. */
  101. if (allow ^ (cg_run(child_dst, do_migration_fn, (void *)(size_t)object_pid) == EXIT_SUCCESS))
  102. goto cleanup;
  103. ret = KSFT_PASS;
  104. cleanup:
  105. if (object_pid > 0) {
  106. (void)kill(object_pid, SIGTERM);
  107. (void)clone_reap(object_pid, WEXITED);
  108. }
  109. cg_destroy(child_dst);
  110. free(child_dst_procs);
  111. free(child_dst);
  112. cg_destroy(child_src);
  113. free(child_src_procs);
  114. free(child_src);
  115. cg_destroy(parent);
  116. free(parent_procs);
  117. free(parent);
  118. return ret;
  119. }
  120. static int test_cpuset_perms_object_allow(const char *root)
  121. {
  122. return test_cpuset_perms_object(root, true);
  123. }
  124. static int test_cpuset_perms_object_deny(const char *root)
  125. {
  126. return test_cpuset_perms_object(root, false);
  127. }
  128. /*
  129. * Migrate a process between parent and child implicitely
  130. * Implicit migration happens when a controller is enabled/disabled.
  131. *
  132. */
  133. static int test_cpuset_perms_subtree(const char *root)
  134. {
  135. char *parent = NULL, *child = NULL;
  136. char *parent_procs = NULL, *parent_subctl = NULL, *child_procs = NULL;
  137. const uid_t test_euid = TEST_UID;
  138. int object_pid = 0;
  139. int ret = KSFT_FAIL;
  140. parent = cg_name(root, "cpuset_test_0");
  141. if (!parent)
  142. goto cleanup;
  143. parent_procs = cg_name(parent, "cgroup.procs");
  144. if (!parent_procs)
  145. goto cleanup;
  146. parent_subctl = cg_name(parent, "cgroup.subtree_control");
  147. if (!parent_subctl)
  148. goto cleanup;
  149. if (cg_create(parent))
  150. goto cleanup;
  151. child = cg_name(parent, "cpuset_test_1");
  152. if (!child)
  153. goto cleanup;
  154. child_procs = cg_name(child, "cgroup.procs");
  155. if (!child_procs)
  156. goto cleanup;
  157. if (cg_create(child))
  158. goto cleanup;
  159. /* Enable permissions as in a delegated subtree */
  160. if (chown(parent_procs, test_euid, -1) ||
  161. chown(parent_subctl, test_euid, -1) ||
  162. chown(child_procs, test_euid, -1))
  163. goto cleanup;
  164. /* Put a privileged child in the subtree and modify controller state
  165. * from an unprivileged process, the main process remains privileged
  166. * for cleanup.
  167. * The unprivileged child runs in subtree too to avoid parent and
  168. * internal-node constraing violation.
  169. */
  170. object_pid = cg_run_nowait(child, idle_process_fn, NULL);
  171. if (object_pid < 0)
  172. goto cleanup;
  173. if (cg_run(child, do_controller_fn, parent) != EXIT_SUCCESS)
  174. goto cleanup;
  175. ret = KSFT_PASS;
  176. cleanup:
  177. if (object_pid > 0) {
  178. (void)kill(object_pid, SIGTERM);
  179. (void)clone_reap(object_pid, WEXITED);
  180. }
  181. cg_destroy(child);
  182. free(child_procs);
  183. free(child);
  184. cg_destroy(parent);
  185. free(parent_subctl);
  186. free(parent_procs);
  187. free(parent);
  188. return ret;
  189. }
  190. #define T(x) { x, #x }
  191. struct cpuset_test {
  192. int (*fn)(const char *root);
  193. const char *name;
  194. } tests[] = {
  195. T(test_cpuset_perms_object_allow),
  196. T(test_cpuset_perms_object_deny),
  197. T(test_cpuset_perms_subtree),
  198. };
  199. #undef T
  200. int main(int argc, char *argv[])
  201. {
  202. char root[PATH_MAX];
  203. int i;
  204. ksft_print_header();
  205. ksft_set_plan(ARRAY_SIZE(tests));
  206. if (cg_find_unified_root(root, sizeof(root), NULL))
  207. ksft_exit_skip("cgroup v2 isn't mounted\n");
  208. if (cg_read_strstr(root, "cgroup.subtree_control", "cpuset"))
  209. if (cg_write(root, "cgroup.subtree_control", "+cpuset"))
  210. ksft_exit_skip("Failed to set cpuset controller\n");
  211. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  212. switch (tests[i].fn(root)) {
  213. case KSFT_PASS:
  214. ksft_test_result_pass("%s\n", tests[i].name);
  215. break;
  216. case KSFT_SKIP:
  217. ksft_test_result_skip("%s\n", tests[i].name);
  218. break;
  219. default:
  220. ksft_test_result_fail("%s\n", tests[i].name);
  221. break;
  222. }
  223. }
  224. ksft_finished();
  225. }