Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright 2021 Google LLC. */ #include <test_progs.h> #include <cgroup_helpers.h> #include <network_helpers.h> #include "cgroup_getset_retval_setsockopt.skel.h" #include "cgroup_getset_retval_getsockopt.skel.h" #include "cgroup_getset_retval_hooks.skel.h" #define SOL_CUSTOM 0xdeadbeef static int zero; static void test_setsockopt_set(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_set_eunatch = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that sets EUNATCH, assert that * we actually get that error when we run setsockopt() */ link_set_eunatch = bpf_program__attach_cgroup(obj->progs.set_eunatch, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eunatch, "cg-attach-set_eunatch")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EUNATCH, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 1, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eunatch); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_set_and_get(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_set_eunatch = NULL, *link_get_retval = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that sets EUNATCH, and one that gets the * previously set errno. Assert that we get the same errno back. */ link_set_eunatch = bpf_program__attach_cgroup(obj->progs.set_eunatch, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eunatch, "cg-attach-set_eunatch")) goto close_bpf_object; link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EUNATCH, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 2, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, -EUNATCH, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eunatch); bpf_link__destroy(link_get_retval); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_default_zero(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_get_retval = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that gets the previously set errno. * Assert that, without anything setting one, we get 0. */ link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_OK(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 1, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, 0, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_get_retval); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_default_zero_and_set(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_get_retval = NULL, *link_set_eunatch = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that gets the previously set errno, and then * one that sets the errno to EUNATCH. Assert that the get does not * see EUNATCH set later, and does not prevent EUNATCH from being set. */ link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; link_set_eunatch = bpf_program__attach_cgroup(obj->progs.set_eunatch, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eunatch, "cg-attach-set_eunatch")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EUNATCH, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 2, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, 0, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_get_retval); bpf_link__destroy(link_set_eunatch); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_override(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_set_eunatch = NULL, *link_set_eisconn = NULL; struct bpf_link *link_get_retval = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that sets EUNATCH, then one that sets EISCONN, * and then one that gets the exported errno. Assert both the syscall * and the helper sees the last set errno. */ link_set_eunatch = bpf_program__attach_cgroup(obj->progs.set_eunatch, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eunatch, "cg-attach-set_eunatch")) goto close_bpf_object; link_set_eisconn = bpf_program__attach_cgroup(obj->progs.set_eisconn, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eisconn, "cg-attach-set_eisconn")) goto close_bpf_object; link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EISCONN, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 3, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, -EISCONN, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eunatch); bpf_link__destroy(link_set_eisconn); bpf_link__destroy(link_get_retval); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_legacy_eperm(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_legacy_eperm = NULL, *link_get_retval = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that return a reject without setting errno * (legacy reject), and one that gets the errno. Assert that for * backward compatibility the syscall result in EPERM, and this * is also visible to the helper. */ link_legacy_eperm = bpf_program__attach_cgroup(obj->progs.legacy_eperm, cgroup_fd); if (!ASSERT_OK_PTR(link_legacy_eperm, "cg-attach-legacy_eperm")) goto close_bpf_object; link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EPERM, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 2, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, -EPERM, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_legacy_eperm); bpf_link__destroy(link_get_retval); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_setsockopt_legacy_no_override(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_setsockopt *obj; struct bpf_link *link_set_eunatch = NULL, *link_legacy_eperm = NULL; struct bpf_link *link_get_retval = NULL; obj = cgroup_getset_retval_setsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach setsockopt that sets EUNATCH, then one that return a reject * without setting errno, and then one that gets the exported errno. * Assert both the syscall and the helper's errno are unaffected by * the second prog (i.e. legacy rejects does not override the errno * to EPERM). */ link_set_eunatch = bpf_program__attach_cgroup(obj->progs.set_eunatch, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eunatch, "cg-attach-set_eunatch")) goto close_bpf_object; link_legacy_eperm = bpf_program__attach_cgroup(obj->progs.legacy_eperm, cgroup_fd); if (!ASSERT_OK_PTR(link_legacy_eperm, "cg-attach-legacy_eperm")) goto close_bpf_object; link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_ERR(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &zero, sizeof(int)), "setsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EUNATCH, "setsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 3, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, -EUNATCH, "retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eunatch); bpf_link__destroy(link_legacy_eperm); bpf_link__destroy(link_get_retval); cgroup_getset_retval_setsockopt__destroy(obj); } static void test_getsockopt_get(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_getsockopt *obj; struct bpf_link *link_get_retval = NULL; int buf; socklen_t optlen = sizeof(buf); obj = cgroup_getset_retval_getsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach getsockopt that gets previously set errno. Assert that the * error from kernel is in both ctx_retval_value and retval_value. */ link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_ERR(getsockopt(sock_fd, SOL_CUSTOM, 0, &buf, &optlen), "getsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EOPNOTSUPP, "getsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 1, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, -EOPNOTSUPP, "retval_value")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->ctx_retval_value, -EOPNOTSUPP, "ctx_retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_get_retval); cgroup_getset_retval_getsockopt__destroy(obj); } static void test_getsockopt_override(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_getsockopt *obj; struct bpf_link *link_set_eisconn = NULL; int buf; socklen_t optlen = sizeof(buf); obj = cgroup_getset_retval_getsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach getsockopt that sets retval to -EISCONN. Assert that this * overrides the value from kernel. */ link_set_eisconn = bpf_program__attach_cgroup(obj->progs.set_eisconn, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eisconn, "cg-attach-set_eisconn")) goto close_bpf_object; if (!ASSERT_ERR(getsockopt(sock_fd, SOL_CUSTOM, 0, &buf, &optlen), "getsockopt")) goto close_bpf_object; if (!ASSERT_EQ(errno, EISCONN, "getsockopt-errno")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 1, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eisconn); cgroup_getset_retval_getsockopt__destroy(obj); } static void test_getsockopt_retval_sync(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_getsockopt *obj; struct bpf_link *link_set_eisconn = NULL, *link_clear_retval = NULL; struct bpf_link *link_get_retval = NULL; int buf; socklen_t optlen = sizeof(buf); obj = cgroup_getset_retval_getsockopt__open_and_load(); if (!ASSERT_OK_PTR(obj, "skel-load")) return; /* Attach getsockopt that sets retval to -EISCONN, and one that clears * ctx retval. Assert that the clearing ctx retval is synced to helper * and clears any errors both from kernel and BPF.. */ link_set_eisconn = bpf_program__attach_cgroup(obj->progs.set_eisconn, cgroup_fd); if (!ASSERT_OK_PTR(link_set_eisconn, "cg-attach-set_eisconn")) goto close_bpf_object; link_clear_retval = bpf_program__attach_cgroup(obj->progs.clear_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_clear_retval, "cg-attach-clear_retval")) goto close_bpf_object; link_get_retval = bpf_program__attach_cgroup(obj->progs.get_retval, cgroup_fd); if (!ASSERT_OK_PTR(link_get_retval, "cg-attach-get_retval")) goto close_bpf_object; if (!ASSERT_OK(getsockopt(sock_fd, SOL_CUSTOM, 0, &buf, &optlen), "getsockopt")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->invocations, 3, "invocations")) goto close_bpf_object; if (!ASSERT_FALSE(obj->bss->assertion_error, "assertion_error")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->retval_value, 0, "retval_value")) goto close_bpf_object; if (!ASSERT_EQ(obj->bss->ctx_retval_value, 0, "ctx_retval_value")) goto close_bpf_object; close_bpf_object: bpf_link__destroy(link_set_eisconn); bpf_link__destroy(link_clear_retval); bpf_link__destroy(link_get_retval); cgroup_getset_retval_getsockopt__destroy(obj); } struct exposed_hook { const char *name; int expected_err; } exposed_hooks[] = { #define BPF_RETVAL_HOOK(NAME, SECTION, CTX, EXPECTED_ERR) \ { \ .name = #NAME, \ .expected_err = EXPECTED_ERR, \ }, #include "cgroup_getset_retval_hooks.h" #undef BPF_RETVAL_HOOK }; static void test_exposed_hooks(int cgroup_fd, int sock_fd) { struct cgroup_getset_retval_hooks *skel; struct bpf_program *prog; int err; int i; for (i = 0; i < ARRAY_SIZE(exposed_hooks); i++) { skel = cgroup_getset_retval_hooks__open(); if (!ASSERT_OK_PTR(skel, "cgroup_getset_retval_hooks__open")) continue; prog = bpf_object__find_program_by_name(skel->obj, exposed_hooks[i].name); if (!ASSERT_NEQ(prog, NULL, "bpf_object__find_program_by_name")) goto close_skel; err = bpf_program__set_autoload(prog, true); if (!ASSERT_OK(err, "bpf_program__set_autoload")) goto close_skel; err = cgroup_getset_retval_hooks__load(skel); ASSERT_EQ(err, exposed_hooks[i].expected_err, "expected_err"); close_skel: cgroup_getset_retval_hooks__destroy(skel); } } void test_cgroup_getset_retval(void) { int cgroup_fd = -1; int sock_fd = -1; cgroup_fd = test__join_cgroup("/cgroup_getset_retval"); if (!ASSERT_GE(cgroup_fd, 0, "cg-create")) goto close_fd; sock_fd = start_server(AF_INET, SOCK_DGRAM, NULL, 0, 0); if (!ASSERT_GE(sock_fd, 0, "start-server")) goto close_fd; if (test__start_subtest("setsockopt-set")) test_setsockopt_set(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-set_and_get")) test_setsockopt_set_and_get(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-default_zero")) test_setsockopt_default_zero(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-default_zero_and_set")) test_setsockopt_default_zero_and_set(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-override")) test_setsockopt_override(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-legacy_eperm")) test_setsockopt_legacy_eperm(cgroup_fd, sock_fd); if (test__start_subtest("setsockopt-legacy_no_override")) test_setsockopt_legacy_no_override(cgroup_fd, sock_fd); if (test__start_subtest("getsockopt-get")) test_getsockopt_get(cgroup_fd, sock_fd); if (test__start_subtest("getsockopt-override")) test_getsockopt_override(cgroup_fd, sock_fd); if (test__start_subtest("getsockopt-retval_sync")) test_getsockopt_retval_sync(cgroup_fd, sock_fd); if (test__start_subtest("exposed_hooks")) test_exposed_hooks(cgroup_fd, sock_fd); close_fd: close(cgroup_fd); } |