bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
@ 2023-09-20 10:20 Jakub Sitnicki
  2023-09-20 18:19 ` Kui-Feng Lee
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jakub Sitnicki @ 2023-09-20 10:20 UTC (permalink / raw)
  To: bpf
  Cc: netdev, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, John Fastabend, Cong Wang

With a SOCKMAP/SOCKHASH map and an sk_msg program user can steer messages
sent from one TCP socket (s1) to actually egress from another TCP
socket (s2):

tcp_bpf_sendmsg(s1)		// = sk_prot->sendmsg
  tcp_bpf_send_verdict(s1)	// __SK_REDIRECT case
    tcp_bpf_sendmsg_redir(s2)
      tcp_bpf_push_locked(s2)
	tcp_bpf_push(s2)
	  tcp_rate_check_app_limited(s2) // expects tcp_sock
	  tcp_sendmsg_locked(s2)	 // ditto

There is a hard-coded assumption in the call-chain, that the egress
socket (s2) is a TCP socket.

However in commit 122e6c79efe1 ("sock_map: Update sock type checks for
UDP") we have enabled redirects to non-TCP sockets. This was done for the
sake of BPF sk_skb programs. There was no indention to support sk_msg
send-to-egress use case.

As a result, attempts to send-to-egress through a non-TCP socket lead to a
crash due to invalid downcast from sock to tcp_sock:

 BUG: kernel NULL pointer dereference, address: 000000000000002f
 ...
 Call Trace:
  <TASK>
  ? show_regs+0x60/0x70
  ? __die+0x1f/0x70
  ? page_fault_oops+0x80/0x160
  ? do_user_addr_fault+0x2d7/0x800
  ? rcu_is_watching+0x11/0x50
  ? exc_page_fault+0x70/0x1c0
  ? asm_exc_page_fault+0x27/0x30
  ? tcp_tso_segs+0x14/0xa0
  tcp_write_xmit+0x67/0xce0
  __tcp_push_pending_frames+0x32/0xf0
  tcp_push+0x107/0x140
  tcp_sendmsg_locked+0x99f/0xbb0
  tcp_bpf_push+0x19d/0x3a0
  tcp_bpf_sendmsg_redir+0x55/0xd0
  tcp_bpf_send_verdict+0x407/0x550
  tcp_bpf_sendmsg+0x1a1/0x390
  inet_sendmsg+0x6a/0x70
  sock_sendmsg+0x9d/0xc0
  ? sockfd_lookup_light+0x12/0x80
  __sys_sendto+0x10e/0x160
  ? syscall_enter_from_user_mode+0x20/0x60
  ? __this_cpu_preempt_check+0x13/0x20
  ? lockdep_hardirqs_on+0x82/0x110
  __x64_sys_sendto+0x1f/0x30
  do_syscall_64+0x38/0x90
  entry_SYSCALL_64_after_hwframe+0x63/0xcd

Reject selecting a non-TCP sockets as redirect target from a BPF sk_msg
program to prevent the crash. When attempted, user will receive an EACCES
error from send/sendto/sendmsg() syscall.

Fixes: 122e6c79efe1 ("sock_map: Update sock type checks for UDP")
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
---
FYI, I'm working on revamping the sockmap_listen selftest, which exercises
some of redirect combinations, to cover the whole combination matrix so
that we can catch these kinds of problems early on.

 net/core/sock_map.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/net/core/sock_map.c b/net/core/sock_map.c
index cb11750b1df5..4292c2ed1828 100644
--- a/net/core/sock_map.c
+++ b/net/core/sock_map.c
@@ -668,6 +668,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
 	sk = __sock_map_lookup_elem(map, key);
 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
 		return SK_DROP;
+	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
+		return SK_DROP;
 
 	msg->flags = flags;
 	msg->sk_redir = sk;
@@ -1267,6 +1269,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
 	sk = __sock_hash_lookup_elem(map, key);
 	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
 		return SK_DROP;
+	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
+		return SK_DROP;
 
 	msg->flags = flags;
 	msg->sk_redir = sk;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
  2023-09-20 10:20 [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets Jakub Sitnicki
@ 2023-09-20 18:19 ` Kui-Feng Lee
  2023-09-20 20:59   ` Jakub Sitnicki
  2023-09-25 18:27 ` John Fastabend
  2023-09-29 15:20 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 6+ messages in thread
From: Kui-Feng Lee @ 2023-09-20 18:19 UTC (permalink / raw)
  To: Jakub Sitnicki, bpf
  Cc: netdev, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, John Fastabend, Cong Wang



On 9/20/23 03:20, Jakub Sitnicki wrote:
> 
> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index cb11750b1df5..4292c2ed1828 100644
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -668,6 +668,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
>   	sk = __sock_map_lookup_elem(map, key);
>   	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>   		return SK_DROP;
> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
> +		return SK_DROP;
>   
>   	msg->flags = flags;
>   	msg->sk_redir = sk;
> @@ -1267,6 +1269,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
>   	sk = __sock_hash_lookup_elem(map, key);
>   	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>   		return SK_DROP;
> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
> +		return SK_DROP;
>   
>   	msg->flags = flags;
>   	msg->sk_redir = sk;

