test_cgrp2_attach2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* eBPF example program:
  2. *
  3. * - Creates arraymap in kernel with 4 bytes keys and 8 byte values
  4. *
  5. * - Loads eBPF program
  6. *
  7. * The eBPF program accesses the map passed in to store two pieces of
  8. * information. The number of invocations of the program, which maps
  9. * to the number of packets received, is stored to key 0. Key 1 is
  10. * incremented on each iteration by the number of bytes stored in
  11. * the skb.
  12. *
  13. * - Attaches the new program to a cgroup using BPF_PROG_ATTACH
  14. *
  15. * - Every second, reads map[0] and map[1] to see how many bytes and
  16. * packets were seen on any socket of tasks in the given cgroup.
  17. */
  18. #define _GNU_SOURCE
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <linux/bpf.h>
  24. #include "libbpf.h"
  25. #include "cgroup_helpers.h"
  26. #define FOO "/foo"
  27. #define BAR "/foo/bar/"
  28. #define PING_CMD "ping -c1 -w1 127.0.0.1"
  29. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  30. static int prog_load(int verdict)
  31. {
  32. int ret;
  33. struct bpf_insn prog[] = {
  34. BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
  35. BPF_EXIT_INSN(),
  36. };
  37. size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
  38. ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
  39. prog, insns_cnt, "GPL", 0,
  40. bpf_log_buf, BPF_LOG_BUF_SIZE);
  41. if (ret < 0) {
  42. log_err("Loading program");
  43. printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
  44. return 0;
  45. }
  46. return ret;
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int drop_prog, allow_prog, foo = 0, bar = 0, rc = 0;
  51. allow_prog = prog_load(1);
  52. if (!allow_prog)
  53. goto err;
  54. drop_prog = prog_load(0);
  55. if (!drop_prog)
  56. goto err;
  57. if (setup_cgroup_environment())
  58. goto err;
  59. /* Create cgroup /foo, get fd, and join it */
  60. foo = create_and_get_cgroup(FOO);
  61. if (!foo)
  62. goto err;
  63. if (join_cgroup(FOO))
  64. goto err;
  65. if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
  66. log_err("Attaching prog to /foo");
  67. goto err;
  68. }
  69. printf("Attached DROP prog. This ping in cgroup /foo should fail...\n");
  70. assert(system(PING_CMD) != 0);
  71. /* Create cgroup /foo/bar, get fd, and join it */
  72. bar = create_and_get_cgroup(BAR);
  73. if (!bar)
  74. goto err;
  75. if (join_cgroup(BAR))
  76. goto err;
  77. printf("Attached DROP prog. This ping in cgroup /foo/bar should fail...\n");
  78. assert(system(PING_CMD) != 0);
  79. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
  80. log_err("Attaching prog to /foo/bar");
  81. goto err;
  82. }
  83. printf("Attached PASS prog. This ping in cgroup /foo/bar should pass...\n");
  84. assert(system(PING_CMD) == 0);
  85. if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
  86. log_err("Detaching program from /foo/bar");
  87. goto err;
  88. }
  89. printf("Detached PASS from /foo/bar while DROP is attached to /foo.\n"
  90. "This ping in cgroup /foo/bar should fail...\n");
  91. assert(system(PING_CMD) != 0);
  92. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
  93. log_err("Attaching prog to /foo/bar");
  94. goto err;
  95. }
  96. if (bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
  97. log_err("Detaching program from /foo");
  98. goto err;
  99. }
  100. printf("Attached PASS from /foo/bar and detached DROP from /foo.\n"
  101. "This ping in cgroup /foo/bar should pass...\n");
  102. assert(system(PING_CMD) == 0);
  103. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
  104. log_err("Attaching prog to /foo/bar");
  105. goto err;
  106. }
  107. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
  108. errno = 0;
  109. log_err("Unexpected success attaching prog to /foo/bar");
  110. goto err;
  111. }
  112. if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
  113. log_err("Detaching program from /foo/bar");
  114. goto err;
  115. }
  116. if (!bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
  117. errno = 0;
  118. log_err("Unexpected success in double detach from /foo");
  119. goto err;
  120. }
  121. if (bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
  122. log_err("Attaching non-overridable prog to /foo");
  123. goto err;
  124. }
  125. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
  126. errno = 0;
  127. log_err("Unexpected success attaching non-overridable prog to /foo/bar");
  128. goto err;
  129. }
  130. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 1)) {
  131. errno = 0;
  132. log_err("Unexpected success attaching overridable prog to /foo/bar");
  133. goto err;
  134. }
  135. if (!bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 1)) {
  136. errno = 0;
  137. log_err("Unexpected success attaching overridable prog to /foo");
  138. goto err;
  139. }
  140. if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
  141. log_err("Attaching different non-overridable prog to /foo");
  142. goto err;
  143. }
  144. goto out;
  145. err:
  146. rc = 1;
  147. out:
  148. close(foo);
  149. close(bar);
  150. cleanup_cgroup_environment();
  151. if (!rc)
  152. printf("PASS\n");
  153. else
  154. printf("FAIL\n");
  155. return rc;
  156. }