Skip to contentSkip to conversation

exfat: fix out-of-bounds in exfat_nls_to_ucs2()

PATCH Superseded Jeongjun Park 3 participants 3 messages Oct 10, 2025 – Oct 11, 2025

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 exfat: fix out-of-bounds in exfat_nls_to_ucs2()

In exfat_nls_to_ucs2(), if there is no NLS loss and the char-to-ucs2 conversion is successfully completed, the variable "i" will have the same value as len.

However, exfat_nls_to_ucs2() checks p_cstring[i] to determine whether nls is lost immediately after the while loop ends, so if len is FSLABEL_MAX, "i" will also be FSLABEL_MAX immediately after the while loop ends, resulting in an out-of-bounds read of 1 byte from the p_cstring stack memory.

Therefore, to prevent this and properly determine whether nls has been lost, it should be modified to check if "i" and len are equal, rather than dereferencing p_cstring.

---
 fs/exfat/nls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/exfat/nls.c +1 -1
index 8243d94ceaf4..de06abe426d7 100644
@@ -616,7 +616,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
 		unilen++;
 	}
 
-	if (p_cstring[i] != '\0')
+	if (i != len)
 		lossy |= NLS_NAME_OVERLEN;
 
 	*uniname = '\0';

--

Reviewed-by on v2 1/1
#1 lore raw
Quoted 28 lines · On Friday 10 October 2025 14:03:29 Jeongjun Park wrote:

In exfat_nls_to_ucs2(), if there is no NLS loss and the char-to-ucs2 conversion is successfully completed, the variable "i" will have the same value as len.

However, exfat_nls_to_ucs2() checks p_cstring[i] to determine whether nls is lost immediately after the while loop ends, so if len is FSLABEL_MAX, "i" will also be FSLABEL_MAX immediately after the while loop ends, resulting in an out-of-bounds read of 1 byte from the p_cstring stack memory.

Therefore, to prevent this and properly determine whether nls has been lost, it should be modified to check if "i" and len are equal, rather than dereferencing p_cstring.

---
 fs/exfat/nls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/exfat/nls.c +0 -0
index 8243d94ceaf4..de06abe426d7 100644

fs/exfat/nls.c static int exfat_nls_to_ucs2(struct super_block *sb,

Quoted 9 lines
fs/exfat/nls.c +1 -2
@@ -616,7 +616,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
 		unilen++;
 	}
 
-	if (p_cstring[i] != '\0')
+	if (i != len)
 		lossy |= NLS_NAME_OVERLEN;
 
 	*uniname = '\0';
--

Looks good for me,

maintainer
#2 lore raw
In reply to Jeongjun Park
Quoted 29 lines · Jeongjun Park

In exfat_nls_to_ucs2(), if there is no NLS loss and the char-to-ucs2 conversion is successfully completed, the variable "i" will have the same value as len.

However, exfat_nls_to_ucs2() checks p_cstring[i] to determine whether nls is lost immediately after the while loop ends, so if len is FSLABEL_MAX, "i" will also be FSLABEL_MAX immediately after the while loop ends, resulting in an out-of-bounds read of 1 byte from the p_cstring stack memory.

Therefore, to prevent this and properly determine whether nls has been lost, it should be modified to check if "i" and len are equal, rather than dereferencing p_cstring.

---
 fs/exfat/nls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/exfat/nls.c +0 -0
index 8243d94ceaf4..de06abe426d7 100644

fs/exfat/nls.c static int exfat_nls_to_ucs2(struct super_block *sb, · line 620

Quoted 6 lines
fs/exfat/nls.c +1 -1
@@ -616,7 +616,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
                unilen++;
        }
 
-       if (p_cstring[i] != '\0')
+       if (i != len)
                lossy |= NLS_NAME_OVERLEN;

As I said before, the core problem is passing an incorrect length in the ioctl FS_IOC_SETFSLABEL call. While your change may fix the symptom,
it doesn't fix the root cause.
Furthermore, NLS_NAME_OVERLEN macro is unnecessary.
That will result in different errors when creating a file with trailing periods with utf8 and the other iocharset.
Please remove it and make the same error handling for maximum length consistent with exfat_utf8_to_utf16().

Quoted 3 lines
fs/exfat/nls.c +0 -1
        *uniname = '\0';
--