scsi: ufs: Leave space for '\0' in utf8 desc string
Subsystems (2)
PATCH 1/1 scsi: ufs: Leave space for '\0' in utf8 desc string
utf16s_to_utf8s does not NULL terminate the output string. For us to be able to add a NULL character when utf16s_to_utf8s returns, we need to make sure that there is space for such NULL character at the end of the output buffer. We can achieve this by passing an output buffer size to utf16s_to_utf8s that is one character less than what we allocated.
Other call sites of utf16s_to_utf8s appear to be using the same technique where they artificially reduce the buffer size by one to leave space for a NULL character or line feed character.
--- drivers/ufs/core/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +1 -1
@@ -3632,7 +3632,7 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,*/ ret =utf16s_to_utf8s (uc_str->uc, uc_str->len -QUERY_DESC_HDR_SIZE , -UTF16_BIG_ENDIAN , str, ascii_len); +UTF16_BIG_ENDIAN , str, ascii_len -1 );/* replace non-printable or non-ASCII characters with spaces */ for (i =0 ; i < ret; i++)
Signature · 1 line
2.42.0.655.g421f12c284-goog
Quoted 11 lines
utf16s_to_utf8sdoes not NULL terminate the output string. For us to be able to add a NULL character whenutf16s_to_utf8sreturns, we need to make sure that there is space for such NULL character at the end of the output buffer. We can achieve this by passing an output buffer size toutf16s_to_utf8sthat is one character less than what we allocated.Other call sites of
utf16s_to_utf8sappear to be using the same technique where they artificially reduce the buffer size by one to leave space for a NULL character or line feed character.Fixes 4b828fe156a6 ("scsi: ufs: revamp string descriptor reading")
I think this code goes back to commit b573d484e4ff (scsi: ufs: add support to read device and string descriptors)
Quoted 25 lines
Reviewed-by Mars Cheng <[email protected]>Reviewed-by Bart Van Assche <[email protected]>Reviewed-by Yen-lin Lai <[email protected]>Signed-off-by Daniel Mentz <[email protected]>--- drivers/ufs/core/ufshcd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/ufs/core/ufshcd.c index renamed from drivers/ufs/core/ufshcd.c +0
-0
8382e8cfa414..5767642982c1100644drivers/ufs/core/ufshcd.c +2
-2@@ -3632,7 +3632,7 @@ int ufshcd_read_string_desc(struct ufs_hba *hba,u8 desc_index, */ ret =utf16s_to_utf8s (uc_str->uc, uc_str->len -QUERY_DESC_HDR_SIZE , -UTF16_BIG_ENDIAN , str, ascii_len); +UTF16_BIG_ENDIAN , str, ascii_len - +1 );/* replace non-printable or non-ASCII characters with spaces */ for (i =0 ; i < ret; i++) --2.42.0.655.g421f12c284-goog
Bart Van Assche
reviewerQuoted 2 lines · Avri Altman
Quoted 1 line
Fixes 4b828fe156a6 ("scsi: ufs: revamp string descriptor reading")I think this code goes back to commit
b573d484e4ff(scsi: ufs: add support to read device and string descriptors)
Hmm ... it seems to me that there was no buffer overflow in commit b573d484e4ff but that the buffer overflow was introduced by commit 4b828fe156a6?
Thanks,
Bart.
Quoted 8 lines · Bart Van Assche
Quoted 2 lines · Avri Altman
Quoted 1 line
Fixes 4b828fe156a6 ("scsi: ufs: revamp string descriptor reading")I think this code goes back to commit
b573d484e4ff(scsi: ufs: add support to read device and string descriptors)Hmm ... it seems to me that there was no buffer overflow in commit
b573d484e4ffbut that the buffer overflow was introduced by commit4b828fe156a6?
Thank you for the review Avri.
To me, it appears as if those two commits had different issues:
commit b573d484e4ff ("scsi: ufs: add support to read device and string descriptors") failed to reliably NULL terminate the output string (in the case where ascii_len == size - QUERY_DESC_HDR_SIZE).
commit 4b828fe156a6 ("scsi: ufs: revamp string descriptor reading") potentially performs an out-of-bounds array access while NULL terminating the output string.
I would argue that the proposed fix wouldn't even fix the former and older commit b573d484e4ff, because that commit might have required more fixes like using kzalloc instead of kmalloc.
I find that the newer commit 4b828fe156a6 did enough of refactoring for it to be considered the commit that needs this fix.
Martin K. Petersen
AppliedDaniel,
Quoted 5 lines
utf16s_to_utf8sdoes not NULL terminate the output string. For us to be able to add a NULL character whenutf16s_to_utf8sreturns, we need to make sure that there is space for such NULL character at the end of the output buffer. We can achieve this by passing an output buffer size toutf16s_to_utf8sthat is one character less than what we allocated.
Applied to 6.7/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering