Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH] can: j1939: fix possibility of UAF on module unload
@ 2026-12-01 15:26 Tetsuo Handa
  2026-12-01 15:44 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tetsuo Handa @ 2026-12-01 15:26 UTC (permalink / raw)
  To: linux-can

[-- Attachment #1: Type: text/plain, Size: 6586 bytes --]

Since module refcount is currently associated with lifecycle of j1939
sockets, we need to make sure that all resources associated with socket
("struct j1939_priv", "struct j1939_ecu", "struct j1939_session") are
synchronously destructed before socket is destructed. Otherwise,
j1939_module_exit() can complete and memory for this module is unmapped
and cause UAF.

Fix this problem by making the following changes.

(1) introduce a counter which tracks "struct j1939_priv" instances and
    works like __module_get(THIS_MODULE) and module_put(THIS_MODULE) but
    works in a way that prevents "rmmod can_j1939" from returning EBUSY

(2) defer release steps of "struct j1939_priv" to WQ context using
    a module-local WQ

(3) introduce a wait queue that waits at the beginning of
    j1939_module_exit() until the counter for (1) drops to 0

(4) wait for completion of (2) from j1939_module_exit() using
    destroy_workqueue()

This patch calls alloc_workqueue() with WQ_HIGHPRI flag when creating
j1939_wq, for we don't want to delay memory reclaim and completion of (3)
for too long. Subsequent patches might also defer release steps of "struct
j1939_ecu" and "struct j1939_session" to WQ context, for this patch will
guarantee that "rmmod can_j1939" no longer cause UAF.

This patch calls alloc_workqueue() with WQ_PERCPU flag when creating
j1939_wq due to commit 21c05ca88a54 ("workqueue: Add warnings and ensure
one among WQ_PERCPU or WQ_UNBOUND is present"). You can drop WQ_PERCPU flag
when backporting to older kernels.

Fixes: 9d71dd0c7009 ("can: add support of SAE J1939 protocol")
Cc: stable@vger.kernel.org
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
A reproducer, an output without this patch (kernel panic), and
an output with this patch (hung task) are attached. The hung task is
a result of "struct j1939_ecu" refcount leak, which happens because

  j1939_ecu_get(ecu);
  priv->ents[ecu->addr] = ecu;

in j1939_ecu_map_locked() from j1939_ecu_timer_handler() can succeed
even after

  priv->ents[ecu->addr] = NULL;
  j1939_ecu_put(ecu);

in j1939_ecu_unmap_locked() from j1939_ecu_unmap_all() from
__j1939_rx_release() from j1939_netdev_stop() has completed, which
will be addressed by subsequent patches.

 net/can/j1939/j1939-priv.h |  4 +++
 net/can/j1939/main.c       | 53 +++++++++++++++++++++++++++++++++-----
 2 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/net/can/j1939/j1939-priv.h b/net/can/j1939/j1939-priv.h
index cf26352d1d8c..ff90683f33f4 100644
--- a/net/can/j1939/j1939-priv.h
+++ b/net/can/j1939/j1939-priv.h
@@ -93,6 +93,8 @@ struct j1939_priv {
 
 	struct kref rx_kref;
 	u32 rx_tskey;
+
+	struct work_struct release_work;
 };
 
 void j1939_ecu_put(struct j1939_ecu *ecu);
@@ -345,4 +347,6 @@ void j1939_session_timers_cancel(struct j1939_session *session);
 /* CAN protocol */
 extern const struct can_proto j1939_can_proto;
 
+extern struct workqueue_struct *j1939_wq;
+
 #endif /* _J1939_PRIV_H_ */
diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c
index d9384cf0e356..858f9fa1a02f 100644
--- a/net/can/j1939/main.c
+++ b/net/can/j1939/main.c
@@ -20,6 +20,12 @@
 
 #include "j1939-priv.h"
 
+struct workqueue_struct *j1939_wq __ro_after_init;
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+static atomic_t j1939_unload_blocker_count;
+static DECLARE_WAIT_QUEUE_HEAD(j1939_unload_wait);
+#endif
+
 MODULE_DESCRIPTION("PF_CAN SAE J1939");
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("EIA Electronics (Kurt Van Dijck & Pieter Beyens)");
@@ -132,6 +138,10 @@ static struct j1939_priv *j1939_priv_create(struct net_device *ndev)
 	if (!priv)
 		return NULL;
 
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+	atomic_inc(&j1939_unload_blocker_count);
+#endif
+
 	rwlock_init(&priv->lock);
 	INIT_LIST_HEAD(&priv->ecus);
 	priv->ndev = ndev;
@@ -152,9 +162,9 @@ static inline void j1939_priv_set(struct net_device *ndev,
 	can_ml->j1939_priv = priv;
 }
 
-static void __j1939_priv_release(struct kref *kref)
+static void __j1939_priv_release_work(struct work_struct *work)
 {
-	struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
+	struct j1939_priv *priv = container_of(work, struct j1939_priv, release_work);
 	struct net_device *ndev = priv->ndev;
 
 	netdev_dbg(priv->ndev, "%s: 0x%p\n", __func__, priv);
@@ -165,6 +175,18 @@ static void __j1939_priv_release(struct kref *kref)
 
 	netdev_put(ndev, &priv->dev_tracker);
 	kfree(priv);
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+	if (atomic_dec_and_test(&j1939_unload_blocker_count))
+		wake_up(&j1939_unload_wait);
+#endif
+}
+
+static void __j1939_priv_release(struct kref *kref)
+{
+	struct j1939_priv *priv = container_of(kref, struct j1939_priv, kref);
+
+	INIT_WORK(&priv->release_work, __j1939_priv_release_work);
+	queue_work(j1939_wq, &priv->release_work);
 }
 
 void j1939_priv_put(struct j1939_priv *priv)
@@ -281,8 +303,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
 		 */
 		kref_get(&priv_new->rx_kref);
 		mutex_unlock(&j1939_netdev_lock);
-		netdev_put(ndev, &priv->dev_tracker);
-		kfree(priv);
+		j1939_priv_put(priv);
 		return priv_new;
 	}
 	j1939_priv_set(ndev, priv);
@@ -297,9 +318,7 @@ struct j1939_priv *j1939_netdev_start(struct net_device *ndev)
  out_priv_put:
 	j1939_priv_set(ndev, NULL);
 	mutex_unlock(&j1939_netdev_lock);
-
-	netdev_put(ndev, &priv->dev_tracker);
-	kfree(priv);
+	j1939_priv_put(priv);
 
 	return ERR_PTR(ret);
 }
