Skip to contentSkip to conversation

netdevsim: fix use-after-free in nsim_create and __nsim_dev_port_del

PATCH In review Hrushiraj Gandhi 3 participants 3 messages Jun 23 – Jun 27

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

PATCH v2 1/1 netdevsim: fix use-after-free in nsim_create and __nsim_dev_port_del

debugfs files created under a port's ddir (ethtool/get_err, ethtool/set_err, ring params, bpf_offloaded_id, udp_ports/inject_error, etc.) store raw pointers directly into the netdevsim struct, which lives in the net_device private data kmalloc slab.

If these files outlive the netdevsim struct, a concurrent reader can trigger a slab-use-after-free by passing debugfs_file_get() (which only checks dentry lifetime) and then dereferencing the freed data pointer in debugfs_u32_get().

In __nsim_dev_port_del(), nsim_destroy() is called before nsim_dev_port_debugfs_exit(). However, nsim_destroy() calls free_netdev() at its end, while nsim_dev_port_debugfs_exit() removes the port's debugfs directory. This means the slab is freed before the debugfs files are removed.

The same window exists on nsim_create()'s error path:
nsim_ethtool_init() creates debugfs files under ddir with pointers into ns before nsim_init_netdevsim()/nsim_init_netdevsim_vf() which can fail, and the err_free_netdev label calls free_netdev() while those debugfs entries are still live.

Fix both paths by calling debugfs_remove_recursive() on the port's ddir before every free_netdev() call. The subsequent nsim_dev_port_debugfs_exit() calls become harmless no-ops since ddir is set to NULL.

Reported-by [email protected]
Fixes e05b2d141fef ("netdevsim: move netdev creation/destruction to dev probe")

---
v2:
- Also fix the same use-after-free window on the error path of nsim_create() as suggested by Simon Horman.
- Shorten the code comment in nsim_destroy() to be more concise.

 drivers/net/netdevsim/netdev.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/net/netdevsim/netdev.c +9 -0
index 27e5f109f933..f2824e75cddd 100644
@@ -1165,6 +1165,8 @@ struct netdevsim *nsim_create(struct nsim_dev *nsim_dev,
 	return ns;
 
 err_free_netdev:
+	debugfs_remove_recursive(nsim_dev_port->ddir);
+	nsim_dev_port->ddir = NULL;
 	free_netdev(dev);
 	return ERR_PTR(err);
 }
@@ -1214,6 +1216,13 @@ void nsim_destroy(struct netdevsim *ns)
 		ns->page = NULL;
 	}
 
+	/*
+	 * Remove per-port debugfs files before free_netdev() releases the
+	 * netdevsim struct to prevent use-after-free in concurrent readers.
+	 */
+	debugfs_remove_recursive(ns->nsim_dev_port->ddir);
+	ns->nsim_dev_port->ddir = NULL;
+
 	free_netdev(dev);
 }
 
Signature · 1 line
2.47.3
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 27 lines · Hrushiraj Gandhi

debugfs files created under a port's ddir (ethtool/get_err, ethtool/set_err, ring params, bpf_offloaded_id, udp_ports/inject_error, etc.) store raw pointers directly into the netdevsim struct, which lives in the net_device private data kmalloc slab.

If these files outlive the netdevsim struct, a concurrent reader can trigger a slab-use-after-free by passing debugfs_file_get() (which only checks dentry lifetime) and then dereferencing the freed data pointer in debugfs_u32_get().

In __nsim_dev_port_del(), nsim_destroy() is called before nsim_dev_port_debugfs_exit(). However, nsim_destroy() calls free_netdev() at its end, while nsim_dev_port_debugfs_exit() removes the port's debugfs directory. This means the slab is freed before the debugfs files are removed.

The same window exists on nsim_create()'s error path:
nsim_ethtool_init() creates debugfs files under ddir with pointers into ns before nsim_init_netdevsim()/nsim_init_netdevsim_vf() which can fail, and the err_free_netdev label calls free_netdev() while those debugfs entries are still live.

Fix both paths by calling debugfs_remove_recursive() on the port's ddir before every free_netdev() call. The subsequent nsim_dev_port_debugfs_exit() calls become harmless no-ops since ddir is set to NULL.

Quoted 8 lines
Reported-by [email protected]
Fixes e05b2d141fef ("netdevsim: move netdev creation/destruction to dev probe")

---
v2:
- Also fix the same use-after-free window on the error path of nsim_create() as suggested by Simon Horman.
- Shorten the code comment in nsim_destroy() to be more concise.

Thanks for the updates.

maintainer
#2 lore raw
In reply to Hrushiraj Gandhi
Quoted 4 lines · On Tue, 23 Jun 2026 20:14:47 +0530 Hrushiraj Gandhi wrote:

Fix both paths by calling debugfs_remove_recursive() on the port's ddir before every free_netdev() call. The subsequent nsim_dev_port_debugfs_exit() calls become harmless no-ops since ddir is set to NULL.

Looks like the wrong fix. All features clean up after themselves with the exception of ethtool. Save the ethtool ddir and remove just that one. This will align with how the other features behave.

Signature · 1 line
pw-bot: cr