Just be curious! Can it happen to other socket types?
I mean to redirect a msg from a sk of any type to one of another type.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
  2023-09-20 18:19 ` Kui-Feng Lee
@ 2023-09-20 20:59   ` Jakub Sitnicki
  2023-09-20 21:11     ` Kui-Feng Lee
  0 siblings, 1 reply; 6+ messages in thread
From: Jakub Sitnicki @ 2023-09-20 20:59 UTC (permalink / raw)
  To: Kui-Feng Lee
  Cc: bpf, netdev, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, John Fastabend, Cong Wang

On Wed, Sep 20, 2023 at 11:19 AM -07, Kui-Feng Lee wrote:
> On 9/20/23 03:20, Jakub Sitnicki wrote:
>> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
>> index cb11750b1df5..4292c2ed1828 100644
>> --- a/net/core/sock_map.c
>> +++ b/net/core/sock_map.c
>> @@ -668,6 +668,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
>>   	sk = __sock_map_lookup_elem(map, key);
>>   	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>>   		return SK_DROP;
>> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
>> +		return SK_DROP;
>>     	msg->flags = flags;
>>   	msg->sk_redir = sk;
>> @@ -1267,6 +1269,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
>>   	sk = __sock_hash_lookup_elem(map, key);
>>   	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>>   		return SK_DROP;
>> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
>> +		return SK_DROP;
>>     	msg->flags = flags;
>>   	msg->sk_redir = sk;
>
> Just be curious! Can it happen to other socket types?
> I mean to redirect a msg from a sk of any type to one of another type.

Today sk_msg redirects are implemented only for tcp4 and tcp6.

Here's a full matrix of what redirects are supported [1].

[1] https://gist.github.com/jsitnicki/578fdd614d181bed2b02922b17972b4e

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
  2023-09-20 20:59   ` Jakub Sitnicki
@ 2023-09-20 21:11     ` Kui-Feng Lee
  0 siblings, 0 replies; 6+ messages in thread
From: Kui-Feng Lee @ 2023-09-20 21:11 UTC (permalink / raw)
  To: Jakub Sitnicki
  Cc: bpf, netdev, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, John Fastabend, Cong Wang



On 9/20/23 13:59, Jakub Sitnicki wrote:
> On Wed, Sep 20, 2023 at 11:19 AM -07, Kui-Feng Lee wrote:
>> On 9/20/23 03:20, Jakub Sitnicki wrote:
>>> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
>>> index cb11750b1df5..4292c2ed1828 100644
>>> --- a/net/core/sock_map.c
>>> +++ b/net/core/sock_map.c
>>> @@ -668,6 +668,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
>>>    	sk = __sock_map_lookup_elem(map, key);
>>>    	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>>>    		return SK_DROP;
>>> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
>>> +		return SK_DROP;
>>>      	msg->flags = flags;
>>>    	msg->sk_redir = sk;
>>> @@ -1267,6 +1269,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
>>>    	sk = __sock_hash_lookup_elem(map, key);
>>>    	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>>>    		return SK_DROP;
>>> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
>>> +		return SK_DROP;
>>>      	msg->flags = flags;
>>>    	msg->sk_redir = sk;
>>
>> Just be curious! Can it happen to other socket types?
>> I mean to redirect a msg from a sk of any type to one of another type.
> 
> Today sk_msg redirects are implemented only for tcp4 and tcp6.
> 
> Here's a full matrix of what redirects are supported [1].
> 
> [1] https://gist.github.com/jsitnicki/578fdd614d181bed2b02922b17972b4e

Thanks!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
  2023-09-20 10:20 [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets Jakub Sitnicki
  2023-09-20 18:19 ` Kui-Feng Lee
