Linux ATA/IDE development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Phil Pemberton" <philpem@philpem.me.uk>
Cc: linux-ide@vger.kernel.org, cassel@kernel.org, dlemoal@kernel.org
Subject: Re: [PATCH v5 3/6] ata: libata-scsi: route non-zero LUN commands for multi-LUN ATAPI
Date: Thu, 14 May 2026 01:12:37 +0000	[thread overview]
Message-ID: <20260514011237.CDBF3C19425@smtp.kernel.org> (raw)
In-Reply-To: <20260512202728.299414-4-philpem@philpem.me.uk>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Unconditional LUN injection into CDB byte 1 clobbers command-specific flags for modern ATAPI commands.
- [Critical] Returning AC_ERR_INVALID from atapi_xlat without setting cmd->result causes silent command success (Silent Data Corruption).
- [High] Reachable WARN_ON_ONCE splat and broken LUN > 0 feature due to hardcoded dev->nr_luns.
- [High] Lifecycle functions fail to clean up LUNs > 0 when dev->nr_luns remains 1, leading to resource leaks and use-after-free.
--

commit 3063d8fc6690940538378993230bab65cc937027
Author: Phil Pemberton <philpem@philpem.me.uk>

ata: libata-scsi: route non-zero LUN commands for multi-LUN ATAPI

This commit updates __ata_scsi_find_dev() to allow commands directed at
ATAPI devices with non-zero LUNs to proceed. It also modifies atapi_xlat()
to encode the LUN into bits 7:5 of CDB byte 1 to support legacy SCSI-2
era ATAPI devices.

> diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
> index 7c3d31dc49a17..2d714efc855f0 100644
> --- a/drivers/ata/libata-scsi.c
> +++ b/drivers/ata/libata-scsi.c
> @@ -2953,6 +2953,15 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
>  	memset(qc->cdb, 0, dev->cdb_len);
>  	memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
>  
> +	/*
> +	 * SCSI-2 CDB LUN encoding: bits 7:5 of byte 1 (3-bit field).
> +	 * The SCSI layer caps the LUN at shost->max_lun (<= ATAPI_MAX_LUN),
> +	 * so this should never trip; warn and reject if it does.
> +	 */
> +	if (WARN_ON_ONCE(scmd->device->lun >= dev->nr_luns))
> +		return AC_ERR_INVALID;

In the libata subsystem, dev->nr_luns is statically initialized to 1.
If an administrator triggers a manual SCSI scan to LUN 1 via sysfs, will
this check evaluate to true and trigger a kernel warning splat?

Does returning AC_ERR_INVALID here also block the multi-LUN feature for
standard ATAPI devices that do not have a specific quirk to increment
dev->nr_luns?

If atapi_xlat() returns AC_ERR_INVALID here, ata_scsi_translate() will
detect the non-zero return and directly call scsi_done().

Because cmd->result remains at its initialized value of 0 (SAM_STAT_GOOD),
will the SCSI and block layers treat the dropped I/O as successfully
completed? Could this return uninitialized memory to the caller for
read commands?

Additionally, if a non-zero LUN is instantiated because a manual sysfs
scan silently succeeds due to the AC_ERR_INVALID behavior above, but
dev->nr_luns remains 1, will lifecycle loops in functions like
ata_scsi_remove_dev() fail to process the extra LUNs?

Could this leak the scsi_device structure for LUN 1 and cause a
use-after-free when the underlying ATA port is destroyed?

> +	qc->cdb[1] = (qc->cdb[1] & 0x1f) | ((u8)scmd->device->lun << 5);

For modern ATAPI devices using the SCSI-3/MMC command set, bits 7:5 of
CDB byte 1 are repurposed for command-specific parameters, such as the
RDPROTECT and WRPROTECT flags in read/write(10), or the blanking type
bits in the blank command.

Does unconditionally masking and setting these bits silently corrupt
those valid flags?

Also, since the Linux SCSI midlayer automatically performs this injection
based on the sdev->lun_in_cdb flag for legacy SCSI-2 devices, is this
manual injection redundant?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260512202728.299414-1-philpem@philpem.me.uk?part=3

  parent reply	other threads:[~2026-05-14  1:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 20:27 [PATCH v5 0/6] ata: libata-scsi: multi-LUN ATAPI device support Phil Pemberton
2026-05-12 20:27 ` [PATCH v5 1/6] ata: libata-scsi: add atapi_max_lun module parameter Phil Pemberton
2026-05-14  0:12   ` sashiko-bot
2026-05-12 20:27 ` [PATCH v5 2/6] ata: libata-scsi: convert dev->sdev to per-LUN array Phil Pemberton
2026-05-13 13:07   ` Hannes Reinecke
2026-05-14  0:36   ` sashiko-bot
2026-05-12 20:27 ` [PATCH v5 3/6] ata: libata-scsi: route non-zero LUN commands for multi-LUN ATAPI Phil Pemberton
2026-05-13 13:11   ` Hannes Reinecke
2026-05-14  1:12   ` sashiko-bot [this message]
2026-05-12 20:27 ` [PATCH v5 4/6] scsi: add BLIST_NO_LUN_1F blacklist flag Phil Pemberton
2026-05-14  1:26   ` sashiko-bot
2026-05-12 20:27 ` [PATCH v5 5/6] ata: libata-scsi: probe additional LUNs for multi-LUN ATAPI devices Phil Pemberton
2026-05-13 13:12   ` Hannes Reinecke
2026-05-14  2:33   ` sashiko-bot
2026-05-12 20:27 ` [PATCH v5 6/6] scsi: scsi_devinfo: add COMPAQ PD-1 multi-LUN ATAPI device quirk Phil Pemberton
2026-05-14  2:48   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260514011237.CDBF3C19425@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cassel@kernel.org \
    --cc=dlemoal@kernel.org \
    --cc=linux-ide@vger.kernel.org \
    --cc=philpem@philpem.me.uk \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox