Technical Root Cause Analysis
1. Involved Subsystems, Files, and Entities
- Source File:
drivers/infiniband/core/device.c
- Functions:
remove_one_compat_dev(), remove_compat_devs(), disable_device(), rdma_dev_exit_net(), rdma_init_coredev()
- Structs & Fields:
struct ib_device (device->compat_devs, device->compat_devs_mutex)
struct ib_core_device (cdev->rdma_net, cdev->dev)
struct net (net->ns.ns_id)
struct kernfs_node (sd->ns)
struct class ib_class (ib_class.namespace, ib_class.ns_type)
2. Lifecycle and Invariants
RDMA devices support compat devices in network namespaces other than the device's primary namespace.
- In
add_one_compat_dev() (drivers/infiniband/core/device.c:993), rdma_init_coredev() associates the compat device (cdev) with the target namespace via write_pnet(&coredev->rdma_net, read_pnet(&rnet->net)). Compat devices do not increment the reference count of struct net.
- Because
cdev->dev.class = &ib_class defines .namespace = net_namespace (drivers/infiniband/core/device.c:550), sysfs directory and symlink kernfs nodes for cdev->dev store targ->sd->ns = to_ns_common(net) (&net->ns).
- Removal of compat devices can be triggered in two paths:
- When an RDMA device is unregistered:
__ib_unregister_device() $\rightarrow$ disable_device() $\rightarrow$ remove_compat_devs() $\rightarrow$ remove_one_compat_dev().
- When a network namespace exits:
cleanup_net() $\rightarrow$ rdma_dev_exit_net() $\rightarrow$ remove_one_compat_dev().
Because cdev does not take a reference on struct net, the invariant requires that all sysfs nodes referencing net (cdev->dev) are fully deleted (device_del(&cdev->dev)) before rdma_dev_exit_net() returns and allows cleanup_net() to free struct net.
3. Root Cause and Trigger Mechanism
In remove_one_compat_dev() (drivers/infiniband/core/device.c:1027-1039):
static void remove_one_compat_dev(struct ib_device *device, u32 id)
{
struct ib_core_device *cdev;
mutex_lock(&device->compat_devs_mutex);
cdev = xa_erase(&device->compat_devs, id);
mutex_unlock(&device->compat_devs_mutex);
if (cdev) {
ib_free_port_attrs(cdev);
device_del(&cdev->dev);
put_device(&cdev->dev);
}
}
remove_one_compat_dev() releases device->compat_devs_mutex immediately after erasing cdev from device->compat_devs, invoking device_del(&cdev->dev) outside the lock.
When IB device unregistration races with network namespace teardown:
- Device unregistration thread (e.g.
nldev_dellink):
Calls remove_one_compat_dev(device, rnet->id). It acquires compat_devs_mutex, erases cdev = xa_erase(&device->compat_devs, id), drops compat_devs_mutex, and enters device_del(&cdev->dev).
- Netns cleanup thread (
cleanup_net workqueue):
rdma_dev_exit_net(net) iterates over devices and invokes remove_one_compat_dev(dev, rnet->id) for the same device and namespace ID. It takes compat_devs_mutex, but xa_erase() returns NULL because the entry was already erased by the device unregistration thread. It drops compat_devs_mutex and returns immediately without synchronizing or waiting for the in-progress device_del(&cdev->dev).
rdma_dev_exit_net() finishes its device iteration and returns.
cleanup_net() finishes ops_exit_list(net) and calls net_complete_free(net) $\rightarrow$ kmem_cache_free(net_namespace, net), deallocating the struct net slab object.
- Device unregistration thread:
Still executing
device_del(&cdev->dev), it proceeds to device_remove_class_symlinks(&cdev->dev) $\rightarrow$ sysfs_delete_link(&sp->subsys.kobj, &dev->kobj, dev_name(dev)).
In sysfs_delete_link(), ns = targ->sd->ns is retrieved from cdev->dev.kobj.sd->ns (which points to &net->ns of the now-freed struct net), and passed to kernfs_remove_by_name_ns(..., ns) $\rightarrow$ kernfs_find_ns() $\rightarrow$ kernfs_name_hash(name, ns) $\rightarrow$ kernfs_ns_id(ns).
kernfs_ns_id() dereferences ns->ns_id (offset 488 within the freed struct net), causing the reported slab use-after-free read.