@ 2023-09-25 18:27 ` John Fastabend
  2023-09-29 15:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: John Fastabend @ 2023-09-25 18:27 UTC (permalink / raw)
  To: Jakub Sitnicki, bpf
  Cc: netdev, kernel-team, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, John Fastabend, Cong Wang

Jakub Sitnicki wrote:
> With a SOCKMAP/SOCKHASH map and an sk_msg program user can steer messages
> sent from one TCP socket (s1) to actually egress from another TCP
> socket (s2):
> 
> tcp_bpf_sendmsg(s1)		// = sk_prot->sendmsg
>   tcp_bpf_send_verdict(s1)	// __SK_REDIRECT case
>     tcp_bpf_sendmsg_redir(s2)
>       tcp_bpf_push_locked(s2)
> 	tcp_bpf_push(s2)
> 	  tcp_rate_check_app_limited(s2) // expects tcp_sock
> 	  tcp_sendmsg_locked(s2)	 // ditto
> 
> There is a hard-coded assumption in the call-chain, that the egress
> socket (s2) is a TCP socket.
> 
> However in commit 122e6c79efe1 ("sock_map: Update sock type checks for
> UDP") we have enabled redirects to non-TCP sockets. This was done for the
> sake of BPF sk_skb programs. There was no indention to support sk_msg
> send-to-egress use case.
> 
> As a result, attempts to send-to-egress through a non-TCP socket lead to a
> crash due to invalid downcast from sock to tcp_sock:
> 
>  BUG: kernel NULL pointer dereference, address: 000000000000002f
>  ...
>  Call Trace:
>   <TASK>
>   ? show_regs+0x60/0x70
>   ? __die+0x1f/0x70
>   ? page_fault_oops+0x80/0x160
>   ? do_user_addr_fault+0x2d7/0x800
>   ? rcu_is_watching+0x11/0x50
>   ? exc_page_fault+0x70/0x1c0
>   ? asm_exc_page_fault+0x27/0x30
>   ? tcp_tso_segs+0x14/0xa0
>   tcp_write_xmit+0x67/0xce0
>   __tcp_push_pending_frames+0x32/0xf0
>   tcp_push+0x107/0x140
>   tcp_sendmsg_locked+0x99f/0xbb0
>   tcp_bpf_push+0x19d/0x3a0
>   tcp_bpf_sendmsg_redir+0x55/0xd0
>   tcp_bpf_send_verdict+0x407/0x550
>   tcp_bpf_sendmsg+0x1a1/0x390
>   inet_sendmsg+0x6a/0x70
>   sock_sendmsg+0x9d/0xc0
>   ? sockfd_lookup_light+0x12/0x80
>   __sys_sendto+0x10e/0x160
>   ? syscall_enter_from_user_mode+0x20/0x60
>   ? __this_cpu_preempt_check+0x13/0x20
>   ? lockdep_hardirqs_on+0x82/0x110
>   __x64_sys_sendto+0x1f/0x30
>   do_syscall_64+0x38/0x90
>   entry_SYSCALL_64_after_hwframe+0x63/0xcd
> 
> Reject selecting a non-TCP sockets as redirect target from a BPF sk_msg
> program to prevent the crash. When attempted, user will receive an EACCES
> error from send/sendto/sendmsg() syscall.
> 
> Fixes: 122e6c79efe1 ("sock_map: Update sock type checks for UDP")
> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
> ---
> FYI, I'm working on revamping the sockmap_listen selftest, which exercises
> some of redirect combinations, to cover the whole combination matrix so
> that we can catch these kinds of problems early on.

Yes this would be appreciated.

> 
>  net/core/sock_map.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index cb11750b1df5..4292c2ed1828 100644
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -668,6 +668,8 @@ BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg *, msg,
>  	sk = __sock_map_lookup_elem(map, key);
>  	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>  		return SK_DROP;
> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
> +		return SK_DROP;
>  
>  	msg->flags = flags;
>  	msg->sk_redir = sk;
> @@ -1267,6 +1269,8 @@ BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg *, msg,
>  	sk = __sock_hash_lookup_elem(map, key);
>  	if (unlikely(!sk || !sock_map_redirect_allowed(sk)))
>  		return SK_DROP;
> +	if (!(flags & BPF_F_INGRESS) && !sk_is_tcp(sk))
> +		return SK_DROP;

As a stop gap I think this is fine. If anyone wants to add support though
I do think as a use case it would make sense to redirect TCP into an
AF_UNIX socket and vice versa.

Acked-by: John Fastabend <john.fastabend@gmail.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
  2023-09-20 10:20 [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets Jakub Sitnicki
  2023-09-20 18:19 ` Kui-Feng Lee
  2023-09-25 18:27 ` John Fastabend
@ 2023-09-29 15:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-09-29 15:20 UTC (permalink / raw)
  To: Jakub Sitnicki
  Cc: bpf, netdev, kernel-team, ast, daniel, andrii, john.fastabend, cong.wang

Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed, 20 Sep 2023 12:20:55 +0200 you wrote:
> With a SOCKMAP/SOCKHASH map and an sk_msg program user can steer messages
> sent from one TCP socket (s1) to actually egress from another TCP
> socket (s2):
> 
> tcp_bpf_sendmsg(s1)		// = sk_prot->sendmsg
>   tcp_bpf_send_verdict(s1)	// __SK_REDIRECT case
>     tcp_bpf_sendmsg_redir(s2)
>       tcp_bpf_push_locked(s2)
> 	tcp_bpf_push(s2)
> 	  tcp_rate_check_app_limited(s2) // expects tcp_sock
> 	  tcp_sendmsg_locked(s2)	 // ditto
> 
> [...]

Here is the summary with links:
  - [bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets
    https://git.kernel.org/bpf/bpf/c/b80e31baa436

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-09-29 15:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-20 10:20 [PATCH bpf] bpf, sockmap: Reject sk_msg egress redirects to non-TCP sockets Jakub Sitnicki
2023-09-20 18:19 ` Kui-Feng Lee
2023-09-20 20:59   ` Jakub Sitnicki
2023-09-20 21:11     ` Kui-Feng Lee
2023-09-25 18:27 ` John Fastabend
2023-09-29 15:20 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).