scsi: target: core_free_device_list_for_node() walks an RCU hlist it is freeing without rcu_read_lock()
Subsystems (2)
Hi,
core_free_device_list_for_node() (drivers/target/target_core_device.c:233-243, mainline ebb7a8dc1e74) is:
mutex_lock(&nacl->lun_entry_mutex);
hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
core_disable_device_list_for_node(deve->se_lun, deve,
nacl, tpg);
mutex_unlock(&nacl->lun_entry_mutex);The loop body unlinks the current entry with hlist_del_rcu(&orig->link) and queues its free with call_rcu(&orig->rcu_head, target_free_dev_entry) (core_disable_device_list_for_node(), :467 and :479). The iterator then reads deve->link.next of that entry to advance. lun_entry_mutex serialises the writers but does not hold off the RCU callback, and there is no rcu_read_lock() around the walk. On a preemptible-RCU kernel, if the walker is preempted after call_rcu() for a full grace period, target_free_dev_entry() frees the entry and the next iterator step reads freed memory.
Since the walk holds the writer-side mutex, hlist_for_each_entry_safe() (or an rcu_read_lock() around the loop) looks like the natural fix.
Reachability: core_free_device_list_for_node() runs when a node ACL is removed (configfs rmdir, core_tpg_del_initiator_node_acl()) and on TPG deregistration; with demo-mode dynamic ACLs (generate_node_acls=1, no ACL caching) it can also follow an initiator's session teardown. It needs CONFIG_PREEMPT_RCU and a preemption lasting a grace period inside the window, so it is narrow. Impact: a use-after-free read of se_dev_entry (KASAN would report it); with slab reuse, a wild pointer for the next iteration.
Affected: the loop is unchanged in mainline ebb7a8dc1e74 and next-20261125. This is not the problem fixed by 6a6e7d7230cd ("Fix demo-mode dynamic ACL dev-entry UAF vs LUN teardown").
Found by a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and this report. The tool runs verbatim copies of the kernel functions with stubs for the rest of the kernel; it found the interleaving above, and the interleaving was replayed natively on that harness under the tool's schedule. Functions
involved: core_free_device_list_for_node(), core_disable_device_list_for_node(), target_free_dev_entry(), with an RCU callback model. It has not been reproduced on a booted kernel.
Thanks,
Zhenwen Liang