bpf fix for unconnect af_unix socket
Subsystems (7)
John Fastabend
maintainercover bpf fix for unconnect af_unix socket
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.
John Fastabend (2):
bpf: syzkaller found null ptr deref in unix_bpf proto add
bpf: sockmap, test for unconnected af_unix sock
include/net/sock.h | 5 +++ net/core/sock_map.c | 2 ++ .../selftests/bpf/prog_tests/sockmap_basic.c | 34 +++++++++++++++++++ 3 files changed, 41 insertions(+)
Signature · 1 line
2.33.0
John Fastabend
maintainerPATCH 1/2 bpf: syzkaller found null ptr deref in unix_bpf proto add
I added logic to track the sock pair for stream_unix sockets so that we ensure lifetime of the sock matches the time a sockmap could reference the sock (see fixes tag). I forgot though that we allow af_unix unconnected sockets into a sock{map|hash} map.
This is problematic because previous fixed expected sk_pair() to exist and did not NULL check it. Because unconnected sockets have a NULL sk_pair this resulted in the NULL ptr dereference found by syzkaller.
BUG: KASAN: null-ptr-deref in unix_stream_bpf_update_proto+0x72/0x430 net/unix/unix_bpf.c:171 Write of size 4 at addr 0000000000000080 by task syz-executor360/5073 Call Trace: <TASK> ... sock_hold include/net/sock.h:777 [inline] unix_stream_bpf_update_proto+0x72/0x430 net/unix/unix_bpf.c:171 sock_map_init_proto net/core/sock_map.c:190 [inline] sock_map_link+0xb87/0x1100 net/core/sock_map.c:294 sock_map_update_common+0xf6/0x870 net/core/sock_map.c:483 sock_map_update_elem_sys+0x5b6/0x640 net/core/sock_map.c:577 bpf_map_update_value+0x3af/0x820 kernel/bpf/syscall.c:167
We considered just checking for the null ptr and skipping taking a ref on the NULL peer sock. But, if the socket is then connected() after being added to the sockmap we can cause the original issue again. So instead this patch blocks adding af_unix sockets that are not in the ESTABLISHED state.
--- include/net/sock.h | 5 +++++ net/core/sock_map.c | 2 ++ 2 files changed, 7 insertions(+)
include/net/sock.h +5 -0
@@ -2799,6 +2799,11 @@ static inline bool sk_is_tcp(const struct sock *sk)return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; } +static inline bool sk_is_unix (const struct sock *sk) +{ +return sk->sk_family ==AF_UNIX && sk->sk_type ==SOCK_STREAM ; +} +/** * sk_eat_skb - Release a skb if it is no longer needed * @sk: socket to eat this skb from
net/core/sock_map.c +2 -0
@@ -536,6 +536,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk) {if (sk_is_tcp (sk))return (1 << sk->sk_state) & (TCPF_ESTABLISHED |TCPF_LISTEN ); +if (sk_is_unix (sk)) +return (1 << sk->sk_state) &TCPF_ESTABLISHED ;return true; }
Signature · 1 line
2.33.0
John Fastabend
maintainerPATCH 2/2 bpf: sockmap, test for unconnected af_unix sock
In reply to John FastabendAdd 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.
--- .../selftests/bpf/prog_tests/sockmap_basic.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +34 -0
@@ -524,6 +524,37 @@ static void test_sockmap_skb_verdict_peek(void)test_sockmap_pass_prog__destroy (pass); } +static void test_sockmap_unconnected_unix (void ) +{ +int err, map, stream =0 , dgram =0 , zero =0 ; +struct test_sockmap_pass_prog *skel; + + skel =test_sockmap_pass_prog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + map =bpf_map__fd (skel->maps.sock_map_rx); + + stream =xsocket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (stream,-1 ,"socket(AF_UNIX, SOCK_STREAM)" )) +return ; + + dgram =xsocket (AF_UNIX ,SOCK_DGRAM ,0 ); +if (!ASSERT_GT (dgram,-1 ,"socket(AF_UNIX, SOCK_DGRAM)" )) { +close (stream); +return ; + } + + err =bpf_map_update_elem (map, &zero, &stream,BPF_ANY ); +ASSERT_ERR (err,"bpf_map_update_elem(stream)" ); + + err =bpf_map_update_elem (map, &zero, &dgram,BPF_ANY ); +ASSERT_OK (err,"bpf_map_update_elem(dgram)" ); + +close (stream); +close (dgram); +} +void test_sockmap_basic (void ) {if (test__start_subtest ("sockmap create_update_free" ))
@@ -566,4 +597,7 @@ void test_sockmap_basic(void)test_sockmap_skb_verdict_fionread (false);if (test__start_subtest ("sockmap skb_verdict msg_f_peek" ))test_sockmap_skb_verdict_peek (); + +if (test__start_subtest ("sockmap unconnected af_unix" )) +test_sockmap_unconnected_unix (); }
Signature · 1 line
2.33.0
Re: [PATCH bpf 1/2] bpf: syzkaller found null ptr deref in unix_bpf proto add
In reply to John FastabendQuoted 23 lines · John Fastabend
I added logic to track the sock pair for
stream_unixsockets so that we ensure lifetime of the sock matches the time a sockmap could reference the sock (see fixes tag). I forgot though that we allowaf_unixunconnected sockets into a sock{map|hash} map.This is problematic because previous fixed expected
sk_pair()to exist and did not NULL check it. Because unconnected sockets have a NULLsk_pairthis resulted in the NULL ptr dereference found by syzkaller.BUG: KASAN: null-ptr-deref in unix_stream_bpf_update_proto+0x72/0x430 net/unix/unix_bpf.c:171 Write of size 4 at addr 0000000000000080 by task syz-executor360/5073 Call Trace: <TASK> ... sock_hold include/net/sock.h:777 [inline] unix_stream_bpf_update_proto+0x72/0x430 net/unix/unix_bpf.c:171 sock_map_init_proto net/core/sock_map.c:190 [inline] sock_map_link+0xb87/0x1100 net/core/sock_map.c:294 sock_map_update_common+0xf6/0x870 net/core/sock_map.c:483 sock_map_update_elem_sys+0x5b6/0x640 net/core/sock_map.c:577 bpf_map_update_value+0x3af/0x820 kernel/bpf/syscall.c:167
Quoted 5 lines
We considered just checking for the null ptr and skipping taking a ref on the NULL peer sock. But, if the socket is then
connected()after being added to the sockmap we can cause the original issue again. So instead this patch blocks addingaf_unixsockets that are not in the ESTABLISHED state.
This (and the name chosen for sk_is_unix() helper) is a bit confusing ?
When you say "af_unix sockets" you seem to imply STREAM sockets.
Quoted 15 lines
Reported-by Eric Dumazet <[email protected]>Reported-by [email protected]Fixes 8866730aed51 ("bpf, sockmap: af_unix stream sockets need to hold ref for pair sock")Signed-off-by John Fastabend <[email protected]>--- include/net/sock.h | 5 +++++ net/core/sock_map.c | 2 ++ 2 files changed, 7 insertions(+)include/net/sock.h +0
-0
include/net/sock.h static inline bool sk_is_tcp(const struct sock *sk) · line 2802
Quoted 4 lines
include/net/sock.h +1
-0@@ -2799,6 +2799,11 @@ static inline bool sk_is_tcp(const struct sock *sk)return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; } +static inline bool sk_is_unix (const struct sock *sk)
Maybe sk_is_stream_unix() ?
Quoted 23 lines
include/net/sock.h +4
-0+{ +return sk->sk_family ==AF_UNIX && sk->sk_type ==SOCK_STREAM ; +} +/** * sk_eat_skb - Release a skb if it is no longer needed * @sk: socket to eat this skb from net/core/sock_map.c +2
-1@@ -536,6 +536,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk) {if (sk_is_tcp (sk))return (1 << sk->sk_state) & (TCPF_ESTABLISHED |TCPF_LISTEN ); +if (sk_is_unix (sk)) +return (1 << sk->sk_state) &TCPF_ESTABLISHED ;return true; } --2.33.0
Jakub Sitnicki
maintainerRe: [PATCH bpf 1/2] bpf: syzkaller found null ptr deref in unix_bpf proto add
Quoted 1 line · Eric Dumazet
On Fri, Dec 1, 2023 at 4:23 AM John Fastabend <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 8 lines
include/net/sock.h +0
-0@@ -2799,6 +2799,11 @@ static inline bool sk_is_tcp(const struct sock *sk)return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; }
Quoted 4 lines
include/net/sock.h static inline bool sk_is_tcp(const struct sock *sk) · line 2802
Quoted 1 line
include/net/sock.h +1
-0+static inline bool sk_is_unix (const struct sock *sk)Maybe
sk_is_stream_unix()?
+1. I found it confusing as well.
[...]
Jakub Sitnicki
maintainerRe: [PATCH bpf 2/2] bpf: sockmap, test for unconnected af_unix sock
In reply to John FastabendQuoted 21 lines · John Fastabend
Add test to
sockmap_basicto ensureaf_unixsockets 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]>--- .../selftests/bpf/prog_tests/sockmap_basic.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +4
-0@@ -524,6 +524,37 @@ static void test_sockmap_skb_verdict_peek(void)test_sockmap_pass_prog__destroy (pass); } +static void test_sockmap_unconnected_unix (void ) +{ +int err, map, stream =0 , dgram =0 , zero =0 ; +struct test_sockmap_pass_prog *skel;
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c static void test_sockmap_skb_verdict_peek(void) · line 540
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +10
-0+ + skel =test_sockmap_pass_prog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + map =bpf_map__fd (skel->maps.sock_map_rx); + + stream =xsocket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (stream,-1 ,"socket(AF_UNIX, SOCK_STREAM)" )) +return ;
Isn't it redudant to use both the xsocket wrapper and ASSERT_* macro? Or is there some debugging value that comes from that, which I missed?
Quoted 28 lines
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +20
-0+ + dgram =xsocket (AF_UNIX ,SOCK_DGRAM ,0 ); +if (!ASSERT_GT (dgram,-1 ,"socket(AF_UNIX, SOCK_DGRAM)" )) { +close (stream); +return ; + } + + err =bpf_map_update_elem (map, &zero, &stream,BPF_ANY ); +ASSERT_ERR (err,"bpf_map_update_elem(stream)" ); + + err =bpf_map_update_elem (map, &zero, &dgram,BPF_ANY ); +ASSERT_OK (err,"bpf_map_update_elem(dgram)" ); + +close (stream); +close (dgram); +} +void test_sockmap_basic (void ) {if (test__start_subtest ("sockmap create_update_free" ))@@ -566,4 +597,7 @@ void test_sockmap_basic(void)test_sockmap_skb_verdict_fionread (false);if (test__start_subtest ("sockmap skb_verdict msg_f_peek" ))test_sockmap_skb_verdict_peek (); + +if (test__start_subtest ("sockmap unconnected af_unix" )) +test_sockmap_unconnected_unix (); }
John Fastabend
maintainerRe: [PATCH bpf 1/2] bpf: syzkaller found null ptr deref in unix_bpf proto add
In reply to Jakub SitnickiQuoted 13 lines · Jakub Sitnicki
Quoted 1 line · Eric Dumazet
On Fri, Dec 1, 2023 at 4:23 AM John Fastabend <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 8 lines
include/net/sock.h +0
-0@@ -2799,6 +2799,11 @@ static inline bool sk_is_tcp(const struct sock *sk)return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; }
Quoted 8 lines
Quoted 4 lines
include/net/sock.h static inline bool sk_is_tcp(const struct sock *sk) · line 2802
Quoted 1 line
include/net/sock.h +1
-0+static inline bool sk_is_unix (const struct sock *sk)Maybe
sk_is_stream_unix()?+1. I found it confusing as well.
[...]
OK will do v2 with sk_is_stream_unix() so it reads better. Thanks.
John Fastabend
maintainerRe: [PATCH bpf 2/2] bpf: sockmap, test for unconnected af_unix sock
In reply to Jakub SitnickiQuoted 27 lines · Jakub Sitnicki
Quoted 26 lines · John Fastabend
Add test to
sockmap_basicto ensureaf_unixsockets 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]>--- .../selftests/bpf/prog_tests/sockmap_basic.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +9
-0@@ -524,6 +524,37 @@ static void test_sockmap_skb_verdict_peek(void)test_sockmap_pass_prog__destroy (pass); } +static void test_sockmap_unconnected_unix (void ) +{ +int err, map, stream =0 , dgram =0 , zero =0 ; +struct test_sockmap_pass_prog *skel; + + skel =test_sockmap_pass_prog__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; +
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c static void test_sockmap_skb_verdict_peek(void) · line 540
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +5
-0+ map =bpf_map__fd (skel->maps.sock_map_rx); + + stream =xsocket (AF_UNIX ,SOCK_STREAM ,0 ); +if (!ASSERT_GT (stream,-1 ,"socket(AF_UNIX, SOCK_STREAM)" )) +return ;Isn't it redudant to use both the xsocket wrapper and ASSERT_* macro? Or is there some debugging value that comes from that, which I missed?
It looks like xsocket does an error_at_liine() so guess not you will have the line number if it fails so will know if it was stream or dgram that failed. Probably can just drop the if altogether and let the thing fall through and fail.
Quoted 30 lines
Quoted 29 lines
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +20
-0+ + dgram =xsocket (AF_UNIX ,SOCK_DGRAM ,0 ); +if (!ASSERT_GT (dgram,-1 ,"socket(AF_UNIX, SOCK_DGRAM)" )) { +close (stream); +return ; + } + + err =bpf_map_update_elem (map, &zero, &stream,BPF_ANY ); +ASSERT_ERR (err,"bpf_map_update_elem(stream)" ); + + err =bpf_map_update_elem (map, &zero, &dgram,BPF_ANY ); +ASSERT_OK (err,"bpf_map_update_elem(dgram)" ); + +close (stream); +close (dgram); +} +void test_sockmap_basic (void ) {if (test__start_subtest ("sockmap create_update_free" ))@@ -566,4 +597,7 @@ void test_sockmap_basic(void)test_sockmap_skb_verdict_fionread (false);if (test__start_subtest ("sockmap skb_verdict msg_f_peek" ))test_sockmap_skb_verdict_peek (); + +if (test__start_subtest ("sockmap unconnected af_unix" )) +test_sockmap_unconnected_unix (); }