Skip to contentSkip to conversation

ovl: reserve space for the casefolded name terminator

PATCH In review Hungyu Lin 3 participants 3 messages Oct 28 – Oct 30

Subsystems (3)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH 1/1 ovl: reserve space for the casefolded name terminator

ovl_casefold() allocates NAME_MAX bytes and passes that capacity to utf8_casefold(), which also needs room for a terminating NUL. A valid name whose folded form is exactly NAME_MAX bytes therefore fails to fold, and ovl_fill_merge() falls back to comparing the original bytes.

For example, when two casefolded layers contain directories named with 255 uppercase and lowercase ASCII letters respectively, readdir lists both names even though lookup treats them as the same directory. The same names merge correctly at 254 bytes.

Reserve one additional byte in both the allocation and the folding capacity so that NAME_MAX-byte folded names use the canonical merge key.

Fixes ee95c5fc86dd ("ovl: Create ovl_casefold() to support casefolded strncmp()")
---
 fs/overlayfs/readdir.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/overlayfs/readdir.c +2 -2
index 7d6f7f6022eb..6113d690f8ad 100644
@@ -81,13 +81,13 @@ static int ovl_casefold(struct ovl_readdir_data *rdd, const char *str, int len,
 	    name_is_dot_dotdot(str, len))
 		return 0;
 
-	cf_name = kmalloc(NAME_MAX, GFP_KERNEL);
+	cf_name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
 	if (!cf_name) {
 		rdd->err = -ENOMEM;
 		return -ENOMEM;
 	}
 
-	cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX);
+	cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX + 1);
 	if (cf_len > 0)
 		*dst = cf_name;
 	else
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 42 lines · Hungyu Lin

ovl_casefold() allocates NAME_MAX bytes and passes that capacity to utf8_casefold(), which also needs room for a terminating NUL. A valid name whose folded form is exactly NAME_MAX bytes therefore fails to fold, and ovl_fill_merge() falls back to comparing the original bytes.

For example, when two casefolded layers contain directories named with 255 uppercase and lowercase ASCII letters respectively, readdir lists both names even though lookup treats them as the same directory. The same names merge correctly at 254 bytes.

Reserve one additional byte in both the allocation and the folding capacity so that NAME_MAX-byte folded names use the canonical merge key.

Fixes ee95c5fc86dd ("ovl: Create ovl_casefold() to support casefolded strncmp()")
---
 fs/overlayfs/readdir.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/overlayfs/readdir.c +2 -3
index 7d6f7f6022eb..6113d690f8ad 100644
@@ -81,13 +81,13 @@ static int ovl_casefold(struct ovl_readdir_data *rdd, const char *str, int len,
            name_is_dot_dotdot(str, len))
                return 0;
 
-       cf_name = kmalloc(NAME_MAX, GFP_KERNEL);
+       cf_name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
        if (!cf_name) {
                rdd->err = -ENOMEM;
                return -ENOMEM;
        }
 
-       cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX);
+       cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX + 1);
        if (cf_len > 0)
                *dst = cf_name;
        else
--
Quoted 2 lines

2.43.0

Queued for ovl-fixes

Thanks,
Amir.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Hungyu Lin

Hi Hungyu, Amir,

Sorry for the delay and thanks for the patch!

Em 28/10/2026 16:51, Hungyu Lin escreveu:

Quoted 10 lines

ovl_casefold() allocates NAME_MAX bytes and passes that capacity to utf8_casefold(), which also needs room for a terminating NUL. A valid name whose folded form is exactly NAME_MAX bytes therefore fails to fold, and ovl_fill_merge() falls back to comparing the original bytes.

For example, when two casefolded layers contain directories named with 255 uppercase and lowercase ASCII letters respectively, readdir lists both names even though lookup treats them as the same directory. The same names merge correctly at 254 bytes.

Quoted 6 lines

Reserve one additional byte in both the allocation and the folding capacity so that NAME_MAX-byte folded names use the canonical merge key.

Fixes ee95c5fc86dd ("ovl: Create ovl_casefold() to support casefolded strncmp()")

I've tested and confirmed that files with 255 filenames will appear duplicated.

Quoted 11 lines
---
  fs/overlayfs/readdir.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/overlayfs/readdir.c +0 -0
index 7d6f7f6022eb..6113d690f8ad 100644
@@ -81,13 +81,13 @@ static int ovl_casefold(struct ovl_readdir_data *rdd, const char *str, int len,
  	    name_is_dot_dotdot(str, len))
  		return 0;

fs/overlayfs/readdir.c static int ovl_casefold(struct ovl_readdir_data *rdd, const char *str, int len, · line 90

Quoted 10 lines
fs/overlayfs/readdir.c +2 -2
 
-	cf_name = kmalloc(NAME_MAX, GFP_KERNEL);
+	cf_name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
  	if (!cf_name) {
  		rdd->err = -ENOMEM;
  		return -ENOMEM;
  	}
 
-	cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX);
+	cf_len = utf8_casefold(rdd->map, &qstr, cf_name, NAME_MAX + 1);

I wonder if the other callers for utf8_casefold() also need a fix?

Quoted 3 lines
fs/overlayfs/readdir.c +0 -0
  	if (cf_len > 0)
  		*dst = cf_name;
  	else