test_cgrp2_attach2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <bpf/bpf.h>
  25. #include "bpf_insn.h"
  26. #include "cgroup_helpers.h"
  27. #define FOO "/foo"
  28. #define BAR "/foo/bar/"
  29. #define PING_CMD "ping -c1 -w1 127.0.0.1 > /dev/null"
  30. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  31. static int prog_load(int verdict)
  32. {
  33. int ret;
  34. struct bpf_insn prog[] = {
  35. BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
  36. BPF_EXIT_INSN(),
  37. };
  38. size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
  39. ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
  40. prog, insns_cnt, "GPL", 0,
  41. bpf_log_buf, BPF_LOG_BUF_SIZE);
  42. if (ret < 0) {
  43. log_err("Loading program");
  44. printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
  45. return 0;
  46. }
  47. return ret;
  48. }
  49. static int test_foo_bar(void)
  50. {
  51. int drop_prog, allow_prog, foo = 0, bar = 0, rc = 0;
  52. allow_prog = prog_load(1);
  53. if (!allow_prog)
  54. goto err;
  55. drop_prog = prog_load(0);
  56. if (!drop_prog)
  57. goto err;
  58. if (setup_cgroup_environment())
  59. goto err;
  60. /* Create cgroup /foo, get fd, and join it */
  61. foo = create_and_get_cgroup(FOO);
  62. if (!foo)
  63. goto err;
  64. if (join_cgroup(FOO))
  65. goto err;
  66. if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS,
  67. BPF_F_ALLOW_OVERRIDE)) {
  68. log_err("Attaching prog to /foo");
  69. goto err;
  70. }
  71. printf("Attached DROP prog. This ping in cgroup /foo should fail...\n");
  72. assert(system(PING_CMD) != 0);
  73. /* Create cgroup /foo/bar, get fd, and join it */
  74. bar = create_and_get_cgroup(BAR);
  75. if (!bar)
  76. goto err;
  77. if (join_cgroup(BAR))
  78. goto err;
  79. printf("Attached DROP prog. This ping in cgroup /foo/bar should fail...\n");
  80. assert(system(PING_CMD) != 0);
  81. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
  82. BPF_F_ALLOW_OVERRIDE)) {
  83. log_err("Attaching prog to /foo/bar");
  84. goto err;
  85. }
  86. printf("Attached PASS prog. This ping in cgroup /foo/bar should pass...\n");
  87. assert(system(PING_CMD) == 0);
  88. if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
  89. log_err("Detaching program from /foo/bar");
  90. goto err;
  91. }
  92. printf("Detached PASS from /foo/bar while DROP is attached to /foo.\n"
  93. "This ping in cgroup /foo/bar should fail...\n");
  94. assert(system(PING_CMD) != 0);
  95. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
  96. BPF_F_ALLOW_OVERRIDE)) {
  97. log_err("Attaching prog to /foo/bar");
  98. goto err;
  99. }
  100. if (bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
  101. log_err("Detaching program from /foo");
  102. goto err;
  103. }
  104. printf("Attached PASS from /foo/bar and detached DROP from /foo.\n"
  105. "This ping in cgroup /foo/bar should pass...\n");
  106. assert(system(PING_CMD) == 0);
  107. if (bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
  108. BPF_F_ALLOW_OVERRIDE)) {
  109. log_err("Attaching prog to /foo/bar");
  110. goto err;
  111. }
  112. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
  113. errno = 0;
  114. log_err("Unexpected success attaching prog to /foo/bar");
  115. goto err;
  116. }
  117. if (bpf_prog_detach(bar, BPF_CGROUP_INET_EGRESS)) {
  118. log_err("Detaching program from /foo/bar");
  119. goto err;
  120. }
  121. if (!bpf_prog_detach(foo, BPF_CGROUP_INET_EGRESS)) {
  122. errno = 0;
  123. log_err("Unexpected success in double detach from /foo");
  124. goto err;
  125. }
  126. if (bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
  127. log_err("Attaching non-overridable prog to /foo");
  128. goto err;
  129. }
  130. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS, 0)) {
  131. errno = 0;
  132. log_err("Unexpected success attaching non-overridable prog to /foo/bar");
  133. goto err;
  134. }
  135. if (!bpf_prog_attach(allow_prog, bar, BPF_CGROUP_INET_EGRESS,
  136. BPF_F_ALLOW_OVERRIDE)) {
  137. errno = 0;
  138. log_err("Unexpected success attaching overridable prog to /foo/bar");
  139. goto err;
  140. }
  141. if (!bpf_prog_attach(allow_prog, foo, BPF_CGROUP_INET_EGRESS,
  142. BPF_F_ALLOW_OVERRIDE)) {
  143. errno = 0;
  144. log_err("Unexpected success attaching overridable prog to /foo");
  145. goto err;
  146. }
  147. if (bpf_prog_attach(drop_prog, foo, BPF_CGROUP_INET_EGRESS, 0)) {
  148. log_err("Attaching different non-overridable prog to /foo");
  149. goto err;
  150. }
  151. goto out;
  152. err:
  153. rc = 1;
  154. out:
  155. close(foo);
  156. close(bar);
  157. cleanup_cgroup_environment();
  158. if (!rc)
  159. printf("### override:PASS\n");
  160. else
  161. printf("### override:FAIL\n");
  162. return rc;
  163. }
  164. static int map_fd = -1;
  165. static int prog_load_cnt(int verdict, int val)
  166. {
  167. if (map_fd < 0)
  168. map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
  169. if (map_fd < 0) {
  170. printf("failed to create map '%s'\n", strerror(errno));
  171. return -1;
  172. }
  173. struct bpf_insn prog[] = {
  174. BPF_MOV32_IMM(BPF_REG_0, 0),
  175. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
  176. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  177. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
  178. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  179. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  180. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  181. BPF_MOV64_IMM(BPF_REG_1, val), /* r1 = 1 */
  182. BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
  183. BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
  184. BPF_EXIT_INSN(),
  185. };
  186. size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
  187. int ret;
  188. ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
  189. prog, insns_cnt, "GPL", 0,
  190. bpf_log_buf, BPF_LOG_BUF_SIZE);
  191. if (ret < 0) {
  192. log_err("Loading program");
  193. printf("Output from verifier:\n%s\n-------\n", bpf_log_buf);
  194. return 0;
  195. }
  196. return ret;
  197. }
  198. static int test_multiprog(void)
  199. {
  200. __u32 prog_ids[4], prog_cnt = 0, attach_flags, saved_prog_id;
  201. int cg1 = 0, cg2 = 0, cg3 = 0, cg4 = 0, cg5 = 0, key = 0;
  202. int drop_prog, allow_prog[6] = {}, rc = 0;
  203. unsigned long long value;
  204. int i = 0;
  205. for (i = 0; i < 6; i++) {
  206. allow_prog[i] = prog_load_cnt(1, 1 << i);
  207. if (!allow_prog[i])
  208. goto err;
  209. }
  210. drop_prog = prog_load_cnt(0, 1);
  211. if (!drop_prog)
  212. goto err;
  213. if (setup_cgroup_environment())
  214. goto err;
  215. cg1 = create_and_get_cgroup("/cg1");
  216. if (!cg1)
  217. goto err;
  218. cg2 = create_and_get_cgroup("/cg1/cg2");
  219. if (!cg2)
  220. goto err;
  221. cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
  222. if (!cg3)
  223. goto err;
  224. cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
  225. if (!cg4)
  226. goto err;
  227. cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
  228. if (!cg5)
  229. goto err;
  230. if (join_cgroup("/cg1/cg2/cg3/cg4/cg5"))
  231. goto err;
  232. if (bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
  233. BPF_F_ALLOW_MULTI)) {
  234. log_err("Attaching prog to cg1");
  235. goto err;
  236. }
  237. if (!bpf_prog_attach(allow_prog[0], cg1, BPF_CGROUP_INET_EGRESS,
  238. BPF_F_ALLOW_MULTI)) {
  239. log_err("Unexpected success attaching the same prog to cg1");
  240. goto err;
  241. }
  242. if (bpf_prog_attach(allow_prog[1], cg1, BPF_CGROUP_INET_EGRESS,
  243. BPF_F_ALLOW_MULTI)) {
  244. log_err("Attaching prog2 to cg1");
  245. goto err;
  246. }
  247. if (bpf_prog_attach(allow_prog[2], cg2, BPF_CGROUP_INET_EGRESS,
  248. BPF_F_ALLOW_OVERRIDE)) {
  249. log_err("Attaching prog to cg2");
  250. goto err;
  251. }
  252. if (bpf_prog_attach(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS,
  253. BPF_F_ALLOW_MULTI)) {
  254. log_err("Attaching prog to cg3");
  255. goto err;
  256. }
  257. if (bpf_prog_attach(allow_prog[4], cg4, BPF_CGROUP_INET_EGRESS,
  258. BPF_F_ALLOW_OVERRIDE)) {
  259. log_err("Attaching prog to cg4");
  260. goto err;
  261. }
  262. if (bpf_prog_attach(allow_prog[5], cg5, BPF_CGROUP_INET_EGRESS, 0)) {
  263. log_err("Attaching prog to cg5");
  264. goto err;
  265. }
  266. assert(system(PING_CMD) == 0);
  267. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  268. assert(value == 1 + 2 + 8 + 32);
  269. /* query the number of effective progs in cg5 */
  270. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
  271. NULL, NULL, &prog_cnt) == 0);
  272. assert(prog_cnt == 4);
  273. /* retrieve prog_ids of effective progs in cg5 */
  274. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
  275. &attach_flags, prog_ids, &prog_cnt) == 0);
  276. assert(prog_cnt == 4);
  277. assert(attach_flags == 0);
  278. saved_prog_id = prog_ids[0];
  279. /* check enospc handling */
  280. prog_ids[0] = 0;
  281. prog_cnt = 2;
  282. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
  283. &attach_flags, prog_ids, &prog_cnt) == -1 &&
  284. errno == ENOSPC);
  285. assert(prog_cnt == 4);
  286. /* check that prog_ids are returned even when buffer is too small */
  287. assert(prog_ids[0] == saved_prog_id);
  288. /* retrieve prog_id of single attached prog in cg5 */
  289. prog_ids[0] = 0;
  290. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, 0,
  291. NULL, prog_ids, &prog_cnt) == 0);
  292. assert(prog_cnt == 1);
  293. assert(prog_ids[0] == saved_prog_id);
  294. /* detach bottom program and ping again */
  295. if (bpf_prog_detach2(-1, cg5, BPF_CGROUP_INET_EGRESS)) {
  296. log_err("Detaching prog from cg5");
  297. goto err;
  298. }
  299. value = 0;
  300. assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
  301. assert(system(PING_CMD) == 0);
  302. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  303. assert(value == 1 + 2 + 8 + 16);
  304. /* detach 3rd from bottom program and ping again */
  305. errno = 0;
  306. if (!bpf_prog_detach2(0, cg3, BPF_CGROUP_INET_EGRESS)) {
  307. log_err("Unexpected success on detach from cg3");
  308. goto err;
  309. }
  310. if (bpf_prog_detach2(allow_prog[3], cg3, BPF_CGROUP_INET_EGRESS)) {
  311. log_err("Detaching from cg3");
  312. goto err;
  313. }
  314. value = 0;
  315. assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
  316. assert(system(PING_CMD) == 0);
  317. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  318. assert(value == 1 + 2 + 16);
  319. /* detach 2nd from bottom program and ping again */
  320. if (bpf_prog_detach2(-1, cg4, BPF_CGROUP_INET_EGRESS)) {
  321. log_err("Detaching prog from cg4");
  322. goto err;
  323. }
  324. value = 0;
  325. assert(bpf_map_update_elem(map_fd, &key, &value, 0) == 0);
  326. assert(system(PING_CMD) == 0);
  327. assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
  328. assert(value == 1 + 2 + 4);
  329. prog_cnt = 4;
  330. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, BPF_F_QUERY_EFFECTIVE,
  331. &attach_flags, prog_ids, &prog_cnt) == 0);
  332. assert(prog_cnt == 3);
  333. assert(attach_flags == 0);
  334. assert(bpf_prog_query(cg5, BPF_CGROUP_INET_EGRESS, 0,
  335. NULL, prog_ids, &prog_cnt) == 0);
  336. assert(prog_cnt == 0);
  337. goto out;
  338. err:
  339. rc = 1;
  340. out:
  341. for (i = 0; i < 6; i++)
  342. if (allow_prog[i] > 0)
  343. close(allow_prog[i]);
  344. close(cg1);
  345. close(cg2);
  346. close(cg3);
  347. close(cg4);
  348. close(cg5);
  349. cleanup_cgroup_environment();
  350. if (!rc)
  351. printf("### multi:PASS\n");
  352. else
  353. printf("### multi:FAIL\n");
  354. return rc;
  355. }
  356. int main(int argc, char **argv)
  357. {
  358. int rc = 0;
  359. rc = test_foo_bar();
  360. if (rc)
  361. return rc;
  362. return test_multiprog();
  363. }