Skip to content

Commit 2ce3ee9

Browse files
ebiggerstytso
authored andcommitted
ext4: 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: b886ee3 ("ext4: Support case-insensitive file name lookups") Cc: <[email protected]> # v5.2+ 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: Andreas Dilger <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 5adacca commit 2ce3ee9

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

fs/ext4/dir.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,
675675
struct qstr qstr = {.name = str, .len = len };
676676
const struct dentry *parent = READ_ONCE(dentry->d_parent);
677677
const struct inode *inode = READ_ONCE(parent->d_inode);
678+
char strbuf[DNAME_INLINE_LEN];
678679

679680
if (!inode || !IS_CASEFOLDED(inode) ||
680681
!EXT4_SB(inode->i_sb)->s_encoding) {
@@ -683,6 +684,21 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,
683684
return memcmp(str, name->name, len);
684685
}
685686

687+
/*
688+
* If the dentry name is stored in-line, then it may be concurrently
689+
* modified by a rename. If this happens, the VFS will eventually retry
690+
* the lookup, so it doesn't matter what ->d_compare() returns.
691+
* However, it's unsafe to call utf8_strncasecmp() with an unstable
692+
* string. Therefore, we have to copy the name into a temporary buffer.
693+
*/
694+
if (len <= DNAME_INLINE_LEN - 1) {
695+
memcpy(strbuf, str, len);
696+
strbuf[len] = 0;
697+
qstr.name = strbuf;
698+
/* prevent compiler from optimizing out the temporary buffer */
699+
barrier();
700+
}
701+
686702
return ext4_ci_compare(inode, name, &qstr, false);
687703
}
688704

0 commit comments

Comments
 (0)