Skip to content

Commit 976b4f3

Browse files
rdnaAlexei Starovoitov
authored andcommitted
selftests/bpf: Test [::] -> [::1] rewrite in sys_sendmsg in test_sock_addr
Test that sys_sendmsg BPF hook doesn't break sys_sendmsg behaviour to rewrite destination IPv6 = [::] with [::1] (BSD'ism). Two test cases are added: 1) User passes dst IPv6 = [::] and BPF_CGROUP_UDP6_SENDMSG program doesn't touch it. 2) User passes dst IPv6 != [::], but BPF_CGROUP_UDP6_SENDMSG program rewrites it with [::]. In both cases [::1] is used by sys_sendmsg code eventually and datagram is sent successfully for unconnected UDP socket. Example of relevant output: Test case: sendmsg6: set dst IP = [::] (BSD'ism) .. [PASS] Test case: sendmsg6: preserve dst IP = [::] (BSD'ism) .. [PASS] Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent e8e3698 commit 976b4f3

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

tools/testing/selftests/bpf/test_sock_addr.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define SERV6_V4MAPPED_IP "::ffff:192.168.0.4"
4545
#define SRC6_IP "::1"
4646
#define SRC6_REWRITE_IP "::6"
47+
#define WILDCARD6_IP "::"
4748
#define SERV6_PORT 6060
4849
#define SERV6_REWRITE_PORT 6666
4950

@@ -85,12 +86,14 @@ static int bind4_prog_load(const struct sock_addr_test *test);
8586
static int bind6_prog_load(const struct sock_addr_test *test);
8687
static int connect4_prog_load(const struct sock_addr_test *test);
8788
static int connect6_prog_load(const struct sock_addr_test *test);
89+
static int sendmsg_allow_prog_load(const struct sock_addr_test *test);
8890
static int sendmsg_deny_prog_load(const struct sock_addr_test *test);
8991
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test);
9092
static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test);
9193
static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test);
9294
static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test);
9395
static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test);
96+
static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test);
9497

9598
static struct sock_addr_test tests[] = {
9699
/* bind */
@@ -462,6 +465,34 @@ static struct sock_addr_test tests[] = {
462465
SRC6_REWRITE_IP,
463466
SYSCALL_ENOTSUPP,
464467
},
468+
{
469+
"sendmsg6: set dst IP = [::] (BSD'ism)",
470+
sendmsg6_rw_wildcard_prog_load,
471+
BPF_CGROUP_UDP6_SENDMSG,
472+
BPF_CGROUP_UDP6_SENDMSG,
473+
AF_INET6,
474+
SOCK_DGRAM,
475+
SERV6_IP,
476+
SERV6_PORT,
477+
SERV6_REWRITE_IP,
478+
SERV6_REWRITE_PORT,
479+
SRC6_REWRITE_IP,
480+
SUCCESS,
481+
},
482+
{
483+
"sendmsg6: preserve dst IP = [::] (BSD'ism)",
484+
sendmsg_allow_prog_load,
485+
BPF_CGROUP_UDP6_SENDMSG,
486+
BPF_CGROUP_UDP6_SENDMSG,
487+
AF_INET6,
488+
SOCK_DGRAM,
489+
WILDCARD6_IP,
490+
SERV6_PORT,
491+
SERV6_REWRITE_IP,
492+
SERV6_PORT,
493+
SRC6_IP,
494+
SUCCESS,
495+
},
465496
{
466497
"sendmsg6: deny call",
467498
sendmsg_deny_prog_load,
@@ -734,16 +765,27 @@ static int connect6_prog_load(const struct sock_addr_test *test)
734765
return load_path(test, CONNECT6_PROG_PATH);
735766
}
736767

737-
static int sendmsg_deny_prog_load(const struct sock_addr_test *test)
768+
static int sendmsg_ret_only_prog_load(const struct sock_addr_test *test,
769+
int32_t rc)
738770
{
739771
struct bpf_insn insns[] = {
740-
/* return 0 */
741-
BPF_MOV64_IMM(BPF_REG_0, 0),
772+
/* return rc */
773+
BPF_MOV64_IMM(BPF_REG_0, rc),
742774
BPF_EXIT_INSN(),
743775
};
744776
return load_insns(test, insns, sizeof(insns) / sizeof(struct bpf_insn));
745777
}
746778

779+
static int sendmsg_allow_prog_load(const struct sock_addr_test *test)
780+
{
781+
return sendmsg_ret_only_prog_load(test, /*rc*/ 1);
782+
}
783+
784+
static int sendmsg_deny_prog_load(const struct sock_addr_test *test)
785+
{
786+
return sendmsg_ret_only_prog_load(test, /*rc*/ 0);
787+
}
788+
747789
static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test)
748790
{
749791
struct sockaddr_in dst4_rw_addr;
@@ -864,6 +906,11 @@ static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test)
864906
return sendmsg6_rw_dst_asm_prog_load(test, SERV6_V4MAPPED_IP);
865907
}
866908

909+
static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test)
910+
{
911+
return sendmsg6_rw_dst_asm_prog_load(test, WILDCARD6_IP);
912+
}
913+
867914
static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test)
868915
{
869916
return load_path(test, SENDMSG6_PROG_PATH);

0 commit comments

Comments
 (0)