Re: [PATCH v8 5/6] ata: libata-scsi: probe additional LUNs for multi-LUN ATAPI devices

[email protected] Fri, 31 Jul 2026 22:10:27 +0000
Newsgroups org.kernel.vger.linux-ide,org.kernel.vger.linux-scsi
Message-ID <[email protected]>
Thank you for your contribution! Sashiko AI review found 1 potential issue(=
s) to consider:

Pre-existing issues:
- [High] A Time-of-Check to Time-of-Use (TOCTOU) race condition exists betw=
een checking `dev->sdev[0]` and dereferencing it inside `ata_scsi_assign_of=
node()`, potentially leading to a NULL pointer dereference.
--

--- Patch [5]: [PATCH v8 5/6] ata: libata-scsi: probe additional LUNs for m=
ulti-LUN ATAPI devices ---
commit 26698e8a692fea36d598b32f39fd7513b22c4cf9
Author: Phil Pemberton <[email protected]>

ata: libata-scsi: probe additional LUNs for multi-LUN ATAPI devices

This commit checks the BLIST_FORCELUN flag after LUN 0 is added for an ATAPI
device. If set, it triggers a sequential LUN scan for that target using
scsi_scan_target() to probe additional LUNs. It also moves the call to
ata_scsi_assign_ofnode() before scsi_device_put() to hold the device refere=
nce.

> diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
> index 0b1e4842860c1..5bbb3169bea70 100644
> --- a/drivers/ata/libata-scsi.c
> +++ b/drivers/ata/libata-scsi.c

[ ... ]

> @@ -5281,13 +5282,27 @@ void ata_scsi_scan_host(struct ata_port *ap, int =
sync)
> =20
>  			sdev =3D __scsi_add_device(ap->scsi_host, channel, id, 0,
>  						 NULL);
> -			if (!IS_ERR(sdev)) {
> -				dev->sdev[0] =3D sdev;
> -				ata_scsi_assign_ofnode(dev, ap);
> -				scsi_device_put(sdev);
> -			} else {
> +			if (IS_ERR(sdev)) {
>  				dev->sdev[0] =3D NULL;
> +				continue;
>  			}
> +
> +			/*
> +			 * For multi-LUN ATAPI (BLIST_FORCELUN), trigger a
> +			 * sequential scan for this target.  pdt_1f_for_no_lun,
> +			 * set during LUN 0 configure, ensures non-responding
> +			 * LUNs are silently skipped; dev->sdev[] is populated
> +			 * by ata_scsi_dev_config() during the scan.
> +			 */
> +			if (dev->class =3D=3D ATA_DEV_ATAPI &&
> +			    sdev->sdev_bflags & BLIST_FORCELUN &&
> +			    !WARN_ON_ONCE(ap->scsi_host->max_lun > ATAPI_MAX_LUN))
> +				scsi_scan_target(&ap->scsi_host->shost_gendev,
> +						 channel, id, SCAN_WILD_CARD,
> +						 SCSI_SCAN_RESCAN);
> +			if (dev->sdev[0])
> +				ata_scsi_assign_ofnode(dev, ap);

[Severity: High]
This is a pre-existing issue, but does this check leave a race condition wh=
ere
concurrent device removals can cause a NULL pointer dereference?

In ata_scsi_scan_host(), the lockless if (dev->sdev[0]) check is evaluated
after releasing shost->scan_mutex (which was temporarily held by the preced=
ing
scsi_scan_target and __scsi_add_device calls).

Concurrently, a hot-unplug event can trigger ata_scsi_remove_dev() on a
workqueue, which takes the scan_mutex and sets dev->sdev[0] =3D NULL.

If this concurrent removal occurs exactly after the check but before
ata_scsi_assign_ofnode() re-reads dev->sdev[0]:

drivers/ata/libata-scsi.c:ata_scsi_assign_ofnode() {
	struct scsi_device *sdev =3D ata_dev_scsi_device(dev, 0);
	...
			sdev->sdev_gendev.of_node =3D child;
}

Can sdev be NULL when it is unconditionally dereferenced? Would it be safer
to pass the safely refcounted local sdev directly to ata_scsi_assign_ofnode=
()?

> +			scsi_device_put(sdev);
>  		}
>  	}

--=20
Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20260731213503.2379=
[email protected]?part=3D5