@@ -401,6 +420,12 @@ static __init int j1939_module_init(void)
 
 	pr_info("can: SAE J1939\n");
 
+	j1939_wq = alloc_workqueue("j1939_wq", WQ_HIGHPRI | WQ_PERCPU, 0);
+	if (!j1939_wq) {
+		ret = -ENOMEM;
+		goto fail_wq;
+	}
+
 	ret = register_netdevice_notifier(&j1939_netdev_notifier);
 	if (ret)
 		goto fail_notifier;
@@ -416,14 +441,28 @@ static __init int j1939_module_init(void)
  fail_sk:
 	unregister_netdevice_notifier(&j1939_netdev_notifier);
  fail_notifier:
+	destroy_workqueue(j1939_wq);
+ fail_wq:
 	return ret;
 }
 
 static __exit void j1939_module_exit(void)
 {
+#if defined(MODULE) && defined(CONFIG_MODULE_UNLOAD)
+	/* Wait until all "struct j1939_priv", "struct j1939_ecu" and
+	 * "struct j1939_session" are released.
+	 */
+	wait_event(j1939_unload_wait, !atomic_read(&j1939_unload_blocker_count));
+#endif
+
 	can_proto_unregister(&j1939_can_proto);
 
 	unregister_netdevice_notifier(&j1939_netdev_notifier);
+
+	/* Wait until __j1939_priv_release_work() completes
+	 * before unmapping memory for this module.
+	 */
+	destroy_workqueue(j1939_wq);
 }
 
 module_init(j1939_module_init);
