mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised
@ 2026-09-22 13:19 Binbin Deng
  2026-09-22 14:22 ` Eric Dumazet
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Binbin Deng @ 2026-09-22 13:19 UTC (permalink / raw)
  To: jk, matt, davem, edumazet, kuba, pabeni
  Cc: horms, netdev, linux-kernel, Binbin Deng

From: Binbin Deng <18983559317@163.com>

mctp_add_dev() links the new mctp_dev into dev->mctp_ptr before mdev->dev
is assigned, so a reader that picks the device up in that window
dereferences a net_device pointer that is still NULL:

KASAN: null-ptr-deref in range [0x00000000000000b0-0x00000000000000b7]
Call Trace:
 <TASK>
 ? __pfx_mctp_sendmsg (./include/linux/sockptr.h:49)
 ? __pfx_mctp_dst_output (net/mctp/route.c:41)
 ? selinux_socket_sendmsg (security/selinux/hooks.c:5278)
 ____sys_sendmsg (net/socket.c:775 (discriminator 1) net/socket.c:790
 (discriminator 1) net/socket.c:2684 (discriminator 1))
 ? __pfx_____sys_sendmsg (net/socket.c:1131)
 ? __pfx_copy_msghdr_from_user (net/socket.c:2590)
 ? update_cfs_rq_load_avg (kernel/sched/fair.c:5478)
 ___sys_sendmsg (net/socket.c:2738)
 ? __pfx____sys_sendmsg (net/socket.c:2625)
 ? perf_event_task_tick (./include/linux/rcupdate.h:838)
 ? sched_tick (kernel/sched/core.c:5818)
 ? clockevents_program_event (kernel/time/clockevents.c:372)
 ? fdget (./include/linux/rcupdate.h:873 fs/file.c:1100)
 __sys_sendmsg (net/socket.c:2770)
 ? __pfx___sys_sendmsg (net/socket.c:2751)
 do_syscall_64 (arch/x86/entry/syscall_64.c:63)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
 </TASK>

Fix by publishing only once the object is fully initialised:
__mctp_dev_get() runs under rcu_read_lock() only and hands
this object to readers as soon as mctp_ptr is assigned, so
they must never observe mdev->dev == NULL.

Fixes: 583be982d934 ("mctp: Add device handling and netlink interface")
Signed-off-by: Binbin Deng <18983559317@163.com>
---
 net/mctp/device.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/net/mctp/device.c b/net/mctp/device.c
index 822120e860c8..b76715ea50e1 100644
--- a/net/mctp/device.c
+++ b/net/mctp/device.c
@@ -344,13 +344,18 @@ static struct mctp_dev *mctp_add_dev(struct net_device *dev)
 
 	mdev->net = mctp_default_net(dev_net(dev));
 
-	/* associate to net_device */
 	refcount_set(&mdev->refs, 1);
-	rcu_assign_pointer(dev->mctp_ptr, mdev);
-
 	dev_hold(dev);
 	mdev->dev = dev;
 
+	/*
+	 * Associate to net_device.  Publish only once the object is fully
+	 * initialised: __mctp_dev_get() runs under rcu_read_lock() only and
+	 * hands this object to readers as soon as mctp_ptr is assigned, so
+	 * they must never observe mdev->dev == NULL.
+	 */
+	rcu_assign_pointer(dev->mctp_ptr, mdev);
+
 	return mdev;
 }
 
-- 
2.43.0

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

* Re: [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised
  2026-09-22 13:19 [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised Binbin Deng
@ 2026-09-22 14:22 ` Eric Dumazet
  2026-09-25 23:35 ` Jakub Kicinski
  2026-09-25 23:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Dumazet @ 2026-09-22 14:22 UTC (permalink / raw)
  To: Binbin Deng
  Cc: jk, matt, davem, kuba, pabeni, horms, netdev, linux-kernel, Binbin Deng

On Tue, Sep 22, 2026 at 3:19 PM Binbin Deng <18983559317@163.com> wrote:
>
> From: Binbin Deng <18983559317@163.com>
>
> mctp_add_dev() links the new mctp_dev into dev->mctp_ptr before mdev->dev
> is assigned, so a reader that picks the device up in that window
> dereferences a net_device pointer that is still NULL:
>
>

> Fix by publishing only once the object is fully initialised:
> __mctp_dev_get() runs under rcu_read_lock() only and hands
> this object to readers as soon as mctp_ptr is assigned, so
> they must never observe mdev->dev == NULL.
>
> Fixes: 583be982d934 ("mctp: Add device handling and netlink interface")
> Signed-off-by: Binbin Deng <18983559317@163.com>
> ---

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

Please next time wait ~24 hours before sending a new version.

Details in Documentation/process/maintainer-netdev.rst

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

* Re: [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised
  2026-09-22 13:19 [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised Binbin Deng
  2026-09-22 14:22 ` Eric Dumazet
