bpf, sockmap IPv6/TCP state fixes
Subsystems (3)
v2 cover bpf, sockmap IPv6/TCP state fixes
ULP are only valid with TCP in ESTABLISHED states. Sockmap was not following this rule so add a fix to only allow ESTABLISHED states to be added from the userspace side. On the BPF side we continue to allow adding sockets to maps from sock_ops events, but only events that are triggered when entering the ESTABLISHED state. This blocks users from adding sockets to maps that will not be in the correct TCP state.
Also we stomped on the tcpv6_prot pointer overwriting with the tcp_prot. This was discovered by syzbot (thanks!) and not found by selftests because we only have local tests in selftest so even with ipv6 selftests we did not trigger the splat.
Will follow up with IPv6 tests for selftest regardless it seems like a miss to not have any IPv6 selftests.
Also these need to go to stable. There will be a small conflict on the second patch where we add check to the sockhash update function which did not exist until recently.
---
John Fastabend (2):
bpf: sockmap, fix crash when ipv6 sock is added
bpf: sockmap only allow ESTABLISHED sock state
kernel/bpf/sockmap.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-)
PATCH v2 1/2 bpf: sockmap, fix crash when ipv6 sock is added
This fixes a crash where we assign tcp_prot to IPv6 sockets instead of tcpv6_prot.
Previously we overwrote the sk->prot field with tcp_prot even in the AF_INET6 case. This patch ensures the correct tcp_prot and tcpv6_prot are used. Further, only allow ESTABLISHED connections to join the map per note in TLS ULP,
/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'. The ESTABLISHED check resolves the previously crashing case here.
--- kernel/bpf/sockmap.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
kernel/bpf/sockmap.c +21 -3
@@ -41,6 +41,7 @@#include <linux/mm.h> #include <net/strparser.h> #include <net/tcp.h> +#include <net/transp_v6.h> #include <linux/ptr_ring.h> #include <net/inet_common.h> #include <linux/sched/signal.h>
@@ -140,6 +141,7 @@ static int bpf_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,static int bpf_tcp_sendmsg (struct sock *sk,struct msghdr *msg,size_t size);static int bpf_tcp_sendpage (struct sock *sk,struct page *page,int offset,size_t size,int flags); +static void bpf_tcp_close (struct sock *sk,long timeout);static inline struct smap_psock *smap_psock_sk (const struct sock *sk) {
@@ -162,6 +164,8 @@ static bool bpf_tcp_stream_read(const struct sock *sk) }static struct proto tcp_bpf_proto; +static struct proto tcpv6_bpf_proto; +static int bpf_tcp_init (struct sock *sk) {struct smap_psock *psock;
@@ -181,14 +185,30 @@ static int bpf_tcp_init(struct sock *sk) psock->save_close = sk->sk_prot->close; psock->sk_proto = sk->sk_prot; +if (sk->sk_family ==AF_INET6 ) { + tcpv6_bpf_proto = *sk->sk_prot; + tcpv6_bpf_proto.close = bpf_tcp_close; + }else { + tcp_bpf_proto = *sk->sk_prot; + tcp_bpf_proto.close = bpf_tcp_close; + } +if (psock->bpf_tx_msg) { + tcpv6_bpf_proto.sendmsg = bpf_tcp_sendmsg; + tcpv6_bpf_proto.sendpage = bpf_tcp_sendpage; + tcpv6_bpf_proto.recvmsg = bpf_tcp_recvmsg; + tcpv6_bpf_proto.stream_memory_read = bpf_tcp_stream_read; tcp_bpf_proto.sendmsg = bpf_tcp_sendmsg; tcp_bpf_proto.sendpage = bpf_tcp_sendpage; tcp_bpf_proto.recvmsg = bpf_tcp_recvmsg; tcp_bpf_proto.stream_memory_read = bpf_tcp_stream_read; } - sk->sk_prot = &tcp_bpf_proto; +if (sk->sk_family ==AF_INET6 ) + sk->sk_prot = &tcpv6_bpf_proto; +else + sk->sk_prot = &tcp_bpf_proto; +rcu_read_unlock ();return 0 ; }
@@ -1111,8 +1131,6 @@ static void bpf_tcp_msg_add(struct smap_psock *psock,static int bpf_tcp_ulp_register (void ) { - tcp_bpf_proto = tcp_prot; - tcp_bpf_proto.close = bpf_tcp_close;/* Once BPF TX ULP is registered it is never unregistered. It * will be in the ULP list for the lifetime of the system. Doing * duplicate registers is not a problem.
Daniel Borkmann
maintainerRe: [bpf PATCH v2 1/2] bpf: sockmap, fix crash when ipv6 sock is added
Hi John,
Quoted 15 lines · John Fastabend
This fixes a crash where we assign
tcp_protto IPv6 sockets instead oftcpv6_prot.Previously we overwrote the
sk->protfield withtcp_proteven in theAF_INET6case. This patch ensures the correcttcp_protandtcpv6_protare used. Further, only allow ESTABLISHED connections to join the map per note in TLS ULP,/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/
Quoted 8 lines
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'. The ESTABLISHED check resolves the previously crashing case here.
Fixes 174a79ff9515 ("bpf: sockmap with sk redirect support")Reported-by [email protected]Signed-off-by John Fastabend <[email protected]>Signed-off-by Wei Wang <[email protected]>
[...]
Still one question for some more clarification below that popped up while review:
kernel/bpf/sockmap.c static bool bpf_tcp_stream_read(const struct sock *sk) · line 167
Quoted 5 lines
kernel/bpf/sockmap.c +1
-0@@ -162,6 +164,8 @@ static bool bpf_tcp_stream_read(const struct sock *sk) }static struct proto tcp_bpf_proto; +static struct proto tcpv6_bpf_proto;
These two are global, w/o locking.
Quoted 4 lines
kernel/bpf/sockmap.c +0
-0static int bpf_tcp_init (struct sock *sk) {struct smap_psock *psock;
kernel/bpf/sockmap.c static int bpf_tcp_init(struct sock *sk) · line 194
Quoted 10 lines
kernel/bpf/sockmap.c +7
-0@@ -181,14 +185,30 @@ static int bpf_tcp_init(struct sock *sk) psock->save_close = sk->sk_prot->close; psock->sk_proto = sk->sk_prot; +if (sk->sk_family ==AF_INET6 ) { + tcpv6_bpf_proto = *sk->sk_prot; + tcpv6_bpf_proto.close = bpf_tcp_close; + }else { + tcp_bpf_proto = *sk->sk_prot; + tcp_bpf_proto.close = bpf_tcp_close; + }
And each time we add a BPF ULP to a v4/v6 socket, we override tcp{,v6}_bpf_proto from scratch.
Quoted 6 lines
kernel/bpf/sockmap.c +4
-0if (psock->bpf_tx_msg) { + tcpv6_bpf_proto.sendmsg = bpf_tcp_sendmsg; + tcpv6_bpf_proto.sendpage = bpf_tcp_sendpage; + tcpv6_bpf_proto.recvmsg = bpf_tcp_recvmsg; + tcpv6_bpf_proto.stream_memory_read = bpf_tcp_stream_read; tcp_bpf_proto.sendmsg = bpf_tcp_sendmsg;
kernel/bpf/sockmap.c static int bpf_tcp_init(struct sock *sk) · line 210
Quoted 10 lines
kernel/bpf/sockmap.c +4
-1tcp_bpf_proto.sendpage = bpf_tcp_sendpage; tcp_bpf_proto.recvmsg = bpf_tcp_recvmsg; tcp_bpf_proto.stream_memory_read = bpf_tcp_stream_read; } - sk->sk_prot = &tcp_bpf_proto; +if (sk->sk_family ==AF_INET6 ) + sk->sk_prot = &tcpv6_bpf_proto; +else + sk->sk_prot = &tcp_bpf_proto;
Where every active socket would be affected from it as well. Isn't that generally racy? E.g. existing ones where tcpv6_bpf_proto.sendmsg points to bpf_tcp_sendmsg would get overridden with earlier assignment on the tcpv6_bpf_proto = *sk->sk_prot during their lifetime after bpf_tcp_init().
In the kTLS case, the v4 protos are built up in module init via tls_register() and never change from there. The v6 ones are only reloaded when their addr changes e.g. module reload would come to mind, which should only be possible once no active v6 socket is present. What speaks against adapting similar scheme resp. what am I missing that the above would work? (Would be nice if there was some discussion in commit log related to it on 'why' this approach was done differently.)
Thanks,
Daniel
Quoted 13 lines
kernel/bpf/sockmap.c +0
-2rcu_read_unlock ();return 0 ; }@@ -1111,8 +1131,6 @@ static void bpf_tcp_msg_add(struct smap_psock *psock,static int bpf_tcp_ulp_register (void ) { - tcp_bpf_proto = tcp_prot; - tcp_bpf_proto.close = bpf_tcp_close;/* Once BPF TX ULP is registered it is never unregistered. It * will be in the ULP list for the lifetime of the system. Doing * duplicate registers is not a problem.
Re: [bpf PATCH v2 1/2] bpf: sockmap, fix crash when ipv6 sock is added
Quoted 68 lines · Daniel Borkmann
Hi John,
Quoted 15 lines · John Fastabend
This fixes a crash where we assign
tcp_protto IPv6 sockets instead oftcpv6_prot.Previously we overwrote the
sk->protfield withtcp_proteven in theAF_INET6case. This patch ensures the correcttcp_protandtcpv6_protare used. Further, only allow ESTABLISHED connections to join the map per note in TLS ULP,/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/Quoted 8 lines
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'. The ESTABLISHED check resolves the previously crashing case here.
Fixes 174a79ff9515 ("bpf: sockmap with sk redirect support")Reported-by [email protected]Signed-off-by John Fastabend <[email protected]>Signed-off-by Wei Wang <[email protected]>[...]
Still one question for some more clarification below that popped up while review:
kernel/bpf/sockmap.c static bool bpf_tcp_stream_read(const struct sock *sk) · line 167
Quoted 5 lines
kernel/bpf/sockmap.c +1
-0@@ -162,6 +164,8 @@ static bool bpf_tcp_stream_read(const struct sock *sk) }static struct proto tcp_bpf_proto; +static struct proto tcpv6_bpf_proto;These two are global, w/o locking.
Quoted 4 lines
kernel/bpf/sockmap.c +0
-0static int bpf_tcp_init (struct sock *sk) {struct smap_psock *psock;kernel/bpf/sockmap.c static int bpf_tcp_init(struct sock *sk) · line 194
Quoted 10 lines
kernel/bpf/sockmap.c +7
-0@@ -181,14 +185,30 @@ static int bpf_tcp_init(struct sock *sk) psock->save_close = sk->sk_prot->close; psock->sk_proto = sk->sk_prot; +if (sk->sk_family ==AF_INET6 ) { + tcpv6_bpf_proto = *sk->sk_prot; + tcpv6_bpf_proto.close = bpf_tcp_close; + }else { + tcp_bpf_proto = *sk->sk_prot; + tcp_bpf_proto.close = bpf_tcp_close; + }And each time we add a BPF ULP to a v4/v6 socket, we override tcp{,v6}_bpf_proto from scratch.
Quoted 11 lines
kernel/bpf/sockmap.c +4
-0if (psock->bpf_tx_msg) { + tcpv6_bpf_proto.sendmsg = bpf_tcp_sendmsg; + tcpv6_bpf_proto.sendpage = bpf_tcp_sendpage; + tcpv6_bpf_proto.recvmsg = bpf_tcp_recvmsg; + tcpv6_bpf_proto.stream_memory_read = bpf_tcp_stream_read; tcp_bpf_proto.sendmsg = bpf_tcp_sendmsg; tcp_bpf_proto.sendpage = bpf_tcp_sendpage; tcp_bpf_proto.recvmsg = bpf_tcp_recvmsg; tcp_bpf_proto.stream_memory_read = bpf_tcp_stream_read; }
Quoted 11 lines
kernel/bpf/sockmap.c static int bpf_tcp_init(struct sock *sk) · line 210
Quoted 5 lines
kernel/bpf/sockmap.c +4
-1- sk->sk_prot = &tcp_bpf_proto; +if (sk->sk_family ==AF_INET6 ) + sk->sk_prot = &tcpv6_bpf_proto; +else + sk->sk_prot = &tcp_bpf_proto;Where every active socket would be affected from it as well. Isn't that generally racy? E.g. existing ones where tcpv6_bpf_proto.sendmsg points to
bpf_tcp_sendmsgwould get overridden with earlier assignment on thetcpv6_bpf_proto=*sk->sk_protduring their lifetime afterbpf_tcp_init().
In general yes. At best it does feel fragile.
Quoted 7 lines
In the kTLS case, the v4 protos are built up in module init via
tls_register()and never change from there. The v6 ones are only reloaded when their addr changes e.g. module reload would come to mind, which should only be possible once no active v6 socket is present. What speaks against adapting similar scheme resp. what am I missing that the above would work? (Would be nice if there was some discussion in commit log related to it on 'why' this approach was done differently.)
I think its best to use the same scheme. Will post a new version. Also would be nice to fix the selftests in the same series. Finally, I set these pointers lazily adding a sendmsg hook for example even if it not needed. Its harmless but does create an extra call through bpf for no reason on some socks. To be complete we should avoid that.
Quoted 18 lines
Thanks,
DanielQuoted 13 lines
kernel/bpf/sockmap.c +0
-2rcu_read_unlock ();return 0 ; }@@ -1111,8 +1131,6 @@ static void bpf_tcp_msg_add(struct smap_psock *psock,static int bpf_tcp_ulp_register (void ) { - tcp_bpf_proto = tcp_prot; - tcp_bpf_proto.close = bpf_tcp_close;/* Once BPF TX ULP is registered it is never unregistered. It * will be in the ULP list for the lifetime of the system. Doing * duplicate registers is not a problem.
PATCH v2 2/2 bpf: sockmap only allow ESTABLISHED sock state
In reply to John FastabendPer the note in the TLS ULP (which is actually a generic statement regarding ULPs)
/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/
After this patch we only allow socks that are in ESTABLISHED state or are being added via a sock_ops event that is transitioning into an ESTABLISHED state. By allowing sock_ops events we allow users to manage sockmaps directly from sock ops programs. The two supported sock_ops ops are BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB and BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB.
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'.
--- kernel/bpf/sockmap.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
kernel/bpf/sockmap.c +31 -1
@@ -1956,8 +1956,12 @@ static int sock_map_update_elem(struct bpf_map *map,return -EINVAL ; } +/* ULPs are currently supported only for TCP sockets in ESTABLISHED +* state. +*/ if (skops.sk->sk_type !=SOCK_STREAM || - skops.sk->sk_protocol !=IPPROTO_TCP ) { + skops.sk->sk_protocol !=IPPROTO_TCP || + skops.sk->sk_state !=TCP_ESTABLISHED ) {fput (socket->file);return -EOPNOTSUPP ; }
@@ -2318,6 +2322,16 @@ static int sock_hash_update_elem(struct bpf_map *map,return -EINVAL ; } +/* ULPs are currently supported only for TCP sockets in ESTABLISHED +* state. +*/ +if (skops.sk->sk_type !=SOCK_STREAM || + skops.sk->sk_protocol !=IPPROTO_TCP || + skops.sk->sk_state !=TCP_ESTABLISHED ) { +fput (socket->file); +return -EOPNOTSUPP ; + } + err =sock_hash_ctx_update_elem (&skops, map, key, flags);fput (socket->file);return err;
@@ -2403,10 +2417,23 @@ struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key) .map_delete_elem = sock_hash_delete_elem, }; +static bool bpf_is_valid_sock (struct bpf_sock_ops_kern *ops) +{ +return ops->op ==BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB || + ops->op ==BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB ; +} + BPF_CALL_4(bpf_sock_map_update, struct bpf_sock_ops_kern *, bpf_sock, struct bpf_map *, map, void *, key, u64, flags) {WARN_ON_ONCE (!rcu_read_lock_held ()); + +/* ULPs are currently supported only for TCP sockets in ESTABLISHED +* state. This checks that the sock ops triggering the update is +* one indicating we are (or will be soon) in an ESTABLISHED state. +*/ +if (!bpf_is_valid_sock (bpf_sock)) +return -EOPNOTSUPP ;return sock_map_ctx_update_elem (bpf_sock, map, key, flags); }
@@ -2425,6 +2452,9 @@ struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key) struct bpf_map *, map, void *, key, u64, flags) { WARN_ON_ONCE(!rcu_read_lock_held()); + +if (!bpf_is_valid_sock (bpf_sock)) +return -EOPNOTSUPP ;return sock_hash_ctx_update_elem (bpf_sock, map, key, flags); }
Daniel Borkmann
maintainerRe: [bpf PATCH v2 2/2] bpf: sockmap only allow ESTABLISHED sock state
Hi John,
Quoted 17 lines · John Fastabend
Per the note in the TLS ULP (which is actually a generic statement regarding ULPs)
/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/After this patch we only allow socks that are in ESTABLISHED state or are being added via a
sock_opsevent that is transitioning into an ESTABLISHED state. By allowingsock_opsevents we allow users to manage sockmaps directly from sock ops programs. The two supportedsock_opsops areBPF_SOCK_OPS_PASSIVE_ESTABLISHED_CBandBPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB.
Quoted 5 lines
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'.
Reported-by Eric Dumazet <[email protected]>Signed-off-by John Fastabend <[email protected]>
Please also add a Fixes tag to this one. Ok to just reply with one.
Thanks,
Daniel
Re: [bpf PATCH v2 2/2] bpf: sockmap only allow ESTABLISHED sock state
In reply to John FastabendQuoted 17 lines · John Fastabend
Per the note in the TLS ULP (which is actually a generic statement regarding ULPs)
/* The TLS ulp is currently supported only for TCP sockets
* in ESTABLISHED state.
* Supporting sockets in LISTEN state will require us
* to modify the accept implementation to clone rather then
* share the ulp context.
*/After this patch we only allow socks that are in ESTABLISHED state or are being added via a
sock_opsevent that is transitioning into an ESTABLISHED state. By allowingsock_opsevents we allow users to manage sockmaps directly from sock ops programs. The two supportedsock_opsops areBPF_SOCK_OPS_PASSIVE_ESTABLISHED_CBandBPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB.
Quoted 6 lines
Also tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'.
Reported-by Eric Dumazet <[email protected]>Signed-off-by John Fastabend <[email protected]>---