-- 
2.56.1

[-- Attachment #2: after.txt --]
[-- Type: text/plain, Size: 2949 bytes --]

[   69.466314][  T483] CAN device driver interface
[   69.509012][  T483] vcan: Virtual CAN interface driver
[   69.591791][  T484] can: controller area network core
[   69.596952][  T484] NET: Registered PF_CAN protocol family
[   69.654037][  T485] can: SAE J1939
[  247.998077][  T112] INFO: task rmmod:486 blocked for more than 122 seconds.
[  248.002980][  T112]       Not tainted 7.4.0-rc5+ #2
[  248.004950][  T112] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
[  248.009822][  T112] task:rmmod           state:D stack:0     pid:486   tgid:486   ppid:480    task_flags:0x400100 flags:0x00080000
[  248.018353][  T112] Call Trace:
[  248.021277][  T112]  <TASK>
[  248.023830][  T112]  __schedule+0x81a/0x1ec0
[  248.027454][  T112]  ? lock_acquire+0xeb/0x2b0
[  248.031029][  T112]  ? schedule+0x37/0x100
[  248.034549][  T112]  ? lock_release+0xfc/0x350
[  248.038253][  T112]  ? schedule+0x37/0x100
[  248.041677][  T112]  schedule+0xb2/0x100
[  248.045178][  T112]  cleanup_module+0x68/0x630 [can_j1939]
[  248.049746][  T112]  ? __pfx_autoremove_wake_function+0x10/0x10
[  248.054490][  T112]  __se_sys_delete_module+0x227/0x360
[  248.058666][  T112]  do_syscall_64+0x125/0x490
[  248.062695][  T112]  ? trace_irq_disable+0x1d/0xa0
[  248.066581][  T112]  ? prandom_u32_state+0xd/0x80
[  248.070714][  T112]  ? do_syscall_64+0x77/0x490
[  248.074674][  T112]  ? entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  248.079733][  T112]  ? trace_irq_disable+0x1d/0xa0
[  248.083807][  T112]  ? clear_bhb_loop+0x60/0xb0
[  248.087841][  T112]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  248.092479][  T112] RIP: 0033:0x7f5c79bec3eb
[  248.096368][  T112] RSP: 002b:00007ffca4a1d9b8 EFLAGS: 00000206 ORIG_RAX: 00000000000000b0
[  248.103144][  T112] RAX: ffffffffffffffda RBX: 000055a86c4184b0 RCX: 00007f5c79bec3eb
[  248.109703][  T112] RDX: 0000000000000000 RSI: 0000000000000800 RDI: 000055a86c418510
[  248.115717][  T112] RBP: 00007ffca4a1d9e0 R08: 0000000000000000 R09: 0000000000000000
[  248.121578][  T112] R10: 0000000000000000 R11: 0000000000000206 R12: 0000000000000000
[  248.126220][  T112] R13: 000055a86c4184b0 R14: 00007ffca4a1dc30 R15: 0000000000000000
[  248.130883][  T112]  </TASK>
[  248.133399][  T112] 
[  248.133399][  T112] Showing all locks held in the system:
[  248.138909][  T112] locks held by pr/ttyS0/111: 2, on CPU#9:
[  248.143268][  T112]  #0: ffffffff99eaedd0 (console_srcu){....}-{0:0}, at: console_srcu_read_lock+0x35/0x70
[  248.150256][  T112]  #1: ffffffff9cc0b308 (&port_lock_key){-.-.}-{3:3}, at: univ8250_console_device_lock+0x3a/0x60
[  248.156729][  T112] locks held by khungtaskd/112: 1, on CPU#1:
[  248.160386][  T112]  #0: ffffffff99fc2978 (rcu_read_lock){....}-{1:3}, at: debug_show_all_locks+0x2e/0x1e0
[  248.166912][  T112] 
[  248.169111][  T112] =============================================
[  248.169111][  T112] 

