All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] Fixes for scsi_mode_sense/select()
@ 2021-08-20  7:02 Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 1/3] scsi: fix scsi_mode_sense() buffer length handling Damien Le Moal
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Damien Le Moal @ 2021-08-20  7:02 UTC (permalink / raw)
  To: linux-scsi, Martin K . Petersen

The first patch in this series is the formet standalone patch titled
"scsi: fix scsi_mode_sense()". Patch 2 fixes similar buffer length
handling problems found in scsi_mode_select().
Patch 3 fixes the use of scsi_mode_sense() in sd.c to ensure that calls
are issued with a sensible buffer size for devices that explicitly
requested the use of MODE SENSE 10 (e.g. SATA drives on AHCI).

Changes from v2:
* Added patch 3

Changes from v1:
* Patch 1:
  - Added check on the buffer length not being larger than 65535 bytes
    for the MODE SENSE 10 case.
  - Automatically try MODE SENSE 10 for large requests even if the
    device does not have use_10_for_ms set
* Added patch 2

Damien Le Moal (3):
  scsi: fix scsi_mode_sense() buffer length handling
  scsi: fix scsi_mode_select() buffer length handling
  scsi: sd: fix sd_do_mode_sense() buffer length handling

 drivers/scsi/scsi_lib.c | 46 +++++++++++++++++++++++++----------------
 drivers/scsi/sd.c       |  7 +++++++
 2 files changed, 35 insertions(+), 18 deletions(-)

-- 
2.31.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/3] scsi: fix scsi_mode_sense() buffer length handling
  2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
@ 2021-08-20  7:02 ` Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 2/3] scsi: fix scsi_mode_select() " Damien Le Moal
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Damien Le Moal @ 2021-08-20  7:02 UTC (permalink / raw)
  To: linux-scsi, Martin K . Petersen

Several problems exist with scsi_mode_sense() buffer length handling:
1) The allocation length field of the MODE SENSE 10 command is 16-bits,
   occupying bytes 7 and 8 of the CDB. With this command, access to mode
   pages larger than 255 bytes is thus possible. However, the CDB
   allocation length field is set by assigning len to byte 8 only, thus
   truncating buffer length larger than 255.
2) If scsi_mode_sense() is called with len smaller than 8 with
   sdev->use_10_for_ms set, or smaller than 4 otherwise, the buffer
   length is increased to 8 and 4 respectively, and the buffer is zero
   filled with these increased values, thus corrupting the memory
   following the buffer.

Fix these 2 problems by using put_unaligned_be16() to set the allocation
length field of MODE SENSE 10 CDB and by returning an error when len is
too small.

Furthermore, if len is larger than 255B, always try MODE SENSE 10 first,
even if the device driver did not set sdev->use_10_for_ms. In case of
invalid opcode error for MODE SENSE 10, access to mode pages larger
than 255 bytes are not retried using MODE SENSE (6). To avoid buffer
length overflows for the MODE_SENSE 10 case, check that len is smaller
than 65535 bytes

While at it, also fix the folowing:
* use get_unaligned_be16() to retrieve the mode data length and block
  descriptor length fields of the mode sense reply header instead of
  using an open coded calculation.
* Fix the kdoc dbd argument explanation: the DBD bit stands for
  Disable Block Descriptor, which is the opposite of what the dbd
  argument description was.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
---
 drivers/scsi/scsi_lib.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 572673873ddf..327ea74a5e31 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -2075,7 +2075,7 @@ EXPORT_SYMBOL_GPL(scsi_mode_select);
 /**
  *	scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  *	@sdev:	SCSI device to be queried
- *	@dbd:	set if mode sense will allow block descriptors to be returned
+ *	@dbd:	set to prevent mode sense from returning block descriptors
  *	@modepage: mode page being requested
  *	@buffer: request buffer (may not be smaller than eight bytes)
  *	@len:	length of request buffer.
@@ -2110,18 +2110,18 @@ scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
 		sshdr = &my_sshdr;
 
  retry:
-	use_10_for_ms = sdev->use_10_for_ms;
+	use_10_for_ms = sdev->use_10_for_ms || len > 255;
 
 	if (use_10_for_ms) {
-		if (len < 8)
-			len = 8;
+		if (len < 8 || len > 65535)
+			return -EINVAL;
 
 		cmd[0] = MODE_SENSE_10;
-		cmd[8] = len;
+		put_unaligned_be16(len, &cmd[7]);
 		header_length = 8;
 	} else {
 		if (len < 4)
-			len = 4;
+			return -EINVAL;
 
 		cmd[0] = MODE_SENSE;
 		cmd[4] = len;
@@ -2145,9 +2145,15 @@ scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
 			if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
 			    (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
 				/*
-				 * Invalid command operation code
+				 * Invalid command operation code: retry using
+				 * MODE SENSE (6) if this was a MODE SENSE 10
+				 * request, except if the request mode page is
+				 * too large for MODE SENSE single byte
+				 * allocation length field.
 				 */
 				if (use_10_for_ms) {
+					if (len > 255)
+						return -EIO;
 					sdev->use_10_for_ms = 0;
 					goto retry;
 				}
@@ -2171,12 +2177,11 @@ scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
 		data->longlba = 0;
 		data->block_descriptor_length = 0;
 	} else if (use_10_for_ms) {
-		data->length = buffer[0]*256 + buffer[1] + 2;
+		data->length = get_unaligned_be16(&buffer[0]) + 2;
 		data->medium_type = buffer[2];
 		data->device_specific = buffer[3];
 		data->longlba = buffer[4] & 0x01;
-		data->block_descriptor_length = buffer[6]*256
-			+ buffer[7];
+		data->block_descriptor_length = get_unaligned_be16(&buffer[6]);
 	} else {
 		data->length = buffer[0] + 1;
 		data->medium_type = buffer[1];
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/3] scsi: fix scsi_mode_select() buffer length handling
  2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 1/3] scsi: fix scsi_mode_sense() buffer length handling Damien Le Moal
@ 2021-08-20  7:02 ` Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 3/3] scsi: sd: fix sd_do_mode_sense() " Damien Le Moal
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Damien Le Moal @ 2021-08-20  7:02 UTC (permalink / raw)
  To: linux-scsi, Martin K . Petersen

