scsi: ufs: Fix memory corruption by ufshcd_read_desc_param()
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.
--- drivers/scsi/ufs/ufshcd.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
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); }
Quoted 6 lines
If
param_offset>buff_lenthen thememcpy()statement inufshcd_read_desc_param()corrupts memory since it copies
256 +buff_len-param_offsetbytes into a buffer with sizebuff_len. Sinceparam_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 duringmemcpy")
Signed-off-by Bart Van Assche <[email protected]>--- 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..00502ffe9b4a100644drivers/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); }
Quoted 7 lines · Avri Altman
Quoted 5 lines
If
param_offset>buff_lenthen thememcpy()statement inufshcd_read_desc_param()corrupts memory since it copies
256 +buff_len-param_offsetbytes into a buffer with sizebuff_len. Sinceparam_offset< 256 this results in writing past the bound of the output buffer.
param_offset>=buff_lenis 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.
Martin K. Petersen
AppliedQuoted 5 lines · Bart Van Assche
If
param_offset>buff_lenthen thememcpy()statement inufshcd_read_desc_param()corrupts memory since it copies 256 +buff_len-param_offsetbytes into a buffer with sizebuff_len. Sinceparam_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
Quoted 13 lines · Martin K. Petersen
Quoted 7 lines · Bart Van Assche
If
param_offset>buff_lenthen thememcpy()statement inufshcd_read_desc_param()corrupts memory since it copies 256 +buff_len-param_offsetbytes into a buffer with sizebuff_len.
Sinceparam_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
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