Skip to contentSkip to conversation

fs: exfat: Use strncpy() and bail on too long filenames

PATCH In review Marek Vasut 2 participants 3 messages Apr 30, 2025 – May 5, 2025 exFAT file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 fs: exfat: Use strncpy() and bail on too long filenames

In case the filename is too long, longer than PATH_MAX - 1, it would overflow dirs->dirname array. Add missing check and also use strncpy() to prevent the overflow in any case.

Fixes CID 550305: Security best practices violations (STRING_OVERFLOW)

---
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
 fs/exfat/io.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/exfat/io.c +4 -1
index c56f5675987..77cd2dfb6dc 100644
@@ -720,6 +720,9 @@ int exfat_fs_opendir(const char *filename, struct fs_dir_stream **dirsp)
 	struct exfat_node *dnode;
 	int err;
 
+	if (strlen(filename) >= PATH_MAX)
+		return -ENAMETOOLONG;
+
 	err = exfat_lookup_realpath(&ctxt.ef, &dnode, filename);
 	if (err)
 		return err;
@@ -736,7 +739,7 @@ int exfat_fs_opendir(const char *filename, struct fs_dir_stream **dirsp)
 	if (!dirs)
 		return -ENOMEM;
 
-	strcpy(dirs->dirname, filename);
+	strncpy(dirs->dirname, filename, PATH_MAX - 1);
 	dirs->offset = -1;
 
 	*dirsp = &dirs->fs_dirs;
Signature · 1 line
2.47.2
#1 lore raw

PATCH 2/2 fs: exfat: Inhibit unused exfat_humanize_bytes() and exfat_print_info()

Make sure unused exfat_humanize_bytes() and exfat_print_info() functions are not compiled into U-Boot code base. This also removes CID 550300: Integer handling issues (INTEGER_OVERFLOW) in exfat_humanize_bytes() , which is now surely unreachable.

---
Cc: Tom Rini <[email protected]>
Cc: [email protected]

---
 fs/exfat/exfat.h | 2 ++
 fs/exfat/utils.c | 2 ++
 2 files changed, 4 insertions(+)
fs/exfat/exfat.h +2 -0
index cce3e3021ca..ca6f22b9d58 100644
@@ -210,9 +210,11 @@ uint32_t exfat_vbr_start_checksum(const void* sector, size_t size);
 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum);
 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
 		size_t length);
+#ifndef __UBOOT__
 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb);
 void exfat_print_info(const struct exfat_super_block* sb,
 		uint32_t free_clusters);
+#endif
 bool exfat_match_option(const char* options, const char* option_name);
 
 int exfat_utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
fs/exfat/utils.c +2 -0
index 0d97b65dcab..f99dcba7e3d 100644
@@ -134,6 +134,7 @@ le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
 	return cpu_to_le16(hash);
 }
 
+#ifndef __UBOOT__
 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
 {
 	size_t i;
@@ -178,6 +179,7 @@ void exfat_print_info(const struct exfat_super_block* sb,
 	exfat_humanize_bytes(avail_space, &hb);
 	printf("Available space      %10"PRIu64" %s\n", hb.value, hb.unit);
 }
+#endif
 
 bool exfat_match_option(const char* options, const char* option_name)
 {
Signature · 1 line
2.47.2
#2 lore raw
In reply to Marek Vasut
Quoted 8 lines · Marek Vasut

In case the filename is too long, longer than PATH_MAX - 1, it would overflow dirs->dirname array. Add missing check and also use strncpy() to prevent the overflow in any case.

Fixes CID 550305: Security best practices violations (STRING_OVERFLOW)

[...]

Applied to u-boot/master, thanks!

[1/2] fs: exfat: Use strncpy() and bail on too long filenames

      commit: 4ba2fe14f2cecf7b614220e8e19c1fcdda92b748
[2/2] fs: exfat: Inhibit unused exfat_humanize_bytes() and exfat_print_info()
      commit: 4d3b5c679bc9d5c6cbbeedcc1e4a186f1cc35541
Signature · 1 line
Tom