The MODE SELECT (6) command allows handling mode page buffers that are
up to 255 bytes, including the 4 byte header needed in front of the page
buffer. For requests larger than this limit, automatically use the
MODE SENSE 10 command.

In both cases, since scsi_mode_select() adds the mode select page
header, checks on the buffer length value must include this header size
to avoid overflows of the command CDB allocatione length field.

While at it, use put_unaligned_be16() for setting the header block
descriptor length and CDB allocation length when using MODE SELECT 10.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
---
 drivers/scsi/scsi_lib.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 327ea74a5e31..c9a825f583d6 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -2026,8 +2026,15 @@ scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
 	memset(cmd, 0, sizeof(cmd));
 	cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
 
-	if (sdev->use_10_for_ms) {
-		if (len > 65535)
+	/*
+	 * Use MODE SENSE 10 if the device asked for it or if the mode page
+	 * and the mode select header cannot fit within the maximumm 255B of
+	 * the MODE SELECT (6) command.
+	 */
+	if (sdev->use_10_for_ms ||
+	    len + 4 > 255 ||
+	    data->block_descriptor_length > 255) {
+		if (len > 65535 - 8)
 			return -EINVAL;
 		real_buffer = kmalloc(8 + len, GFP_KERNEL);
 		if (!real_buffer)
@@ -2040,15 +2047,13 @@ scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
 		real_buffer[3] = data->device_specific;
 		real_buffer[4] = data->longlba ? 0x01 : 0;
 		real_buffer[5] = 0;
-		real_buffer[6] = data->block_descriptor_length >> 8;
-		real_buffer[7] = data->block_descriptor_length;
+		put_unaligned_be16(data->block_descriptor_length,
+				   &real_buffer[6]);
 
 		cmd[0] = MODE_SELECT_10;
-		cmd[7] = len >> 8;
-		cmd[8] = len;
+		put_unaligned_be16(len, &cmd[7]);
 	} else {
-		if (len > 255 || data->block_descriptor_length > 255 ||
-		    data->longlba)
+		if (data->longlba)
 			return -EINVAL;
 
 		real_buffer = kmalloc(4 + len, GFP_KERNEL);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/3] scsi: sd: fix sd_do_mode_sense() buffer length handling
  2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 1/3] scsi: fix scsi_mode_sense() buffer length handling Damien Le Moal
  2021-08-20  7:02 ` [PATCH v3 2/3] scsi: fix scsi_mode_select() " Damien Le Moal
@ 2021-08-20  7:02 ` Damien Le Moal
  2021-09-29  4:15 ` [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Martin K. Petersen
  2021-10-05  4:34 ` Martin K. Petersen
  4 siblings, 0 replies; 7+ messages in thread
From: Damien Le Moal @ 2021-08-20  7:02 UTC (permalink / raw)
  To: linux-scsi, Martin K . Petersen

For devices that explicitly asked for MODE SENSE 10 use, make sure that
scsi_mode_sense() is called with a buffer of at least 8B so that mode
sense header fits.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
---
 drivers/scsi/sd.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 920df3a04a7b..34faff96e165 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -2603,6 +2603,13 @@ sd_do_mode_sense(struct scsi_disk *sdkp, int dbd, int modepage,
 		 unsigned char *buffer, int len, struct scsi_mode_data *data,
 		 struct scsi_sense_hdr *sshdr)
 {
+	/*
+	 * If we must use MODE SENSE 10, make sure that the buffer length
+	 * is at least 8B so that the mode sense header fits.
+	 */
+	if (sdkp->device->use_10_for_ms && len < 8)
+		len = 8;
+
 	return scsi_mode_sense(sdkp->device, dbd, modepage, buffer, len,
 			       SD_TIMEOUT, sdkp->max_retries, data,
 			       sshdr);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] Fixes for scsi_mode_sense/select()
  2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
                   ` (2 preceding siblings ...)
  2021-08-20  7:02 ` [PATCH v3 3/3] scsi: sd: fix sd_do_mode_sense() " Damien Le Moal