[-- Attachment #3: before.txt --]
[-- Type: text/plain, Size: 4489 bytes --]

[   63.587104][  T475] CAN device driver interface
[   63.631676][  T475] vcan: Virtual CAN interface driver
[   63.718527][  T476] can: controller area network core
[   63.724163][  T476] NET: Registered PF_CAN protocol family
[   63.780853][  T477] can: SAE J1939
[   64.044042][    C9] Oops: int3: 0000 [#1] SMP PTI
[   64.047645][    C9] CPU: 9 UID: 0 PID: 0 Comm: swapper/9 Not tainted 7.4.0-rc5+ #2 PREEMPT(full) 
[   64.054283][    C9] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 11/12/2020
[   64.063389][    C9] RIP: 0010:0xffffffffc04b9a91
[   64.067243][    C9] Code: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc <cc> cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[   64.083226][    C9] RSP: 0018:ffffcd2140ec0eb8 EFLAGS: 00000246
[   64.088078][    C9] RAX: ffffffff9d4aa3db RBX: ffffffffc04b9a90 RCX: ffff8cfe024f0000
[   64.094658][    C9] RDX: 0000000000000100 RSI: ffff8cfeb00a21d8 RDI: ffff8cfe0a9a4da0
[   64.101387][    C9] RBP: ffff8cfeb00a2480 R08: 0000000000000000 R09: 0000000000000001
[   64.107715][    C9] R10: ffff8cfe024f0000 R11: ffffffffc04b9a90 R12: ffff8cfeb00a21c0
[   64.114316][    C9] R13: ffff8cfe024f0000 R14: 0000000000000005 R15: ffff8cfe0a9a4da0
[   64.120587][    C9] FS:  0000000000000000(0000) GS:ffff8cff0ebb7000(0000) knlGS:0000000000000000
[   64.127724][    C9] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   64.133245][    C9] CR2: 00007f90651da940 CR3: 00000001b7e66002 CR4: 0000000000370ef0
[   64.140087][    C9] Call Trace:
[   64.142592][    C9]  <IRQ>
[   64.144598][    C9]  ? __hrtimer_run_queues+0x19a/0x410
[   64.149211][    C9]  ? hrtimer_run_softirq+0xb9/0x200
[   64.152993][    C9]  ? handle_softirqs+0x1bf/0x540
[   64.157329][    C9]  ? __irq_exit_rcu+0x86/0x170
[   64.161882][    C9]  ? irq_exit_rcu+0xe/0x20
[   64.165847][    C9]  ? sysvec_apic_timer_interrupt+0x6c/0x80
[   64.170840][    C9]  </IRQ>
[   64.173565][    C9]  <TASK>
[   64.176105][    C9]  ? asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   64.181526][    C9]  ? do_idle+0x115/0x360
[   64.184988][    C9]  ? tick_nohz_idle_stop_tick+0xd7/0xf0
[   64.189691][    C9]  ? tick_nohz_idle_stop_tick+0xd7/0xf0
[   64.194579][    C9]  ? do_idle+0x115/0x360
[   64.198163][    C9]  ? pv_native_safe_halt+0xf/0x20
[   64.202838][    C9]  ? do_idle+0x115/0x360
[   64.206410][    C9]  ? default_idle+0x9/0x20
[   64.210207][    C9]  ? default_idle_call+0x70/0xb0
[   64.214366][    C9]  ? do_idle+0x115/0x360
[   64.217851][    C9]  ? cpu_startup_entry+0x29/0x30
[   64.222512][    C9]  ? start_secondary+0x9f/0xa0
[   64.226519][    C9]  ? common_startup_64+0x13e/0x150
[   64.230893][    C9]  </TASK>
[   64.233475][    C9] Modules linked in: can vcan can_dev xfs nvme mptspi usbhid nvme_core mptscsih nvme_keyring mptbase nvme_auth scsi_transport_spi serio_raw fuse scsi_dh_alua scsi_dh_rdac scsi_dh_emc [last unloaded: can_j1939]
[   64.250881][    C9] ---[ end trace 0000000000000000 ]---
[   64.254859][    C9] RIP: 0010:0xffffffffc04b9a91
[   64.254883][    C9] Code: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc <cc> cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc
[   64.254894][    C9] RSP: 0018:ffffcd2140ec0eb8 EFLAGS: 00000246
[   64.254904][    C9] RAX: ffffffff9d4aa3db RBX: ffffffffc04b9a90 RCX: ffff8cfe024f0000
[   64.254912][    C9] RDX: 0000000000000100 RSI: ffff8cfeb00a21d8 RDI: ffff8cfe0a9a4da0
[   64.254920][    C9] RBP: ffff8cfeb00a2480 R08: 0000000000000000 R09: 0000000000000001
[   64.254926][    C9] R10: ffff8cfe024f0000 R11: ffffffffc04b9a90 R12: ffff8cfeb00a21c0
[   64.254933][    C9] R13: ffff8cfe024f0000 R14: 0000000000000005 R15: ffff8cfe0a9a4da0
[   64.254941][    C9] FS:  0000000000000000(0000) GS:ffff8cff0ebb7000(0000) knlGS:0000000000000000
[   64.254949][    C9] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   64.254956][    C9] CR2: 00007f90651da940 CR3: 00000001b7e66002 CR4: 0000000000370ef0
[   64.255027][    C9] Kernel panic - not syncing: Fatal exception in interrupt
[   64.337763][    C9] Kernel Offset: 0x1c000000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
[   64.602413][    C9] ---[ end Kernel panic - not syncing: Fatal exception in interrupt ]---

