Skip to contentSkip to conversation

scsi: ufs: Fix memory corruption by ufshcd_read_desc_param()

PATCH Applied Bart Van Assche 4 participants 6 messages Jul 19, 2021 – Jul 30, 2021 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: ufs: Fix memory corruption by ufshcd_read_desc_param()

If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies 256 + buff_len - param_offset bytes into a buffer with size buff_len. Since param_offset < 256 this results in writing past the bound of the output buffer.

Fixes cbe193f6f093 ("scsi: ufs: Fix potential NULL pointer access during memcpy")
---
 drivers/scsi/ufs/ufshcd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c +3 -1
index 89da2cf2c969..00502ffe9b4a 100644
@@ -3365,7 +3365,9 @@ int ufshcd_read_desc_param(struct ufs_hba *hba,
 
 	if (is_kmalloc) {
 		/* Make sure we don't copy more data than available */
-		if (param_offset + param_size > buff_len)
+		if (buff_len < param_offset)
+			param_size = 0;
+		else if (param_offset + param_size > buff_len)
 			param_size = buff_len - param_offset;
 		memcpy(param_read_buf, &desc_buf[param_offset], param_size);
 	}
#1 lore raw
Quoted 6 lines

If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies
256 + buff_len - param_offset bytes into a buffer with size buff_len. Since param_offset < 256 this results in writing past the bound of the output buffer.

param_offset >= buff_len is tested in line 3381?

Thanks,
Avri

Quoted 23 lines
Fixes cbe193f6f093 ("scsi: ufs: Fix potential NULL pointer access during

memcpy")

---
 drivers/scsi/ufs/ufshcd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/ufs/ufshcd.c index renamed from drivers/scsi/ufs/ufshcd.c +0 -0

89da2cf2c969..00502ffe9b4a 100644

drivers/scsi/ufs/ufshcd.c +3 -1
@@ -3365,7 +3365,9 @@ int ufshcd_read_desc_param(struct ufs_hba *hba,
 
        if (is_kmalloc) {
                /* Make sure we don't copy more data than available */
-               if (param_offset + param_size > buff_len)
+               if (buff_len < param_offset)
+                       param_size = 0;
+               else if (param_offset + param_size > buff_len)
                        param_size = buff_len - param_offset;
                memcpy(param_read_buf, &desc_buf[param_offset], param_size);
        }
#2 lore raw
Quoted 7 lines · Avri Altman
Quoted 5 lines

If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies
256 + buff_len - param_offset bytes into a buffer with size buff_len. Since param_offset < 256 this results in writing past the bound of the output buffer.

param_offset >= buff_len is tested in line 3381?

Hi Avri,

That's correct. However, a few lines lower there is the following code:

ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,

				desc_id, desc_index, 0,
				desc_buf, &buff_len);

That call may modify (reduce) 'buff_len'. Hence, a second check is needed.

Thanks,

Bart.

#3 lore raw
In reply to Bart Van Assche
Quoted 5 lines · Bart Van Assche

If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies 256 + buff_len - param_offset bytes into a buffer with size buff_len. Since param_offset < 256 this results in writing past the bound of the output buffer.

Applied to 5.14/scsi-fixes, thanks!

[1/1] scsi: ufs: Fix memory corruption by ufshcd_read_desc_param()

      https://git.kernel.org/mkp/scsi/c/b1d5de8c6ea2
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#4 lore raw
Quoted 13 lines · Martin K. Petersen
Quoted 7 lines · Bart Van Assche

If param_offset > buff_len then the memcpy() statement in ufshcd_read_desc_param() corrupts memory since it copies 256 + buff_len - param_offset bytes into a buffer with size buff_len.
Since param_offset < 256 this results in writing past the bound of the
output buffer.

Quoted 7 lines

Applied to 5.14/scsi-fixes, thanks!

[1/1] scsi: ufs: Fix memory corruption by ufshcd_read_desc_param()

      https://git.kernel.org/mkp/scsi/c/b1d5de8c6ea2

Hi Martin,
This patch has a problem, we should revert it.

and the correct fix patch is in Bart's another series of patch:
https://patchwork.kernel.org/project/linux-scsi/patch/[email protected]/

Bean

#5 lore raw

Bean,

Quoted 3 lines

This patch has a problem, we should revert it.

and the correct fix patch is in Bart's another series of patch:

OK, that explains why b4 was confused. Dropped.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering