netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue
@ 2025-12-23 20:35 bestswngs
  2025-12-24  8:52 ` Eric Dumazet
  2026-01-04 18:22 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 4+ messages in thread
From: bestswngs @ 2025-12-23 20:35 UTC (permalink / raw)
  To: security
  Cc: edumazet, davem, kuba, pabeni, horms, netdev, linux-kernel, xmei5,
	Weiming Shi

From: Weiming Shi <bestswngs@gmail.com>

skbuff_fclone_cache was created without defining a usercopy region,
[1] unlike skbuff_head_cache which properly whitelists the cb[] field.
[2] This causes a usercopy BUG() when CONFIG_HARDENED_USERCOPY is
enabled and the kernel attempts to copy sk_buff.cb data to userspace
via sock_recv_errqueue() -> put_cmsg().

The crash occurs when: 1. TCP allocates an skb using alloc_skb_fclone()
   (from skbuff_fclone_cache) [1]
2. The skb is cloned via skb_clone() using the pre-allocated fclone
[3] 3. The cloned skb is queued to sk_error_queue for timestamp
reporting 4. Userspace reads the error queue via recvmsg(MSG_ERRQUEUE)
5. sock_recv_errqueue() calls put_cmsg() to copy serr->ee from skb->cb
[4] 6. __check_heap_object() fails because skbuff_fclone_cache has no
   usercopy whitelist [5]

When cloned skbs allocated from skbuff_fclone_cache are used in the
socket error queue, accessing the sock_exterr_skb structure in skb->cb
via put_cmsg() triggers a usercopy hardening violation:

[    5.379589] usercopy: Kernel memory exposure attempt detected from SLUB object 'skbuff_fclone_cache' (offset 296, size 16)!
[    5.382796] kernel BUG at mm/usercopy.c:102!
[    5.383923] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
[    5.384903] CPU: 1 UID: 0 PID: 138 Comm: poc_put_cmsg Not tainted 6.12.57 #7
[    5.384903] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[    5.384903] RIP: 0010:usercopy_abort+0x6c/0x80
[    5.384903] Code: 1a 86 51 48 c7 c2 40 15 1a 86 41 52 48 c7 c7 c0 15 1a 86 48 0f 45 d6 48 c7 c6 80 15 1a 86 48 89 c1 49 0f 45 f3 e8 84 27 88 ff <0f> 0b 490
[    5.384903] RSP: 0018:ffffc900006f77a8 EFLAGS: 00010246
[    5.384903] RAX: 000000000000006f RBX: ffff88800f0ad2a8 RCX: 1ffffffff0f72e74
[    5.384903] RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffffffff87b973a0
[    5.384903] RBP: 0000000000000010 R08: 0000000000000000 R09: fffffbfff0f72e74
[    5.384903] R10: 0000000000000003 R11: 79706f6372657375 R12: 0000000000000001
[    5.384903] R13: ffff88800f0ad2b8 R14: ffffea00003c2b40 R15: ffffea00003c2b00
[    5.384903] FS:  0000000011bc4380(0000) GS:ffff8880bf100000(0000) knlGS:0000000000000000
[    5.384903] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[    5.384903] CR2: 000056aa3b8e5fe4 CR3: 000000000ea26004 CR4: 0000000000770ef0
[    5.384903] PKRU: 55555554
[    5.384903] Call Trace:
[    5.384903]  <TASK>
[    5.384903]  __check_heap_object+0x9a/0xd0
[    5.384903]  __check_object_size+0x46c/0x690
[    5.384903]  put_cmsg+0x129/0x5e0
[    5.384903]  sock_recv_errqueue+0x22f/0x380
[    5.384903]  tls_sw_recvmsg+0x7ed/0x1960
[    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
[    5.384903]  ? schedule+0x6d/0x270
[    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
[    5.384903]  ? mutex_unlock+0x81/0xd0
[    5.384903]  ? __pfx_mutex_unlock+0x10/0x10
[    5.384903]  ? __pfx_tls_sw_recvmsg+0x10/0x10
[    5.384903]  ? _raw_spin_lock_irqsave+0x8f/0xf0
[    5.384903]  ? _raw_read_unlock_irqrestore+0x20/0x40
[    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5

The crash offset 296 corresponds to skb2->cb within skbuff_fclones:
  - sizeof(struct sk_buff) = 232 - offsetof(struct sk_buff, cb) = 40 -
  offset of skb2.cb in fclones = 232 + 40 = 272 - crash offset 296 =
  272 + 24 (inside sock_exterr_skb.ee)

This patch uses a local stack variable as a bounce buffer to avoid the hardened usercopy check failure.

[1] https://elixir.bootlin.com/linux/v6.12.62/source/net/ipv4/tcp.c#L885 
[2] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5104
[3] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5566
[4] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5491
[5] https://elixir.bootlin.com/linux/v6.12.62/source/mm/slub.c#L5719

Fixes: 6d07d1cd300f ("usercopy: Restrict non-usercopy caches to size 0")
Reported-by: Xiang Mei <xmei5@asu.edu> 
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
v2: Fix the Commit Message
v3: Add "From" email address, Fix "CC" and "TO" email address
v4: Fix The Patch Code
v5: Use a bounce buffer for copying skb->mark

 net/core/sock.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/net/core/sock.c b/net/core/sock.c
index 45c98bf524b2..a1c8b47b0d56 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -3896,7 +3896,7 @@ void sock_enable_timestamp(struct sock *sk, enum sock_flags flag)
 int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
 		       int level, int type)
 {
-	struct sock_exterr_skb *serr;
+	struct sock_extended_err ee;
 	struct sk_buff *skb;
 	int copied, err;
 
@@ -3916,8 +3916,9 @@ int sock_recv_errqueue(struct sock *sk, struct msghdr *msg, int len,
 
 	sock_recv_timestamp(msg, sk, skb);
 
-	serr = SKB_EXT_ERR(skb);
-	put_cmsg(msg, level, type, sizeof(serr->ee), &serr->ee);
+	/* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
+	ee = SKB_EXT_ERR(skb)->ee;
+	put_cmsg(msg, level, type, sizeof(ee), &ee);
 
 	msg->msg_flags |= MSG_ERRQUEUE;
 	err = copied;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue
  2025-12-23 20:35 [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue bestswngs
@ 2025-12-24  8:52 ` Eric Dumazet
  2025-12-24 17:29   ` Weiming Shi
  2026-01-04 18:22 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Dumazet @ 2025-12-24  8:52 UTC (permalink / raw)
  To: bestswngs
  Cc: security, davem, kuba, pabeni, horms, netdev, linux-kernel, xmei5

On Tue, Dec 23, 2025 at 9:36 PM <bestswngs@gmail.com> wrote:
>
> From: Weiming Shi <bestswngs@gmail.com>
>
> skbuff_fclone_cache was created without defining a usercopy region,
> [1] unlike skbuff_head_cache which properly whitelists the cb[] field.
> [2] This causes a usercopy BUG() when CONFIG_HARDENED_USERCOPY is
> enabled and the kernel attempts to copy sk_buff.cb data to userspace
> via sock_recv_errqueue() -> put_cmsg().
>
> The crash occurs when: 1. TCP allocates an skb using alloc_skb_fclone()
>    (from skbuff_fclone_cache) [1]
> 2. The skb is cloned via skb_clone() using the pre-allocated fclone
> [3] 3. The cloned skb is queued to sk_error_queue for timestamp
> reporting 4. Userspace reads the error queue via recvmsg(MSG_ERRQUEUE)
> 5. sock_recv_errqueue() calls put_cmsg() to copy serr->ee from skb->cb
> [4] 6. __check_heap_object() fails because skbuff_fclone_cache has no
>    usercopy whitelist [5]
>
> When cloned skbs allocated from skbuff_fclone_cache are used in the
> socket error queue, accessing the sock_exterr_skb structure in skb->cb
> via put_cmsg() triggers a usercopy hardening violation:
>
> [    5.379589] usercopy: Kernel memory exposure attempt detected from SLUB object 'skbuff_fclone_cache' (offset 296, size 16)!
> [    5.382796] kernel BUG at mm/usercopy.c:102!
> [    5.383923] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
> [    5.384903] CPU: 1 UID: 0 PID: 138 Comm: poc_put_cmsg Not tainted 6.12.57 #7
> [    5.384903] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
> [    5.384903] RIP: 0010:usercopy_abort+0x6c/0x80
> [    5.384903] Code: 1a 86 51 48 c7 c2 40 15 1a 86 41 52 48 c7 c7 c0 15 1a 86 48 0f 45 d6 48 c7 c6 80 15 1a 86 48 89 c1 49 0f 45 f3 e8 84 27 88 ff <0f> 0b 490
> [    5.384903] RSP: 0018:ffffc900006f77a8 EFLAGS: 00010246
> [    5.384903] RAX: 000000000000006f RBX: ffff88800f0ad2a8 RCX: 1ffffffff0f72e74
> [    5.384903] RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffffffff87b973a0
> [    5.384903] RBP: 0000000000000010 R08: 0000000000000000 R09: fffffbfff0f72e74
> [    5.384903] R10: 0000000000000003 R11: 79706f6372657375 R12: 0000000000000001
> [    5.384903] R13: ffff88800f0ad2b8 R14: ffffea00003c2b40 R15: ffffea00003c2b00
> [    5.384903] FS:  0000000011bc4380(0000) GS:ffff8880bf100000(0000) knlGS:0000000000000000
> [    5.384903] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [    5.384903] CR2: 000056aa3b8e5fe4 CR3: 000000000ea26004 CR4: 0000000000770ef0
> [    5.384903] PKRU: 55555554
> [    5.384903] Call Trace:
> [    5.384903]  <TASK>
> [    5.384903]  __check_heap_object+0x9a/0xd0
> [    5.384903]  __check_object_size+0x46c/0x690
> [    5.384903]  put_cmsg+0x129/0x5e0
> [    5.384903]  sock_recv_errqueue+0x22f/0x380
> [    5.384903]  tls_sw_recvmsg+0x7ed/0x1960
> [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
> [    5.384903]  ? schedule+0x6d/0x270
> [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
> [    5.384903]  ? mutex_unlock+0x81/0xd0
> [    5.384903]  ? __pfx_mutex_unlock+0x10/0x10
> [    5.384903]  ? __pfx_tls_sw_recvmsg+0x10/0x10
> [    5.384903]  ? _raw_spin_lock_irqsave+0x8f/0xf0
> [    5.384903]  ? _raw_read_unlock_irqrestore+0x20/0x40
> [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
>

Nit : Next time you send a stack trace, please run it though
scripts/decode_stacktrace.sh
to get meaningful symbols.

> The crash offset 296 corresponds to skb2->cb within skbuff_fclones:
>   - sizeof(struct sk_buff) = 232 - offsetof(struct sk_buff, cb) = 40 -
>   offset of skb2.cb in fclones = 232 + 40 = 272 - crash offset 296 =
>   272 + 24 (inside sock_exterr_skb.ee)
>
> This patch uses a local stack variable as a bounce buffer to avoid the hardened usercopy check failure.
>
> [1] https://elixir.bootlin.com/linux/v6.12.62/source/net/ipv4/tcp.c#L885
> [2] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5104
> [3] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5566
> [4] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5491
> [5] https://elixir.bootlin.com/linux/v6.12.62/source/mm/slub.c#L5719
>
> Fixes: 6d07d1cd300f ("usercopy: Restrict non-usercopy caches to size 0")
> Reported-by: Xiang Mei <xmei5@asu.edu>
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> ---

Reviewed-by: Eric Dumazet <edumazet@google.com>

Also please next time do not CC security@kernel.org if you made the
bug public (say on netdev@kernel.org),
because security@kernel.org will be of no help.

Congratulations on your first linux contribution !

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue
  2025-12-24  8:52 ` Eric Dumazet
