Skip to content

scsi: fix scsi_debug module-exit panic and pseudo target leak

Laurence Oberman linux-arm-kernel linux-scsi lkml 1 version New

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 0 repliesNew

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2scsi: core: Fix target reap_ref leak in scsi_get_pseudo_sdev()drivers/scsi/scsi_scan.c+8 -70 replies
2/2scsi: scsi_debug: Wait for async target cleanup on module exitdrivers/scsi/scsi_debug.c+2 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/2] scsi: fix scsi_debug module-exit panic and pseudo target leak

CKI reported a NULL pointer dereference in debugfs_remove() from sdebug_tartget_cleanup_async() while blktests throtl/001 unloaded scsi_debug on 7.4.0-rc1 (arm64). The bug is not arm64 specific. It reproduces on x86_64 with KASAN as a slab-use-after-free of the per-target debugfs dentry.

While fixing it, instrumenting target alloc/destroy in scsi_debug showed a second, independent problem in the SCSI core: the pseudo SCSI device's target is never destroyed, because scsi_get_pseudo_sdev() leaks a reap_ref. With add_host=4 num_tgts=16, ->target_alloc() was called 68 times and ->target_destroy() only 64; the missing four were the pseudo-device targets at id max_id. Any LLD that sets nr_reserved_cmds leaks its pseudo target.

Patch 1 fixes the core leak. Patch 2 makes scsi_debug_exit() wait for the deferred per-target debugfs cleanup before removing the debugfs root and unloading. Patch 1 was found while testing patch 2, and patch 2 was tested on top of it.

Testing (x86_64 VM, KASAN, 7.4.0-rc1): loop
"modprobe scsi_debug add_host=4 num_tgts=16; rmmod scsi_debug", with an msleep(200) temporarily added to sdebug_tartget_cleanup_async() to force the race window.
- unpatched: KASAN slab-use-after-free / oops on the first rmmod
- patch 1: 68 target_alloc / 68 target_destroy, pseudo targets destroyed during scsi_remove_host()
- patches 1+2: 50 iterations clean with the msleep

I had assistatnce from LLM for both of these and ran a local sashiko which was clean other than a pre-existing issue

Laurence Oberman (2):
scsi: core: Fix target reap_ref leak in scsi_get_pseudo_sdev()
scsi: scsi_debug: Wait for async target cleanup on module exit

 drivers/scsi/scsi_debug.c |  2 ++
 drivers/scsi/scsi_scan.c  | 15 ++++++++-------
 2 files changed, 10 insertions(+), 7 deletions(-)
Signature · 1 line
2.43.0