test_current_task_under_cgroup_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #define _GNU_SOURCE
  8. #include <stdio.h>
  9. #include <linux/bpf.h>
  10. #include <unistd.h>
  11. #include "libbpf.h"
  12. #include "bpf_load.h"
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #include <linux/bpf.h>
  17. #include <sched.h>
  18. #include <sys/mount.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <linux/limits.h>
  22. #define CGROUP_MOUNT_PATH "/mnt"
  23. #define CGROUP_PATH "/mnt/my-cgroup"
  24. #define clean_errno() (errno == 0 ? "None" : strerror(errno))
  25. #define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
  26. __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
  27. static int join_cgroup(char *path)
  28. {
  29. int fd, rc = 0;
  30. pid_t pid = getpid();
  31. char cgroup_path[PATH_MAX + 1];
  32. snprintf(cgroup_path, sizeof(cgroup_path), "%s/cgroup.procs", path);
  33. fd = open(cgroup_path, O_WRONLY);
  34. if (fd < 0) {
  35. log_err("Opening Cgroup");
  36. return 1;
  37. }
  38. if (dprintf(fd, "%d\n", pid) < 0) {
  39. log_err("Joining Cgroup");
  40. rc = 1;
  41. }
  42. close(fd);
  43. return rc;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. char filename[256];
  48. int cg2, idx = 0;
  49. pid_t remote_pid, local_pid = getpid();
  50. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  51. if (load_bpf_file(filename)) {
  52. printf("%s", bpf_log_buf);
  53. return 1;
  54. }
  55. /*
  56. * This is to avoid interfering with existing cgroups. Unfortunately,
  57. * most people don't have cgroupv2 enabled at this point in time.
  58. * It's easier to create our own mount namespace and manage it
  59. * ourselves.
  60. */
  61. if (unshare(CLONE_NEWNS)) {
  62. log_err("unshare");
  63. return 1;
  64. }
  65. if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
  66. log_err("mount fakeroot");
  67. return 1;
  68. }
  69. if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL)) {
  70. log_err("mount cgroup2");
  71. return 1;
  72. }
  73. if (mkdir(CGROUP_PATH, 0777) && errno != EEXIST) {
  74. log_err("mkdir cgroup");
  75. return 1;
  76. }
  77. cg2 = open(CGROUP_PATH, O_RDONLY);
  78. if (cg2 < 0) {
  79. log_err("opening target cgroup");
  80. goto cleanup_cgroup_err;
  81. }
  82. if (bpf_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
  83. log_err("Adding target cgroup to map");
  84. goto cleanup_cgroup_err;
  85. }
  86. if (join_cgroup("/mnt/my-cgroup")) {
  87. log_err("Leaving target cgroup");
  88. goto cleanup_cgroup_err;
  89. }
  90. /*
  91. * The installed helper program catched the sync call, and should
  92. * write it to the map.
  93. */
  94. sync();
  95. bpf_lookup_elem(map_fd[1], &idx, &remote_pid);
  96. if (local_pid != remote_pid) {
  97. fprintf(stderr,
  98. "BPF Helper didn't write correct PID to map, but: %d\n",
  99. remote_pid);
  100. goto leave_cgroup_err;
  101. }
  102. /* Verify the negative scenario; leave the cgroup */
  103. if (join_cgroup(CGROUP_MOUNT_PATH))
  104. goto leave_cgroup_err;
  105. remote_pid = 0;
  106. bpf_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
  107. sync();
  108. bpf_lookup_elem(map_fd[1], &idx, &remote_pid);
  109. if (local_pid == remote_pid) {
  110. fprintf(stderr, "BPF cgroup negative test did not work\n");
  111. goto cleanup_cgroup_err;
  112. }
  113. rmdir(CGROUP_PATH);
  114. return 0;
  115. /* Error condition, cleanup */
  116. leave_cgroup_err:
  117. join_cgroup(CGROUP_MOUNT_PATH);
  118. cleanup_cgroup_err:
  119. rmdir(CGROUP_PATH);
  120. return 1;
  121. }