Netdev List
 help / color / mirror / Atom feed
* [PATCH net] netdevsim: take bus device refcount before registering device
@ 2026-08-31 18:39 Adriano Cordova
  2026-09-02  0:06 ` Jakub Kicinski
  0 siblings, 1 reply; 2+ messages in thread
From: Adriano Cordova @ 2026-08-31 18:39 UTC (permalink / raw)
  To: Jakub Kicinski, netdev
  Cc: Andrew Lunn, David S. Miller, Eric Dumazet, Paolo Abeni,
	linux-kernel, Adriano Cordova, syzbot+989ff6506f5c6d1ff56b

nsim_bus_dev_release() drops nsim_bus_devs when a device is freed, but
the matching increment was done in new_device_store() only after
nsim_bus_dev_new() returned successfully.  When device_register() failed
inside nsim_bus_dev_new(), put_device() invoked the release callback and
decremented nsim_bus_devs without the increment, driving the refcount to
zero.  A later successful device creation then incremented from zero and
triggered:

  refcount_t: addition on 0; use-after-free.

Increment nsim_bus_devs in nsim_bus_dev_new() before device_register() so
the release callback's decrement is balanced on both the success and error
paths.

Reported-by: syzbot+989ff6506f5c6d1ff56b@syzkaller.appspot.com
Closes: https://syzkaller.appspot.com/bug?extid=989ff6506f5c6d1ff56b
Fixes: 6aff7cbfe7bfa ("netdevsim: Block until all devices are released")
Signed-off-by: Adriano Cordova <adrianox@gmail.com>
---
 drivers/net/netdevsim/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c
index 5c55c308487b..426fb438b922 100644
--- a/drivers/net/netdevsim/bus.c
+++ b/drivers/net/netdevsim/bus.c
@@ -194,7 +194,6 @@ new_device_store(const struct bus_type *bus, const char *buf, size_t count)
 		goto err;
 	}
 
-	refcount_inc(&nsim_bus_devs);
 	/* Allow using nsim_bus_dev */
 	smp_store_release(&nsim_bus_dev->init, true);
 
@@ -465,6 +464,7 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queu
 	/* Disallow using nsim_bus_dev */
 	smp_store_release(&nsim_bus_dev->init, false);
 
+	refcount_inc(&nsim_bus_devs);
 	err = device_register(&nsim_bus_dev->dev);
 	if (err)
 		goto err_nsim_bus_dev_id_free;
-- 
2.51.0


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

end of thread, other threads:[~2026-09-02  0:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-31 18:39 [PATCH net] netdevsim: take bus device refcount before registering device Adriano Cordova
2026-09-02  0:06 ` Jakub Kicinski

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