ksmbd: fix iface_list use-after-free during server reset
Subsystems (2)
PATCH 1/1 ksmbd: fix iface_list use-after-free during server reset
From: Henry Hu <[email protected]>
ksmbd_find_netdev_name_iface_list() walks iface_list for fsctl_query_iface_info_ioctl(), which holds RTNL. But during server reset, ksmbd_tcp_destroy() unlinks and frees the entries without RTNL, and queued SMB requests can still be running at that point. So an FSCTL_QUERY_NETWORK_INTERFACE_INFO request can therefore race with interface destruction and dereference a freed entry. With debug-only instrumentation to make the race deterministic, KASAN reports:
BUG: KASAN: slab-use-after-free in ksmbd_find_netdev_name_iface_list
ksmbd_find_netdev_name_iface_list
fsctl_query_iface_info_ioctl
smb2_ioctl
handle_ksmbd_work
Freed by:
kfree
ksmbd_tcp_destroy
ksmbd_conn_transport_destroy
kill_server_store
The fix is to detach iface_list to a temporary list under RTNL, then drop RTNL before cleanup because the following cleanup may block and hold RTNL for a long time.
--- fs/smb/server/transport_tcp.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/smb/server/transport_tcp.c +6 -1
@@ -656,10 +656,15 @@ static void ksmbd_tcp_stop_listener(struct interface *iface)void ksmbd_tcp_destroy (void ) {struct interface *iface, *tmp; +LIST_HEAD (iface_list_to_free);unregister_netdevice_notifier (&ksmbd_netdev_notifier); -list_for_each_entry_safe (iface, tmp, &iface_list, entry) { +rtnl_lock (); +list_splice_init (&iface_list, &iface_list_to_free); +rtnl_unlock (); + +list_for_each_entry_safe (iface, tmp, &iface_list_to_free, entry) {ksmbd_tcp_stop_listener (iface);list_del (&iface->entry);kfree (iface->name);
Signature · 1 line
2.25.1
Quoted 29 lines · huhai
From: Henry Hu <[email protected]>
ksmbd_find_netdev_name_iface_list()walksiface_listforfsctl_query_iface_info_ioctl(), which holds RTNL. But during server reset,ksmbd_tcp_destroy()unlinks and frees the entries without RTNL, and queued SMB requests can still be running at that point. So anFSCTL_QUERY_NETWORK_INTERFACE_INFOrequest can therefore race with interface destruction and dereference a freed entry. With debug-only instrumentation to make the race deterministic, KASAN reports:BUG: KASAN: slab-use-after-free in ksmbd_find_netdev_name_iface_list
ksmbd_find_netdev_name_iface_list
fsctl_query_iface_info_ioctl
smb2_ioctl
handle_ksmbd_workFreed by:
kfree
ksmbd_tcp_destroy
ksmbd_conn_transport_destroy
kill_server_storeThe fix is to detach
iface_listto a temporary list under RTNL, then drop RTNL before cleanup because the following cleanup may block and hold RTNL for a long time.
Quoted 5 lines
Fixes b2d99376c5d6 ("ksmbd: browse interfaces list on FSCTL_QUERY_INTERFACE_INFO IOCTL")Reported-by Sashiko <[email protected]>Assisted-by LLMSigned-off-by Henry Hu <[email protected]>
Applied it to #ksmbd-for-next.
Thanks!