test_sock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018 Facebook
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <linux/filter.h>
  9. #include <bpf/bpf.h>
  10. #include "cgroup_helpers.h"
  11. #include <bpf/bpf_endian.h>
  12. #include "bpf_util.h"
  13. #define CG_PATH "/foo"
  14. #define MAX_INSNS 512
  15. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  16. static bool verbose = false;
  17. struct sock_test {
  18. const char *descr;
  19. /* BPF prog properties */
  20. struct bpf_insn insns[MAX_INSNS];
  21. enum bpf_attach_type expected_attach_type;
  22. enum bpf_attach_type attach_type;
  23. /* Socket properties */
  24. int domain;
  25. int type;
  26. /* Endpoint to bind() to */
  27. const char *ip;
  28. unsigned short port;
  29. unsigned short port_retry;
  30. /* Expected test result */
  31. enum {
  32. LOAD_REJECT,
  33. ATTACH_REJECT,
  34. BIND_REJECT,
  35. SUCCESS,
  36. RETRY_SUCCESS,
  37. RETRY_REJECT
  38. } result;
  39. };
  40. static struct sock_test tests[] = {
  41. {
  42. .descr = "bind4 load with invalid access: src_ip6",
  43. .insns = {
  44. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  45. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  46. offsetof(struct bpf_sock, src_ip6[0])),
  47. BPF_MOV64_IMM(BPF_REG_0, 1),
  48. BPF_EXIT_INSN(),
  49. },
  50. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  51. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  52. .result = LOAD_REJECT,
  53. },
  54. {
  55. .descr = "bind4 load with invalid access: mark",
  56. .insns = {
  57. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  58. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  59. offsetof(struct bpf_sock, mark)),
  60. BPF_MOV64_IMM(BPF_REG_0, 1),
  61. BPF_EXIT_INSN(),
  62. },
  63. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  64. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  65. .result = LOAD_REJECT,
  66. },
  67. {
  68. .descr = "bind6 load with invalid access: src_ip4",
  69. .insns = {
  70. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  71. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  72. offsetof(struct bpf_sock, src_ip4)),
  73. BPF_MOV64_IMM(BPF_REG_0, 1),
  74. BPF_EXIT_INSN(),
  75. },
  76. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  77. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  78. .result = LOAD_REJECT,
  79. },
  80. {
  81. .descr = "sock_create load with invalid access: src_port",
  82. .insns = {
  83. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  84. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  85. offsetof(struct bpf_sock, src_port)),
  86. BPF_MOV64_IMM(BPF_REG_0, 1),
  87. BPF_EXIT_INSN(),
  88. },
  89. .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  90. .attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  91. .result = LOAD_REJECT,
  92. },
  93. {
  94. .descr = "sock_create load w/o expected_attach_type (compat mode)",
  95. .insns = {
  96. BPF_MOV64_IMM(BPF_REG_0, 1),
  97. BPF_EXIT_INSN(),
  98. },
  99. .expected_attach_type = 0,
  100. .attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  101. .domain = AF_INET,
  102. .type = SOCK_STREAM,
  103. .ip = "127.0.0.1",
  104. .port = 8097,
  105. .result = SUCCESS,
  106. },
  107. {
  108. .descr = "sock_create load w/ expected_attach_type",
  109. .insns = {
  110. BPF_MOV64_IMM(BPF_REG_0, 1),
  111. BPF_EXIT_INSN(),
  112. },
  113. .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  114. .attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  115. .domain = AF_INET,
  116. .type = SOCK_STREAM,
  117. .ip = "127.0.0.1",
  118. .port = 8097,
  119. .result = SUCCESS,
  120. },
  121. {
  122. .descr = "attach type mismatch bind4 vs bind6",
  123. .insns = {
  124. BPF_MOV64_IMM(BPF_REG_0, 1),
  125. BPF_EXIT_INSN(),
  126. },
  127. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  128. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  129. .result = ATTACH_REJECT,
  130. },
  131. {
  132. .descr = "attach type mismatch bind6 vs bind4",
  133. .insns = {
  134. BPF_MOV64_IMM(BPF_REG_0, 1),
  135. BPF_EXIT_INSN(),
  136. },
  137. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  138. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  139. .result = ATTACH_REJECT,
  140. },
  141. {
  142. .descr = "attach type mismatch default vs bind4",
  143. .insns = {
  144. BPF_MOV64_IMM(BPF_REG_0, 1),
  145. BPF_EXIT_INSN(),
  146. },
  147. .expected_attach_type = 0,
  148. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  149. .result = ATTACH_REJECT,
  150. },
  151. {
  152. .descr = "attach type mismatch bind6 vs sock_create",
  153. .insns = {
  154. BPF_MOV64_IMM(BPF_REG_0, 1),
  155. BPF_EXIT_INSN(),
  156. },
  157. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  158. .attach_type = BPF_CGROUP_INET_SOCK_CREATE,
  159. .result = ATTACH_REJECT,
  160. },
  161. {
  162. .descr = "bind4 reject all",
  163. .insns = {
  164. BPF_MOV64_IMM(BPF_REG_0, 0),
  165. BPF_EXIT_INSN(),
  166. },
  167. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  168. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  169. .domain = AF_INET,
  170. .type = SOCK_STREAM,
  171. .ip = "0.0.0.0",
  172. .result = BIND_REJECT,
  173. },
  174. {
  175. .descr = "bind6 reject all",
  176. .insns = {
  177. BPF_MOV64_IMM(BPF_REG_0, 0),
  178. BPF_EXIT_INSN(),
  179. },
  180. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  181. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  182. .domain = AF_INET6,
  183. .type = SOCK_STREAM,
  184. .ip = "::",
  185. .result = BIND_REJECT,
  186. },
  187. {
  188. .descr = "bind6 deny specific IP & port",
  189. .insns = {
  190. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  191. /* if (ip == expected && port == expected) */
  192. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  193. offsetof(struct bpf_sock, src_ip6[3])),
  194. BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
  195. __bpf_constant_ntohl(0x00000001), 4),
  196. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  197. offsetof(struct bpf_sock, src_port)),
  198. BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x2001, 2),
  199. /* return DENY; */
  200. BPF_MOV64_IMM(BPF_REG_0, 0),
  201. BPF_JMP_A(1),
  202. /* else return ALLOW; */
  203. BPF_MOV64_IMM(BPF_REG_0, 1),
  204. BPF_EXIT_INSN(),
  205. },
  206. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  207. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  208. .domain = AF_INET6,
  209. .type = SOCK_STREAM,
  210. .ip = "::1",
  211. .port = 8193,
  212. .result = BIND_REJECT,
  213. },
  214. {
  215. .descr = "bind4 allow specific IP & port",
  216. .insns = {
  217. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  218. /* if (ip == expected && port == expected) */
  219. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  220. offsetof(struct bpf_sock, src_ip4)),
  221. BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
  222. __bpf_constant_ntohl(0x7F000001), 4),
  223. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  224. offsetof(struct bpf_sock, src_port)),
  225. BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x1002, 2),
  226. /* return ALLOW; */
  227. BPF_MOV64_IMM(BPF_REG_0, 1),
  228. BPF_JMP_A(1),
  229. /* else return DENY; */
  230. BPF_MOV64_IMM(BPF_REG_0, 0),
  231. BPF_EXIT_INSN(),
  232. },
  233. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  234. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  235. .domain = AF_INET,
  236. .type = SOCK_STREAM,
  237. .ip = "127.0.0.1",
  238. .port = 4098,
  239. .result = SUCCESS,
  240. },
  241. {
  242. .descr = "bind4 deny specific IP & port of TCP, and retry",
  243. .insns = {
  244. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  245. /* if (ip == expected && port == expected) */
  246. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  247. offsetof(struct bpf_sock, src_ip4)),
  248. BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
  249. __bpf_constant_ntohl(0x7F000001), 4),
  250. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  251. offsetof(struct bpf_sock, src_port)),
  252. BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x1002, 2),
  253. /* return DENY; */
  254. BPF_MOV64_IMM(BPF_REG_0, 0),
  255. BPF_JMP_A(1),
  256. /* else return ALLOW; */
  257. BPF_MOV64_IMM(BPF_REG_0, 1),
  258. BPF_EXIT_INSN(),
  259. },
  260. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  261. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  262. .domain = AF_INET,
  263. .type = SOCK_STREAM,
  264. .ip = "127.0.0.1",
  265. .port = 4098,
  266. .port_retry = 5000,
  267. .result = RETRY_SUCCESS,
  268. },
  269. {
  270. .descr = "bind4 deny specific IP & port of UDP, and retry",
  271. .insns = {
  272. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  273. /* if (ip == expected && port == expected) */
  274. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  275. offsetof(struct bpf_sock, src_ip4)),
  276. BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
  277. __bpf_constant_ntohl(0x7F000001), 4),
  278. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  279. offsetof(struct bpf_sock, src_port)),
  280. BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x1002, 2),
  281. /* return DENY; */
  282. BPF_MOV64_IMM(BPF_REG_0, 0),
  283. BPF_JMP_A(1),
  284. /* else return ALLOW; */
  285. BPF_MOV64_IMM(BPF_REG_0, 1),
  286. BPF_EXIT_INSN(),
  287. },
  288. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  289. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  290. .domain = AF_INET,
  291. .type = SOCK_DGRAM,
  292. .ip = "127.0.0.1",
  293. .port = 4098,
  294. .port_retry = 5000,
  295. .result = RETRY_SUCCESS,
  296. },
  297. {
  298. .descr = "bind6 deny specific IP & port, and retry",
  299. .insns = {
  300. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
  301. /* if (ip == expected && port == expected) */
  302. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  303. offsetof(struct bpf_sock, src_ip6[3])),
  304. BPF_JMP_IMM(BPF_JNE, BPF_REG_7,
  305. __bpf_constant_ntohl(0x00000001), 4),
  306. BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_6,
  307. offsetof(struct bpf_sock, src_port)),
  308. BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0x2001, 2),
  309. /* return DENY; */
  310. BPF_MOV64_IMM(BPF_REG_0, 0),
  311. BPF_JMP_A(1),
  312. /* else return ALLOW; */
  313. BPF_MOV64_IMM(BPF_REG_0, 1),
  314. BPF_EXIT_INSN(),
  315. },
  316. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  317. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  318. .domain = AF_INET6,
  319. .type = SOCK_STREAM,
  320. .ip = "::1",
  321. .port = 8193,
  322. .port_retry = 9000,
  323. .result = RETRY_SUCCESS,
  324. },
  325. {
  326. .descr = "bind4 allow all",
  327. .insns = {
  328. BPF_MOV64_IMM(BPF_REG_0, 1),
  329. BPF_EXIT_INSN(),
  330. },
  331. .expected_attach_type = BPF_CGROUP_INET4_POST_BIND,
  332. .attach_type = BPF_CGROUP_INET4_POST_BIND,
  333. .domain = AF_INET,
  334. .type = SOCK_STREAM,
  335. .ip = "0.0.0.0",
  336. .result = SUCCESS,
  337. },
  338. {
  339. .descr = "bind6 allow all",
  340. .insns = {
  341. BPF_MOV64_IMM(BPF_REG_0, 1),
  342. BPF_EXIT_INSN(),
  343. },
  344. .expected_attach_type = BPF_CGROUP_INET6_POST_BIND,
  345. .attach_type = BPF_CGROUP_INET6_POST_BIND,
  346. .domain = AF_INET6,
  347. .type = SOCK_STREAM,
  348. .ip = "::",
  349. .result = SUCCESS,
  350. },
  351. };
  352. static size_t probe_prog_length(const struct bpf_insn *fp)
  353. {
  354. size_t len;
  355. for (len = MAX_INSNS - 1; len > 0; --len)
  356. if (fp[len].code != 0 || fp[len].imm != 0)
  357. break;
  358. return len + 1;
  359. }
  360. static int load_sock_prog(const struct bpf_insn *prog,
  361. enum bpf_attach_type attach_type)
  362. {
  363. LIBBPF_OPTS(bpf_prog_load_opts, opts);
  364. int ret, insn_cnt;
  365. insn_cnt = probe_prog_length(prog);
  366. opts.expected_attach_type = attach_type;
  367. opts.log_buf = bpf_log_buf;
  368. opts.log_size = BPF_LOG_BUF_SIZE;
  369. opts.log_level = 2;
  370. ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", prog, insn_cnt, &opts);
  371. if (verbose && ret < 0)
  372. fprintf(stderr, "%s\n", bpf_log_buf);
  373. return ret;
  374. }
  375. static int attach_sock_prog(int cgfd, int progfd,
  376. enum bpf_attach_type attach_type)
  377. {
  378. return bpf_prog_attach(progfd, cgfd, attach_type, BPF_F_ALLOW_OVERRIDE);
  379. }
  380. static int bind_sock(int domain, int type, const char *ip,
  381. unsigned short port, unsigned short port_retry)
  382. {
  383. struct sockaddr_storage addr;
  384. struct sockaddr_in6 *addr6;
  385. struct sockaddr_in *addr4;
  386. int sockfd = -1;
  387. socklen_t len;
  388. int res = SUCCESS;
  389. sockfd = socket(domain, type, 0);
  390. if (sockfd < 0)
  391. goto err;
  392. memset(&addr, 0, sizeof(addr));
  393. if (domain == AF_INET) {
  394. len = sizeof(struct sockaddr_in);
  395. addr4 = (struct sockaddr_in *)&addr;
  396. addr4->sin_family = domain;
  397. addr4->sin_port = htons(port);
  398. if (inet_pton(domain, ip, (void *)&addr4->sin_addr) != 1)
  399. goto err;
  400. } else if (domain == AF_INET6) {
  401. len = sizeof(struct sockaddr_in6);
  402. addr6 = (struct sockaddr_in6 *)&addr;
  403. addr6->sin6_family = domain;
  404. addr6->sin6_port = htons(port);
  405. if (inet_pton(domain, ip, (void *)&addr6->sin6_addr) != 1)
  406. goto err;
  407. } else {
  408. goto err;
  409. }
  410. if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) {
  411. /* sys_bind() may fail for different reasons, errno has to be
  412. * checked to confirm that BPF program rejected it.
  413. */
  414. if (errno != EPERM)
  415. goto err;
  416. if (port_retry)
  417. goto retry;
  418. res = BIND_REJECT;
  419. goto out;
  420. }
  421. goto out;
  422. retry:
  423. if (domain == AF_INET)
  424. addr4->sin_port = htons(port_retry);
  425. else
  426. addr6->sin6_port = htons(port_retry);
  427. if (bind(sockfd, (const struct sockaddr *)&addr, len) == -1) {
  428. if (errno != EPERM)
  429. goto err;
  430. res = RETRY_REJECT;
  431. } else {
  432. res = RETRY_SUCCESS;
  433. }
  434. goto out;
  435. err:
  436. res = -1;
  437. out:
  438. close(sockfd);
  439. return res;
  440. }
  441. static int run_test_case(int cgfd, const struct sock_test *test)
  442. {
  443. int progfd = -1;
  444. int err = 0;
  445. int res;
  446. printf("Test case: %s .. ", test->descr);
  447. progfd = load_sock_prog(test->insns, test->expected_attach_type);
  448. if (progfd < 0) {
  449. if (test->result == LOAD_REJECT)
  450. goto out;
  451. else
  452. goto err;
  453. }
  454. if (attach_sock_prog(cgfd, progfd, test->attach_type) < 0) {
  455. if (test->result == ATTACH_REJECT)
  456. goto out;
  457. else
  458. goto err;
  459. }
  460. res = bind_sock(test->domain, test->type, test->ip, test->port,
  461. test->port_retry);
  462. if (res > 0 && test->result == res)
  463. goto out;
  464. err:
  465. err = -1;
  466. out:
  467. /* Detaching w/o checking return code: best effort attempt. */
  468. if (progfd != -1)
  469. bpf_prog_detach(cgfd, test->attach_type);
  470. close(progfd);
  471. printf("[%s]\n", err ? "FAIL" : "PASS");
  472. return err;
  473. }
  474. static int run_tests(int cgfd)
  475. {
  476. int passes = 0;
  477. int fails = 0;
  478. int i;
  479. for (i = 0; i < ARRAY_SIZE(tests); ++i) {
  480. if (run_test_case(cgfd, &tests[i]))
  481. ++fails;
  482. else
  483. ++passes;
  484. }
  485. printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
  486. return fails ? -1 : 0;
  487. }
  488. int main(int argc, char **argv)
  489. {
  490. int cgfd = -1;
  491. int err = 0;
  492. cgfd = cgroup_setup_and_join(CG_PATH);
  493. if (cgfd < 0)
  494. goto err;
  495. /* Use libbpf 1.0 API mode */
  496. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  497. if (run_tests(cgfd))
  498. goto err;
  499. goto out;
  500. err:
  501. err = -1;
  502. out:
  503. close(cgfd);
  504. cleanup_cgroup_environment();
  505. return err;
  506. }