Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Fix endianness issues in sk_lookup/ctx_narrow_access

This test makes a lot of narrow load checks while assuming little
endian architecture, and therefore fails on s390.

Fix by introducing LSB and LSW macros and using them to perform narrow
loads.

Fixes: 0ab5539f8584 ("selftests/bpf: Tests for BPF_SK_LOOKUP attach point")
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200929201814.44360-1-iii@linux.ibm.com

authored by

Ilya Leoshkevich and committed by
Alexei Starovoitov
6458bde3 c810b31e

+101 -115
+101 -115
tools/testing/selftests/bpf/progs/test_sk_lookup.c
··· 19 19 #define IP6(aaaa, bbbb, cccc, dddd) \ 20 20 { bpf_htonl(aaaa), bpf_htonl(bbbb), bpf_htonl(cccc), bpf_htonl(dddd) } 21 21 22 + /* Macros for least-significant byte and word accesses. */ 23 + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 24 + #define LSE_INDEX(index, size) (index) 25 + #else 26 + #define LSE_INDEX(index, size) ((size) - (index) - 1) 27 + #endif 28 + #define LSB(value, index) \ 29 + (((__u8 *)&(value))[LSE_INDEX((index), sizeof(value))]) 30 + #define LSW(value, index) \ 31 + (((__u16 *)&(value))[LSE_INDEX((index), sizeof(value) / 2)]) 32 + 22 33 #define MAX_SOCKS 32 23 34 24 35 struct { ··· 380 369 { 381 370 struct bpf_sock *sk; 382 371 int err, family; 383 - __u16 *half; 384 - __u8 *byte; 385 372 bool v4; 386 373 387 374 v4 = (ctx->family == AF_INET); 388 375 389 376 /* Narrow loads from family field */ 390 - byte = (__u8 *)&ctx->family; 391 - half = (__u16 *)&ctx->family; 392 - if (byte[0] != (v4 ? AF_INET : AF_INET6) || 393 - byte[1] != 0 || byte[2] != 0 || byte[3] != 0) 377 + if (LSB(ctx->family, 0) != (v4 ? AF_INET : AF_INET6) || 378 + LSB(ctx->family, 1) != 0 || LSB(ctx->family, 2) != 0 || LSB(ctx->family, 3) != 0) 394 379 return SK_DROP; 395 - if (half[0] != (v4 ? AF_INET : AF_INET6)) 380 + if (LSW(ctx->family, 0) != (v4 ? AF_INET : AF_INET6)) 396 381 return SK_DROP; 397 382 398 - byte = (__u8 *)&ctx->protocol; 399 - if (byte[0] != IPPROTO_TCP || 400 - byte[1] != 0 || byte[2] != 0 || byte[3] != 0) 383 + /* Narrow loads from protocol field */ 384 + if (LSB(ctx->protocol, 0) != IPPROTO_TCP || 385 + LSB(ctx->protocol, 1) != 0 || LSB(ctx->protocol, 2) != 0 || LSB(ctx->protocol, 3) != 0) 401 386 return SK_DROP; 402 - half = (__u16 *)&ctx->protocol; 403 - if (half[0] != IPPROTO_TCP) 387 + if (LSW(ctx->protocol, 0) != IPPROTO_TCP) 404 388 return SK_DROP; 405 389 406 390 /* Narrow loads from remote_port field. Expect non-0 value. */ 407 - byte = (__u8 *)&ctx->remote_port; 408 - if (byte[0] == 0 && byte[1] == 0 && byte[2] == 0 && byte[3] == 0) 391 + if (LSB(ctx->remote_port, 0) == 0 && LSB(ctx->remote_port, 1) == 0 && 392 + LSB(ctx->remote_port, 2) == 0 && LSB(ctx->remote_port, 3) == 0) 409 393 return SK_DROP; 410 - half = (__u16 *)&ctx->remote_port; 411 - if (half[0] == 0) 394 + if (LSW(ctx->remote_port, 0) == 0) 412 395 return SK_DROP; 413 396 414 397 /* Narrow loads from local_port field. Expect DST_PORT. */ 415 - byte = (__u8 *)&ctx->local_port; 416 - if (byte[0] != ((DST_PORT >> 0) & 0xff) || 417 - byte[1] != ((DST_PORT >> 8) & 0xff) || 418 - byte[2] != 0 || byte[3] != 0) 398 + if (LSB(ctx->local_port, 0) != ((DST_PORT >> 0) & 0xff) || 399 + LSB(ctx->local_port, 1) != ((DST_PORT >> 8) & 0xff) || 400 + LSB(ctx->local_port, 2) != 0 || LSB(ctx->local_port, 3) != 0) 419 401 return SK_DROP; 420 - half = (__u16 *)&ctx->local_port; 421 - if (half[0] != DST_PORT) 402 + if (LSW(ctx->local_port, 0) != DST_PORT) 422 403 return SK_DROP; 423 404 424 405 /* Narrow loads from IPv4 fields */ 425 406 if (v4) { 426 407 /* Expect non-0.0.0.0 in remote_ip4 */ 427 - byte = (__u8 *)&ctx->remote_ip4; 428 - if (byte[0] == 0 && byte[1] == 0 && 429 - byte[2] == 0 && byte[3] == 0) 408 + if (LSB(ctx->remote_ip4, 0) == 0 && LSB(ctx->remote_ip4, 1) == 0 && 409 + LSB(ctx->remote_ip4, 2) == 0 && LSB(ctx->remote_ip4, 3) == 0) 430 410 return SK_DROP; 431 - half = (__u16 *)&ctx->remote_ip4; 432 - if (half[0] == 0 && half[1] == 0) 411 + if (LSW(ctx->remote_ip4, 0) == 0 && LSW(ctx->remote_ip4, 1) == 0) 433 412 return SK_DROP; 434 413 435 414 /* Expect DST_IP4 in local_ip4 */ 436 - byte = (__u8 *)&ctx->local_ip4; 437 - if (byte[0] != ((DST_IP4 >> 0) & 0xff) || 438 - byte[1] != ((DST_IP4 >> 8) & 0xff) || 439 - byte[2] != ((DST_IP4 >> 16) & 0xff) || 440 - byte[3] != ((DST_IP4 >> 24) & 0xff)) 415 + if (LSB(ctx->local_ip4, 0) != ((DST_IP4 >> 0) & 0xff) || 416 + LSB(ctx->local_ip4, 1) != ((DST_IP4 >> 8) & 0xff) || 417 + LSB(ctx->local_ip4, 2) != ((DST_IP4 >> 16) & 0xff) || 418 + LSB(ctx->local_ip4, 3) != ((DST_IP4 >> 24) & 0xff)) 441 419 return SK_DROP; 442 - half = (__u16 *)&ctx->local_ip4; 443 - if (half[0] != ((DST_IP4 >> 0) & 0xffff) || 444 - half[1] != ((DST_IP4 >> 16) & 0xffff)) 420 + if (LSW(ctx->local_ip4, 0) != ((DST_IP4 >> 0) & 0xffff) || 421 + LSW(ctx->local_ip4, 1) != ((DST_IP4 >> 16) & 0xffff)) 445 422 return SK_DROP; 446 423 } else { 447 424 /* Expect 0.0.0.0 IPs when family != AF_INET */ 448 - byte = (__u8 *)&ctx->remote_ip4; 449 - if (byte[0] != 0 || byte[1] != 0 && 450 - byte[2] != 0 || byte[3] != 0) 425 + if (LSB(ctx->remote_ip4, 0) != 0 || LSB(ctx->remote_ip4, 1) != 0 || 426 + LSB(ctx->remote_ip4, 2) != 0 || LSB(ctx->remote_ip4, 3) != 0) 451 427 return SK_DROP; 452 - half = (__u16 *)&ctx->remote_ip4; 453 - if (half[0] != 0 || half[1] != 0) 428 + if (LSW(ctx->remote_ip4, 0) != 0 || LSW(ctx->remote_ip4, 1) != 0) 454 429 return SK_DROP; 455 430 456 - byte = (__u8 *)&ctx->local_ip4; 457 - if (byte[0] != 0 || byte[1] != 0 && 458 - byte[2] != 0 || byte[3] != 0) 431 + if (LSB(ctx->local_ip4, 0) != 0 || LSB(ctx->local_ip4, 1) != 0 || 432 + LSB(ctx->local_ip4, 2) != 0 || LSB(ctx->local_ip4, 3) != 0) 459 433 return SK_DROP; 460 - half = (__u16 *)&ctx->local_ip4; 461 - if (half[0] != 0 || half[1] != 0) 434 + if (LSW(ctx->local_ip4, 0) != 0 || LSW(ctx->local_ip4, 1) != 0) 462 435 return SK_DROP; 463 436 } 464 437 465 438 /* Narrow loads from IPv6 fields */ 466 439 if (!v4) { 467 - /* Expenct non-:: IP in remote_ip6 */ 468 - byte = (__u8 *)&ctx->remote_ip6; 469 - if (byte[0] == 0 && byte[1] == 0 && 470 - byte[2] == 0 && byte[3] == 0 && 471 - byte[4] == 0 && byte[5] == 0 && 472 - byte[6] == 0 && byte[7] == 0 && 473 - byte[8] == 0 && byte[9] == 0 && 474 - byte[10] == 0 && byte[11] == 0 && 475 - byte[12] == 0 && byte[13] == 0 && 476 - byte[14] == 0 && byte[15] == 0) 440 + /* Expect non-:: IP in remote_ip6 */ 441 + if (LSB(ctx->remote_ip6[0], 0) == 0 && LSB(ctx->remote_ip6[0], 1) == 0 && 442 + LSB(ctx->remote_ip6[0], 2) == 0 && LSB(ctx->remote_ip6[0], 3) == 0 && 443 + LSB(ctx->remote_ip6[1], 0) == 0 && LSB(ctx->remote_ip6[1], 1) == 0 && 444 + LSB(ctx->remote_ip6[1], 2) == 0 && LSB(ctx->remote_ip6[1], 3) == 0 && 445 + LSB(ctx->remote_ip6[2], 0) == 0 && LSB(ctx->remote_ip6[2], 1) == 0 && 446 + LSB(ctx->remote_ip6[2], 2) == 0 && LSB(ctx->remote_ip6[2], 3) == 0 && 447 + LSB(ctx->remote_ip6[3], 0) == 0 && LSB(ctx->remote_ip6[3], 1) == 0 && 448 + LSB(ctx->remote_ip6[3], 2) == 0 && LSB(ctx->remote_ip6[3], 3) == 0) 477 449 return SK_DROP; 478 - half = (__u16 *)&ctx->remote_ip6; 479 - if (half[0] == 0 && half[1] == 0 && 480 - half[2] == 0 && half[3] == 0 && 481 - half[4] == 0 && half[5] == 0 && 482 - half[6] == 0 && half[7] == 0) 450 + if (LSW(ctx->remote_ip6[0], 0) == 0 && LSW(ctx->remote_ip6[0], 1) == 0 && 451 + LSW(ctx->remote_ip6[1], 0) == 0 && LSW(ctx->remote_ip6[1], 1) == 0 && 452 + LSW(ctx->remote_ip6[2], 0) == 0 && LSW(ctx->remote_ip6[2], 1) == 0 && 453 + LSW(ctx->remote_ip6[3], 0) == 0 && LSW(ctx->remote_ip6[3], 1) == 0) 483 454 return SK_DROP; 484 - 485 455 /* Expect DST_IP6 in local_ip6 */ 486 - byte = (__u8 *)&ctx->local_ip6; 487 - if (byte[0] != ((DST_IP6[0] >> 0) & 0xff) || 488 - byte[1] != ((DST_IP6[0] >> 8) & 0xff) || 489 - byte[2] != ((DST_IP6[0] >> 16) & 0xff) || 490 - byte[3] != ((DST_IP6[0] >> 24) & 0xff) || 491 - byte[4] != ((DST_IP6[1] >> 0) & 0xff) || 492 - byte[5] != ((DST_IP6[1] >> 8) & 0xff) || 493 - byte[6] != ((DST_IP6[1] >> 16) & 0xff) || 494 - byte[7] != ((DST_IP6[1] >> 24) & 0xff) || 495 - byte[8] != ((DST_IP6[2] >> 0) & 0xff) || 496 - byte[9] != ((DST_IP6[2] >> 8) & 0xff) || 497 - byte[10] != ((DST_IP6[2] >> 16) & 0xff) || 498 - byte[11] != ((DST_IP6[2] >> 24) & 0xff) || 499 - byte[12] != ((DST_IP6[3] >> 0) & 0xff) || 500 - byte[13] != ((DST_IP6[3] >> 8) & 0xff) || 501 - byte[14] != ((DST_IP6[3] >> 16) & 0xff) || 502 - byte[15] != ((DST_IP6[3] >> 24) & 0xff)) 456 + if (LSB(ctx->local_ip6[0], 0) != ((DST_IP6[0] >> 0) & 0xff) || 457 + LSB(ctx->local_ip6[0], 1) != ((DST_IP6[0] >> 8) & 0xff) || 458 + LSB(ctx->local_ip6[0], 2) != ((DST_IP6[0] >> 16) & 0xff) || 459 + LSB(ctx->local_ip6[0], 3) != ((DST_IP6[0] >> 24) & 0xff) || 460 + LSB(ctx->local_ip6[1], 0) != ((DST_IP6[1] >> 0) & 0xff) || 461 + LSB(ctx->local_ip6[1], 1) != ((DST_IP6[1] >> 8) & 0xff) || 462 + LSB(ctx->local_ip6[1], 2) != ((DST_IP6[1] >> 16) & 0xff) || 463 + LSB(ctx->local_ip6[1], 3) != ((DST_IP6[1] >> 24) & 0xff) || 464 + LSB(ctx->local_ip6[2], 0) != ((DST_IP6[2] >> 0) & 0xff) || 465 + LSB(ctx->local_ip6[2], 1) != ((DST_IP6[2] >> 8) & 0xff) || 466 + LSB(ctx->local_ip6[2], 2) != ((DST_IP6[2] >> 16) & 0xff) || 467 + LSB(ctx->local_ip6[2], 3) != ((DST_IP6[2] >> 24) & 0xff) || 468 + LSB(ctx->local_ip6[3], 0) != ((DST_IP6[3] >> 0) & 0xff) || 469 + LSB(ctx->local_ip6[3], 1) != ((DST_IP6[3] >> 8) & 0xff) || 470 + LSB(ctx->local_ip6[3], 2) != ((DST_IP6[3] >> 16) & 0xff) || 471 + LSB(ctx->local_ip6[3], 3) != ((DST_IP6[3] >> 24) & 0xff)) 503 472 return SK_DROP; 504 - half = (__u16 *)&ctx->local_ip6; 505 - if (half[0] != ((DST_IP6[0] >> 0) & 0xffff) || 506 - half[1] != ((DST_IP6[0] >> 16) & 0xffff) || 507 - half[2] != ((DST_IP6[1] >> 0) & 0xffff) || 508 - half[3] != ((DST_IP6[1] >> 16) & 0xffff) || 509 - half[4] != ((DST_IP6[2] >> 0) & 0xffff) || 510 - half[5] != ((DST_IP6[2] >> 16) & 0xffff) || 511 - half[6] != ((DST_IP6[3] >> 0) & 0xffff) || 512 - half[7] != ((DST_IP6[3] >> 16) & 0xffff)) 473 + if (LSW(ctx->local_ip6[0], 0) != ((DST_IP6[0] >> 0) & 0xffff) || 474 + LSW(ctx->local_ip6[0], 1) != ((DST_IP6[0] >> 16) & 0xffff) || 475 + LSW(ctx->local_ip6[1], 0) != ((DST_IP6[1] >> 0) & 0xffff) || 476 + LSW(ctx->local_ip6[1], 1) != ((DST_IP6[1] >> 16) & 0xffff) || 477 + LSW(ctx->local_ip6[2], 0) != ((DST_IP6[2] >> 0) & 0xffff) || 478 + LSW(ctx->local_ip6[2], 1) != ((DST_IP6[2] >> 16) & 0xffff) || 479 + LSW(ctx->local_ip6[3], 0) != ((DST_IP6[3] >> 0) & 0xffff) || 480 + LSW(ctx->local_ip6[3], 1) != ((DST_IP6[3] >> 16) & 0xffff)) 513 481 return SK_DROP; 514 482 } else { 515 483 /* Expect :: IPs when family != AF_INET6 */ 516 - byte = (__u8 *)&ctx->remote_ip6; 517 - if (byte[0] != 0 || byte[1] != 0 || 518 - byte[2] != 0 || byte[3] != 0 || 519 - byte[4] != 0 || byte[5] != 0 || 520 - byte[6] != 0 || byte[7] != 0 || 521 - byte[8] != 0 || byte[9] != 0 || 522 - byte[10] != 0 || byte[11] != 0 || 523 - byte[12] != 0 || byte[13] != 0 || 524 - byte[14] != 0 || byte[15] != 0) 484 + if (LSB(ctx->remote_ip6[0], 0) != 0 || LSB(ctx->remote_ip6[0], 1) != 0 || 485 + LSB(ctx->remote_ip6[0], 2) != 0 || LSB(ctx->remote_ip6[0], 3) != 0 || 486 + LSB(ctx->remote_ip6[1], 0) != 0 || LSB(ctx->remote_ip6[1], 1) != 0 || 487 + LSB(ctx->remote_ip6[1], 2) != 0 || LSB(ctx->remote_ip6[1], 3) != 0 || 488 + LSB(ctx->remote_ip6[2], 0) != 0 || LSB(ctx->remote_ip6[2], 1) != 0 || 489 + LSB(ctx->remote_ip6[2], 2) != 0 || LSB(ctx->remote_ip6[2], 3) != 0 || 490 + LSB(ctx->remote_ip6[3], 0) != 0 || LSB(ctx->remote_ip6[3], 1) != 0 || 491 + LSB(ctx->remote_ip6[3], 2) != 0 || LSB(ctx->remote_ip6[3], 3) != 0) 525 492 return SK_DROP; 526 - half = (__u16 *)&ctx->remote_ip6; 527 - if (half[0] != 0 || half[1] != 0 || 528 - half[2] != 0 || half[3] != 0 || 529 - half[4] != 0 || half[5] != 0 || 530 - half[6] != 0 || half[7] != 0) 493 + if (LSW(ctx->remote_ip6[0], 0) != 0 || LSW(ctx->remote_ip6[0], 1) != 0 || 494 + LSW(ctx->remote_ip6[1], 0) != 0 || LSW(ctx->remote_ip6[1], 1) != 0 || 495 + LSW(ctx->remote_ip6[2], 0) != 0 || LSW(ctx->remote_ip6[2], 1) != 0 || 496 + LSW(ctx->remote_ip6[3], 0) != 0 || LSW(ctx->remote_ip6[3], 1) != 0) 531 497 return SK_DROP; 532 498 533 - byte = (__u8 *)&ctx->local_ip6; 534 - if (byte[0] != 0 || byte[1] != 0 || 535 - byte[2] != 0 || byte[3] != 0 || 536 - byte[4] != 0 || byte[5] != 0 || 537 - byte[6] != 0 || byte[7] != 0 || 538 - byte[8] != 0 || byte[9] != 0 || 539 - byte[10] != 0 || byte[11] != 0 || 540 - byte[12] != 0 || byte[13] != 0 || 541 - byte[14] != 0 || byte[15] != 0) 499 + if (LSB(ctx->local_ip6[0], 0) != 0 || LSB(ctx->local_ip6[0], 1) != 0 || 500 + LSB(ctx->local_ip6[0], 2) != 0 || LSB(ctx->local_ip6[0], 3) != 0 || 501 + LSB(ctx->local_ip6[1], 0) != 0 || LSB(ctx->local_ip6[1], 1) != 0 || 502 + LSB(ctx->local_ip6[1], 2) != 0 || LSB(ctx->local_ip6[1], 3) != 0 || 503 + LSB(ctx->local_ip6[2], 0) != 0 || LSB(ctx->local_ip6[2], 1) != 0 || 504 + LSB(ctx->local_ip6[2], 2) != 0 || LSB(ctx->local_ip6[2], 3) != 0 || 505 + LSB(ctx->local_ip6[3], 0) != 0 || LSB(ctx->local_ip6[3], 1) != 0 || 506 + LSB(ctx->local_ip6[3], 2) != 0 || LSB(ctx->local_ip6[3], 3) != 0) 542 507 return SK_DROP; 543 - half = (__u16 *)&ctx->local_ip6; 544 - if (half[0] != 0 || half[1] != 0 || 545 - half[2] != 0 || half[3] != 0 || 546 - half[4] != 0 || half[5] != 0 || 547 - half[6] != 0 || half[7] != 0) 508 + if (LSW(ctx->remote_ip6[0], 0) != 0 || LSW(ctx->remote_ip6[0], 1) != 0 || 509 + LSW(ctx->remote_ip6[1], 0) != 0 || LSW(ctx->remote_ip6[1], 1) != 0 || 510 + LSW(ctx->remote_ip6[2], 0) != 0 || LSW(ctx->remote_ip6[2], 1) != 0 || 511 + LSW(ctx->remote_ip6[3], 0) != 0 || LSW(ctx->remote_ip6[3], 1) != 0) 548 512 return SK_DROP; 549 513 } 550 514