Skip to content

Commit fc3bb09

Browse files
ebiggersJaegeuk Kim
authored andcommitted
f2fs: avoid utf8_strncasecmp() with unstable name
If the dentry name passed to ->d_compare() fits in dentry::d_iname, then it may be concurrently modified by a rename. This can cause undefined behavior (possibly out-of-bounds memory accesses or crashes) in utf8_strncasecmp(), since fs/unicode/ isn't written to handle strings that may be concurrently modified. Fix this by first copying the filename to a stack buffer if needed. This way we get a stable snapshot of the filename. Fixes: 2c2eb7a ("f2fs: Support case-insensitive file name lookups") Cc: <[email protected]> # v5.4+ Cc: Al Viro <[email protected]> Cc: Daniel Rosenberg <[email protected]> Cc: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 0b6d4ca commit fc3bb09

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

fs/f2fs/dir.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,11 +1114,27 @@ static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
11141114
const struct inode *dir = READ_ONCE(parent->d_inode);
11151115
const struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
11161116
struct qstr entry = QSTR_INIT(str, len);
1117+
char strbuf[DNAME_INLINE_LEN];
11171118
int res;
11181119

11191120
if (!dir || !IS_CASEFOLDED(dir))
11201121
goto fallback;
11211122

1123+
/*
1124+
* If the dentry name is stored in-line, then it may be concurrently
1125+
* modified by a rename. If this happens, the VFS will eventually retry
1126+
* the lookup, so it doesn't matter what ->d_compare() returns.
1127+
* However, it's unsafe to call utf8_strncasecmp() with an unstable
1128+
* string. Therefore, we have to copy the name into a temporary buffer.
1129+
*/
1130+
if (len <= DNAME_INLINE_LEN - 1) {
1131+
memcpy(strbuf, str, len);
1132+
strbuf[len] = 0;
1133+
entry.name = strbuf;
1134+
/* prevent compiler from optimizing out the temporary buffer */
1135+
barrier();
1136+
}
1137+
11221138
res = utf8_strncasecmp(sbi->s_encoding, name, &entry);
11231139
if (res >= 0)
11241140
return res;

0 commit comments

Comments
 (0)