Skip to contentSkip to conversation

net/mctp: publish the mctp_dev only after it is initialised

PATCH Applied Binbin Deng 4 participants 4 messages Sep 22 – Sep 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/mctp: publish the mctp_dev only after it is initialised

From: Binbin Deng <[email protected]>

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")
---
 net/mctp/device.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
net/mctp/device.c +8 -3
index 822120e860c8..b76715ea50e1 100644
@@ -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;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Binbin Deng

From: Binbin Deng <[email protected]>

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:

Quoted 8 lines

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")

---

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

Details in Documentation/process/maintainer-netdev.rst

maintainer
#2 lore raw
In reply to Binbin Deng

net/mctp/device.c static struct mctp_dev *mctp_add_dev(struct net_device *dev) · line 356

Quoted 6 lines · On Tue, 22 Sep 2026 21:19:19 +0800 (CST) Binbin Deng wrote:
net/mctp/device.c +6 -0
+	/*
+	 * 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

#3 lore raw
In reply to Binbin Deng
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 29 lines · On Tue, 22 Sep 2026 21:19:19 +0800 (CST) you wrote:

From: Binbin Deng <[email protected]>

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>
Quoted 1 line

[...]

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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html