Skip to content

Commit 50d96f0

Browse files
jrfastabMartin KaFai Lau
authored andcommitted
bpf: sockmap, test for unconnected af_unix sock
Add test to sockmap_basic to ensure af_unix sockets that are not connected can not be added to the map. Ensure we keep DGRAM sockets working however as these will not be connected typically. Signed-off-by: John Fastabend <[email protected]> Acked-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 8d66506 commit 50d96f0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_basic.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,37 @@ static void test_sockmap_skb_verdict_peek(void)
524524
test_sockmap_pass_prog__destroy(pass);
525525
}
526526

527+
static void test_sockmap_unconnected_unix(void)
528+
{
529+
int err, map, stream = 0, dgram = 0, zero = 0;
530+
struct test_sockmap_pass_prog *skel;
531+
532+
skel = test_sockmap_pass_prog__open_and_load();
533+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
534+
return;
535+
536+
map = bpf_map__fd(skel->maps.sock_map_rx);
537+
538+
stream = xsocket(AF_UNIX, SOCK_STREAM, 0);
539+
if (stream < 0)
540+
return;
541+
542+
dgram = xsocket(AF_UNIX, SOCK_DGRAM, 0);
543+
if (dgram < 0) {
544+
close(stream);
545+
return;
546+
}
547+
548+
err = bpf_map_update_elem(map, &zero, &stream, BPF_ANY);
549+
ASSERT_ERR(err, "bpf_map_update_elem(stream)");
550+
551+
err = bpf_map_update_elem(map, &zero, &dgram, BPF_ANY);
552+
ASSERT_OK(err, "bpf_map_update_elem(dgram)");
553+
554+
close(stream);
555+
close(dgram);
556+
}
557+
527558
void test_sockmap_basic(void)
528559
{
529560
if (test__start_subtest("sockmap create_update_free"))
@@ -566,4 +597,7 @@ void test_sockmap_basic(void)
566597
test_sockmap_skb_verdict_fionread(false);
567598
if (test__start_subtest("sockmap skb_verdict msg_f_peek"))
568599
test_sockmap_skb_verdict_peek();
600+
601+
if (test__start_subtest("sockmap unconnected af_unix"))
602+
test_sockmap_unconnected_unix();
569603
}

0 commit comments

Comments
 (0)