bpf: tcp: Fix type confusion in bpf_tcp_sock().
Subsystems (6)
cover bpf: tcp: Fix type confusion in bpf_tcp_sock().
bpf_tcp_sock() only check if sk->sk_protocol is IPPROTO_TCP, but RAW socket can bypass it:
socket(AF_INET, SOCK_RAW, IPPROTO_TCP)
Patch 1 fixes it and Patch 2 adds a test.
Kuniyuki Iwashima (2):
bpf: tcp: Fix type confusion in bpf_tcp_sock().
selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
net/core/filter.c | 2 +- .../selftests/bpf/prog_tests/sockopt_sk.c | 17 ++++++++++++++++- tools/testing/selftests/bpf/progs/sockopt_sk.c | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH 1/2 bpf: tcp: Fix type confusion in bpf_tcp_sock().
bpf_tcp_sock() only check if sk->sk_protocol is IPPROTO_TCP, but RAW socket can bypass it:
socket(AF_INET, SOCK_RAW, IPPROTO_TCP)
Calling bpf_setsockopt() in SOCKOPT prog triggers out-of-bounds access to another slab object. [0]
Let's use sk_is_tcp().
[0]:
BUG: KASAN: slab-out-of-bounds in sol_tcp_sockopt (net/core/filter.c:5519) Read of size 8 at addr ffff88801083d760 by task test_progs/1259 CPU: 1 UID: 0 PID: 1259 Comm: test_progs Tainted: G OE 7.0.0-11175-gb5c111f4967b #1 PREEMPT(full) Tainted: [O]=OOT_MODULE, [E]=UNSIGNED_MODULE Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014 Call Trace: <TASK> dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120) print_report (mm/kasan/report.c:378 mm/kasan/report.c:482) kasan_report (mm/kasan/report.c:595) sol_tcp_sockopt (net/core/filter.c:5519) __bpf_getsockopt (net/core/filter.c:5633) bpf_sk_getsockopt (net/core/filter.c:5654) bpf_prog_629ba00a1601e9f2__setsockopt+0x86/0x22c __cgroup_bpf_run_filter_setsockopt (./include/linux/bpf.h:1402 ./include/linux/filter.h:722 ./include/linux/filter.h:729 kernel/bpf/cgroup.c:81 kernel/bpf/cgroup.c:2026) do_sock_setsockopt (net/socket.c:2363) __x64_sys_setsockopt (net/socket.c:2406) do_syscall_64 (arch/x86/entry/syscall_64.c:63) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) RIP: 0033:0x7f85f82fe7de Code: 55 48 63 c9 48 63 ff 45 89 c9 48 89 e5 48 83 ec 08 6a 2c e8 34 69 f7 ff c9 c3 66 90 f3 0f 1e fa 49 89 ca b8 36 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 0a c3 66 0f 1f 84 00 00 00 00 00 48 8b 15 e1 RSP: 002b:00007ffe59dcecd8 EFLAGS: 00000202 ORIG_RAX: 0000000000000036 RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f85f82fe7de RDX: 000000000000001c RSI: 0000000000000006 RDI: 000000000000000d RBP: 00007ffe59dcef20 R08: 000000000000003c R09: 0000000000000000 R10: 00007ffe59dcef00 R11: 0000000000000202 R12: 00007ffe59dcf268 R13: 0000000000000003 R14: 00007f85f9da5000 R15: 000055b2f3201400 </TASK> The buggy address belongs to the object at ffff88801083d280 which belongs to the cache RAW of size 1792
The buggy address is located 1248 bytes inside of
allocated 1792-byte region [ffff88801083d280, ffff88801083d980)
--- net/core/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
@@ -7475,7 +7475,7 @@ u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, BPF_CALL_1(bpf_tcp_sock, struct sock *, sk) { -if (sk_fullsock (sk) && sk->sk_protocol ==IPPROTO_TCP ) +if (sk_fullsock (sk) &&sk_is_tcp (sk))return (unsigned long )sk;return (unsigned long )NULL ;
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH 2/2 selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
In reply to Kuniyuki IwashimaLet's extend sockopt_sk.c to cover bpf_tcp_sock() for the wrong socket type.
Before:
# ./test_progs -t sockopt_sk [ 151.948613] ================================================================== [ 151.951376] BUG: KASAN: slab-out-of-bounds in sol_tcp_sockopt+0xc7/0x8e0 [ 151.954159] Read of size 8 at addr ffff88801083d760 by task test_progs/1259 ... run_test:FAIL:getsetsockopt unexpected error: -1 (errno 22) #427 sockopt_sk:FAIL
After:
#427 sockopt_sk:OK
While at it, missing free() is fixed up.
--- .../selftests/bpf/prog_tests/sockopt_sk.c | 17 ++++++++++++++++- tools/testing/selftests/bpf/progs/sockopt_sk.c | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +16 -1
@@ -190,7 +190,7 @@ static int getsetsockopt(void) fd =socket (AF_NETLINK ,SOCK_RAW ,0 );if (fd <0 ) {log_err ("Failed to create AF_NETLINK socket" ); -return -1 ; + goto err; } buf.u32 =1 ;
@@ -211,6 +211,21 @@ static int getsetsockopt(void) }ASSERT_EQ (optlen,8 ,"Unexpected NETLINK_LIST_MEMBERSHIPS value" ); +/* Trick bpf_tcp_sock() with IPPROTO_TCP */ +close (fd); + fd =socket (AF_INET ,SOCK_RAW ,IPPROTO_TCP ); +if (fd <0 ) { +log_err ("Failed to create RAW socket" ); + goto err; + } + + optlen =60 ; + err =setsockopt (fd,SOL_TCP ,TCP_SAVED_SYN , &buf, optlen); +if (err) { +log_err ("Unexpected setsockopt(TCP_SAVED_SYN)" ); + goto err; + } +free (big_buf);close (fd);return 0 ;
tools/testing/selftests/bpf/progs/sockopt_sk.c +16 -0
@@ -149,6 +149,20 @@ int _setsockopt(struct bpf_sockopt *ctx)if (sk && sk->family ==AF_NETLINK ) goto out; +if (sk && sk->family ==AF_INET && sk->type ==SOCK_RAW ) { +struct bpf_tcp_sock *tp =bpf_tcp_sock (sk); + +if (tp) { +char saved_syn[60 ]; + +bpf_getsockopt (sk,SOL_TCP ,TCP_SAVED_SYN , + &saved_syn,sizeof (saved_syn)); + goto consumed; + } + + goto out; + } +/* Make sure bpf_get_netns_cookie is callable. */ if (bpf_get_netns_cookie (NULL ) ==0 )
@@ -224,6 +238,8 @@ int _setsockopt(struct bpf_sockopt *ctx)return 0 ;/* couldn't get sk storage */ storage->val = optval[0 ]; + +consumed: ctx->optlen =-1 ;/* BPF has consumed this option, don't call kernel * setsockopt handler. */
Signature · 1 line
2.54.0.545.g6539524ca2-goog
Re: [PATCH v1 bpf 2/2] selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
Quoted 42 lines · Kuniyuki Iwashima
Let's extend
sockopt_sk.cto coverbpf_tcp_sock()for the wrong socket type.Before:
# ./test_progs -t sockopt_sk [ 151.948613] ================================================================== [ 151.951376] BUG: KASAN: slab-out-of-bounds in sol_tcp_sockopt+0xc7/0x8e0 [ 151.954159] Read of size 8 at addr ffff88801083d760 by task test_progs/1259 ... run_test:FAIL:getsetsockopt unexpected error: -1 (errno 22) #427 sockopt_sk:FAILAfter:
#427 sockopt_sk:OKWhile at it, missing
free()is fixed up.Signed-off-by Kuniyuki Iwashima <[email protected]>--- .../selftests/bpf/prog_tests/sockopt_sk.c | 17 ++++++++++++++++- tools/testing/selftests/bpf/progs/sockopt_sk.c | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +2
-1@@ -190,7 +190,7 @@ static int getsetsockopt(void) fd =socket (AF_NETLINK ,SOCK_RAW ,0 );if (fd <0 ) {log_err ("Failed to create AF_NETLINK socket" ); -return -1 ; + goto err; } buf.u32 =1 ;@@ -211,6 +211,21 @@ static int getsetsockopt(void) }ASSERT_EQ (optlen,8 ,"Unexpected NETLINK_LIST_MEMBERSHIPS value" ); +/* Trick bpf_tcp_sock() with IPPROTO_TCP */
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 224
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +10
-0+close (fd); + fd =socket (AF_INET ,SOCK_RAW ,IPPROTO_TCP ); +if (fd <0 ) { +log_err ("Failed to create RAW socket" ); + goto err; + } + + optlen =60 ; + err =setsockopt (fd,SOL_TCP ,TCP_SAVED_SYN , &buf, optlen); +if (err) {
Ugh, I forgot to commit s/err/!err/ change.. :/
pw-bot: cr
Quoted 43 lines · Kuniyuki Iwashima
bpf_tcp_sock()only check ifsk->sk_protocolisIPPROTO_TCP, but RAW socket can bypass it:
socket(AF_INET, SOCK_RAW, IPPROTO_TCP)Calling
bpf_setsockopt()in SOCKOPT prog triggers out-of-bounds access to another slab object. [0]Let's use
sk_is_tcp().[0]:
BUG: KASAN: slab-out-of-bounds in sol_tcp_sockopt (net/core/filter.c:5519) Read of size 8 at addr ffff88801083d760 by task test_progs/1259 CPU: 1 UID: 0 PID: 1259 Comm: test_progs Tainted: G OE 7.0.0-11175-gb5c111f4967b #1 PREEMPT(full) Tainted: [O]=OOT_MODULE, [E]=UNSIGNED_MODULE Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014 Call Trace: <TASK> dump_stack_lvl (lib/dump_stack.c:94 lib/dump_stack.c:120) print_report (mm/kasan/report.c:378 mm/kasan/report.c:482) kasan_report (mm/kasan/report.c:595) sol_tcp_sockopt (net/core/filter.c:5519) __bpf_getsockopt (net/core/filter.c:5633) bpf_sk_getsockopt (net/core/filter.c:5654) bpf_prog_629ba00a1601e9f2__setsockopt+0x86/0x22c __cgroup_bpf_run_filter_setsockopt (./include/linux/bpf.h:1402 ./include/linux/filter.h:722 ./include/linux/filter.h:729 kernel/bpf/cgroup.c:81 kernel/bpf/cgroup.c:2026) do_sock_setsockopt (net/socket.c:2363) __x64_sys_setsockopt (net/socket.c:2406) do_syscall_64 (arch/x86/entry/syscall_64.c:63) entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121) RIP: 0033:0x7f85f82fe7de Code: 55 48 63 c9 48 63 ff 45 89 c9 48 89 e5 48 83 ec 08 6a 2c e8 34 69 f7 ff c9 c3 66 90 f3 0f 1e fa 49 89 ca b8 36 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 0a c3 66 0f 1f 84 00 00 00 00 00 48 8b 15 e1 RSP: 002b:00007ffe59dcecd8 EFLAGS: 00000202 ORIG_RAX: 0000000000000036 RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f85f82fe7de RDX: 000000000000001c RSI: 0000000000000006 RDI: 000000000000000d RBP: 00007ffe59dcef20 R08: 000000000000003c R09: 0000000000000000 R10: 00007ffe59dcef00 R11: 0000000000000202 R12: 00007ffe59dcf268 R13: 0000000000000003 R14: 00007f85f9da5000 R15: 000055b2f3201400 </TASK> The buggy address belongs to the object at ffff88801083d280 which belongs to the cache RAW of size 1792
Quoted 6 lines
The buggy address is located 1248 bytes inside of
allocated 1792-byte region [ffff88801083d280,ffff88801083d980)Fixes 655a51e536c0 ("bpf: Add struct bpf_tcp_sock and BPF_FUNC_tcp_sock")Reported-by Damiano Melotti <[email protected]>Signed-off-by Kuniyuki Iwashima <[email protected]>
First one lgtm:
Thanks!
Re: [PATCH v1 bpf 2/2] selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
In reply to Kuniyuki IwashimaQuoted 43 lines · Kuniyuki Iwashima
Quoted 41 lines · Kuniyuki Iwashima
Let's extend
sockopt_sk.cto coverbpf_tcp_sock()for the wrong socket type.Before:
# ./test_progs -t sockopt_sk [ 151.948613] ================================================================== [ 151.951376] BUG: KASAN: slab-out-of-bounds in sol_tcp_sockopt+0xc7/0x8e0 [ 151.954159] Read of size 8 at addr ffff88801083d760 by task test_progs/1259 ... run_test:FAIL:getsetsockopt unexpected error: -1 (errno 22) #427 sockopt_sk:FAILAfter:
#427 sockopt_sk:OKWhile at it, missing
free()is fixed up.Signed-off-by Kuniyuki Iwashima <[email protected]>--- .../selftests/bpf/prog_tests/sockopt_sk.c | 17 ++++++++++++++++- tools/testing/selftests/bpf/progs/sockopt_sk.c | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-)tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +1
-1@@ -190,7 +190,7 @@ static int getsetsockopt(void) fd =socket (AF_NETLINK ,SOCK_RAW ,0 );if (fd <0 ) {log_err ("Failed to create AF_NETLINK socket" ); -return -1 ; + goto err; } buf.u32 =1 ;@@ -211,6 +211,21 @@ static int getsetsockopt(void) }ASSERT_EQ (optlen,8 ,"Unexpected NETLINK_LIST_MEMBERSHIPS value" );
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 224
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +11
-0+/* Trick bpf_tcp_sock() with IPPROTO_TCP */ +close (fd); + fd =socket (AF_INET ,SOCK_RAW ,IPPROTO_TCP ); +if (fd <0 ) { +log_err ("Failed to create RAW socket" ); + goto err; + } + + optlen =60 ; + err =setsockopt (fd,SOL_TCP ,TCP_SAVED_SYN , &buf, optlen); +if (err) {Ugh, I forgot to commit s/err/!err/ change.. :/
I'll include Matt's followup and extend the test
accordingly in v2.
https://lore.kernel.org/mptcp/[email protected]/
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Incomplete fix: `sol_tcp_sockopt()` remains vulnerable to slab out-of-bounds access. - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Incomplete fix: sol_tcp_sockopt() remains vulnerable to slab out-of-bounds access.
- [High] Missed vulnerability: bpf_skc_to_tcp_sock() and bpf_skc_to_tcp6_sock() suffer from the exact same type confusion.
- [High] Missed vulnerability: bpf_mptcp_sock_from_subflow() suffers from the same type confusion and causes immediate OOB reads. --
commit 72139ec7da09274112730adc692a99e89ef3eb2e
Author: Kuniyuki Iwashima <[email protected]>
bpf: tcp: Fix type confusion in bpf_tcp_sock().
This commit updates bpf_tcp_sock() to use sk_is_tcp(sk) instead of just checking if the protocol is IPPROTO_TCP. This prevents RAW sockets from causing out-of-bounds access to slab objects.
net/core/filter.c u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, · line 7479
Quoted 11 lines
net/core/filter.c +1
-1@@ -7475,7 +7475,7 @@ u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type, BPF_CALL_1(bpf_tcp_sock, struct sock *, sk) { -if (sk_fullsock (sk) && sk->sk_protocol ==IPPROTO_TCP ) +if (sk_fullsock (sk) &&sk_is_tcp (sk))return (unsigned long )sk;
Since ctx->sk evaluates to PTR_TO_SOCKET in a CGROUP_SOCKOPT program, and is directly accepted by bpf_setsockopt() and bpf_getsockopt() via ARG_PTR_TO_BTF_ID_SOCK_COMMON, does sol_tcp_sockopt() still remain vulnerable?
Looking at sol_tcp_sockopt(), it appears to only check sk->sk_protocol == IPPROTO_TCP. Can a user create a RAW socket with IPPROTO_TCP and directly pass ctx->sk to bpf_setsockopt(), bypassing bpf_tcp_sock() entirely and triggering the same slab out-of-bounds access?
Additionally, do bpf_skc_to_tcp_sock() and bpf_skc_to_tcp6_sock() suffer from the same type confusion?
They appear to continue checking sk->sk_protocol == IPPROTO_TCP without verifying sk->sk_type == SOCK_STREAM. Could a BPF program pass a RAW socket with IPPROTO_TCP to these helpers and receive a valid BTF pointer to struct tcp_sock, allowing out-of-bounds slab reads?
Finally, does bpf_mptcp_sock_from_subflow() in net/mptcp/bpf.c also need a similar update?
It checks sk->sk_protocol == IPPROTO_TCP and immediately evaluates sk_is_mptcp(sk). Since sk_is_mptcp() indiscriminately casts the socket to struct tcp_sock and accesses tcp_sk(sk)->is_mptcp, passing a RAW socket could trigger an immediate out-of-bounds slab read.
If this memory happens to be non-zero, it might proceed to call mptcp_subflow_ctx(sk)->conn, which would read a garbage pointer from out-of-bounds memory.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v1 bpf 2/2] selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
In reply to Kuniyuki IwashimaThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The selftest logic is inverted: it will fail on a correctly fixed kernel and pass on a buggy…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The selftest logic is inverted: it will fail on a correctly fixed kernel and pass on a buggy kernel (if KASAN is disabled). --
commit ba32f094566f3d6a513a47837fc65f3b355951f9
Author: Kuniyuki Iwashima <[email protected]>
selftest: bpf: Add test for bpf_tcp_sock() and RAW socket.
This patch extends sockopt_sk.c to test the behavior of bpf_tcp_sock() when called on a socket with the wrong type (SOCK_RAW).
Quoted 4 lines
tools/testing/selftests/bpf/progs/sockopt_sk.c +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/bpf/progs/sockopt_sk.c +3
-0@@ -149,6 +149,20 @@ int _setsockopt(struct bpf_sockopt *ctx)if (sk && sk->family ==AF_NETLINK ) goto out; +if (sk && sk->family ==AF_INET && sk->type ==SOCK_RAW ) { +struct bpf_tcp_sock *tp =bpf_tcp_sock (sk); +
tools/testing/selftests/bpf/progs/sockopt_sk.c int _setsockopt(struct bpf_sockopt *ctx) · line 164
Quoted 10 lines
tools/testing/selftests/bpf/progs/sockopt_sk.c +10
-0+if (tp) { +char saved_syn[60 ]; + +bpf_getsockopt (sk,SOL_TCP ,TCP_SAVED_SYN , + &saved_syn,sizeof (saved_syn)); + goto consumed; + } + + goto out; + }
On a fixed kernel where bpf_tcp_sock() correctly returns NULL for a SOCK_RAW socket, won't tp evaluate to false? This causes the program to jump to the out label instead of the consumed label.
When jumping to out, ctx->optlen is left unmodified, which tells the kernel to proceed with the native setsockopt handler instead of bypassing it.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +0
-0
[ ... ]
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +4
-0@@ -211,6 +211,21 @@ static int getsetsockopt(void) }ASSERT_EQ (optlen,8 ,"Unexpected NETLINK_LIST_MEMBERSHIPS value" ); +/* Trick bpf_tcp_sock() with IPPROTO_TCP */ +close (fd); + fd =socket (AF_INET ,SOCK_RAW ,IPPROTO_TCP ); +if (fd <0 ) {
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c static int getsetsockopt(void) · line 227
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/sockopt_sk.c +10
-0+log_err ("Failed to create RAW socket" ); + goto err; + } + + optlen =60 ; + err =setsockopt (fd,SOL_TCP ,TCP_SAVED_SYN , &buf, optlen); +if (err) { +log_err ("Unexpected setsockopt(TCP_SAVED_SYN)" ); + goto err; + }
If the BPF program falls back to the native handler (because bpf_tcp_sock() returned NULL on a fixed kernel), the kernel will process this through raw_setsockopt() and ip_setsockopt(). Since the level is SOL_TCP, doesn't ip_setsockopt() return -ENOPROTOOPT here?
This would cause setsockopt() to return -1 and err to be non-zero, meaning the test will fail unconditionally on a correctly fixed kernel.
Ironically, on a buggy kernel where bpf_tcp_sock() incorrectly returns a pointer, the BPF program hits the consumed label, sets ctx->optlen = -1 to bypass the kernel, and setsockopt() returns 0, causing the test to falsely pass.
Should this test logic be inverted to expect an error (such as -ENOPROTOOPT) to properly validate the fix?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2