[-- Attachment #4: repro.c --]
[-- Type: text/plain, Size: 1331 bytes --]

#define _GNU_SOURCE
#include <fcntl.h>
//#include <sched.h>
//#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/can.h>
#include <linux/can/j1939.h>

static void execute_one(void)
{
	struct sockaddr_can addr = { };
	struct iovec iov = { .iov_base = "\x08\x07\x06\x05\x04\x03\x02\x01", .iov_len = 8 };
	struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
	int idx = 0;
	const int ONE = 1;
	const int fd = socket(0x1d, 2, 7);
	char buf[20] = "vcan0";

	if (ioctl(fd, 0x8933, buf) != -1)
		idx = *(uint32_t*)(buf + 16);
	setsockopt(fd, 1, 6, &ONE, sizeof(ONE));
	addr.can_family = 0x1d;
	addr.can_ifindex = idx;
	addr.can_addr.j1939.name = 0x102030405060708;
	addr.can_addr.j1939.addr= 0x20;
	bind(fd, (struct sockaddr *) &addr, 0x18);
	memset(&addr.can_addr.j1939, 0, sizeof(addr.can_addr.j1939));
	addr.can_addr.j1939.pgn = htonl(0x00ee0000);
	addr.can_addr.j1939.addr = 0xff;
	connect(fd, (struct sockaddr *) &addr, 0x18);
	sendmsg(fd, &msg, 0);
	close(fd);
}

int main(int argc, char *argv[])
{
	//unshare(CLONE_NEWNET);
	system("ip link add dev vcan0 type vcan; ip link set up vcan0");
	execute_one();
	system("rmmod can_j1939");
	return 0;
}

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

end of thread, other threads:[~2026-12-01 15:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:26 [PATCH] can: j1939: fix possibility of UAF on module unload Tetsuo Handa
2026-12-01 15:44 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox