Skip to content

Commit 2f2fee2

Browse files
author
Martin KaFai Lau
committed
Merge branch ' bpf fix for unconnect af_unix socket'
John Fastabend says: ==================== Eric reported a syzbot splat from a null ptr deref from recent fix to resolve a use-after-free with af-unix stream sockets and BPF sockmap usage. The issue is I missed is we allow unconnected af_unix STREAM sockets to be added to the sockmap. Fix this by blocking unconnected sockets. v2: change sk_is_unix to sk_is_stream_unix (Eric) and remove duplicate ASSERTS in selftests the xsocket helper already marks FAIL (Jakub) ==================== Signed-off-by: Martin KaFai Lau <[email protected]>
2 parents e307b5a + 50d96f0 commit 2f2fee2

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

include/net/sock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,11 @@ static inline bool sk_is_tcp(const struct sock *sk)
27992799
return sk->sk_type == SOCK_STREAM && sk->sk_protocol == IPPROTO_TCP;
28002800
}
28012801

2802+
static inline bool sk_is_stream_unix(const struct sock *sk)
2803+
{
2804+
return sk->sk_family == AF_UNIX && sk->sk_type == SOCK_STREAM;
2805+
}
2806+
28022807
/**
28032808
* sk_eat_skb - Release a skb if it is no longer needed
28042809
* @sk: socket to eat this skb from

net/core/sock_map.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
536536
{
537537
if (sk_is_tcp(sk))
538538
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
539+
if (sk_is_stream_unix(sk))
540+
return (1 << sk->sk_state) & TCPF_ESTABLISHED;
539541
return true;
540542
}
541543

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)