bpf: fix UAF in sock clone early bailouts
Subsystems (4)
PATCH 1/1 bpf: fix UAF in sock clone early bailouts
Similar to recent commit 9b51a6155d14 ("bpf,fork: wipe ->bpf_storage before bailouts that access it"), sk_clone() performs an initial shallow copy of the socket field ->sk_bpf_storage via sock_copy() for the cloned socket newsk.
If sk_clone() bails out early (e.g. if sk_filter_charge() fails) prior to calling bpf_sk_storage_clone(), newsk->sk_bpf_storage still points to the parent socket's BPF local storage. When newsk is subsequently freed via sk_free(), the deallocation path (__sk_destruct() -> bpf_sk_storage_free()) destroys the parent socket's BPF local storage, leading to a use-after-free (UAF) on the parent socket.
Fix this by resetting newsk->sk_bpf_storage to NULL immediately after sock_copy() in sk_clone(), and remove the now redundant initialization from bpf_sk_storage_clone().
--- net/core/bpf_sk_storage.c | 2 -- net/core/sock.c | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-)
net/core/bpf_sk_storage.c +0 -2
@@ -158,8 +158,6 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)struct bpf_local_storage_elem *selem;int ret =0 ; -RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); -rcu_read_lock_dont_migrate (); sk_storage =rcu_dereference (sk->sk_bpf_storage);
net/core/sock.c +3 -0
@@ -2492,6 +2492,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,sock_copy (newsk, sk); newsk->sk_prot_creator = prot; +#ifdef CONFIG_BPF_SYSCALL +RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); +#endif /* SANITY */ if (likely (newsk->sk_net_refcnt)) {
Signature · 1 line
2.55.0.795.g602f6c329a-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] BPF map corruption via shallow-copied sk_user_data and sk_reuseport_cb during sk_clone early bailouts.
- [High] Unbalanced decrement of global network timestamp static key during sk_clone early bailouts. --
--- Patch [1]: [PATCH bpf] bpf: fix UAF in sock clone early bailouts --- commit 81577dfc9f1b10000a3b1dfc19a5b577e986ec33
Author: Matt Bobrowski <[email protected]>
bpf: fix UAF in sock clone early bailouts
This commit fixes a use-after-free on parent socket's BPF local storage caused by an early bailout in sk_clone() prior to clearing newsk->sk_bpf_storage. By resetting newsk->sk_bpf_storage to NULL immediately after sock_copy(), the deallocation path won't destroy the parent socket's storage.
net/core/sock.c struct sock *sk_clone(const struct sock *sk, const gfp_t priority, · line 2497
Quoted 11 lines
net/core/sock.c +3
-0@@ -2492,6 +2492,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,sock_copy (newsk, sk); newsk->sk_prot_creator = prot; +#ifdef CONFIG_BPF_SYSCALL +RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); +#endif
[Severity: High]
This is a pre-existing issue, but I noticed there might be a similar issue with sk_reuseport_cb and sk_user_data. Since sock_copy() does a shallow copy, newsk initially points to the parent's sk_reuseport_cb and sk_user_data.
If sk_clone() bails out early (e.g., sk_filter_charge() fails):
net/core/sock.c:sk_clone() {
...
if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) {
...
goto free;
}
RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL);
...
}The bailout happens before sk_reuseport_cb is cleared. When newsk is freed, bpf_sk_reuseport_detach() can follow the copied sk_user_data to the parent's BPF map slot and write NULL to it:
kernel/bpf/reuseport_array.c:bpf_sk_reuseport_detach() {
...
RCU_INIT_POINTER(*socks, NULL);
...
}Does this corrupt the parent socket's BPF map during early bailouts?
[Severity: High]
This is also a pre-existing issue, but could the same early bailout pattern cause an unbalanced decrement of the global network timestamp static key?
sock_copy() copies SK_FLAGS_TIMESTAMP into newsk->sk_flags. The global timestamp counter is only incremented at the end of sk_clone():
net/core/sock.c:sk_clone() {
...
if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP)
net_enable_timestamp();
}However, during destruction of the aborted clone, the global counter is decremented unconditionally because the copied flag was set:
net/core/sock.c:__sk_destruct() {
...
sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);
...
}Could an early bailout skip the net_enable_timestamp() call but still execute the sock_disable_timestamp() call on free, leading to an underflow?
Quoted 2 lines
net/core/sock.c +0
-0/* SANITY */ if (likely (newsk->sk_net_refcnt)) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Daniel Borkmann
maintainerHi Matt, hi Kuniyuki,
Quoted 5 lines · On 7/9/26 5:20 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] BPF map corruption via shallow-copiedsk_user_dataandsk_reuseport_cbduringsk_cloneearly bailouts.
- [High] Unbalanced decrement of global network timestamp static key duringsk_cloneearly bailouts.
Do either one of you also have a chance to look into these?
Thanks a lot,
Daniel
Quoted 84 lines
--- Patch [1]: [PATCH bpf] bpf: fix UAF in sock clone early bailouts --- commit
81577dfc9f1b10000a3b1dfc19a5b577e986ec33
Author: Matt Bobrowski <[email protected]>bpf: fix UAF in sock clone early bailouts
This commit fixes a use-after-free on parent socket's BPF local storage caused by an early bailout in
sk_clone()prior to clearingnewsk->sk_bpf_storage. By resettingnewsk->sk_bpf_storageto NULL immediately aftersock_copy(), the deallocation path won't destroy the parent socket's storage.net/core/sock.c struct sock *sk_clone(const struct sock *sk, const gfp_t priority, · line 2497
Quoted 11 lines
net/core/sock.c +3
-0@@ -2492,6 +2492,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,sock_copy (newsk, sk); newsk->sk_prot_creator = prot; +#ifdef CONFIG_BPF_SYSCALL +RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); +#endif [Severity: High]
This is a pre-existing issue, but I noticed there might be a similar issue withsk_reuseport_cbandsk_user_data. Sincesock_copy()does a shallow copy, newsk initially points to the parent'ssk_reuseport_cbandsk_user_data.If
sk_clone()bails out early (e.g.,sk_filter_charge()fails):net/core/sock.c:sk_clone() { ... if (unlikely(!is_charged || xfrm_sk_clone_policy(newsk, sk))) { ... goto free; } RCU_INIT_POINTER(newsk->sk_reuseport_cb, NULL); ... }The bailout happens before
sk_reuseport_cbis cleared. When newsk is freed,bpf_sk_reuseport_detach()can follow the copiedsk_user_datato the parent's BPF map slot and write NULL to it:kernel/bpf/reuseport_array.c:bpf_sk_reuseport_detach() { ... RCU_INIT_POINTER(*socks, NULL); ... }Does this corrupt the parent socket's BPF map during early bailouts?
[Severity: High]
This is also a pre-existing issue, but could the same early bailout pattern cause an unbalanced decrement of the global network timestamp static key?
sock_copy()copiesSK_FLAGS_TIMESTAMPintonewsk->sk_flags. The global timestamp counter is only incremented at the end ofsk_clone():net/core/sock.c:sk_clone() { ... if (sock_needs_netstamp(sk) && newsk->sk_flags & SK_FLAGS_TIMESTAMP) net_enable_timestamp(); }However, during destruction of the aborted clone, the global counter is decremented unconditionally because the copied flag was set:
net/core/sock.c:__sk_destruct() { ... sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); ... }Could an early bailout skip the
net_enable_timestamp()call but still execute thesock_disable_timestamp()call on free, leading to an underflow?Quoted 2 lines
net/core/sock.c +0
-0/* SANITY */ if (likely (newsk->sk_net_refcnt)) {
Kuniyuki Iwashima
maintainerQuoted 11 lines · Daniel Borkmann
Hi Matt, hi Kuniyuki,
Quoted 5 lines · On 7/9/26 5:20 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] BPF map corruption via shallow-copiedsk_user_dataandsk_reuseport_cbduringsk_cloneearly bailouts.
- [High] Unbalanced decrement of global network timestamp static key duringsk_cloneearly bailouts.Do either one of you also have a chance to look into these?
I'll post a fix.
Thank you, Daniel !
[ +Kuniyuki ]
Quoted 12 lines · Matt Bobrowski
Similar to recent commit
9b51a6155d14("bpf,fork: wipe ->bpf_storage before bailouts that access it"),sk_clone()performs an initial shallow copy of the socket field ->sk_bpf_storage viasock_copy()for the cloned socket newsk.If
sk_clone()bails out early (e.g. ifsk_filter_charge()fails) prior to callingbpf_sk_storage_clone(),newsk->sk_bpf_storagestill points to the parent socket's BPF local storage. When newsk is subsequently freed viask_free(), the deallocation path (__sk_destruct()->bpf_sk_storage_free()) destroys the parent socket's BPF local storage, leading to a use-after-free (UAF) on the parent socket.
Quoted 8 lines
Fix this by resetting
newsk->sk_bpf_storageto NULL immediately aftersock_copy()insk_clone(), and remove the now redundant initialization frombpf_sk_storage_clone().Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")Fixes f12dd75959b0 ("bpf: net: Set sk_bpf_storage back to NULL for cloned sk")Signed-off-by Matt Bobrowski <[email protected]>
LGTM, thanks!
Quoted 31 lines
net/core/bpf_sk_storage.c | 2 -- net/core/sock.c | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-)net/core/bpf_sk_storage.c +0
-2@@ -158,8 +158,6 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)struct bpf_local_storage_elem *selem;int ret =0 ; -RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); -rcu_read_lock_dont_migrate (); sk_storage =rcu_dereference (sk->sk_bpf_storage);net/core/sock.c +3
-0@@ -2492,6 +2492,9 @@ struct sock *sk_clone(const struct sock *sk, const gfp_t priority,sock_copy (newsk, sk); newsk->sk_prot_creator = prot; +#ifdef CONFIG_BPF_SYSCALL +RCU_INIT_POINTER (newsk->sk_bpf_storage,NULL ); +#endif /* SANITY */ if (likely (newsk->sk_net_refcnt)) {
Quoted 20 lines · Daniel Borkmann
[ +Kuniyuki ]
Quoted 16 lines · Matt Bobrowski
Similar to recent commit
9b51a6155d14("bpf,fork: wipe ->bpf_storage before bailouts that access it"),sk_clone()performs an initial shallow copy of the socket field ->sk_bpf_storage viasock_copy()for the cloned socket newsk.If
sk_clone()bails out early (e.g. ifsk_filter_charge()fails) prior to callingbpf_sk_storage_clone(),newsk->sk_bpf_storagestill points to the parent socket's BPF local storage. When newsk is subsequently freed viask_free(), the deallocation path (__sk_destruct()->bpf_sk_storage_free()) destroys the parent socket's BPF local storage, leading to a use-after-free (UAF) on the parent socket.Fix this by resetting
newsk->sk_bpf_storageto NULL immediately aftersock_copy()insk_clone(), and remove the now redundant initialization frombpf_sk_storage_clone().
Quoted 8 lines
Quoted 4 lines
Fixes 6ac99e8f23d4 ("bpf: Introduce bpf sk local storage")Fixes f12dd75959b0 ("bpf: net: Set sk_bpf_storage back to NULL for cloned sk")Signed-off-by Matt Bobrowski <[email protected]>LGTM, thanks!
Acked-by Daniel Borkmann <[email protected]>
This patch was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:
Quoted 5 lines · On Thu, 9 Jul 2026 02:53:16 +0000 you wrote:
Similar to recent commit
9b51a6155d14("bpf,fork: wipe ->bpf_storage before bailouts that access it"),sk_clone()performs an initial shallow copy of the socket field ->sk_bpf_storage viasock_copy()for the cloned socket newsk.
Quoted 8 lines
If
sk_clone()bails out early (e.g. ifsk_filter_charge()fails) prior to callingbpf_sk_storage_clone(),newsk->sk_bpf_storagestill points to the parent socket's BPF local storage. When newsk is subsequently freed viask_free(), the deallocation path (__sk_destruct()->bpf_sk_storage_free()) destroys the parent socket's BPF local storage, leading to a use-after-free (UAF) on the parent socket.[...]
Here is the summary with links:
- [bpf] bpf: fix UAF in sock clone early bailouts
https://git.kernel.org/bpf/bpf/c/7cbd0c4cebe4
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html