ovl: Fix uninit-value in ovl_fill_real
PATCH 1/1 ovl: Fix uninit-value in ovl_fill_real
Syzbot reported a KMSAN uninit-value issue in ovl_fill_real.
This iusse's call chain is:__do_sys_getdents64()
-> iterate_dir()
...
-> ext4_readdir()
-> fscrypt_fname_alloc_buffer() // alloc
-> fscrypt_fname_disk_to_usr // write without tail '\0'
-> dir_emit()
-> ovl_fill_real() // read by strcmp()The string is used to store the decrypted directory entry name for an encrypted inode. As shown in the call chain, fscrypt_fname_disk_to_usr() write it wthout null-terminate. However, ovl_fill_real() uses strcmp() to compare the name against "..", which assumes a null-terminated string and may trigger a KMSAN uninit-value warning when the buffer tail contains uninit data.
--- fs/overlayfs/readdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/readdir.c +1 -1
@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino &&strncmp (name,".." ,2 ) ==0 ) { ino = rdt->parent_ino; }else if (rdt->cache) {struct ovl_cache_entry *p;
Signature · 1 line
2.34.1
Miklos Szeredi
maintainerQuoted 33 lines · Qing Wang
Syzbot reported a KMSAN uninit-value issue in
ovl_fill_real.This iusse's call chain is:
__do_sys_getdents64()-> iterate_dir() ... -> ext4_readdir() -> fscrypt_fname_alloc_buffer() // alloc -> fscrypt_fname_disk_to_usr // write without tail '\0' -> dir_emit() -> ovl_fill_real() // read by strcmp()The string is used to store the decrypted directory entry name for an encrypted inode. As shown in the call chain,
fscrypt_fname_disk_to_usr()write it wthout null-terminate. However,ovl_fill_real()usesstrcmp()to compare the name against "..", which assumes a null-terminated string and may trigger a KMSAN uninit-value warning when the buffer tail contains uninit data.Reported-by [email protected]Fixes 4edb83bb1041 ("ovl: constant d_ino for non-merge dirs")Signed-off-by Qing Wang <[email protected]>--- fs/overlayfs/readdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/overlayfs/readdir.c +0
-0
fs/overlayfs/readdir.c static bool ovl_fill_real(struct dir_context *ctx, const char *name, · line 758
Quoted 5 lines
fs/overlayfs/readdir.c +1
-1@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino &&strncmp (name,".." ,2 ) ==0 ) {
Need to compare namelen as well.
Thanks,
Miklos
PATCH v2 1/1 ovl: Fix uninit-value in ovl_fill_real
Syzbot reported a KMSAN uninit-value issue in ovl_fill_real.
This iusse's call chain is:__do_sys_getdents64()
-> iterate_dir()
...
-> ext4_readdir()
-> fscrypt_fname_alloc_buffer() // alloc
-> fscrypt_fname_disk_to_usr // write without tail '\0'
-> dir_emit()
-> ovl_fill_real() // read by strcmp()The string is used to store the decrypted directory entry name for an encrypted inode. As shown in the call chain, fscrypt_fname_disk_to_usr() write it wthout null-terminate. However, ovl_fill_real() uses strcmp() to compare the name against "..", which assumes a null-terminated string and may trigger a KMSAN uninit-value warning when the buffer tail contains uninit data.
--- fs/overlayfs/readdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/readdir.c +1 -1
@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino && namelen ==2 && !strncmp (name,".." , namelen)) { ino = rdt->parent_ino; }else if (rdt->cache) {struct ovl_cache_entry *p;
Signature · 1 line
2.34.1
Quoted 13 lines · Qing Wang
Syzbot reported a KMSAN uninit-value issue in
ovl_fill_real.This iusse's call chain is:
__do_sys_getdents64()-> iterate_dir() ... -> ext4_readdir() -> fscrypt_fname_alloc_buffer() // alloc -> fscrypt_fname_disk_to_usr // write without tail '\0' -> dir_emit() -> ovl_fill_real() // read by strcmp()
Quoted 3 lines
The string is used to store the decrypted directory entry name for an encrypted inode. As shown in the call chain,
fscrypt_fname_disk_to_usr()write it wthout null-terminate. However,ovl_fill_real()usesstrcmp()to
typo: without
Quoted 8 lines
compare the name against "..", which assumes a null-terminated string and may trigger a KMSAN uninit-value warning when the buffer tail contains uninit data.
Reported-by [email protected]Fixes 4edb83bb1041 ("ovl: constant d_ino for non-merge dirs")Signed-off-by Qing Wang <[email protected]>
Quoted 18 lines
--- fs/overlayfs/readdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/overlayfs/readdir.c +1
-2@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino && namelen ==2 && !strncmp (name,".." , namelen)) { ino = rdt->parent_ino; }else if (rdt->cache) {struct ovl_cache_entry *p; --
Quoted 2 lines
2.34.1
Eric,
Considering that fscrypt_fname_alloc_buffer() anyway allocates the byte for NUL termination and that decrypted names are zero padded (right?).
How about reinforcing this fix with:
fs/crypto/fname.c +1 -0
@@ -276,6 +276,7 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,if (fscrypt_is_dot_dotdot (&qname)) { oname->name[0 ] ='.' ; oname->name[iname->len -1 ] ='.' ; + oname->name[iname->len] =0 ; oname->len = iname->len;return 0 ; }
Thanks,
Amir.
Eric Biggers
maintainerQuoted 4 lines · Amir Goldstein
Eric,
Considering that
fscrypt_fname_alloc_buffer()anyway allocates the byte for NUL termination and that decrypted names are zero padded (right?).
Only when the length of the original filename isn't already a multiple of the padding amount, as configured by FSCRYPT_POLICY_FLAGS_PAD_*.
fs/crypto/fname.c int fscrypt_fname_disk_to_usr(const struct inode *inode, · line 281
Quoted 13 lines
How about reinforcing this fix with:
fs/crypto/fname.c +1
-0@@ -276,6 +276,7 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,if (fscrypt_is_dot_dotdot (&qname)) { oname->name[0 ] ='.' ; oname->name[iname->len -1 ] ='.' ; + oname->name[iname->len] =0 ; oname->len = iname->len;return 0 ;
Do you propose to do the same for all callers of dir_emit() in all filesystems? It seems not NUL-terminating the name is normal. Just usually the name is in the pagecache rather than slab memory, which makes KMSAN not usually notice the out-of-bounds read in overlayfs.
- Eric
Miklos Szeredi
maintainerQuoted 33 lines · Qing Wang
Syzbot reported a KMSAN uninit-value issue in
ovl_fill_real.This iusse's call chain is:
__do_sys_getdents64()-> iterate_dir() ... -> ext4_readdir() -> fscrypt_fname_alloc_buffer() // alloc -> fscrypt_fname_disk_to_usr // write without tail '\0' -> dir_emit() -> ovl_fill_real() // read by strcmp()The string is used to store the decrypted directory entry name for an encrypted inode. As shown in the call chain,
fscrypt_fname_disk_to_usr()write it wthout null-terminate. However,ovl_fill_real()usesstrcmp()to compare the name against "..", which assumes a null-terminated string and may trigger a KMSAN uninit-value warning when the buffer tail contains uninit data.Reported-by [email protected]Fixes 4edb83bb1041 ("ovl: constant d_ino for non-merge dirs")Signed-off-by Qing Wang <[email protected]>--- fs/overlayfs/readdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/overlayfs/readdir.c +0
-0
fs/overlayfs/readdir.c static bool ovl_fill_real(struct dir_context *ctx, const char *name, · line 758
Quoted 5 lines
fs/overlayfs/readdir.c +1
-1@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino && namelen ==2 && !strncmp (name,".." , namelen)) {
Yeah, and also the str... functions only make sense on null terminated strings, so that needs to be memcmp.
Thanks,
Miklos
Quoted 11 lines · Miklos Szeredi
fs/overlayfs/readdir.c +1
-1@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino && namelen ==2 && !strncmp (name,".." , namelen)) {
Quoted 2 lines
Yeah, and also the str... functions only make sense on null terminated strings, so that needs to be memcmp.
Thanks for your idea, but I think strncmp is same as memncmp in this case. strncmp dons't use null terminated.
```
int strncmp(const char *cs, const char *ct, size_t count)
{
unsigned char c1, c2;
while (count) {
c1 = *cs++;
c2 = *ct++;
if (c1 != c2)
return c1 < c2 ? -1 : 1;
if (!c1)
break;
count--;
}
return 0;
}
```What do you think?
--
Best Regards,
Qing
Amir Goldstein
maintainerQuoted 37 lines · Qing Wang
Quoted 11 lines · Miklos Szeredi
fs/overlayfs/readdir.c +1
-1@@ -755,7 +755,7 @@ static bool ovl_fill_real(struct dir_context *ctx, const char *name,struct dir_context *orig_ctx = rdt->orig_ctx;bool res; -if (rdt->parent_ino &&strcmp (name,".." ) ==0 ) { +if (rdt->parent_ino && namelen ==2 && !strncmp (name,".." , namelen)) {Quoted 2 lines
Yeah, and also the str... functions only make sense on null terminated strings, so that needs to be memcmp.
Thanks for your idea, but I think strncmp is same as memncmp in this case. strncmp dons't use null terminated.
``` int strncmp(const char *cs, const char *ct, size_t count) { unsigned char c1, c2; while (count) { c1 = *cs++; c2 = *ct++; if (c1 != c2) return c1 < c2 ? -1 : 1; if (!c1) break; count--; } return 0; } ```
Quoted 1 line
What do you think?
I think this discussion is not worth spending our time.
After namelen == 2, it really does not matter if we use strncmp or memcmp IMO. I will post a followup patch to add name_is_dotdot() helper and use those helpers in overlayfs readdir code instead of all the many open coded variants that it has now, which may be safe, but still ugly.
In the meanwhile I'd rather apply v2 as is so it could be picked by stable.
Thanks,
Amir.
Miklos Szeredi
maintainerQuoted 1 line · Amir Goldstein
In the meanwhile I'd rather apply v2 as is so it could be picked by stable.
ACK.
Thanks,
Miklos