sockmap: Fix UAF and broken memory accounting for UDP.
Subsystems (7)
Kuniyuki Iwashima
maintainerv4 cover sockmap: Fix UAF and broken memory accounting for UDP.
syzbot reported 3 issues in SOCKMAP for UDP.
Patch 1 - 2 fix lockless accesses to sk->sk_data_ready() and sk->sk_write_space().
Patch 3 fixes UAF in sk_msg_recvmsg().
Patch 4 - 5 consolidate sk_psock_skb_ingress_self() into sk_psock_skb_ingress() as prep.
Patch 6 fixes broken memory accounting.
Changes:
v4:
Patch 4: Don't pass gfp_flags and instead change it based on
take_ref in the next patch
Patch 5: Fix memory accounting condition from (skb->sk != sk) to
(skb->sk != sk && take_ref), the cause of selftest failure
Patch 6: Fix deadlock when requeued
v3: https://lore.kernel.org/netdev/[email protected]/
Patch 2: Use WRITE_ONCE() in udp_bpf_update_proto()
v2: https://lore.kernel.org/netdev/[email protected]/
Patch 2: Cache sk->sk_write_space in sock_wfree()
Patch 5: Keep msg->sk assignment
Patch 6: Fix build failure when CONFIG_INET=nv1: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (6):
sockmap: Annotate sk->sk_data_ready() for UDP.
sockmap: Annotate sk->sk_write_space() for UDP.
sockmap: Fix use-after-free in udp_bpf_recvmsg().
sockmap: Inline sk_psock_create_ingress_msg().
sockmap: Consolidate sk_psock_skb_ingress_self().
sockmap: Fix broken memory accounting for UDP.
include/net/udp.h | 9 ++++ net/core/skmsg.c | 100 +++++++++++++++++++++------------------------ net/core/sock.c | 8 +++- net/ipv4/udp.c | 11 ++++- net/ipv4/udp_bpf.c | 11 ++++- 5 files changed, 81 insertions(+), 58 deletions(-)
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Kuniyuki Iwashima
maintainerPATCH v4 1/6 sockmap: Annotate sk->sk_data_ready() for UDP.
syzbot reported data race of sk->sk_data_ready(). [0]
UDP fast path does not hold bh_lock_sock(), instead spin_lock_bh(&sk->sk_receive_queue.lock) is used.
Let's use WRITE_ONCE() and READ_ONCE() for sk->sk_data_ready().
Another option is to hold sk->sk_receive_queue.lock in sock_map_sk_acquire() if sk_is_udp() is true, but this is overkill and also does not work for sk->sk_write_space().
[0]:
BUG: KCSAN: data-race in __udp_enqueue_schedule_skb / sk_psock_drop
write to 0xffff88811d063048 of 8 bytes by task 23114 on cpu 0:
sk_psock_stop_verdict net/core/skmsg.c:1287 [inline] sk_psock_drop+0x12f/0x270 net/core/skmsg.c:873 sk_psock_put include/linux/skmsg.h:473 [inline] sock_map_unref+0x2a5/0x300 net/core/sock_map.c:185 __sock_map_delete net/core/sock_map.c:426 [inline] sock_map_delete_from_link net/core/sock_map.c:439 [inline] sock_map_unlink net/core/sock_map.c:1608 [inline] sock_map_remove_links+0x228/0x340 net/core/sock_map.c:1623 sock_map_close+0xa1/0x340 net/core/sock_map.c:1684 inet_release+0xcd/0xf0 net/ipv4/af_inet.c:437 __sock_release net/socket.c:662 [inline] sock_close+0x6b/0x150 net/socket.c:1455 __fput+0x29b/0x650 fs/file_table.c:468 ____fput+0x1c/0x30 fs/file_table.c:496 task_work_run+0x130/0x1a0 kernel/task_work.c:233 resume_user_mode_work include/linux/resume_user_mode.h:50 [inline] __exit_to_user_mode_loop kernel/entry/common.c:44 [inline] exit_to_user_mode_loop+0x1f7/0x6f0 kernel/entry/common.c:75 __exit_to_user_mode_prepare include/linux/irq-entry-common.h:226 [inline] syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:256 [inline] syscall_exit_to_user_mode_work include/linux/entry-common.h:159 [inline] syscall_exit_to_user_mode include/linux/entry-common.h:194 [inline] do_syscall_64+0x1d3/0x2a0 arch/x86/entry/syscall_64.c:100 entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff88811d063048 of 8 bytes by task 23117 on cpu 1:
__udp_enqueue_schedule_skb+0x6c1/0x840 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0x709/0xc20 net/ipv4/udp.c:2475 udp_queue_rcv_skb+0x20e/0x2b0 net/ipv4/udp.c:2493 __udp4_lib_mcast_deliver+0x6e8/0x790 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x96f/0x1260 net/ipv4/udp.c:2724 udp_rcv+0x4f/0x60 net/ipv4/udp.c:2911 ip_protocol_deliver_rcu+0x3f9/0x780 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x1fc/0x2f0 net/ipv4/ip_input.c:241 NF_HOOK include/linux/netfilter.h:318 [inline] ip_local_deliver+0xe8/0x1e0 net/ipv4/ip_input.c:262 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish net/ipv4/ip_input.c:584 [inline] ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x42b/0x6d0 net/ipv4/ip_input.c:644 ip_list_rcv+0x261/0x290 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x4dc/0x500 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x47d/0x5f0 net/core/dev.c:6385 netif_receive_skb_list+0x31/0x1f0 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x104c/0x1360 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x57b/0xa10 net/bpf/test_run.c:1396 bpf_prog_test_run+0x204/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x4c0/0x7b0 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x41/0x50 kernel/bpf/syscall.c:6272 x64_sys_call+0x28e1/0x3000 arch/x86/include/generated/asm/syscalls_64.h:322 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xc0/0x2a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0xffffffff847b24d0 -> 0xffffffff84673410
Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 23117 Comm: syz.8.5085 Tainted: G W syzkaller #0 PREEMPT(voluntary) Tainted: [W]=WARN Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
--- net/core/skmsg.c | 4 ++-- net/ipv4/udp.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
net/core/skmsg.c +2 -2
@@ -1296,7 +1296,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)return ; psock->saved_data_ready = sk->sk_data_ready; - sk->sk_data_ready = sk_psock_verdict_data_ready; +WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); sk->sk_write_space = sk_psock_write_space; }
@@ -1308,6 +1308,6 @@ void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)if (!psock->saved_data_ready)return ; - sk->sk_data_ready = psock->saved_data_ready; +WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); psock->saved_data_ready =NULL ; }
net/ipv4/udp.c +1 -1
@@ -1787,7 +1787,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)* using prepare_to_wait_exclusive(). */ while (nb) { -INDIRECT_CALL_1 (sk->sk_data_ready, +INDIRECT_CALL_1 (READ_ONCE (sk->sk_data_ready), sock_def_readable, sk); nb--; }
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Re: [PATCH v4 bpf/net 1/6] sockmap: Annotate sk->sk_data_ready() for UDP.
Quoted 83 lines · Kuniyuki Iwashima
syzbot reported data race of
sk->sk_data_ready(). [0]UDP fast path does not hold
bh_lock_sock(), insteadspin_lock_bh(&sk->sk_receive_queue.lock)is used.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_data_ready().Another option is to hold
sk->sk_receive_queue.lockinsock_map_sk_acquire()ifsk_is_udp()is true, but this is overkill and also does not work forsk->sk_write_space().[0]:
BUG: KCSAN: data-race in __udp_enqueue_schedule_skb / sk_psock_dropwrite to
0xffff88811d063048of 8 bytes by task 23114 on cpu 0:sk_psock_stop_verdict net/core/skmsg.c:1287 [inline] sk_psock_drop+0x12f/0x270 net/core/skmsg.c:873 sk_psock_put include/linux/skmsg.h:473 [inline] sock_map_unref+0x2a5/0x300 net/core/sock_map.c:185 __sock_map_delete net/core/sock_map.c:426 [inline] sock_map_delete_from_link net/core/sock_map.c:439 [inline] sock_map_unlink net/core/sock_map.c:1608 [inline] sock_map_remove_links+0x228/0x340 net/core/sock_map.c:1623 sock_map_close+0xa1/0x340 net/core/sock_map.c:1684 inet_release+0xcd/0xf0 net/ipv4/af_inet.c:437 __sock_release net/socket.c:662 [inline] sock_close+0x6b/0x150 net/socket.c:1455 __fput+0x29b/0x650 fs/file_table.c:468 ____fput+0x1c/0x30 fs/file_table.c:496 task_work_run+0x130/0x1a0 kernel/task_work.c:233 resume_user_mode_work include/linux/resume_user_mode.h:50 [inline] __exit_to_user_mode_loop kernel/entry/common.c:44 [inline] exit_to_user_mode_loop+0x1f7/0x6f0 kernel/entry/common.c:75 __exit_to_user_mode_prepare include/linux/irq-entry-common.h:226 [inline] syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:256 [inline] syscall_exit_to_user_mode_work include/linux/entry-common.h:159 [inline] syscall_exit_to_user_mode include/linux/entry-common.h:194 [inline] do_syscall_64+0x1d3/0x2a0 arch/x86/entry/syscall_64.c:100 entry_SYSCALL_64_after_hwframe+0x77/0x7fread to
0xffff88811d063048of 8 bytes by task 23117 on cpu 1:__udp_enqueue_schedule_skb+0x6c1/0x840 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0x709/0xc20 net/ipv4/udp.c:2475 udp_queue_rcv_skb+0x20e/0x2b0 net/ipv4/udp.c:2493 __udp4_lib_mcast_deliver+0x6e8/0x790 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x96f/0x1260 net/ipv4/udp.c:2724 udp_rcv+0x4f/0x60 net/ipv4/udp.c:2911 ip_protocol_deliver_rcu+0x3f9/0x780 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x1fc/0x2f0 net/ipv4/ip_input.c:241 NF_HOOK include/linux/netfilter.h:318 [inline] ip_local_deliver+0xe8/0x1e0 net/ipv4/ip_input.c:262 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish net/ipv4/ip_input.c:584 [inline] ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x42b/0x6d0 net/ipv4/ip_input.c:644 ip_list_rcv+0x261/0x290 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x4dc/0x500 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x47d/0x5f0 net/core/dev.c:6385 netif_receive_skb_list+0x31/0x1f0 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x104c/0x1360 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x57b/0xa10 net/bpf/test_run.c:1396 bpf_prog_test_run+0x204/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x4c0/0x7b0 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x41/0x50 kernel/bpf/syscall.c:6272 x64_sys_call+0x28e1/0x3000 arch/x86/include/generated/asm/syscalls_64.h:322 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xc0/0x2a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7fvalue changed:
0xffffffff847b24d0->0xffffffff84673410Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 23117 Comm: syz.8.5085 Tainted: G W syzkaller #0 PREEMPT(voluntary) Tainted: [W]=WARN Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025
Quoted 5 lines
Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
Sorry for the delay. Got caught up in skb metadata stuff...
Re: [PATCH v4 bpf/net 1/6] sockmap: Annotate sk->sk_data_ready() for UDP.
In reply to Kuniyuki IwashimaQuoted 129 lines · Kuniyuki Iwashima
syzbot reported data race of
sk->sk_data_ready(). [0]UDP fast path does not hold
bh_lock_sock(), insteadspin_lock_bh(&sk->sk_receive_queue.lock)is used.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_data_ready().Another option is to hold
sk->sk_receive_queue.lockinsock_map_sk_acquire()ifsk_is_udp()is true, but this is overkill and also does not work forsk->sk_write_space().[0]:
BUG: KCSAN: data-race in __udp_enqueue_schedule_skb / sk_psock_dropwrite to
0xffff88811d063048of 8 bytes by task 23114 on cpu 0:sk_psock_stop_verdict net/core/skmsg.c:1287 [inline] sk_psock_drop+0x12f/0x270 net/core/skmsg.c:873 sk_psock_put include/linux/skmsg.h:473 [inline] sock_map_unref+0x2a5/0x300 net/core/sock_map.c:185 __sock_map_delete net/core/sock_map.c:426 [inline] sock_map_delete_from_link net/core/sock_map.c:439 [inline] sock_map_unlink net/core/sock_map.c:1608 [inline] sock_map_remove_links+0x228/0x340 net/core/sock_map.c:1623 sock_map_close+0xa1/0x340 net/core/sock_map.c:1684 inet_release+0xcd/0xf0 net/ipv4/af_inet.c:437 __sock_release net/socket.c:662 [inline] sock_close+0x6b/0x150 net/socket.c:1455 __fput+0x29b/0x650 fs/file_table.c:468 ____fput+0x1c/0x30 fs/file_table.c:496 task_work_run+0x130/0x1a0 kernel/task_work.c:233 resume_user_mode_work include/linux/resume_user_mode.h:50 [inline] __exit_to_user_mode_loop kernel/entry/common.c:44 [inline] exit_to_user_mode_loop+0x1f7/0x6f0 kernel/entry/common.c:75 __exit_to_user_mode_prepare include/linux/irq-entry-common.h:226 [inline] syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:256 [inline] syscall_exit_to_user_mode_work include/linux/entry-common.h:159 [inline] syscall_exit_to_user_mode include/linux/entry-common.h:194 [inline] do_syscall_64+0x1d3/0x2a0 arch/x86/entry/syscall_64.c:100 entry_SYSCALL_64_after_hwframe+0x77/0x7fread to
0xffff88811d063048of 8 bytes by task 23117 on cpu 1:__udp_enqueue_schedule_skb+0x6c1/0x840 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0x709/0xc20 net/ipv4/udp.c:2475 udp_queue_rcv_skb+0x20e/0x2b0 net/ipv4/udp.c:2493 __udp4_lib_mcast_deliver+0x6e8/0x790 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x96f/0x1260 net/ipv4/udp.c:2724 udp_rcv+0x4f/0x60 net/ipv4/udp.c:2911 ip_protocol_deliver_rcu+0x3f9/0x780 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x1fc/0x2f0 net/ipv4/ip_input.c:241 NF_HOOK include/linux/netfilter.h:318 [inline] ip_local_deliver+0xe8/0x1e0 net/ipv4/ip_input.c:262 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish net/ipv4/ip_input.c:584 [inline] ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x42b/0x6d0 net/ipv4/ip_input.c:644 ip_list_rcv+0x261/0x290 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x4dc/0x500 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x47d/0x5f0 net/core/dev.c:6385 netif_receive_skb_list+0x31/0x1f0 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x104c/0x1360 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x57b/0xa10 net/bpf/test_run.c:1396 bpf_prog_test_run+0x204/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x4c0/0x7b0 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x41/0x50 kernel/bpf/syscall.c:6272 x64_sys_call+0x28e1/0x3000 arch/x86/include/generated/asm/syscalls_64.h:322 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xc0/0x2a0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7fvalue changed:
0xffffffff847b24d0->0xffffffff84673410Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 23117 Comm: syz.8.5085 Tainted: G W syzkaller #0 PREEMPT(voluntary) Tainted: [W]=WARN Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/25/2025Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/core/skmsg.c | 4 ++-- net/ipv4/udp.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)net/core/skmsg.c +2
-2@@ -1296,7 +1296,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)return ; psock->saved_data_ready = sk->sk_data_ready; - sk->sk_data_ready = sk_psock_verdict_data_ready; +WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); sk->sk_write_space = sk_psock_write_space; }@@ -1308,6 +1308,6 @@ void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)if (!psock->saved_data_ready)return ; - sk->sk_data_ready = psock->saved_data_ready; +WRITE_ONCE (sk->sk_data_ready, psock->saved_data_ready); psock->saved_data_ready =NULL ; }net/ipv4/udp.c +1
-1@@ -1787,7 +1787,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)* using prepare_to_wait_exclusive(). */ while (nb) { -INDIRECT_CALL_1 (sk->sk_data_ready, +INDIRECT_CALL_1 (READ_ONCE (sk->sk_data_ready), sock_def_readable, sk); nb--; }Signature · 1 line
2.53.0.371.g1d285c8824-goog
Kuniyuki Iwashima
maintainerPATCH v4 2/6 sockmap: Annotate sk->sk_write_space() for UDP.
In reply to Kuniyuki IwashimaUDP TX skb->destructor() is sock_wfree(), and UDP only holds lock_sock() for UDP_CORK / MSG_MORE sendmsg().
Otherwise, sk->sk_write_space() is read locklessly.
Let's use WRITE_ONCE() and READ_ONCE() for sk->sk_write_space().
---
v3: Use WRITE_ONCE() in udp_bpf_update_proto()
v2: Cache sk->sk_write_space in sock_wfree()
--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)
net/core/skmsg.c +1 -1
@@ -1297,7 +1297,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) psock->saved_data_ready = sk->sk_data_ready;WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); - sk->sk_write_space = sk_psock_write_space; +WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }void sk_psock_stop_verdict (struct sock *sk,struct sk_psock *psock)
net/core/sock.c +6 -2
@@ -2673,8 +2673,12 @@ void sock_wfree(struct sk_buff *skb)int old;if (!sock_flag (sk,SOCK_USE_WRITE_QUEUE )) { +void (*sk_write_space)(struct sock *sk); + + sk_write_space =READ_ONCE (sk->sk_write_space); +if (sock_flag (sk,SOCK_RCU_FREE ) && - sk->sk_write_space == sock_def_write_space) { + sk_write_space == sock_def_write_space) {rcu_read_lock (); free =__refcount_sub_and_test (len, &sk->sk_wmem_alloc, &old);
@@ -2690,7 +2694,7 @@ void sock_wfree(struct sk_buff *skb)* after sk_write_space() call */ WARN_ON (refcount_sub_and_test (len -1 , &sk->sk_wmem_alloc)); - sk->sk_write_space (sk); +sk_write_space (sk); len =1 ; }/*
net/ipv4/udp_bpf.c +1 -1
@@ -158,7 +158,7 @@ int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)int family = sk->sk_family ==AF_INET ?UDP_BPF_IPV4 :UDP_BPF_IPV6 ;if (restore) { - sk->sk_write_space = psock->saved_write_space; +WRITE_ONCE (sk->sk_write_space, psock->saved_write_space);sock_replace_proto (sk, psock->sk_proto);return 0 ; }
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
Quoted 23 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0
net/core/skmsg.c void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) · line 1301
Quoted 6 lines
net/core/skmsg.c +1
-1@@ -1297,7 +1297,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) psock->saved_data_ready = sk->sk_data_ready;WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); - sk->sk_write_space = sk_psock_write_space; +WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }
I noticed that Patch 1 and 2 were submitted earlier and are on bpf tree. Given that Eric's commit (which you reviewed) is now in net.git, these two patches should probably be dropped in the next version, right? Just confirming.
Quoted 44 lines
net/core/skmsg.c +0
-0void sk_psock_stop_verdict (struct sock *sk,struct sk_psock *psock)net/core/sock.c +6
-2@@ -2673,8 +2673,12 @@ void sock_wfree(struct sk_buff *skb)int old;if (!sock_flag (sk,SOCK_USE_WRITE_QUEUE )) { +void (*sk_write_space)(struct sock *sk); + + sk_write_space =READ_ONCE (sk->sk_write_space); +if (sock_flag (sk,SOCK_RCU_FREE ) && - sk->sk_write_space == sock_def_write_space) { + sk_write_space == sock_def_write_space) {rcu_read_lock (); free =__refcount_sub_and_test (len, &sk->sk_wmem_alloc, &old);@@ -2690,7 +2694,7 @@ void sock_wfree(struct sk_buff *skb)* after sk_write_space() call */ WARN_ON (refcount_sub_and_test (len -1 , &sk->sk_wmem_alloc)); - sk->sk_write_space (sk); +sk_write_space (sk); len =1 ; }/* net/ipv4/udp_bpf.c +1
-1@@ -158,7 +158,7 @@ int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)int family = sk->sk_family ==AF_INET ?UDP_BPF_IPV4 :UDP_BPF_IPV6 ;if (restore) { - sk->sk_write_space = psock->saved_write_space; +WRITE_ONCE (sk->sk_write_space, psock->saved_write_space);sock_replace_proto (sk, psock->sk_proto);return 0 ; }Signature · 1 line
2.53.0.371.g1d285c8824-goog
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
Quoted 26 lines · Jiayuan Chen
Quoted 24 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0@@ -1297,7 +1297,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
Quoted 9 lines
net/core/skmsg.c void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) · line 1301
Quoted 5 lines
net/core/skmsg.c +1
-1psock->saved_data_ready = sk->sk_data_ready;WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); - sk->sk_write_space = sk_psock_write_space; +WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }I noticed that Patch 1 and 2 were submitted earlier and are on bpf tree. Given that Eric's commit (which you reviewed) is now in net.git, these two patches should probably be dropped in the next version, right? Just confirming.
I was thinking git can just handle them well, and even the patch 2 has a delta in sock_wfree().
Quoted 47 lines
Quoted 44 lines
net/core/skmsg.c +0
-0void sk_psock_stop_verdict (struct sock *sk,struct sk_psock *psock)net/core/sock.c +6
-2@@ -2673,8 +2673,12 @@ void sock_wfree(struct sk_buff *skb)int old;if (!sock_flag (sk,SOCK_USE_WRITE_QUEUE )) { +void (*sk_write_space)(struct sock *sk); + + sk_write_space =READ_ONCE (sk->sk_write_space); +if (sock_flag (sk,SOCK_RCU_FREE ) && - sk->sk_write_space == sock_def_write_space) { + sk_write_space == sock_def_write_space) {rcu_read_lock (); free =__refcount_sub_and_test (len, &sk->sk_wmem_alloc, &old);@@ -2690,7 +2694,7 @@ void sock_wfree(struct sk_buff *skb)* after sk_write_space() call */ WARN_ON (refcount_sub_and_test (len -1 , &sk->sk_wmem_alloc)); - sk->sk_write_space (sk); +sk_write_space (sk); len =1 ; }/* net/ipv4/udp_bpf.c +1
-2@@ -158,7 +158,7 @@ int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)int family = sk->sk_family ==AF_INET ?UDP_BPF_IPV4 :UDP_BPF_IPV6 ;if (restore) { - sk->sk_write_space = psock->saved_write_space; +WRITE_ONCE (sk->sk_write_space, psock->saved_write_space);sock_replace_proto (sk, psock->sk_proto);return 0 ; } --2.53.0.371.g1d285c8824-goog
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
Quoted 37 lines · Kuniyuki Iwashima
Quoted 26 lines · Jiayuan Chen
Quoted 24 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0@@ -1297,7 +1297,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)Quoted 9 lines
net/core/skmsg.c void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) · line 1301
Quoted 5 lines
net/core/skmsg.c +1
-1psock->saved_data_ready = sk->sk_data_ready;WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); - sk->sk_write_space = sk_psock_write_space; +WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }I noticed that Patch 1 and 2 were submitted earlier and are on bpf tree. Given that Eric's commit (which you reviewed) is now in net.git, these two patches should probably be dropped in the next version, right? Just confirming.
Quoted 2 lines
I was thinking git can just handle them well, and even the patch 2 has a delta in
sock_wfree().
At least for patch 1, I am not sure git can handle it well without human intervention because the sk_write_space is backward from Eric.
For the missing change in sock_wfree (?), it probably makes sense to create a separate patch on top of Eric's change and target net. My understanding is patch 3-6 can be on its own without patch 1+2.
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
Quoted 44 lines · Martin KaFai Lau
Quoted 37 lines · Kuniyuki Iwashima
Quoted 26 lines · Jiayuan Chen
Quoted 24 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0@@ -1297,7 +1297,7 @@ void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)Quoted 9 lines
net/core/skmsg.c void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock) · line 1301
Quoted 5 lines
net/core/skmsg.c +1
-1psock->saved_data_ready = sk->sk_data_ready;WRITE_ONCE (sk->sk_data_ready, sk_psock_verdict_data_ready); - sk->sk_write_space = sk_psock_write_space; +WRITE_ONCE (sk->sk_write_space, sk_psock_write_space); }I noticed that Patch 1 and 2 were submitted earlier and are on bpf tree. Given that Eric's commit (which you reviewed) is now in net.git, these two patches should probably be dropped in the next version, right? Just confirming.
Quoted 2 lines
I was thinking git can just handle them well, and even the patch 2 has a delta in
sock_wfree().
Quoted 6 lines
At least for patch 1, I am not sure git can handle it well without human intervention because the
sk_write_spaceis backward from Eric.For the missing change in
sock_wfree(?), it probably makes sense to create a separate patch on top of Eric's change and target net. My understanding is patch 3-6 can be on its own without patch 1+2.
Sure, I posted 1&2 in the same series as I thought they are sockmap stuff, but I will post 3~6 separately.
Re: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
In reply to Kuniyuki IwashimaQuoted 9 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>
Quoted 11 lines
---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()--- net/core/skmsg.c | 2 +- net/core/sock.c | 8 ++++++-- net/ipv4/udp_bpf.c | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-)net/core/skmsg.c +0
-0
[...]
Re: [PATCH v4 bpf/net 2/6] sockmap: Annotate sk->sk_write_space() for UDP.
In reply to Kuniyuki IwashimaQuoted 5 lines · Kuniyuki Iwashima
UDP TX
skb->destructor()issock_wfree(), and UDP only holdslock_sock()forUDP_CORK/MSG_MOREsendmsg().Otherwise,
sk->sk_write_space()is read locklessly.
Quoted 8 lines
Let's use
WRITE_ONCE()andREAD_ONCE()forsk->sk_write_space().Fixes 7b98cd42b049 ("bpf: sockmap: Add UDP support")Signed-off-by Kuniyuki Iwashima <[email protected]>---
v3: UseWRITE_ONCE()inudp_bpf_update_proto()
v2: Cachesk->sk_write_spaceinsock_wfree()
---
I agree with Jiayuan that it would be less confusing to drop the overlapping parts but Eric's patch went through net tree and this is targeted for bpf tree (?). I'm actually not sure what exactly the bpf/net target signifies.
So if it makes everyone's life easier:
Kuniyuki Iwashima
maintainerPATCH v4 3/6 sockmap: Fix use-after-free in udp_bpf_recvmsg().
In reply to Kuniyuki Iwashimasyzbot reported use-after-free of struct sk_msg in sk_msg_recvmsg(). [0]
sk_msg_recvmsg() peeks sk_msg from psock->ingress_msg under a lock, but its processing is lockless.
Thus, sk_msg_recvmsg() must be serialised by callers, otherwise multiple threads could touch the same sk_msg.
For example, TCP uses lock_sock(), and AF_UNIX uses unix_sk(sk)->iolock.
Initially, udp_bpf_recvmsg() had used lock_sock(), but the cited commit accidentally removed it.
Let's serialise sk_msg_recvmsg() with lock_sock() in udp_bpf_recvmsg().
Note that holding spin_lock_bh(&sk->sk_receive_queue.lock) is not an option due to copy_page_to_iter() in sk_msg_recvmsg().
[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0xba/0x230 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fb319f9aeb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fb31ad97028 EFLAGS: 00000246 ORIG_RAX: 000000000000012b RAX: ffffffffffffffda RBX: 00007fb31a216090 RCX: 00007fb319f9aeb9 RDX: 0000000000000001 RSI: 0000200000000400 RDI: 0000000000000004 RBP: 00007fb31a008c1f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000040000021 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fb31a216128 R14: 00007fb31a216090 R15: 00007ffe21dd0a98 </TASK> Allocated by task 6019: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 poison_kmalloc_redzone mm/kasan/common.c:398 [inline] __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415 kasan_kmalloc include/linux/kasan.h:263 [inline] __kmalloc_cache_noprof+0x3d1/0x6e0 mm/slub.c:5780 kmalloc_noprof include/linux/slab.h:957 [inline] kzalloc_noprof include/linux/slab.h:1094 [inline] alloc_sk_msg net/core/skmsg.c:510 [inline] sk_psock_skb_ingress_self+0x60/0x350 net/core/skmsg.c:612 sk_psock_verdict_apply net/core/skmsg.c:1038 [inline] sk_psock_verdict_recv+0x7d9/0x8d0 net/core/skmsg.c:1236 udp_read_skb+0x73e/0x7e0 net/ipv4/udp.c:2045 sk_psock_verdict_data_ready+0x12d/0x550 net/core/skmsg.c:1257 __udp_enqueue_schedule_skb+0xc54/0x10b0 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0xac5/0x19c0 net/ipv4/udp.c:2475 __udp4_lib_mcast_deliver+0xc06/0xcf0 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x10f6/0x2620 net/ipv4/udp.c:2724 ip_protocol_deliver_rcu+0x282/0x440 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x3bb/0x6f0 net/ipv4/ip_input.c:241 NF_HOOK+0x336/0x3c0 include/linux/netfilter.h:318 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish+0x221/0x2a0 net/ipv4/ip_input.c:584 ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x5c6/0xa70 net/ipv4/ip_input.c:644 ip_list_rcv+0x3f1/0x450 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x7e5/0x810 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x995/0xcf0 net/core/dev.c:6385 netif_receive_skb_list+0x54/0x410 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x1946/0x1cf0 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x81c/0x1160 net/bpf/test_run.c:1396 bpf_prog_test_run+0x2c7/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x5cb/0x920 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:6272 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 6021: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:584 poison_slab_object mm/kasan/common.c:253 [inline] __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285 kasan_slab_free include/linux/kasan.h:235 [inline] slab_free_hook mm/slub.c:2540 [inline] slab_free mm/slub.c:6674 [inline] kfree+0x1be/0x650 mm/slub.c:6882 kfree_sk_msg include/linux/skmsg.h:385 [inline] sk_msg_recvmsg+0xaa8/0xc30 net/core/skmsg.c:483 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
--- net/ipv4/udp_bpf.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/ipv4/udp_bpf.c +9 -0
@@ -52,7 +52,9 @@ static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock,sk_set_bit (SOCKWQ_ASYNC_WAITDATA , sk); ret =udp_msg_has_data (sk, psock);if (!ret) { +release_sock (sk);wait_woken (&wait,TASK_INTERRUPTIBLE , timeo); +lock_sock (sk); ret =udp_msg_has_data (sk, psock); }sk_clear_bit (SOCKWQ_ASYNC_WAITDATA , sk);
@@ -81,6 +83,7 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, goto out; } +lock_sock (sk); msg_bytes_ready: copied =sk_msg_recvmsg (sk, psock, msg, len, flags);if (!copied) {
@@ -92,11 +95,17 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,if (data) {if (psock_has_data (psock)) goto msg_bytes_ready; + +release_sock (sk); + ret =sk_udp_recvmsg (sk, msg, len, flags, addr_len); goto out; } copied = -EAGAIN ; } + +release_sock (sk); + ret = copied; out:sk_psock_put (sk, psock);
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 3/6] sockmap: Fix use-after-free in udp_bpf_recvmsg().
Quoted 31 lines · Kuniyuki Iwashima
syzbot reported use-after-free of struct
sk_msginsk_msg_recvmsg(). [0]
sk_msg_recvmsg()peekssk_msgfrompsock->ingress_msgunder a lock, but its processing is lockless.Thus,
sk_msg_recvmsg()must be serialised by callers, otherwise multiple threads could touch the samesk_msg.For example, TCP uses
lock_sock(), andAF_UNIXuses unix_sk(sk)->iolock.Initially,
udp_bpf_recvmsg()had usedlock_sock(), but the cited commit accidentally removed it.Let's serialise
sk_msg_recvmsg()withlock_sock()inudp_bpf_recvmsg().Note that holding
spin_lock_bh(&sk->sk_receive_queue.lock)is not an option due tocopy_page_to_iter()insk_msg_recvmsg().[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
[...]
Quoted 51 lines
Fixes 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/ipv4/udp_bpf.c | 9 +++++++++ 1 file changed, 9 insertions(+)net/ipv4/udp_bpf.c +9
-0@@ -52,7 +52,9 @@ static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock,sk_set_bit (SOCKWQ_ASYNC_WAITDATA , sk); ret =udp_msg_has_data (sk, psock);if (!ret) { +release_sock (sk);wait_woken (&wait,TASK_INTERRUPTIBLE , timeo); +lock_sock (sk); ret =udp_msg_has_data (sk, psock); }sk_clear_bit (SOCKWQ_ASYNC_WAITDATA , sk);@@ -81,6 +83,7 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, goto out; } +lock_sock (sk); msg_bytes_ready: copied =sk_msg_recvmsg (sk, psock, msg, len, flags);if (!copied) {@@ -92,11 +95,17 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,if (data) {if (psock_has_data (psock)) goto msg_bytes_ready; + +release_sock (sk); + ret =sk_udp_recvmsg (sk, msg, len, flags, addr_len); goto out; } copied = -EAGAIN ; } + +release_sock (sk); + ret = copied; out:sk_psock_put (sk, psock);Signature · 1 line
2.53.0.371.g1d285c8824-goog
LGTM.
One minor observation for a possible follow-up:psock_has_data() is checked before lock_sock(), which leaves a TOCTOU window.
If the BPF verdict path consumes the skb from the UDP receive queue and redirects it to psock->ingress_msg between the check and sk_udp_recvmsg(), the thread may block in __skb_recv_udp() while data is already available in the psock.
Moving lock_sock() before the psock_has_data() check (as the original code did before 9f2470fbc4cb) would narrow this window, similar to how __tcp_bpf_recvmsg() acquires lock_sock() upfront. But that can be a separate patch.
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 3/6] sockmap: Fix use-after-free in udp_bpf_recvmsg().
Quoted 89 lines · Jiayuan Chen
Quoted 31 lines · Kuniyuki Iwashima
syzbot reported use-after-free of struct
sk_msginsk_msg_recvmsg(). [0]
sk_msg_recvmsg()peekssk_msgfrompsock->ingress_msgunder a lock, but its processing is lockless.Thus,
sk_msg_recvmsg()must be serialised by callers, otherwise multiple threads could touch the samesk_msg.For example, TCP uses
lock_sock(), andAF_UNIXuses unix_sk(sk)->iolock.Initially,
udp_bpf_recvmsg()had usedlock_sock(), but the cited commit accidentally removed it.Let's serialise
sk_msg_recvmsg()withlock_sock()inudp_bpf_recvmsg().Note that holding
spin_lock_bh(&sk->sk_receive_queue.lock)is not an option due tocopy_page_to_iter()insk_msg_recvmsg().[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f[...]
Quoted 49 lines
Fixes 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/ipv4/udp_bpf.c | 9 +++++++++ 1 file changed, 9 insertions(+)net/ipv4/udp_bpf.c +9
-1@@ -52,7 +52,9 @@ static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock,sk_set_bit (SOCKWQ_ASYNC_WAITDATA , sk); ret =udp_msg_has_data (sk, psock);if (!ret) { +release_sock (sk);wait_woken (&wait,TASK_INTERRUPTIBLE , timeo); +lock_sock (sk); ret =udp_msg_has_data (sk, psock); }sk_clear_bit (SOCKWQ_ASYNC_WAITDATA , sk);@@ -81,6 +83,7 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, goto out; } +lock_sock (sk); msg_bytes_ready: copied =sk_msg_recvmsg (sk, psock, msg, len, flags);if (!copied) {@@ -92,11 +95,17 @@ static int udp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,if (data) {if (psock_has_data (psock)) goto msg_bytes_ready; + +release_sock (sk); + ret =sk_udp_recvmsg (sk, msg, len, flags, addr_len); goto out; } copied = -EAGAIN ; } + +release_sock (sk); + ret = copied; out:sk_psock_put (sk, psock); --Quoted 2 lines
2.53.0.371.g1d285c8824-goog
LGTM.
Quoted 2 lines
One minor observation for a possible follow-up:
psock_has_data()is checked beforelock_sock(), which leaves a TOCTOU window.
This is intentional. We don't want to hold lock_sock() just to check if the queue is empty, which can be done locklessly. Otherwise, you will introduce an unnecessary lock dance for every sk_udp_recvmsg() in case the psock queue is empty, which is too bad.
Quoted 4 lines
If the BPF verdict path consumes the skb from the UDP receive queue and redirects it to
psock->ingress_msgbetween the check andsk_udp_recvmsg(), the thread may block in__skb_recv_udp()while data is already available in the psock.
And this is just a matter of timing. If it's already empty before the first check, the thread is anyway blocked in sk_udp_recvmsg().
Quoted 5 lines
Moving
lock_sock()before thepsock_has_data()check (as the original code did before9f2470fbc4cb) would narrow this window, similar to how__tcp_bpf_recvmsg()acquireslock_sock()upfront. But that can be a separate patch.
Re: [PATCH v4 bpf/net 3/6] sockmap: Fix use-after-free in udp_bpf_recvmsg().
In reply to Kuniyuki IwashimaQuoted 128 lines · Kuniyuki Iwashima
syzbot reported use-after-free of struct
sk_msginsk_msg_recvmsg(). [0]
sk_msg_recvmsg()peekssk_msgfrompsock->ingress_msgunder a lock, but its processing is lockless.Thus,
sk_msg_recvmsg()must be serialised by callers, otherwise multiple threads could touch the samesk_msg.For example, TCP uses
lock_sock(), andAF_UNIXuses unix_sk(sk)->iolock.Initially,
udp_bpf_recvmsg()had usedlock_sock(), but the cited commit accidentally removed it.Let's serialise
sk_msg_recvmsg()withlock_sock()inudp_bpf_recvmsg().Note that holding
spin_lock_bh(&sk->sk_receive_queue.lock)is not an option due tocopy_page_to_iter()insk_msg_recvmsg().[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0xba/0x230 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fb319f9aeb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fb31ad97028 EFLAGS: 00000246 ORIG_RAX: 000000000000012b RAX: ffffffffffffffda RBX: 00007fb31a216090 RCX: 00007fb319f9aeb9 RDX: 0000000000000001 RSI: 0000200000000400 RDI: 0000000000000004 RBP: 00007fb31a008c1f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000040000021 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fb31a216128 R14: 00007fb31a216090 R15: 00007ffe21dd0a98 </TASK> Allocated by task 6019: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 poison_kmalloc_redzone mm/kasan/common.c:398 [inline] __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415 kasan_kmalloc include/linux/kasan.h:263 [inline] __kmalloc_cache_noprof+0x3d1/0x6e0 mm/slub.c:5780 kmalloc_noprof include/linux/slab.h:957 [inline] kzalloc_noprof include/linux/slab.h:1094 [inline] alloc_sk_msg net/core/skmsg.c:510 [inline] sk_psock_skb_ingress_self+0x60/0x350 net/core/skmsg.c:612 sk_psock_verdict_apply net/core/skmsg.c:1038 [inline] sk_psock_verdict_recv+0x7d9/0x8d0 net/core/skmsg.c:1236 udp_read_skb+0x73e/0x7e0 net/ipv4/udp.c:2045 sk_psock_verdict_data_ready+0x12d/0x550 net/core/skmsg.c:1257 __udp_enqueue_schedule_skb+0xc54/0x10b0 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0xac5/0x19c0 net/ipv4/udp.c:2475 __udp4_lib_mcast_deliver+0xc06/0xcf0 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x10f6/0x2620 net/ipv4/udp.c:2724 ip_protocol_deliver_rcu+0x282/0x440 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x3bb/0x6f0 net/ipv4/ip_input.c:241 NF_HOOK+0x336/0x3c0 include/linux/netfilter.h:318 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish+0x221/0x2a0 net/ipv4/ip_input.c:584 ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x5c6/0xa70 net/ipv4/ip_input.c:644 ip_list_rcv+0x3f1/0x450 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x7e5/0x810 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x995/0xcf0 net/core/dev.c:6385 netif_receive_skb_list+0x54/0x410 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x1946/0x1cf0 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x81c/0x1160 net/bpf/test_run.c:1396 bpf_prog_test_run+0x2c7/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x5cb/0x920 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:6272 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 6021: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:584 poison_slab_object mm/kasan/common.c:253 [inline] __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285 kasan_slab_free include/linux/kasan.h:235 [inline] slab_free_hook mm/slub.c:2540 [inline] slab_free mm/slub.c:6674 [inline] kfree+0x1be/0x650 mm/slub.c:6882 kfree_sk_msg include/linux/skmsg.h:385 [inline] sk_msg_recvmsg+0xaa8/0xc30 net/core/skmsg.c:483 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 4 lines
Fixes 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>
Quoted 9 lines
--- net/ipv4/udp_bpf.c | 9 +++++++++ 1 file changed, 9 insertions(+)net/ipv4/udp_bpf.c +0
-0@@ -52,7 +52,9 @@ static int udp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
[...]
Re: [PATCH v4 bpf/net 3/6] sockmap: Fix use-after-free in udp_bpf_recvmsg().
In reply to Kuniyuki IwashimaQuoted 5 lines · Kuniyuki Iwashima
syzbot reported use-after-free of struct
sk_msginsk_msg_recvmsg(). [0]
sk_msg_recvmsg()peekssk_msgfrompsock->ingress_msgunder a lock, but its processing is lockless.
Quoted 7 lines
Thus,
sk_msg_recvmsg()must be serialised by callers, otherwise multiple threads could touch the samesk_msg.For example, TCP uses
lock_sock(), andAF_UNIXuses unix_sk(sk)->iolock.Initially,
udp_bpf_recvmsg()had usedlock_sock(), but the cited commit accidentally removed it.
FWIW, it doesn't sound like commit 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy") removed it by accident. The commit message calls it out explicitly:
Also, UDP does not lock the sock during BH Rx path, it makes no sense for its ->recvmsg() to lock the sock. It is always possible for ->recvmsg() to be called before packets actually arrive in the receive queue, we just use best effort to make it accurate here.
Looks like we just didn't understand the consequences at that time.
Quoted 116 lines
Let's serialise
sk_msg_recvmsg()withlock_sock()inudp_bpf_recvmsg().Note that holding
spin_lock_bh(&sk->sk_receive_queue.lock)is not an option due tocopy_page_to_iter()insk_msg_recvmsg().[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0xba/0x230 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fb319f9aeb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fb31ad97028 EFLAGS: 00000246 ORIG_RAX: 000000000000012b RAX: ffffffffffffffda RBX: 00007fb31a216090 RCX: 00007fb319f9aeb9 RDX: 0000000000000001 RSI: 0000200000000400 RDI: 0000000000000004 RBP: 00007fb31a008c1f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000040000021 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fb31a216128 R14: 00007fb31a216090 R15: 00007ffe21dd0a98 </TASK> Allocated by task 6019: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 poison_kmalloc_redzone mm/kasan/common.c:398 [inline] __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415 kasan_kmalloc include/linux/kasan.h:263 [inline] __kmalloc_cache_noprof+0x3d1/0x6e0 mm/slub.c:5780 kmalloc_noprof include/linux/slab.h:957 [inline] kzalloc_noprof include/linux/slab.h:1094 [inline] alloc_sk_msg net/core/skmsg.c:510 [inline] sk_psock_skb_ingress_self+0x60/0x350 net/core/skmsg.c:612 sk_psock_verdict_apply net/core/skmsg.c:1038 [inline] sk_psock_verdict_recv+0x7d9/0x8d0 net/core/skmsg.c:1236 udp_read_skb+0x73e/0x7e0 net/ipv4/udp.c:2045 sk_psock_verdict_data_ready+0x12d/0x550 net/core/skmsg.c:1257 __udp_enqueue_schedule_skb+0xc54/0x10b0 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0xac5/0x19c0 net/ipv4/udp.c:2475 __udp4_lib_mcast_deliver+0xc06/0xcf0 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x10f6/0x2620 net/ipv4/udp.c:2724 ip_protocol_deliver_rcu+0x282/0x440 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x3bb/0x6f0 net/ipv4/ip_input.c:241 NF_HOOK+0x336/0x3c0 include/linux/netfilter.h:318 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish+0x221/0x2a0 net/ipv4/ip_input.c:584 ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x5c6/0xa70 net/ipv4/ip_input.c:644 ip_list_rcv+0x3f1/0x450 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x7e5/0x810 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x995/0xcf0 net/core/dev.c:6385 netif_receive_skb_list+0x54/0x410 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x1946/0x1cf0 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x81c/0x1160 net/bpf/test_run.c:1396 bpf_prog_test_run+0x2c7/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x5cb/0x920 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:6272 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 6021: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:584 poison_slab_object mm/kasan/common.c:253 [inline] __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285 kasan_slab_free include/linux/kasan.h:235 [inline] slab_free_hook mm/slub.c:2540 [inline] slab_free mm/slub.c:6674 [inline] kfree+0x1be/0x650 mm/slub.c:6882 kfree_sk_msg include/linux/skmsg.h:385 [inline] sk_msg_recvmsg+0xaa8/0xc30 net/core/skmsg.c:483 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 5 lines
Fixes 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 3/6] sockmap: Fix use-after-free in udp_bpf_recvmsg().
Quoted 19 lines · Jakub Sitnicki
Quoted 5 lines · Kuniyuki Iwashima
syzbot reported use-after-free of struct
sk_msginsk_msg_recvmsg(). [0]
sk_msg_recvmsg()peekssk_msgfrompsock->ingress_msgunder a lock, but its processing is lockless.Quoted 7 lines
Thus,
sk_msg_recvmsg()must be serialised by callers, otherwise multiple threads could touch the samesk_msg.For example, TCP uses
lock_sock(), andAF_UNIXuses unix_sk(sk)->iolock.Initially,
udp_bpf_recvmsg()had usedlock_sock(), but the cited commit accidentally removed it.FWIW, it doesn't sound like commit
9f2470fbc4cb("skmsg: Improveudp_bpf_recvmsg()accuracy") removed it by accident. The commit message calls it out explicitly:
Quoted 7 lines
Also, UDP does not lock the sock during BH Rx path, it makes no sense for its ->recvmsg() to lock the sock. It is always possible for ->recvmsg() to be called before packets actually arrive in the receive queue, we just use best effort to make it accurate here.
Looks like we just didn't understand the consequences at that time.
I meant that's the accident :)
Quoted 1 line
by mistake
https://dictionary.cambridge.org/us/dictionary/english/accidentally
Quoted 117 lines
Quoted 117 lines
Let's serialise
sk_msg_recvmsg()withlock_sock()inudp_bpf_recvmsg().Note that holding
spin_lock_bh(&sk->sk_receive_queue.lock)is not an option due tocopy_page_to_iter()insk_msg_recvmsg().[0]:
BUG: KASAN: slab-use-after-free in sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 Read of size 4 at addr ffff88814cdcf000 by task syz.0.24/6020 CPU: 1 UID: 0 PID: 6020 Comm: syz.0.24 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 Call Trace: <TASK> dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0xba/0x230 mm/kasan/report.c:482 kasan_report+0x117/0x150 mm/kasan/report.c:595 sk_msg_recvmsg+0xb54/0xc30 net/core/skmsg.c:428 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fb319f9aeb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007fb31ad97028 EFLAGS: 00000246 ORIG_RAX: 000000000000012b RAX: ffffffffffffffda RBX: 00007fb31a216090 RCX: 00007fb319f9aeb9 RDX: 0000000000000001 RSI: 0000200000000400 RDI: 0000000000000004 RBP: 00007fb31a008c1f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000040000021 R11: 0000000000000246 R12: 0000000000000000 R13: 00007fb31a216128 R14: 00007fb31a216090 R15: 00007ffe21dd0a98 </TASK> Allocated by task 6019: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 poison_kmalloc_redzone mm/kasan/common.c:398 [inline] __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:415 kasan_kmalloc include/linux/kasan.h:263 [inline] __kmalloc_cache_noprof+0x3d1/0x6e0 mm/slub.c:5780 kmalloc_noprof include/linux/slab.h:957 [inline] kzalloc_noprof include/linux/slab.h:1094 [inline] alloc_sk_msg net/core/skmsg.c:510 [inline] sk_psock_skb_ingress_self+0x60/0x350 net/core/skmsg.c:612 sk_psock_verdict_apply net/core/skmsg.c:1038 [inline] sk_psock_verdict_recv+0x7d9/0x8d0 net/core/skmsg.c:1236 udp_read_skb+0x73e/0x7e0 net/ipv4/udp.c:2045 sk_psock_verdict_data_ready+0x12d/0x550 net/core/skmsg.c:1257 __udp_enqueue_schedule_skb+0xc54/0x10b0 net/ipv4/udp.c:1789 __udp_queue_rcv_skb net/ipv4/udp.c:2346 [inline] udp_queue_rcv_one_skb+0xac5/0x19c0 net/ipv4/udp.c:2475 __udp4_lib_mcast_deliver+0xc06/0xcf0 net/ipv4/udp.c:2585 __udp4_lib_rcv+0x10f6/0x2620 net/ipv4/udp.c:2724 ip_protocol_deliver_rcu+0x282/0x440 net/ipv4/ip_input.c:207 ip_local_deliver_finish+0x3bb/0x6f0 net/ipv4/ip_input.c:241 NF_HOOK+0x336/0x3c0 include/linux/netfilter.h:318 dst_input include/net/dst.h:474 [inline] ip_sublist_rcv_finish+0x221/0x2a0 net/ipv4/ip_input.c:584 ip_list_rcv_finish net/ipv4/ip_input.c:628 [inline] ip_sublist_rcv+0x5c6/0xa70 net/ipv4/ip_input.c:644 ip_list_rcv+0x3f1/0x450 net/ipv4/ip_input.c:678 __netif_receive_skb_list_ptype net/core/dev.c:6195 [inline] __netif_receive_skb_list_core+0x7e5/0x810 net/core/dev.c:6242 __netif_receive_skb_list net/core/dev.c:6294 [inline] netif_receive_skb_list_internal+0x995/0xcf0 net/core/dev.c:6385 netif_receive_skb_list+0x54/0x410 net/core/dev.c:6437 xdp_recv_frames net/bpf/test_run.c:269 [inline] xdp_test_run_batch net/bpf/test_run.c:350 [inline] bpf_test_run_xdp_live+0x1946/0x1cf0 net/bpf/test_run.c:379 bpf_prog_test_run_xdp+0x81c/0x1160 net/bpf/test_run.c:1396 bpf_prog_test_run+0x2c7/0x340 kernel/bpf/syscall.c:4703 __sys_bpf+0x5cb/0x920 kernel/bpf/syscall.c:6182 __do_sys_bpf kernel/bpf/syscall.c:6274 [inline] __se_sys_bpf kernel/bpf/syscall.c:6272 [inline] __x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:6272 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f Freed by task 6021: kasan_save_stack mm/kasan/common.c:57 [inline] kasan_save_track+0x3e/0x80 mm/kasan/common.c:78 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:584 poison_slab_object mm/kasan/common.c:253 [inline] __kasan_slab_free+0x5c/0x80 mm/kasan/common.c:285 kasan_slab_free include/linux/kasan.h:235 [inline] slab_free_hook mm/slub.c:2540 [inline] slab_free mm/slub.c:6674 [inline] kfree+0x1be/0x650 mm/slub.c:6882 kfree_sk_msg include/linux/skmsg.h:385 [inline] sk_msg_recvmsg+0xaa8/0xc30 net/core/skmsg.c:483 udp_bpf_recvmsg+0x4bd/0xe00 net/ipv4/udp_bpf.c:84 inet_recvmsg+0x260/0x270 net/ipv4/af_inet.c:891 sock_recvmsg_nosec net/socket.c:1078 [inline] sock_recvmsg+0x1a8/0x270 net/socket.c:1100 ____sys_recvmsg+0x1e6/0x4a0 net/socket.c:2812 ___sys_recvmsg+0x215/0x590 net/socket.c:2854 do_recvmmsg+0x334/0x800 net/socket.c:2949 __sys_recvmmsg net/socket.c:3023 [inline] __do_sys_recvmmsg net/socket.c:3046 [inline] __se_sys_recvmmsg net/socket.c:3039 [inline] __x64_sys_recvmmsg+0x198/0x250 net/socket.c:3039 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 7 lines
Quoted 5 lines
Fixes 9f2470fbc4cb ("skmsg: Improve udp_bpf_recvmsg() accuracy")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
Reviewed-by Jakub Sitnicki <[email protected]>
Thanks
Kuniyuki Iwashima
maintainerPATCH v4 4/6 sockmap: Inline sk_psock_create_ingress_msg().
In reply to Kuniyuki IwashimaSOCKMAP memory accounting for UDP is broken for two reasons:
1. sk->sk_forward_alloc must be changed under
spin_lock_bh(&sk->sk_receive_queue.lock)
2. sk_psock_skb_ingress_self() should not be used for UDP
since UDP may reclaim sk->sk_forward_alloc partially
before passing skb to sockmap, resulting in a negative
sk->sk_forward_allocThis is a prep commit to consolidate sk_psock_skb_ingress_self() and centralise the fix to sk_psock_skb_ingress().
Let's inline sk_psock_create_ingress_msg().
Note that now alloc_sk_msg() is called first.
---
v4: Don't pass gfp_flags and instead change it based on
take_ref in the next patch
--- net/core/skmsg.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
net/core/skmsg.c +15 -16
@@ -529,18 +529,6 @@ static struct sk_msg *alloc_sk_msg(gfp_t gfp)return msg; } -static struct sk_msg *sk_psock_create_ingress_msg (struct sock *sk, -struct sk_buff *skb) -{ -if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) -return NULL ; - -if (!sk_rmem_schedule (sk, skb, skb->truesize)) -return NULL ; - -return alloc_sk_msg (GFP_KERNEL ); -} - static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb, u32 off, u32 len, struct sk_psock *psock,
@@ -592,7 +580,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, {struct sock *sk = psock->sk;struct sk_msg *msg; -int err; +int err = -EAGAIN ;/* If we are receiving on the same sock skb->sk is already assigned, * skip memory accounting and owner transition seeing it already set
@@ -600,9 +588,16 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,*/ if (unlikely (skb->sk == sk))return sk_psock_skb_ingress_self (psock, skb, off, len, true); - msg =sk_psock_create_ingress_msg (sk, skb); + + msg =alloc_sk_msg (GFP_KERNEL );if (!msg) -return -EAGAIN ; + goto out; + +if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + goto free; + +if (!sk_rmem_schedule (sk, skb, skb->truesize)) + goto free;/* This will transition ownership of the data from the socket where * the BPF program was run initiating the redirect to the socket
@@ -613,8 +608,12 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,skb_set_owner_r (skb, sk); err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, true);if (err <0 ) -kfree (msg); + goto free; +out:return err; +free: +kfree (msg); + goto out; }/* Puts an skb on the ingress queue of the socket already assigned to the
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Re: [PATCH v4 bpf/net 4/6] sockmap: Inline sk_psock_create_ingress_msg().
Quoted 15 lines · Kuniyuki Iwashima
SOCKMAP memory accounting for UDP is broken for two reasons:
1.
sk->sk_forward_allocmust be changed underspin_lock_bh(&sk->sk_receive_queue.lock)2.
sk_psock_skb_ingress_self()should not be used for UDPsince UDP may reclaim sk->sk_forward_alloc partially before passing skb to sockmap, resulting in a negative sk->sk_forward_allocThis is a prep commit to consolidate
sk_psock_skb_ingress_self()and centralise the fix tosk_psock_skb_ingress().Let's inline
sk_psock_create_ingress_msg().
Quoted 7 lines
Note that now
alloc_sk_msg()is called first.Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Don't passgfp_flagsand instead change it based on
take_refin the next patch
---
Kuniyuki Iwashima
maintainerPATCH v4 5/6 sockmap: Consolidate sk_psock_skb_ingress_self().
In reply to Kuniyuki IwashimaSOCKMAP memory accounting for UDP is broken, and sk_psock_skb_ingress_self() should not be used for UDP.
Let's consolidate sk_psock_skb_ingress_self() to sk_psock_skb_ingress() so we can centralise the fix.
Note that now sk_psock_skb_ingress() always use GFP_KERNEL if called from sk_psock_backlog().
---
v4: Fix memory accounting condition from (skb->sk != sk) to
(skb->sk != sk && take_ref), the cause of selftest failure v2: Keep msg->sk assignment --- net/core/skmsg.c | 62 +++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 45 deletions(-)
net/core/skmsg.c +17 -45
@@ -572,32 +572,30 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,return copied; } -static int sk_psock_skb_ingress_self (struct sk_psock *psock,struct sk_buff *skb, -u32 off,u32 len,bool take_ref); -static int sk_psock_skb_ingress (struct sk_psock *psock,struct sk_buff *skb, -u32 off,u32 len) +u32 off,u32 len,bool take_ref) {struct sock *sk = psock->sk;struct sk_msg *msg;int err = -EAGAIN ; -/* If we are receiving on the same sock skb->sk is already assigned, -* skip memory accounting and owner transition seeing it already set -* correctly. -*/ -if (unlikely (skb->sk == sk)) -return sk_psock_skb_ingress_self (psock, skb, off, len, true); - - msg =alloc_sk_msg (GFP_KERNEL ); + msg =alloc_sk_msg (take_ref ?GFP_KERNEL :GFP_ATOMIC );if (!msg) goto out; -if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; +if (skb->sk != sk && take_ref) { +if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + goto free; -if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; +if (!sk_rmem_schedule (sk, skb, skb->truesize)) + goto free; + }else { +/* This is used in tcp_bpf_recvmsg_parser() to determine whether the +* data originates from the socket's own protocol stack. No need to +* refcount sk because msg's lifetime is bound to sk via the ingress_msg. +*/ + msg->sk = sk; + }/* This will transition ownership of the data from the socket where * the BPF program was run initiating the redirect to the socket
@@ -606,7 +604,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); - err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, true); + err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:
@@ -616,32 +614,6 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, goto out; } -/* Puts an skb on the ingress queue of the socket already assigned to the -* skb. In this case we do not need to check memory limits or skb_set_owner_r -* because the skb is already accounted for here. -*/ -static int sk_psock_skb_ingress_self (struct sk_psock *psock,struct sk_buff *skb, -u32 off,u32 len,bool take_ref) -{ -struct sk_msg *msg =alloc_sk_msg (GFP_ATOMIC ); -struct sock *sk = psock->sk; -int err; - -if (unlikely (!msg)) -return -EAGAIN ; -skb_set_owner_r (skb, sk); - -/* This is used in tcp_bpf_recvmsg_parser() to determine whether the -* data originates from the socket's own protocol stack. No need to -* refcount sk because msg's lifetime is bound to sk via the ingress_msg. -*/ - msg->sk = sk; - err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref); -if (err <0 ) -kfree (msg); -return err; -} -static int sk_psock_handle_skb (struct sk_psock *psock,struct sk_buff *skb,u32 off,u32 len,bool ingress) {
@@ -651,7 +623,7 @@ static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb,return skb_send_sock (psock->sk, skb, off, len); } -return sk_psock_skb_ingress (psock, skb, off, len); +return sk_psock_skb_ingress (psock, skb, off, len, true); } static void sk_psock_skb_state(struct sk_psock *psock,
@@ -1058,7 +1030,7 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, off = stm->offset; len = stm->full_len; } - err =sk_psock_skb_ingress_self (psock, skb, off, len, false); + err =sk_psock_skb_ingress (psock, skb, off, len, false); }if (err <0 ) {spin_lock_bh (&psock->ingress_lock);
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Kuniyuki Iwashima
maintainerPATCH v4 6/6 sockmap: Fix broken memory accounting for UDP.
In reply to Kuniyuki Iwashimasyzbot reported imbalanced sk->sk_forward_alloc [0] and demonstrated that UDP memory accounting by SOCKMAP is broken.
The repro put a UDP sk into SOCKMAP and redirected skb to itself, where skb->truesize was 4240.
First, udp_rmem_schedule() set sk->sk_forward_alloc to 8192 (2 * PAGE_SIZE), and skb->truesize was charged:
sk->sk_forward_alloc = 0 + 8192 - 4240; // => 3952
Then, udp_read_skb() dequeued the skb by skb_recv_udp(), which finally calls udp_rmem_release() and _partially_ reclaims sk->sk_forward_alloc because skb->truesize was larger than PAGE_SIZE:
sk->sk_forward_alloc += 4240; // => 8192 (PAGE_SIZE is reclaimable) sk->sk_forward_alloc -= 4096; // => 4096
Later, sk_psock_skb_ingress_self() called skb_set_owner_r() to charge the skb again, triggering an sk->sk_forward_alloc underflow:
sk->sk_forward_alloc -= 4240 // => -144
Another problem is that UDP memory accounting is not performed under spin_lock_bh(&sk->sk_receive_queue.lock).
skb_set_owner_r() and sock_rfree() are called locklessly and corrupt sk->sk_forward_alloc, leading to the splat.
Let's not skip memory accounting for UDP and ensure the proper lock is held.
Note that UDP does not need msg->sk assignment, which is for TCP.
[0]:
WARNING: net/ipv4/af_inet.c:157 at inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157, CPU#0: ksoftirqd/0/15 Modules linked in: CPU: 0 UID: 0 PID: 15 Comm: ksoftirqd/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 RIP: 0010:inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157 Code: 0f 0b 90 e9 58 fe ff ff e8 40 55 b3 f7 90 0f 0b 90 e9 8b fe ff ff e8 32 55 b3 f7 90 0f 0b 90 e9 b1 fe ff ff e8 24 55 b3 f7 90 <0f> 0b 90 e9 d7 fe ff ff 89 f9 80 e1 07 80 c1 03 38 c1 0f 8c 95 fc RSP: 0018:ffffc90000147a48 EFLAGS: 00010246 RAX: ffffffff8a1121dc RBX: dffffc0000000000 RCX: ffff88801d2c3d00 RDX: 0000000000000100 RSI: 0000000000000f70 RDI: 0000000000000000 RBP: 0000000000000f70 R08: ffff888030ce1327 R09: 1ffff1100619c264 R10: dffffc0000000000 R11: ffffed100619c265 R12: ffff888030ce1080 R13: dffffc0000000000 R14: ffff888030ce130c R15: ffffffff8fa87e00 FS: 0000000000000000(0000) GS:ffff8881256f8000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000000700 CR3: 000000007200c000 CR4: 00000000003526f0 Call Trace: <TASK> __sk_destruct+0x85/0x880 net/core/sock.c:2350 rcu_do_batch kernel/rcu/tree.c:2605 [inline] rcu_core+0xc9e/0x1750 kernel/rcu/tree.c:2857 handle_softirqs+0x22a/0x7c0 kernel/softirq.c:622 run_ksoftirqd+0x36/0x60 kernel/softirq.c:1063 smpboot_thread_fn+0x541/0xa50 kernel/smpboot.c:160 kthread+0x726/0x8b0 kernel/kthread.c:463 ret_from_fork+0x51b/0xa40 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246 </TASK>
---
v4: Fix deadlock when requeued
v2: Fix build failure when CONFIG_INET=n
--- include/net/udp.h | 9 +++++++++ net/core/skmsg.c | 29 +++++++++++++++++++++++++---- net/ipv4/udp.c | 9 +++++++++ 3 files changed, 43 insertions(+), 4 deletions(-)
include/net/udp.h +9 -0
@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid
net/core/skmsg.c +25 -4
@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h>
@@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;
@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg.
@@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:return err; + +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock); free:kfree (msg); goto out;
net/ipv4/udp.c +9 -0
@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb;
Signature · 1 line
2.53.0.371.g1d285c8824-goog
Martin KaFai Lau
reviewerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 63 lines · Kuniyuki Iwashima
syzbot reported imbalanced
sk->sk_forward_alloc[0] and demonstrated that UDP memory accounting by SOCKMAP is broken.The repro put a UDP sk into SOCKMAP and redirected skb to itself, where
skb->truesizewas 4240.First,
udp_rmem_schedule()setsk->sk_forward_allocto 8192 (2 *PAGE_SIZE), andskb->truesizewas charged:sk->sk_forward_alloc = 0 + 8192 - 4240; // => 3952Then,
udp_read_skb()dequeued the skb byskb_recv_udp(), which finally callsudp_rmem_release()and _partially_ reclaimssk->sk_forward_allocbecauseskb->truesizewas larger thanPAGE_SIZE:sk->sk_forward_alloc += 4240; // => 8192 (PAGE_SIZE is reclaimable) sk->sk_forward_alloc -= 4096; // => 4096Later,
sk_psock_skb_ingress_self()calledskb_set_owner_r()to charge the skb again, triggering ansk->sk_forward_allocunderflow:sk->sk_forward_alloc -= 4240 // => -144Another problem is that UDP memory accounting is not performed under
spin_lock_bh(&sk->sk_receive_queue.lock).
skb_set_owner_r()andsock_rfree()are called locklessly and corruptsk->sk_forward_alloc, leading to the splat.Let's not skip memory accounting for UDP and ensure the proper lock is held.
Note that UDP does not need
msg->skassignment, which is for TCP.[0]:
WARNING: net/ipv4/af_inet.c:157 at inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157, CPU#0: ksoftirqd/0/15 Modules linked in: CPU: 0 UID: 0 PID: 15 Comm: ksoftirqd/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 RIP: 0010:inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157 Code: 0f 0b 90 e9 58 fe ff ff e8 40 55 b3 f7 90 0f 0b 90 e9 8b fe ff ff e8 32 55 b3 f7 90 0f 0b 90 e9 b1 fe ff ff e8 24 55 b3 f7 90 <0f> 0b 90 e9 d7 fe ff ff 89 f9 80 e1 07 80 c1 03 38 c1 0f 8c 95 fc RSP: 0018:ffffc90000147a48 EFLAGS: 00010246 RAX: ffffffff8a1121dc RBX: dffffc0000000000 RCX: ffff88801d2c3d00 RDX: 0000000000000100 RSI: 0000000000000f70 RDI: 0000000000000000 RBP: 0000000000000f70 R08: ffff888030ce1327 R09: 1ffff1100619c264 R10: dffffc0000000000 R11: ffffed100619c265 R12: ffff888030ce1080 R13: dffffc0000000000 R14: ffff888030ce130c R15: ffffffff8fa87e00 FS: 0000000000000000(0000) GS:ffff8881256f8000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000000700 CR3: 000000007200c000 CR4: 00000000003526f0 Call Trace: <TASK> __sk_destruct+0x85/0x880 net/core/sock.c:2350 rcu_do_batch kernel/rcu/tree.c:2605 [inline] rcu_core+0xc9e/0x1750 kernel/rcu/tree.c:2857 handle_softirqs+0x22a/0x7c0 kernel/softirq.c:622 run_ksoftirqd+0x36/0x60 kernel/softirq.c:1063 smpboot_thread_fn+0x541/0xa50 kernel/smpboot.c:160 kthread+0x726/0x8b0 kernel/kthread.c:463 ret_from_fork+0x51b/0xa40 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246 </TASK>
Quoted 1 line
Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")
JakubS and JohnF who are the maintainers of sockmap and skmsg. Please take a look also.
Jiayuan Chen, you had submitted patches(/fixes) for sockmap/skmsg. It is a good chance to gain review credit.
Quoted 21 lines
net/core/skmsg.c +2
-0@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;
net/core/skmsg.c static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, · line 589
Quoted 6 lines
net/core/skmsg.c +2
-1@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree))
A quick question. This case happens when the earlier sk_psock_skb_ingress_enqueue() failed?
Quoted 42 lines
net/core/skmsg.c +21
-3+ goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:return err; + +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock); free:kfree (msg); goto out;
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 100 lines · Martin KaFai Lau
Quoted 63 lines · Kuniyuki Iwashima
syzbot reported imbalanced
sk->sk_forward_alloc[0] and demonstrated that UDP memory accounting by SOCKMAP is broken.The repro put a UDP sk into SOCKMAP and redirected skb to itself, where
skb->truesizewas 4240.First,
udp_rmem_schedule()setsk->sk_forward_allocto 8192 (2 *PAGE_SIZE), andskb->truesizewas charged:sk->sk_forward_alloc = 0 + 8192 - 4240; // => 3952Then,
udp_read_skb()dequeued the skb byskb_recv_udp(), which finally callsudp_rmem_release()and _partially_ reclaimssk->sk_forward_allocbecauseskb->truesizewas larger thanPAGE_SIZE:sk->sk_forward_alloc += 4240; // => 8192 (PAGE_SIZE is reclaimable) sk->sk_forward_alloc -= 4096; // => 4096Later,
sk_psock_skb_ingress_self()calledskb_set_owner_r()to charge the skb again, triggering ansk->sk_forward_allocunderflow:sk->sk_forward_alloc -= 4240 // => -144Another problem is that UDP memory accounting is not performed under
spin_lock_bh(&sk->sk_receive_queue.lock).
skb_set_owner_r()andsock_rfree()are called locklessly and corruptsk->sk_forward_alloc, leading to the splat.Let's not skip memory accounting for UDP and ensure the proper lock is held.
Note that UDP does not need
msg->skassignment, which is for TCP.[0]:
WARNING: net/ipv4/af_inet.c:157 at inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157, CPU#0: ksoftirqd/0/15 Modules linked in: CPU: 0 UID: 0 PID: 15 Comm: ksoftirqd/0 Not tainted syzkaller #0 PREEMPT(full) Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/13/2026 RIP: 0010:inet_sock_destruct+0x62d/0x740 net/ipv4/af_inet.c:157 Code: 0f 0b 90 e9 58 fe ff ff e8 40 55 b3 f7 90 0f 0b 90 e9 8b fe ff ff e8 32 55 b3 f7 90 0f 0b 90 e9 b1 fe ff ff e8 24 55 b3 f7 90 <0f> 0b 90 e9 d7 fe ff ff 89 f9 80 e1 07 80 c1 03 38 c1 0f 8c 95 fc RSP: 0018:ffffc90000147a48 EFLAGS: 00010246 RAX: ffffffff8a1121dc RBX: dffffc0000000000 RCX: ffff88801d2c3d00 RDX: 0000000000000100 RSI: 0000000000000f70 RDI: 0000000000000000 RBP: 0000000000000f70 R08: ffff888030ce1327 R09: 1ffff1100619c264 R10: dffffc0000000000 R11: ffffed100619c265 R12: ffff888030ce1080 R13: dffffc0000000000 R14: ffff888030ce130c R15: ffffffff8fa87e00 FS: 0000000000000000(0000) GS:ffff8881256f8000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000200000000700 CR3: 000000007200c000 CR4: 00000000003526f0 Call Trace: <TASK> __sk_destruct+0x85/0x880 net/core/sock.c:2350 rcu_do_batch kernel/rcu/tree.c:2605 [inline] rcu_core+0xc9e/0x1750 kernel/rcu/tree.c:2857 handle_softirqs+0x22a/0x7c0 kernel/softirq.c:622 run_ksoftirqd+0x36/0x60 kernel/softirq.c:1063 smpboot_thread_fn+0x541/0xa50 kernel/smpboot.c:160 kthread+0x726/0x8b0 kernel/kthread.c:463 ret_from_fork+0x51b/0xa40 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:246 </TASK>Quoted 1 line
Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Cc: Cong Wang, who is the author of the quoted commit and also the author of the full UDP support in sockmap.
JakubS and JohnF who are the maintainers of sockmap and skmsg. Please take a look also.
Jiayuan Chen, you had submitted patches(/fixes) for sockmap/skmsg. It is a good chance to gain review credit.
Quoted 24 lines
net/core/skmsg.c +2
-0@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out;
Quoted 6 lines
net/core/skmsg.c static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, · line 589
Quoted 3 lines
net/core/skmsg.c +2
-1-if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree))A quick question. This case happens when the earlier
sk_psock_skb_ingress_enqueue()failed?
Yes, the skb is charged by skb_set_owner_r() and the lock is released, so I left skb->destructor as is and avoid extra locking when sk_psock_skb_ingress_enqueue() fails.
Quoted 43 lines
Quoted 43 lines
net/core/skmsg.c +21
-3+ goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:return err; + +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock); free:kfree (msg); goto out;
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
In reply to Kuniyuki IwashimaOn Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]
Quoted 127 lines
</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n--- include/net/udp.h | 9 +++++++++ net/core/skmsg.c | 29 +++++++++++++++++++++++++---- net/ipv4/udp.c | 9 +++++++++ 3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +25
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:return err; + +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock); free:kfree (msg); goto out;net/ipv4/udp.c +9
-0@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb;Signature · 1 line
2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Also TCP also has a similar issue with sk_forward_alloc concurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.
I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
include/linux/skmsg.h +20 -2
@@ -80,6 +80,20 @@ struct sk_psock_work_state {u32 off; }; +struct sk_psock ; + +/* +* Callback for protocol-specific memory accounting on the ingress path. +* Each protocol uses different locks to protect sk->sk_forward_alloc: +* TCP: psock->ingress_lock +* UDP: sk->sk_receive_queue.lock +* This callback encapsulates the protocol-specific locking and charging. +* +* Returns 0 on success, negative errno on failure. +*/ +typedef int (*psock_ingress_charge_t )(struct sk_psock *psock,struct sock *sk, +struct sk_buff *skb,struct sk_msg *msg); +struct sk_psock {struct sock *sk;struct sock *sk_redir;
@@ -97,6 +111,7 @@ struct sk_psock {struct sk_buff_head ingress_skb;struct list_head ingress_msg;spinlock_t ingress_lock; +psock_ingress_charge_t ingress_charge;/** @msg_tot_len: Total bytes queued in ingress_msg list. */ u32 msg_tot_len;unsigned long state;
@@ -411,10 +426,13 @@ static inline bool sk_psock_queue_empty(const struct sk_psock *psock)return psock ?list_empty (&psock->ingress_msg) : true; } -static inline void kfree_sk_msg (struct sk_msg *msg) +static inline void kfree_sk_msg (struct sk_psock *psock,struct sk_msg *msg) { -if (msg->skb) +if (msg->skb) { +spin_lock_bh (&psock->ingress_lock);consume_skb (msg->skb); +spin_unlock_bh (&psock->ingress_lock); + }kfree (msg); }
include/net/udp.h +1 -0
@@ -401,6 +401,7 @@ void udp_destruct_common(struct sock *sk);void skb_consume_udp (struct sock *sk,struct sk_buff *skb,int len);int __udp_enqueue_schedule_skb (struct sock *sk,struct sk_buff *skb);void udp_skb_destructor (struct sock *sk,struct sk_buff *skb); +void udp_sock_rfree (struct sk_buff *skb);struct sk_buff *__skb_recv_udp (struct sock *sk,unsigned int flags,int *off,int *err); static inline struct sk_buff *skb_recv_udp(struct sock *sk, unsigned int flags,
net/core/skmsg.c +35 -56
134 lines not on this page · raw message
net/ipv4/tcp_bpf.c +32 -0
@@ -706,6 +706,37 @@ int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc, }#endif /* CONFIG_BPF_STREAM_PARSER */ +static int tcp_psock_ingress_charge (struct sk_psock *psock,struct sock *sk, +struct sk_buff *skb,struct sk_msg *msg) +{ +/* TCP self-redirect: skb is already accounted by TCP stack, and +* lock_sock serializes the verdict path with recvmsg. Skip memory +* accounting but mark msg->sk for tcp_bpf_recvmsg_parser() to +* identify self-originated data. +*/ +if (skb->sk == sk) { +skb_set_owner_r (skb, sk); + msg->sk = sk; +return 0 ; + } + +spin_lock_bh (&psock->ingress_lock); + +if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + goto err; + +if (!sk_rmem_schedule (sk, skb, skb->truesize)) + goto err; + +skb_set_owner_r (skb, sk); +spin_unlock_bh (&psock->ingress_lock); +return 0 ; + +err: +spin_unlock_bh (&psock->ingress_lock); +return -EAGAIN ; +} +int tcp_bpf_update_proto (struct sock *sk,struct sk_psock *psock,bool restore) {int family = sk->sk_family ==AF_INET6 ?TCP_BPF_IPV6 :TCP_BPF_IPV4 ;
@@ -739,6 +770,7 @@ int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)tcp_bpf_check_v6_needs_rebuild (psock->sk_proto); } + psock->ingress_charge = tcp_psock_ingress_charge;/* Pairs with lockless read in sk_clone_lock() */ sock_replace_proto (sk, &tcp_bpf_prots[family][config]);return 0 ;
net/ipv4/udp.c +13 -0
@@ -2039,6 +2039,19 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +/* skb destructor for sockmap-owned UDP skbs. +* sock_rfree modifies sk->sk_forward_alloc which must be protected +* by sk->sk_receive_queue.lock for UDP. +*/ +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb;
net/ipv4/udp_bpf.c +37 -0
@@ -153,6 +153,42 @@ static int __init udp_bpf_v4_build_proto(void) }late_initcall (udp_bpf_v4_build_proto); +static int udp_psock_ingress_charge (struct sk_psock *psock,struct sock *sk, +struct sk_buff *skb,struct sk_msg *msg) +{ +/* When backlog retries a previously charged skb, the destructor +* is already set to udp_sock_rfree. Skip re-charging. +*/ +if (unlikely (skb->destructor == udp_sock_rfree)) +return 0 ; + +/* UDP must always do full memory accounting, even for self-redirect. +* Unlike TCP, udp_rmem_release() partially reclaims sk_forward_alloc +* during skb_recv_udp(), so the skb is no longer properly accounted +* when it reaches the sockmap ingress path. +* +* sk->sk_receive_queue.lock is required because that's the lock UDP +* uses internally to protect sk_forward_alloc. +*/ +spin_lock_bh (&sk->sk_receive_queue.lock); + +if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + goto err; + +if (!sk_rmem_schedule (sk, skb, skb->truesize)) + goto err; + +skb_set_owner_r (skb, sk); +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; +return 0 ; + +err: +spin_unlock_bh (&sk->sk_receive_queue.lock); +return -EAGAIN ; +} +int udp_bpf_update_proto (struct sock *sk,struct sk_psock *psock,bool restore) {int family = sk->sk_family ==AF_INET ?UDP_BPF_IPV4 :UDP_BPF_IPV6 ;
@@ -166,6 +202,7 @@ int udp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)if (sk->sk_family ==AF_INET6 )udp_bpf_check_v6_needs_rebuild (psock->sk_proto); + psock->ingress_charge = udp_psock_ingress_charge;sock_replace_proto (sk, &udp_bpf_prots[family]);return 0 ; }
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 134 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]Quoted 125 lines
</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n--- include/net/udp.h | 9 +++++++++ net/core/skmsg.c | 29 +++++++++++++++++++++++++---- net/ipv4/udp.c | 9 +++++++++ 3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +25
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free; out:return err; + +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock); free:kfree (msg); goto out;net/ipv4/udp.c +9
-1@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb; --Quoted 2 lines
2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 7 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1) tcp_psock_ingress_charge() uses psock->ingress_lock, but it does not protect any tcp_sock fields, especially sk->sk_forwad_alloc.
2) TCP memory accounting happens under bh_lock_sock() _or_ lock_sock(). sendmsg()/recvmsg() works under lock_sock() and TCP fast path puts skb to backlog if lock_sock() is held by userspace. This means even a simple bh_lock_sock() in tcp_psock_ingress_charge() race with memory accounting happening under lock_sock().
Since sk_psock_skb_ingress() is called from both GFP_KERNEL and GFP_ATOMIC context, the simplest fix would be to put lock_sock() in sk_psock_backlog() for TCP, which is ugly though.
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 160 lines · Kuniyuki Iwashima
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h | 9 +++++++++ net/core/skmsg.c | 29 +++++++++++++++++++++++++---- net/ipv4/udp.c | 9 +++++++++ 3 files changed, 43 insertions(+), 4 deletions(-)diff --git
a/include/net/udp.hb/include/net/udp.h
index700dbedcb15f..ae38a4da9388100644
---a/include/net/udp.h
+++b/include/net/udp.h
@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, structsk_buff*skb);
struct sock *udp6_lib_lookup_skb(const structsk_buff*skb,__be16sport,__be16dport);
+
+#ifdefCONFIG_INET
+voidudp_sock_rfree(struct sk_buff *skb);
+#else
+static inline voidudp_sock_rfree(struct sk_buff *skb)
+{
+}
+#endif
+
intudp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);/* UDP uses
skb->dev_scratchto cache as much information as possible and avoid diff --gita/net/core/skmsg.cb/net/core/skmsg.c
index96f43e0dbb17..dd9134a45663100644
---a/net/core/skmsg.c
+++b/net/core/skmsg.c
@@ -7,6 +7,7 @@#include <
net/sock.h>
#include <net/tcp.h>
+#include <net/udp.h>
#include <net/tls.h>
#include <trace/events/sock.h>@@ -576,6 +577,7 @@ static int
sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref)
{
struct sock *sk =psock->sk;
+ boolis_udp=sk_is_udp(sk);
structsk_msg*msg;
int err =-EAGAIN;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct
sk_psock*psock, structsk_buff*skb, if (!msg)
goto out;- if (
skb->sk!= sk &&take_ref) {
+ if (is_udp) {
+ if (unlikely(skb->destructor == udp_sock_rfree))
+ goto enqueue;
+
+spin_lock_bh(&sk->sk_receive_queue.lock);
+ }
+
+ if (is_udp|| (skb->sk!= sk &&take_ref)) { if (atomic_read(&sk->sk_rmem_alloc)>sk->sk_rcvbuf)
- goto free;
+ goto unlock;if (!sk_rmem_schedule(sk, skb,
skb->truesize))
- goto free;
- } else {
+ goto unlock;
+ } else if (skb->sk== sk || !take_ref) {
/* This is used intcp_bpf_recvmsg_parser()to determine whether the
* data originates from the socket's own protocol stack. No need to
* refcount sk because msg's lifetime is bound to sk via theingress_msg.
@@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(structsk_psock*psock, structsk_buff*skb,
* into user buffers.
*/
skb_set_owner_r(skb, sk);
+
+ if (is_udp) {
+spin_unlock_bh(&sk->sk_receive_queue.lock);
+
+skb->destructor=udp_sock_rfree;
+ }
+
+enqueue:
err =sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, take_ref);
if (err < 0)
goto free;
out:
return err;
+
+unlock:
+ if (is_udp)
+spin_unlock_bh(&sk->sk_receive_queue.lock);
free:
kfree(msg);
goto out;
diff --gita/net/ipv4/udp.cb/net/ipv4/udp.c
index422c96fea249..831d26748a90100644
---a/net/ipv4/udp.c
+++b/net/ipv4/udp.c
@@ -2039,6 +2039,15 @@ structsk_buff*__skb_recv_udp(struct sock *sk, unsigned int flags, }
EXPORT_SYMBOL(__skb_recv_udp);+void
udp_sock_rfree(struct sk_buff *skb)
+{
+ struct sock *sk =skb->sk;
+
+spin_lock_bh(&sk->sk_receive_queue.lock);
+sock_rfree(skb);
+spin_unlock_bh(&sk->sk_receive_queue.lock);
+}
+
intudp_read_skb(struct sock *sk, skb_read_actor_t recv_actor){
structsk_buff*skb;
--
2.53.0.371.g1d285c8824-googThere are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().
Quoted 4 lines
Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.
OK, thanks.
Now my only concern is keeping UDP-specific logic out of skmsg.c. We could use a function pointer so that UDP implements its own ingress charge in udp_bpf.c, while other protocols just use a default no-op or even NULL.
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 169 lines · Jiayuan Chen
Quoted 160 lines · Kuniyuki Iwashima
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h | 9 +++++++++ net/core/skmsg.c | 29 +++++++++++++++++++++++++---- net/ipv4/udp.c | 9 +++++++++ 3 files changed, 43 insertions(+), 4 deletions(-)diff --git
a/include/net/udp.hb/include/net/udp.h
index700dbedcb15f..ae38a4da9388100644
---a/include/net/udp.h
+++b/include/net/udp.h
@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, structsk_buff*skb);
struct sock *udp6_lib_lookup_skb(const structsk_buff*skb,__be16sport,__be16dport);
+
+#ifdefCONFIG_INET
+voidudp_sock_rfree(struct sk_buff *skb);
+#else
+static inline voidudp_sock_rfree(struct sk_buff *skb)
+{
+}
+#endif
+
intudp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);/* UDP uses
skb->dev_scratchto cache as much information as possible and avoid diff --gita/net/core/skmsg.cb/net/core/skmsg.c
index96f43e0dbb17..dd9134a45663100644
---a/net/core/skmsg.c
+++b/net/core/skmsg.c
@@ -7,6 +7,7 @@#include <
net/sock.h>
#include <net/tcp.h>
+#include <net/udp.h>
#include <net/tls.h>
#include <trace/events/sock.h>@@ -576,6 +577,7 @@ static int
sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref)
{
struct sock *sk =psock->sk;
+ boolis_udp=sk_is_udp(sk);
structsk_msg*msg;
int err =-EAGAIN;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct
sk_psock*psock, structsk_buff*skb, if (!msg)
goto out;- if (
skb->sk!= sk &&take_ref) {
+ if (is_udp) {
+ if (unlikely(skb->destructor == udp_sock_rfree))
+ goto enqueue;
+
+spin_lock_bh(&sk->sk_receive_queue.lock);
+ }
+
+ if (is_udp|| (skb->sk!= sk &&take_ref)) { if (atomic_read(&sk->sk_rmem_alloc)>sk->sk_rcvbuf)
- goto free;
+ goto unlock;if (!sk_rmem_schedule(sk, skb,
skb->truesize))
- goto free;
- } else {
+ goto unlock;
+ } else if (skb->sk== sk || !take_ref) {
/* This is used intcp_bpf_recvmsg_parser()to determine whether the
* data originates from the socket's own protocol stack. No need to
* refcount sk because msg's lifetime is bound to sk via theingress_msg.
@@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(structsk_psock*psock, structsk_buff*skb,
* into user buffers.
*/
skb_set_owner_r(skb, sk);
+
+ if (is_udp) {
+spin_unlock_bh(&sk->sk_receive_queue.lock);
+
+skb->destructor=udp_sock_rfree;
+ }
+
+enqueue:
err =sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, take_ref);
if (err < 0)
goto free;
out:
return err;
+
+unlock:
+ if (is_udp)
+spin_unlock_bh(&sk->sk_receive_queue.lock);
free:
kfree(msg);
goto out;
diff --gita/net/ipv4/udp.cb/net/ipv4/udp.c
index422c96fea249..831d26748a90100644
---a/net/ipv4/udp.c
+++b/net/ipv4/udp.c
@@ -2039,6 +2039,15 @@ structsk_buff*__skb_recv_udp(struct sock *sk, unsigned int flags, }
EXPORT_SYMBOL(__skb_recv_udp);+void
udp_sock_rfree(struct sk_buff *skb)
+{
+ struct sock *sk =skb->sk;
+
+spin_lock_bh(&sk->sk_receive_queue.lock);
+sock_rfree(skb);
+spin_unlock_bh(&sk->sk_receive_queue.lock);
+}
+
intudp_read_skb(struct sock *sk, skb_read_actor_t recv_actor){
structsk_buff*skb;
--
2.53.0.371.g1d285c8824-googThere are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().Quoted 4 lines
Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.
Quoted 4 lines
OK, thanks.
Now my only concern is keeping UDP-specific logic
out ofskmsg.c.
We already have TCP-only msg->sk logic there that you added. It just does not look like TCP code.
If really needed, we can do such cleanup in bpf-next for TCP and UDP altogether (probably after another TCP fix ?).
Quoted 3 lines
We could use a function pointer so that UDP implements its own ingress charge in
udp_bpf.c, while other protocols just use a default no-op or even NULL.
If TCP requires locking outside of the scope, there's no point adding one more extra layer/complexity just for UDP.
Even if we go that way, most likely we cannot get rid of protocol-specific code (UDP/TCP-only hook) and end up with the indirect call helpers, which will be expensive than simple if logic.
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 169 lines · Kuniyuki Iwashima
Quoted 166 lines · Jiayuan Chen
March 5, 2026 at 15:48, "Kuniyuki Iwashima" <[email protected] mailto:[email protected]?to=%22Kuniyuki%20Iwashima%22%20%3Ckuniyu%40google.com%3E > wrote:
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h| 9 +++++++++net/core/skmsg.c| 29 +++++++++++++++++++++++++----net/ipv4/udp.c| 9 +++++++++3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +21
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free;out:
return err;net/core/skmsg.c +4
-0+ +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock);free:
kfree(msg);
goto out;net/ipv4/udp.c +9
-1@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb; --2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.
Quoted 7 lines
Quoted 5 lines
OK, thanks.
Now my only concern is keeping UDP-specific logic out of
skmsg.c.We already have TCP-only
msg->sklogic there that you added. It just does not look like TCP code.
The msg->sk field marks ingress-self vs ingress-other, which is protocol-agnostic metadata. TCP happens to consume it for seq tracking, but the assignment itself has no TCP-specific check (no is_tcp_sk() or similar).
Quoted 9 lines
If really needed, we can do such cleanup in bpf-next for TCP and UDP altogether (probably after another TCP fix ?).
Quoted 5 lines
We could use a function pointer so that UDP
implements its own ingress charge inudp_bpf.c, while other protocols just use a default no-op or even NULL.
Quoted 9 lines
If TCP requires locking outside of the scope, there's no point adding one more extra layer/complexity just for UDP.
Even if we go that way, most likely we cannot get rid of protocol-specific code (UDP/TCP-only hook) and end up with the indirect call helpers, which will be expensive than simple if logic.
An alternative would be to add such sk_psock_udp_skb_ingress() in udp_bpf.c, without adding any indirection layer.
This way we don't need to design a new abstraction before the approach is settled. Just a UDP-specific ingress helper, which keeps things clean enough for now, and avoid scattering is_udp checks all over sk_psock_skb_ingress, just need one if (is_udp).
Thanks.
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 207 lines · Jiayuan Chen
Quoted 169 lines · Kuniyuki Iwashima
On Thu, Mar 5, 2026 at 12:30 AM Jiayuan Chen <[email protected]> wrote:
March 5, 2026 at 15:48, "Kuniyuki Iwashima" <[email protected] mailto:[email protected]?to=%22Kuniyuki%20Iwashima%22%20%3Ckuniyu%40google.com%3E > wrote:
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h| 9 +++++++++net/core/skmsg.c| 29 +++++++++++++++++++++++++----net/ipv4/udp.c| 9 +++++++++3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +21
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free;out:
return err;net/core/skmsg.c +4
-0+ +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock);free:
kfree(msg);
goto out;net/ipv4/udp.c +9
-1@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb; --2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.Quoted 8 lines
OK, thanks.
Now my only concern is keeping UDP-specific logic out of
skmsg.c.We already have TCP-only
msg->sklogic there that you added. It just does not look like TCP code.The
msg->skfield marks ingress-self vs ingress-other, which is protocol-agnostic metadata. TCP happens to consume it for seq tracking, but the assignment itself has no TCP-specific check (nois_tcp_sk()or similar).Quoted 10 lines
If really needed, we can do such cleanup in bpf-next
for TCP and UDP altogether (probably after another TCP fix ?).We could use a function pointer so that UDP implements its own ingress charge in
udp_bpf.c, while other protocols just use a default no-op or even NULL.Quoted 9 lines
If TCP requires locking outside of the scope, there's no point adding one more extra layer/complexity just for UDP.
Even if we go that way, most likely we cannot get rid of protocol-specific code (UDP/TCP-only hook) and end up with the indirect call helpers, which will be expensive than simple if logic.
An alternative would be to add such
sk_psock_udp_skb_ingress()inudp_bpf.c, without adding any indirection layer.
Quoted 7 lines
This way we don't need to design a new abstraction before the approach is settled. Just a UDP-specific ingress helper, which keeps things clean enough for now, and avoid scattering
is_udpchecks all oversk_psock_skb_ingress, just need one if (is_udp).Thanks.
After thinking a while, since the UDP-specific behaviour only appears in the redirect-to-self path, how about implementing sk_psock_udp_skb_ingress_self() in udp_bpf.c and calling it from sk_psock_skb_ingress_self()? That gives us a single, contained check point rather than scattering is_udp all over skmsg.c, which seems more aligned with the "simple" you described before.
Kuniyuki Iwashima
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 216 lines · Jiayuan Chen
Quoted 212 lines · Jiayuan Chen
Quoted 169 lines · Kuniyuki Iwashima
On Thu, Mar 5, 2026 at 12:30 AM Jiayuan Chen <[email protected]> wrote:
March 5, 2026 at 15:48, "Kuniyuki Iwashima" <[email protected] mailto:[email protected]?to=%22Kuniyuki%20Iwashima%22%20%3Ckuniyu%40google.com%3E > wrote:
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h| 9 +++++++++net/core/skmsg.c| 29 +++++++++++++++++++++++++----net/ipv4/udp.c| 9 +++++++++3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +21
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free;out:
return err;net/core/skmsg.c +4
-0+ +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock);free:
kfree(msg);
goto out;net/ipv4/udp.c +9
-1@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb; --2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.Quoted 8 lines
OK, thanks.
Now my only concern is keeping UDP-specific logic out of
skmsg.c.We already have TCP-only
msg->sklogic there that you added. It just does not look like TCP code.The
msg->skfield marks ingress-self vs ingress-other, which is protocol-agnostic metadata. TCP happens to consume it for seq tracking, but the assignment itself has no TCP-specific check (nois_tcp_sk()or similar).Quoted 10 lines
If really needed, we can do such cleanup in bpf-next
for TCP and UDP altogether (probably after another TCP fix ?).We could use a function pointer so that UDP implements its own ingress charge in
udp_bpf.c, while other protocols just use a default no-op or even NULL.Quoted 9 lines
If TCP requires locking outside of the scope, there's no point adding one more extra layer/complexity just for UDP.
Even if we go that way, most likely we cannot get rid of protocol-specific code (UDP/TCP-only hook) and end up with the indirect call helpers, which will be expensive than simple if logic.
An alternative would be to add such
sk_psock_udp_skb_ingress()inudp_bpf.c, without adding any indirection layer.This way we don't need to design a new abstraction before the approach is settled. Just a UDP-specific ingress helper, which keeps things clean enough for now, and avoid scattering
is_udpchecks all oversk_psock_skb_ingress, just need one if (is_udp).
Quoted 5 lines
Quoted 2 lines
Thanks.
After thinking a while, since the UDP-specific behaviour only appears in the redirect-to-self path,
Are you saying psock->sk == skb->sk is always true and skb_set_owner_r() in the else case is not called for UDP ?
Jiayuan Chen
maintainerRe: [PATCH v4 bpf/net 6/6] sockmap: Fix broken memory accounting for UDP.
Quoted 217 lines · Kuniyuki Iwashima
Quoted 214 lines · Jiayuan Chen
March 5, 2026 at 18:45, "Jiayuan Chen" <[email protected] mailto:[email protected]?to=%22Jiayuan%20Chen%22%20%3Cjiayuan.chen%40linux.dev%3E > wrote:
Quoted 169 lines · Kuniyuki Iwashima
On Thu, Mar 5, 2026 at 12:30 AM Jiayuan Chen <[email protected]> wrote:
March 5, 2026 at 15:48, "Kuniyuki Iwashima" <[email protected] mailto:[email protected]?to=%22Kuniyuki%20Iwashima%22%20%3Ckuniyu%40google.com%3E > wrote:
Quoted 133 lines · Jiayuan Chen
On Sat, Feb 21, 2026 at 11:30:53PM +0800, Kuniyuki Iwashima wrote:
[...]</TASK>Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")Reported-by [email protected]Signed-off-by Kuniyuki Iwashima <[email protected]>---
v4: Fix deadlock when requeued
v2: Fix build failure whenCONFIG_INET=n
---include/net/udp.h| 9 +++++++++net/core/skmsg.c| 29 +++++++++++++++++++++++++----net/ipv4/udp.c| 9 +++++++++3 files changed, 43 insertions(+), 4 deletions(-)include/net/udp.h +9
-0@@ -455,6 +455,15 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb);struct sock *udp6_lib_lookup_skb (const struct sk_buff *skb,__be16 sport,__be16 dport); + +#ifdef CONFIG_INET +void udp_sock_rfree (struct sk_buff *skb); +#else +static inline void udp_sock_rfree (struct sk_buff *skb) +{ +} +#endif +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor);/* UDP uses skb->dev_scratch to cache as much information as possible and avoid net/core/skmsg.c +21
-4@@ -7,6 +7,7 @@#include <net/sock.h> #include <net/tcp.h> +#include <net/udp.h> #include <net/tls.h> #include <trace/events/sock.h> @@ -576,6 +577,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, u32 off, u32 len, bool take_ref) { struct sock *sk = psock->sk; +bool is_udp =sk_is_udp (sk);struct sk_msg *msg;int err = -EAGAIN ;@@ -583,13 +585,20 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,if (!msg) goto out; -if (skb->sk != sk && take_ref) { +if (is_udp) { +if (unlikely (skb->destructor == udp_sock_rfree)) + goto enqueue; + +spin_lock_bh (&sk->sk_receive_queue.lock); + } + +if (is_udp || (skb->sk != sk && take_ref)) {if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - goto free; + goto unlock;if (!sk_rmem_schedule (sk, skb, skb->truesize)) - goto free; - }else { + goto unlock; + }else if (skb->sk == sk || !take_ref) {/* This is used in tcp_bpf_recvmsg_parser() to determine whether the * data originates from the socket's own protocol stack. No need to * refcount sk because msg's lifetime is bound to sk via the ingress_msg. @@ -604,11 +613,23 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,* into user buffers. */ skb_set_owner_r (skb, sk); + +if (is_udp) { +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + } + +enqueue: err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, take_ref);if (err <0 ) goto free;out:
return err;net/core/skmsg.c +4
-0+ +unlock: +if (is_udp) +spin_unlock_bh (&sk->sk_receive_queue.lock);free:
kfree(msg);
goto out;net/ipv4/udp.c +9
-1@@ -2039,6 +2039,15 @@ struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags, }EXPORT_SYMBOL (__skb_recv_udp); +void udp_sock_rfree (struct sk_buff *skb) +{ +struct sock *sk = skb->sk; + +spin_lock_bh (&sk->sk_receive_queue.lock); +sock_rfree (skb); +spin_unlock_bh (&sk->sk_receive_queue.lock); +} +int udp_read_skb (struct sock *sk,skb_read_actor_t recv_actor) {struct sk_buff *skb; --2.53.0.371.g1d285c8824-goog
There are too many protocol-specific checks in the generic skmsg code here. This should be abstracted out.
Quoted 8 lines
Also TCP also has a similar issue with
sk_forward_allocconcurrency between the backlog charge and recvmsg uncharge paths so the abstraction is necessary.I've put together a simple diff based on your patch for reference. I haven't tested it thoroughly, but it at least handles TCP and UDP separately through a callback, keeping the generic code clean.
I can follow up on TCP, but TCP needs another fix, which cannot be factored out.
Some wrong assumptions in your patch:
1)
tcp_psock_ingress_charge()usespsock->ingress_lock, but it does not protect anytcp_sockfields, especiallysk->sk_forwad_alloc.2) TCP memory accounting happens under
bh_lock_sock()_or_lock_sock(). sendmsg()/recvmsg() works underlock_sock()and TCP fast path puts skb to backlog iflock_sock()is held by userspace. This means even a simplebh_lock_sock()intcp_psock_ingress_charge()race with memory accounting happening underlock_sock().Since
sk_psock_skb_ingress()is called from bothGFP_KERNELandGFP_ATOMICcontext, the simplest fix would be to putlock_sock()insk_psock_backlog()for TCP, which is ugly though.Quoted 8 lines
OK, thanks.
Now my only concern is keeping UDP-specific logic out of
skmsg.c.We already have TCP-only
msg->sklogic there that you added. It just does not look like TCP code.The
msg->skfield marks ingress-self vs ingress-other, which is protocol-agnostic metadata. TCP happens to consume it for seq tracking, but the assignment itself has no TCP-specific check (nois_tcp_sk()or similar).Quoted 10 lines
If really needed, we can do such cleanup in bpf-next for TCP and UDP altogether (probably after another TCP fix ?).
We could use a function pointer so that UDP implements its own ingress charge in
udp_bpf.c, while other protocols just use a default no-op or even NULL.Quoted 9 lines
If TCP requires locking outside of the scope, there's no point adding one more extra layer/complexity just for UDP.
Even if we go that way, most likely we cannot get rid of protocol-specific code (UDP/TCP-only hook) and end up with the indirect call helpers, which will be expensive than simple if logic.
An alternative would be to add such
sk_psock_udp_skb_ingress()inudp_bpf.c, without adding any indirection layer.This way we don't need to design a new abstraction before the approach is settled. Just a UDP-specific ingress helper, which keeps things clean enough for now, and avoid scattering
is_udpchecks all oversk_psock_skb_ingress, just need one if (is_udp).
Quoted 8 lines
Quoted 5 lines
Thanks.
After thinking a while, since the UDP-specific behaviour only appears in the redirect-to-self path,
Are you saying
psock->sk==skb->skis always true andskb_set_owner_r()in the else case is not called for UDP ?
Before we figure out how to properly handle TCP, I'm wondering if we can take a simpler approach for UDP like this (diff below).
This keeps the original code cleaner — two sk_is_udp()
dispatch points in sk_psock_skb_ingress() and sk_psock_verdict_apply(), both routing to a dedicated sk_psock_skb_ingress_udp() that handles all UDP-specific locking and accounting in one place.
As for whether sk_psock_skb_ingress_udp() should live in udp_bpf.c instead of skmsg.c, I'm open to either way.
net/core/skmsg.c +52 -1
@@ -8,6 +8,7 @@#include <net/sock.h> #include <net/tcp.h> #include <net/tls.h> +#include <net/udp.h> #include <trace/events/sock.h> static bool sk_msg_try_coalesce_ok (struct sk_msg *msg,int elem_first_coalesce)
@@ -587,6 +588,48 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,static int sk_psock_skb_ingress_self (struct sk_psock *psock,struct sk_buff *skb,u32 off,u32 len,bool take_ref); +/* UDP needs sk_receive_queue.lock to protect sk_forward_alloc from concurrent +* modification by udp_rmem_release(). This handles both self-redirect and +* cross-socket redirect for UDP. +*/ +static int sk_psock_skb_ingress_udp (struct sk_psock *psock,struct sk_buff *skb, +u32 off,u32 len,bool take_ref) +{ +struct sock *sk = psock->sk; +struct sk_msg *msg; +int err; + +if (skb->destructor == udp_sock_rfree) { + msg =alloc_sk_msg (GFP_ATOMIC ); +if (unlikely (!msg)) +return -EAGAIN ; + goto enqueue; + } + + msg =alloc_sk_msg (take_ref ?GFP_KERNEL :GFP_ATOMIC ); +if (unlikely (!msg)) +return -EAGAIN ; + +spin_lock_bh (&sk->sk_receive_queue.lock); +if (atomic_read (&sk->sk_rmem_alloc) > sk->sk_rcvbuf || + !sk_rmem_schedule (sk, skb, skb->truesize)) { +spin_unlock_bh (&sk->sk_receive_queue.lock); +kfree (msg); +return -EAGAIN ; + } +skb_set_owner_r (skb, sk); +spin_unlock_bh (&sk->sk_receive_queue.lock); + + skb->destructor = udp_sock_rfree; + +enqueue: + err =sk_psock_skb_ingress_enqueue (skb, off, len, psock, sk, msg, + take_ref); +if (err <0 ) +kfree (msg); +return err; +} +static int sk_psock_skb_ingress (struct sk_psock *psock,struct sk_buff *skb,u32 off,u32 len) {
@@ -594,6 +637,9 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb,struct sk_msg *msg;int err; +if (sk_is_udp (sk)) +return sk_psock_skb_ingress_udp (psock, skb, off, len, true); +/* If we are receiving on the same sock skb->sk is already assigned, * skip memory accounting and owner transition seeing it already set * correctly.
@@ -1059,7 +1105,12 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb, off = stm->offset; len = stm->full_len; } - err =sk_psock_skb_ingress_self (psock, skb, off, len, false); +if (sk_is_udp (sk_other)) + err =sk_psock_skb_ingress_udp (psock, skb, + off, len, false); +else + err =sk_psock_skb_ingress_self (psock, skb, + off, len, false); }if (err <0 ) {spin_lock_bh (&psock->ingress_lock);