@ 2026-09-25 23:35 ` Jakub Kicinski
  2026-09-25 23:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Jakub Kicinski @ 2026-09-25 23:35 UTC (permalink / raw)
  To: Binbin Deng
  Cc: jk, matt, davem, edumazet, pabeni, horms, netdev, linux-kernel,
	Binbin Deng

On Tue, 22 Sep 2026 21:19:19 +0800 (CST) Binbin Deng wrote:
> +	/*
> +	 * Associate to net_device.  Publish only once the object is fully
> +	 * initialised: __mctp_dev_get() runs under rcu_read_lock() only and
> +	 * hands this object to readers as soon as mctp_ptr is assigned, so
> +	 * they must never observe mdev->dev == NULL.
> +	 */

Too obvious to include, I'll trim when applying

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

* Re: [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised
  2026-09-22 13:19 [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised Binbin Deng
  2026-09-22 14:22 ` Eric Dumazet
  2026-09-25 23:35 ` Jakub Kicinski
@ 2026-09-25 23:40 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-09-25 23:40 UTC (permalink / raw)
  To: Binbin Deng
  Cc: jk, matt, davem, edumazet, kuba, pabeni, horms, netdev,
	linux-kernel, dengbinbin21

Hello:

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

On Tue, 22 Sep 2026 21:19:19 +0800 (CST) you wrote:
> From: Binbin Deng <18983559317@163.com>
> 
> mctp_add_dev() links the new mctp_dev into dev->mctp_ptr before mdev->dev
> is assigned, so a reader that picks the device up in that window
> dereferences a net_device pointer that is still NULL:
> 
> KASAN: null-ptr-deref in range [0x00000000000000b0-0x00000000000000b7]
> Call Trace:
>  <TASK>
>  ? __pfx_mctp_sendmsg (./include/linux/sockptr.h:49)
>  ? __pfx_mctp_dst_output (net/mctp/route.c:41)
>  ? selinux_socket_sendmsg (security/selinux/hooks.c:5278)
>  ____sys_sendmsg (net/socket.c:775 (discriminator 1) net/socket.c:790
>  (discriminator 1) net/socket.c:2684 (discriminator 1))
>  ? __pfx_____sys_sendmsg (net/socket.c:1131)
>  ? __pfx_copy_msghdr_from_user (net/socket.c:2590)
>  ? update_cfs_rq_load_avg (kernel/sched/fair.c:5478)
>  ___sys_sendmsg (net/socket.c:2738)
>  ? __pfx____sys_sendmsg (net/socket.c:2625)
>  ? perf_event_task_tick (./include/linux/rcupdate.h:838)
>  ? sched_tick (kernel/sched/core.c:5818)
>  ? clockevents_program_event (kernel/time/clockevents.c:372)
>  ? fdget (./include/linux/rcupdate.h:873 fs/file.c:1100)
>  __sys_sendmsg (net/socket.c:2770)
>  ? __pfx___sys_sendmsg (net/socket.c:2751)
>  do_syscall_64 (arch/x86/entry/syscall_64.c:63)
>  entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
>  </TASK>
> 
> [...]

Here is the summary with links:
  - [net,v2] net/mctp: publish the mctp_dev only after it is initialised
    https://git.kernel.org/netdev/net/c/aadf655e8ea9

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-09-25 23:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-22 13:19 [PATCH net v2] net/mctp: publish the mctp_dev only after it is initialised Binbin Deng
2026-09-22 14:22 ` Eric Dumazet
2026-09-25 23:35 ` Jakub Kicinski
2026-09-25 23:40 ` 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

all inboxes | Powered by JetHome®