@ 2025-12-24 17:29   ` Weiming Shi
  0 siblings, 0 replies; 4+ messages in thread
From: Weiming Shi @ 2025-12-24 17:29 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: davem, kuba, pabeni, horms, netdev, linux-kernel, xmei5

On 25-12-24 09:52, Eric Dumazet wrote:
> On Tue, Dec 23, 2025 at 9:36 PM <bestswngs@gmail.com> wrote:
> >
> > From: Weiming Shi <bestswngs@gmail.com>
> >
> > skbuff_fclone_cache was created without defining a usercopy region,
> > [1] unlike skbuff_head_cache which properly whitelists the cb[] field.
> > [2] This causes a usercopy BUG() when CONFIG_HARDENED_USERCOPY is
> > enabled and the kernel attempts to copy sk_buff.cb data to userspace
> > via sock_recv_errqueue() -> put_cmsg().
> >
> > The crash occurs when: 1. TCP allocates an skb using alloc_skb_fclone()
> >    (from skbuff_fclone_cache) [1]
> > 2. The skb is cloned via skb_clone() using the pre-allocated fclone
> > [3] 3. The cloned skb is queued to sk_error_queue for timestamp
> > reporting 4. Userspace reads the error queue via recvmsg(MSG_ERRQUEUE)
> > 5. sock_recv_errqueue() calls put_cmsg() to copy serr->ee from skb->cb
> > [4] 6. __check_heap_object() fails because skbuff_fclone_cache has no
> >    usercopy whitelist [5]
> >
> > When cloned skbs allocated from skbuff_fclone_cache are used in the
> > socket error queue, accessing the sock_exterr_skb structure in skb->cb
> > via put_cmsg() triggers a usercopy hardening violation:
> >
> > [    5.379589] usercopy: Kernel memory exposure attempt detected from SLUB object 'skbuff_fclone_cache' (offset 296, size 16)!
> > [    5.382796] kernel BUG at mm/usercopy.c:102!
> > [    5.383923] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
> > [    5.384903] CPU: 1 UID: 0 PID: 138 Comm: poc_put_cmsg Not tainted 6.12.57 #7
> > [    5.384903] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
> > [    5.384903] RIP: 0010:usercopy_abort+0x6c/0x80
> > [    5.384903] Code: 1a 86 51 48 c7 c2 40 15 1a 86 41 52 48 c7 c7 c0 15 1a 86 48 0f 45 d6 48 c7 c6 80 15 1a 86 48 89 c1 49 0f 45 f3 e8 84 27 88 ff <0f> 0b 490
> > [    5.384903] RSP: 0018:ffffc900006f77a8 EFLAGS: 00010246
> > [    5.384903] RAX: 000000000000006f RBX: ffff88800f0ad2a8 RCX: 1ffffffff0f72e74
> > [    5.384903] RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffffffff87b973a0
> > [    5.384903] RBP: 0000000000000010 R08: 0000000000000000 R09: fffffbfff0f72e74
> > [    5.384903] R10: 0000000000000003 R11: 79706f6372657375 R12: 0000000000000001
> > [    5.384903] R13: ffff88800f0ad2b8 R14: ffffea00003c2b40 R15: ffffea00003c2b00
> > [    5.384903] FS:  0000000011bc4380(0000) GS:ffff8880bf100000(0000) knlGS:0000000000000000
> > [    5.384903] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> > [    5.384903] CR2: 000056aa3b8e5fe4 CR3: 000000000ea26004 CR4: 0000000000770ef0
> > [    5.384903] PKRU: 55555554
> > [    5.384903] Call Trace:
> > [    5.384903]  <TASK>
> > [    5.384903]  __check_heap_object+0x9a/0xd0
> > [    5.384903]  __check_object_size+0x46c/0x690
> > [    5.384903]  put_cmsg+0x129/0x5e0
> > [    5.384903]  sock_recv_errqueue+0x22f/0x380
> > [    5.384903]  tls_sw_recvmsg+0x7ed/0x1960
> > [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
> > [    5.384903]  ? schedule+0x6d/0x270
> > [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
> > [    5.384903]  ? mutex_unlock+0x81/0xd0
> > [    5.384903]  ? __pfx_mutex_unlock+0x10/0x10
> > [    5.384903]  ? __pfx_tls_sw_recvmsg+0x10/0x10
> > [    5.384903]  ? _raw_spin_lock_irqsave+0x8f/0xf0
> > [    5.384903]  ? _raw_read_unlock_irqrestore+0x20/0x40
> > [    5.384903]  ? srso_alias_return_thunk+0x5/0xfbef5
> >
> 
> Nit : Next time you send a stack trace, please run it though
> scripts/decode_stacktrace.sh
> to get meaningful symbols.
> 
> > The crash offset 296 corresponds to skb2->cb within skbuff_fclones:
> >   - sizeof(struct sk_buff) = 232 - offsetof(struct sk_buff, cb) = 40 -
> >   offset of skb2.cb in fclones = 232 + 40 = 272 - crash offset 296 =
> >   272 + 24 (inside sock_exterr_skb.ee)
> >
> > This patch uses a local stack variable as a bounce buffer to avoid the hardened usercopy check failure.
> >
> > [1] https://elixir.bootlin.com/linux/v6.12.62/source/net/ipv4/tcp.c#L885
> > [2] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5104
> > [3] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5566
> > [4] https://elixir.bootlin.com/linux/v6.12.62/source/net/core/skbuff.c#L5491
> > [5] https://elixir.bootlin.com/linux/v6.12.62/source/mm/slub.c#L5719
> >
> > Fixes: 6d07d1cd300f ("usercopy: Restrict non-usercopy caches to size 0")
> > Reported-by: Xiang Mei <xmei5@asu.edu>
> > Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> > ---
> 
> Reviewed-by: Eric Dumazet <edumazet@google.com>
> 
> Also please next time do not CC security@kernel.org if you made the
> bug public (say on netdev@kernel.org),
> because security@kernel.org will be of no help.
> 
> Congratulations on your first linux contribution !

Hi Eric,

Thank you very much for your quick review and encouragement.
I will take note of your suggestions next time.

Best regards,
Weiming




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue
  2025-12-23 20:35 [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue bestswngs
  2025-12-24  8:52 ` Eric Dumazet
@ 2026-01-04 18:22 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-01-04 18:22 UTC (permalink / raw)
  To: Weiming Shi
  Cc: security, edumazet, davem, kuba, pabeni, horms, netdev,
	linux-kernel, xmei5

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Wed, 24 Dec 2025 04:35:35 +0800 you wrote:
> From: Weiming Shi <bestswngs@gmail.com>
> 
> skbuff_fclone_cache was created without defining a usercopy region,
> [1] unlike skbuff_head_cache which properly whitelists the cb[] field.
> [2] This causes a usercopy BUG() when CONFIG_HARDENED_USERCOPY is
> enabled and the kernel attempts to copy sk_buff.cb data to userspace
> via sock_recv_errqueue() -> put_cmsg().
> 
> [...]

Here is the summary with links:
  - [net,v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue
    https://git.kernel.org/netdev/net/c/2a71a1a8d0ed

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-01-04 18:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-23 20:35 [PATCH net v5] net: sock: fix hardened usercopy panic in sock_recv_errqueue bestswngs
2025-12-24  8:52 ` Eric Dumazet
2025-12-24 17:29   ` Weiming Shi
2026-01-04 18:22 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).