Fix kcm + sockmap by checking psock type
Subsystems (7)
John Fastabend
maintainerv3 cover Fix kcm + sockmap by checking psock type
We check if the sk_user_data (the psock in skmsg) is in fact a sockmap type to late, after we read the refcnt which is an error. This series moves the check up before reading refcnt and also adds a test to test_maps to test trying to add a KCM socket into a sockmap.
While reviewig this code I also found an issue with KCM and kTLS where each uses sk_data_ready hooks and associated stream parser breaking expectations in kcm, ktls or both. But that fix will need to go to net.
Thanks to Eric for reporting.
v2: Fix up file +/- my scripts lost track of them
v3: return EBUSY if refcnt is zero
John Fastabend (2):
bpf: skmsg, fix psock create on existing kcm/tls port
bpf: test_maps add a test to catch kcm + sockmap
include/linux/skmsg.h | 25 +++++++++--- net/core/sock_map.c | 11 +++--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/sockmap_kcm.c | 14 +++++++ tools/testing/selftests/bpf/test_maps.c | 64 ++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/sockmap_kcm.c
Signature · 1 line
1.9.1
John Fastabend
maintainerPATCH v3 1/2 bpf: skmsg, fix psock create on existing kcm/tls port
Before using the psock returned by sk_psock_get() when adding it to a sockmap we need to ensure it is actually a sockmap based psock. Previously we were only checking this after incrementing the reference counter which was an error. This resulted in a slab-out-of-bounds error when the psock was not actually a sockmap type.
This moves the check up so the reference counter is only used if it is a sockmap psock.
Eric reported the following KASAN BUG,
BUG: KASAN: slab-out-of-bounds in atomic_read include/asm-generic/atomic-instrumented.h:21 [inline] BUG: KASAN: slab-out-of-bounds in refcount_inc_not_zero_checked+0x97/0x2f0 lib/refcount.c:120 Read of size 4 at addr ffff88019548be58 by task syz-executor4/22387 CPU: 1 PID: 22387 Comm: syz-executor4 Not tainted 4.19.0-rc7+ #264 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0x1c4/0x2b4 lib/dump_stack.c:113 print_address_description.cold.8+0x9/0x1ff mm/kasan/report.c:256 kasan_report_error mm/kasan/report.c:354 [inline] kasan_report.cold.9+0x242/0x309 mm/kasan/report.c:412 check_memory_region_inline mm/kasan/kasan.c:260 [inline] check_memory_region+0x13e/0x1b0 mm/kasan/kasan.c:267 kasan_check_read+0x11/0x20 mm/kasan/kasan.c:272 atomic_read include/asm-generic/atomic-instrumented.h:21 [inline] refcount_inc_not_zero_checked+0x97/0x2f0 lib/refcount.c:120 sk_psock_get include/linux/skmsg.h:379 [inline] sock_map_link.isra.6+0x41f/0xe30 net/core/sock_map.c:178 sock_hash_update_common+0x19b/0x11e0 net/core/sock_map.c:669 sock_hash_update_elem+0x306/0x470 net/core/sock_map.c:738 map_update_elem+0x819/0xdf0 kernel/bpf/syscall.c:818
--- include/linux/skmsg.h | 25 ++++++++++++++++++++----- net/core/sock_map.c | 11 ++++++----- 2 files changed, 26 insertions(+), 10 deletions(-)
include/linux/skmsg.h +20 -5
@@ -270,11 +270,6 @@ static inline struct sk_psock *sk_psock(const struct sock *sk)return rcu_dereference_sk_user_data (sk); } -static inline bool sk_has_psock (struct sock *sk) -{ -return sk_psock (sk) !=NULL && sk->sk_prot->recvmsg == tcp_bpf_recvmsg; -} -static inline void sk_psock_queue_msg (struct sk_psock *psock,struct sk_msg *msg) {
@@ -374,6 +369,26 @@ static inline bool sk_psock_test_state(const struct sk_psock *psock,return test_bit (bit, &psock->state); } +static inline struct sk_psock *sk_psock_get_checked (struct sock *sk) +{ +struct sk_psock *psock; + +rcu_read_lock (); + psock =sk_psock (sk); +if (psock) { +if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) { + psock =ERR_PTR (-EBUSY ); + goto out; + } + +if (!refcount_inc_not_zero (&psock->refcnt)) + psock =ERR_PTR (-EBUSY ); + } +out: +rcu_read_unlock (); +return psock; +} +static inline struct sk_psock *sk_psock_get (struct sock *sk) {struct sk_psock *psock;
net/core/sock_map.c +6 -5
@@ -175,12 +175,13 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs, } } - psock =sk_psock_get (sk); + psock =sk_psock_get_checked (sk); +if (IS_ERR (psock)) { + ret =PTR_ERR (psock); + goto out_progs; + } +if (psock) { -if (!sk_has_psock (sk)) { - ret = -EBUSY ; - goto out_progs; - }if ((msg_parser &&READ_ONCE (psock->progs.msg_parser)) || (skb_progs &&READ_ONCE (psock->progs.skb_parser))) {sk_psock_put (sk, psock);
Signature · 1 line
1.9.1
John Fastabend
maintainerPATCH v3 2/2 bpf: test_maps add a test to catch kcm + sockmap
In reply to John FastabendAdding a socket to both sockmap and kcm is not supported due to collision on sk_user_data usage.
If selftests is run without KCM support we will issue a warning and continue with the tests.
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/sockmap_kcm.c | 14 +++++++ tools/testing/selftests/bpf/test_maps.c | 64 ++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/sockmap_kcm.c
tools/testing/selftests/bpf/Makefile +1 -1
@@ -28,7 +28,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \ - sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \ + sockmap_verdict_prog.o sockmap_kcm.o dev_cgroup.o sample_ret0.o test_tracepoint.o \ test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \ sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \ sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \
tools/testing/selftests/bpf/sockmap_kcm.c new file +14 -0
@@ -0,0 +1,14 @@ +#include <linux/bpf.h> +#include "bpf_helpers.h" +#include "bpf_util.h" +#include "bpf_endian.h" + +int _version SEC("version") = 1; + +SEC("socket_kcm") +int bpf_prog1 (struct __sk_buff *skb) +{ +return skb->len; +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/test_maps.c +62 -2
94 lines not on this page · raw message
Signature · 1 line
1.9.1
Quoted 19 lines · John Fastabend
We check if the
sk_user_data(the psock in skmsg) is in fact a sockmap type to late, after we read the refcnt which is an error. This series moves the check up before reading refcnt and also adds a test totest_mapsto test trying to add a KCM socket into a sockmap.While reviewig this code I also found an issue with KCM and kTLS where each uses
sk_data_readyhooks and associated stream parser breaking expectations in kcm, ktls or both. But that fix will need to go to net.Thanks to Eric for reporting.
v2: Fix up file +/- my scripts lost track of them
v3: return EBUSY if refcnt is zeroJohn Fastabend (2):
bpf: skmsg, fix psock create on existing kcm/tls port
bpf:test_mapsadd a test to catch kcm + sockmap
Quoted 7 lines
include/linux/skmsg.h | 25 +++++++++--- net/core/sock_map.c | 11 +++--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/sockmap_kcm.c | 14 +++++++ tools/testing/selftests/bpf/test_maps.c | 64 ++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/sockmap_kcm.c
Applied, thanks!
Daniel Borkmann
maintainerQuoted 28 lines · Daniel Borkmann
Quoted 19 lines · John Fastabend
We check if the
sk_user_data(the psock in skmsg) is in fact a sockmap type to late, after we read the refcnt which is an error. This series moves the check up before reading refcnt and also adds a test totest_mapsto test trying to add a KCM socket into a sockmap.While reviewig this code I also found an issue with KCM and kTLS where each uses
sk_data_readyhooks and associated stream parser breaking expectations in kcm, ktls or both. But that fix will need to go to net.Thanks to Eric for reporting.
v2: Fix up file +/- my scripts lost track of them
v3: return EBUSY if refcnt is zeroJohn Fastabend (2):
bpf: skmsg, fix psock create on existing kcm/tls port
bpf:test_mapsadd a test to catch kcm + sockmapQuoted 7 lines
include/linux/skmsg.h | 25 +++++++++--- net/core/sock_map.c | 11 +++--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/sockmap_kcm.c | 14 +++++++ tools/testing/selftests/bpf/test_maps.c | 64 ++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/sockmap_kcm.c
Quoted 1 line
Applied, thanks!
Fyi, I've only applied patch 1/2 for now to get the bug fixed. The patch 2/2 throws a bunch of warnings that look like the below. Also, I think we leak kcm socket in error paths and once we're done with testing, so would be good to close it once unneeded. Please respin the test as a stand-alone commit, thanks:
[...]
bpf-next/tools/testing/selftests/bpf/libbpf.a -lcap -lelf -lrt -lpthread -o /home/darkstar/trees/bpf-next-ok/tools/testing/selftests/bpf/test_maps test_maps.c: In function ‘test_sockmap’:
test_maps.c:869:0: warning: "AF_KCM" redefined
#define AF_KCM 41
In file included from /usr/include/sys/socket.h:38:0,
from test_maps.c:21:
/usr/include/bits/socket.h:133:0: note: this is the location of the previous definition
#define AF_KCM PF_KCM
John Fastabend
maintainerQuoted 29 lines · Daniel Borkmann
Quoted 28 lines · Daniel Borkmann
Quoted 26 lines · John Fastabend
We check if the
sk_user_data(the psock in skmsg) is in fact a sockmap type to late, after we read the refcnt which is an error. This series moves the check up before reading refcnt and also adds a test totest_mapsto test trying to add a KCM socket into a sockmap.While reviewig this code I also found an issue with KCM and kTLS where each uses
sk_data_readyhooks and associated stream parser breaking expectations in kcm, ktls or both. But that fix will need to go to net.Thanks to Eric for reporting.
v2: Fix up file +/- my scripts lost track of them
v3: return EBUSY if refcnt is zeroJohn Fastabend (2):
bpf: skmsg, fix psock create on existing kcm/tls port
bpf:test_mapsadd a test to catch kcm + sockmapinclude/linux/skmsg.h | 25 +++++++++--- net/core/sock_map.c | 11 +++--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/sockmap_kcm.c | 14 +++++++ tools/testing/selftests/bpf/test_maps.c | 64 ++++++++++++++++++++++++++++++- 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/sockmap_kcm.c
Quoted 7 lines
Quoted 1 line
Applied, thanks!
Fyi, I've only applied patch 1/2 for now to get the bug fixed. The patch 2/2 throws a bunch of warnings that look like the below. Also, I think we leak kcm socket in error paths and once we're done with testing, so would be good to close it once unneeded. Please respin the test as a stand-alone commit, thanks:
Thanks, I didn't see the warnings below locally but will look into spinning a good version tonight with the closing sock fix as well.
John
Quoted 11 lines
[...]
bpf-next/tools/testing/selftests/bpf/libbpf.a -lcap -lelf -lrt -lpthread -o/home/darkstar/trees/bpf-next-ok/tools/testing/selftests/bpf/test_mapstest_maps.c: In function ‘test_sockmap’:
test_maps.c:869:0: warning: "AF_KCM" redefined
#defineAF_KCM41In file included from
/usr/include/sys/socket.h:38:0,from test_maps.c:21:
/usr/include/bits/socket.h:133:0: note: this is the location of the previous definition
#defineAF_KCMPF_KCM