ext4: avoid utf8_strncasecmp() with unstable name
Subsystems (3)
PATCH 1/1 ext4: avoid utf8_strncasecmp() with unstable name
From: Eric Biggers <[email protected]>
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.
--- fs/ext4/dir.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
fs/ext4/dir.c +17 -0
@@ -675,6 +675,7 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,struct qstr qstr = {.name = str, .len = len };const struct dentry *parent =READ_ONCE (dentry->d_parent);const struct inode *inode =READ_ONCE (parent->d_inode); +char strbuf[DNAME_INLINE_LEN ];if (!inode || !IS_CASEFOLDED (inode) || !EXT4_SB (inode->i_sb)->s_encoding) {
@@ -683,6 +684,22 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,return memcmp (str, name->name, len); } +/* +* If the dentry name is stored in-line, then it may be concurrently +* modified by a rename. If this happens, the VFS will eventually retry +* the lookup, so it doesn't matter what ->d_compare() returns. +* However, it's unsafe to call utf8_strncasecmp() with an unstable +* string. Therefore, we have to copy the name into a temporary buffer. +*/ +if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ; + qstr.name = strbuf; + } +return ext4_ci_compare (inode, name, &qstr, false); }
Signature · 1 line
2.26.2
Quoted 43 lines · Eric Biggers
From: Eric Biggers <[email protected]>
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) inutf8_strncasecmp(), sincefs/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 b886ee3e778e ("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]>--- fs/ext4/dir.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)fs/ext4/dir.c +7
-0@@ -675,6 +675,7 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,struct qstr qstr = {.name = str, .len = len };const struct dentry *parent =READ_ONCE (dentry->d_parent);const struct inode *inode =READ_ONCE (parent->d_inode); +char strbuf[DNAME_INLINE_LEN ];if (!inode || !IS_CASEFOLDED (inode) || !EXT4_SB (inode->i_sb)->s_encoding) {@@ -683,6 +684,22 @@ static int ext4_d_compare(const struct dentry *dentry, unsigned int len,return memcmp (str, name->name, len); } +/* +* If the dentry name is stored in-line, then it may be concurrently +* modified by a rename. If this happens, the VFS will eventually retry +* the lookup, so it doesn't matter what ->d_compare() returns. +* However, it's unsafe to call utf8_strncasecmp() with an unstable +* string. Therefore, we have to copy the name into a temporary buffer.
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 702
Quoted 10 lines
fs/ext4/dir.c +10
-0+*/ +if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ; + qstr.name = strbuf; + } +
Could we avoid this if the casefolded version were cached in the dentry? Then we could use utf8_strncasecmp_folded which would be safe. Would this be acceptable for vfs?
Quoted 2 lines
fs/ext4/dir.c +0
-0return ext4_ci_compare (inode, name, &qstr, false); }
Signature · 1 line
Gabriel Krisman Bertazi
Quoted 12 lines · Gabriel Krisman Bertazi
Quoted 12 lines
Quoted 1 line
Quoted 1 line
fs/ext4/dir.c +1
-0+/* fs/ext4/dir.c +11
-0+* If the dentry name is stored in-line, then it may be concurrently +* modified by a rename. If this happens, the VFS will eventually retry +* the lookup, so it doesn't matter what ->d_compare() returns. +* However, it's unsafe to call utf8_strncasecmp() with an unstable +* string. Therefore, we have to copy the name into a temporary buffer. +*/ +if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]);
Quoted 8 lines
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 702
Quoted 4 lines
fs/ext4/dir.c +4
-0+ strbuf[len] =0 ; + qstr.name = strbuf; + } +Could we avoid this if the casefolded version were cached in the dentry? Then we could use
utf8_strncasecmp_foldedwhich would be safe. Would this be acceptable for vfs?
The VFS assumes that each dentry has one name, the one in d_name. That's what it passes to ->d_compare(), and that's what it updates in __d_move().
So while ext4 and f2fs could put the casefolded name in ->d_fsdata, ->d_compare() wouldn't actually have access to it (unless we added d_fsdata as a parameter to ->d_compare()). Also, the casefolded name would get outdated when __d_move() changes d_name.
We could instead make d_name always be the casefolded name. I'm not sure that would be possible, though. For one, I don't think ->lookup() is allowed to just change the dentry name. It would also make getcwd(), /proc/*/fd/, etc. always show casefolded names, which could be problematic. And probably other issues I can't think of off the top of my head.
- Eric
Gabriel Krisman Bertazi
Reviewed-byQuoted 22 lines · Eric Biggers
Quoted 12 lines · Gabriel Krisman Bertazi
Quoted 12 lines
Quoted 1 line
Quoted 1 line
fs/ext4/dir.c +1
-0+/* fs/ext4/dir.c +11
-0+* If the dentry name is stored in-line, then it may be concurrently +* modified by a rename. If this happens, the VFS will eventually retry +* the lookup, so it doesn't matter what ->d_compare() returns. +* However, it's unsafe to call utf8_strncasecmp() with an unstable +* string. Therefore, we have to copy the name into a temporary buffer. +*/ +if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]);Quoted 8 lines
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 702
Quoted 4 lines
fs/ext4/dir.c +4
-0+ strbuf[len] =0 ; + qstr.name = strbuf; + } +Could we avoid this if the casefolded version were cached in the dentry? Then we could use
utf8_strncasecmp_foldedwhich would be safe. Would this be acceptable for vfs?
Quoted 7 lines
The VFS assumes that each dentry has one name, the one in
d_name. That's what it passes to ->d_compare(), and that's what it updates in__d_move().So while ext4 and f2fs could put the casefolded name in ->d_fsdata, ->d_compare() wouldn't actually have access to it (unless we added
d_fsdataas a parameter to ->d_compare()). Also, the casefolded name would get outdated when__d_move()changesd_name.
Thanks for the explanation. I see. I was thinking this cached name could be invalidated if the name changes and all that jazz, but I didn't understand this code well enough to give it a try. I hate the need to do this copy here, though.
Anyway,
Quoted 8 lines
We could instead make
d_namealways be the casefolded name. I'm not sure that would be possible, though. For one, I don't think ->lookup() is allowed to just change the dentry name. It would also makegetcwd(),/proc/*/fd/, etc. always show casefolded names, which could be problematic. And probably other issues I can't think of off the top of my head.- Eric
Signature · 7 lines
Gabriel Krisman Bertazi _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;
This READ_ONCE is going to force the compiler to use byte accesses. What's wrong with using a plain memcpy()?
Quoted 9 lines
fs/ext4/dir.c +3
-0+ qstr.name = strbuf; + } +return ext4_ci_compare (inode, name, &qstr, false); }Signature · 2 lines
-- 2.26.2
Quoted 11 lines · Matthew Wilcox
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;This
READ_ONCEis going to force the compiler to use byte accesses. What's wrong with using a plainmemcpy()?
It's undefined behavior when the source can be concurrently modified.
Compilers can assume that it's not, and remove the memcpy() (instead just using the source data directly) if they can prove that the destination array is never modified again before it goes out of scope.
Do you have any suggestions that don't involve undefined behavior?
- Eric
Al Viro
maintainerQuoted 13 lines · Eric Biggers
Quoted 11 lines · Matthew Wilcox
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;This
READ_ONCEis going to force the compiler to use byte accesses. What's wrong with using a plainmemcpy()?
Quoted 7 lines
It's undefined behavior when the source can be concurrently modified.
Compilers can assume that it's not, and remove the
memcpy()(instead just using the source data directly) if they can prove that the destination array is never modified again before it goes out of scope.Do you have any suggestions that don't involve undefined behavior?
Even memcpy(strbuf, (volatile void *)str, len)? It's been a while since I've looked at these parts of C99...
Quoted 16 lines · Al Viro
Quoted 15 lines · Eric Biggers
Quoted 11 lines · Matthew Wilcox
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;This
READ_ONCEis going to force the compiler to use byte accesses. What's wrong with using a plainmemcpy()?It's undefined behavior when the source can be concurrently modified.
Quoted 8 lines
Quoted 5 lines
Compilers can assume that it's not, and remove the
memcpy()(instead just using the source data directly) if they can prove that the destination array is never modified again before it goes out of scope.Do you have any suggestions that don't involve undefined behavior?
Even
memcpy(strbuf, (volatile void *)str, len)? It's been a while since I've looked at these parts of C99...
That doesn't make sense. memcpy() takes a non-volatile pointer, so the pointer just gets implicitly cast back to (void *), and you get a compiler warning.
- Eric
Quoted 13 lines · Eric Biggers
Quoted 11 lines · Matthew Wilcox
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;This
READ_ONCEis going to force the compiler to use byte accesses. What's wrong with using a plainmemcpy()?
Quoted 7 lines
It's undefined behavior when the source can be concurrently modified.
Compilers can assume that it's not, and remove the
memcpy()(instead just using the source data directly) if they can prove that the destination array is never modified again before it goes out of scope.Do you have any suggestions that don't involve undefined behavior?
void *memcpy_unsafe(void *dst, volatile void *src, __kernel_size_t);
It can just call memcpy() of course, but the compiler can't reason about this function because it's not a stdlib function.
Quoted 20 lines · Matthew Wilcox
Quoted 19 lines · Eric Biggers
Quoted 11 lines · Matthew Wilcox
fs/ext4/dir.c static int ext4_d_compare(const struct dentry *dentry, unsigned int len, · line 699
Quoted 6 lines · Eric Biggers
fs/ext4/dir.c +6
-0+if (len <=DNAME_INLINE_LEN -1 ) { +unsigned int i; + +for (i =0 ; i < len; i++) + strbuf[i] =READ_ONCE (str[i]); + strbuf[len] =0 ;This
READ_ONCEis going to force the compiler to use byte accesses. What's wrong with using a plainmemcpy()?It's undefined behavior when the source can be concurrently modified.
Compilers can assume that it's not, and remove the
memcpy()(instead just using the source data directly) if they can prove that the destination array is never modified again before it goes out of scope.
Quoted 6 lines
Quoted 1 line
Do you have any suggestions that don't involve undefined behavior?
void *memcpy_unsafe(void *dst, volatile void *src,
__kernel_size_t);It can just call
memcpy()of course, but the compiler can't reason about this function because it's not a stdlib function.
The compiler can still reason about it if it's in the same file, if it's an inline function, or if link-time-optimization is enabled. (LTO isn't yet supported by the mainline kernel, but people have been working on it.)
Also, as I mentioned to Al, it's necessary to cast away 'volatile' to call memcpy(). So the 'volatile' serves no purpose.
How about using barrier(), which expands to asm("" : : : "memory") to tell the compiler that memory was clobbered?
if (len <= DNAME_INLINE_LEN - 1) {
memcpy(strbuf, str, len);
strbuf[len] = 0;
/* prevent compiler from optimizing out the temporary buffer */
barrier();
}I think it's still technically undefined to call memcpy() on concurrently modified memory at all, but I think the above would be okay in practice...
Using 'noinline' could be another option.
- Eric