@ 2021-09-29  4:15 ` Martin K. Petersen
  2021-09-29  6:02   ` Damien Le Moal
  2021-10-05  4:34 ` Martin K. Petersen
  4 siblings, 1 reply; 7+ messages in thread
From: Martin K. Petersen @ 2021-09-29  4:15 UTC (permalink / raw)
  To: Damien Le Moal; +Cc: linux-scsi, Martin K . Petersen


Damien,

> The first patch in this series is the formet standalone patch titled
> "scsi: fix scsi_mode_sense()". Patch 2 fixes similar buffer length
> handling problems found in scsi_mode_select().
> Patch 3 fixes the use of scsi_mode_sense() in sd.c to ensure that calls
> are issued with a sensible buffer size for devices that explicitly
> requested the use of MODE SENSE 10 (e.g. SATA drives on AHCI).

Applied to 5.16/scsi-staging with some changes. There was some confusion
between SENSE vs. SELECT in patch #2 in particular.

I tried to clean up the dbd situation a while back but it fell by the
wayside:

https://lore.kernel.org/all/20200325222416.5094-1-martin.petersen@oracle.com/

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] Fixes for scsi_mode_sense/select()
  2021-09-29  4:15 ` [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Martin K. Petersen
@ 2021-09-29  6:02   ` Damien Le Moal
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Le Moal @ 2021-09-29  6:02 UTC (permalink / raw)
  To: Martin K. Petersen; +Cc: linux-scsi

On 2021/09/29 13:15, Martin K. Petersen wrote:
> 
> Damien,
> 
>> The first patch in this series is the formet standalone patch titled
>> "scsi: fix scsi_mode_sense()". Patch 2 fixes similar buffer length
>> handling problems found in scsi_mode_select().
>> Patch 3 fixes the use of scsi_mode_sense() in sd.c to ensure that calls
>> are issued with a sensible buffer size for devices that explicitly
>> requested the use of MODE SENSE 10 (e.g. SATA drives on AHCI).
> 
> Applied to 5.16/scsi-staging with some changes. There was some confusion
> between SENSE vs. SELECT in patch #2 in particular.
> 
> I tried to clean up the dbd situation a while back but it fell by the
> wayside:
> 
> https://lore.kernel.org/all/20200325222416.5094-1-martin.petersen@oracle.com/

This one looks good to me. Repost it may be ?
I have more patches coming around mode sense to be able to handle sub-pages
(command duration limits and the new ata feature set subpages of the control page).

-- 
Damien Le Moal
Western Digital Research

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] Fixes for scsi_mode_sense/select()
  2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
                   ` (3 preceding siblings ...)
  2021-09-29  4:15 ` [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Martin K. Petersen
@ 2021-10-05  4:34 ` Martin K. Petersen
  4 siblings, 0 replies; 7+ messages in thread
From: Martin K. Petersen @ 2021-10-05  4:34 UTC (permalink / raw)
  To: linux-scsi, Damien Le Moal; +Cc: Martin K . Petersen

On Fri, 20 Aug 2021 16:02:52 +0900, Damien Le Moal wrote:

> The first patch in this series is the formet standalone patch titled
> "scsi: fix scsi_mode_sense()". Patch 2 fixes similar buffer length
> handling problems found in scsi_mode_select().
> Patch 3 fixes the use of scsi_mode_sense() in sd.c to ensure that calls
> are issued with a sensible buffer size for devices that explicitly
> requested the use of MODE SENSE 10 (e.g. SATA drives on AHCI).
> 
> [...]

Applied to 5.16/scsi-queue, thanks!

[1/3] scsi: fix scsi_mode_sense() buffer length handling
      https://git.kernel.org/mkp/scsi/c/17b49bcbf835
[2/3] scsi: fix scsi_mode_select() buffer length handling
      https://git.kernel.org/mkp/scsi/c/a7d6840bed0c
[3/3] scsi: sd: fix sd_do_mode_sense() buffer length handling
      https://git.kernel.org/mkp/scsi/c/c749301ebee8

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-10-05  4:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20  7:02 [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Damien Le Moal
2021-08-20  7:02 ` [PATCH v3 1/3] scsi: fix scsi_mode_sense() buffer length handling Damien Le Moal
2021-08-20  7:02 ` [PATCH v3 2/3] scsi: fix scsi_mode_select() " Damien Le Moal
2021-08-20  7:02 ` [PATCH v3 3/3] scsi: sd: fix sd_do_mode_sense() " Damien Le Moal
2021-09-29  4:15 ` [PATCH v3 0/3] Fixes for scsi_mode_sense/select() Martin K. Petersen
2021-09-29  6:02   ` Damien Le Moal
2021-10-05  4:34 ` Martin K. Petersen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.