Skip to contentSkip to conversation

fscrypt: Fix uninit-value in ovl_fill_real

PATCH Superseded Qing Wang 2 participants 6 messages Jan 23 – Jan 27

Subsystems (2)
lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 fscrypt: Fix uninit-value in ovl_fill_real

Syzbot reported a KMSAN uninit-value issue in ovl_fill_real and it was allocated from fscrypt_fname_alloc_buffer. Fixed it by kzalloc.

The call chain is:
__do_sys_getdents64()

    -> iterate_dir()
        ...
            -> ext4_readdir()
                -> fscrypt_fname_alloc_buffer() // alloc
                -> dir_emit()
                    -> ovl_fill_real() // use by strcmp()
Reported-by [email protected]

Close: https://syzkaller.appspot.com/bug?extid=d130f98b2c265fae5297

---
 fs/crypto/fname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/crypto/fname.c +1 -1
index a9a4432d12ba..ba8282b96a2e 100644
@@ -220,7 +220,7 @@ int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
 	u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
 				      max_encrypted_len);
 
-	crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
+	crypto_str->name = kzalloc(max_presented_len + 1, GFP_NOFS);
 	if (!crypto_str->name)
 		return -ENOMEM;
 	crypto_str->len = max_presented_len;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 24 lines · Qing Wang

Syzbot reported a KMSAN uninit-value issue in ovl_fill_real and it was allocated from fscrypt_fname_alloc_buffer. Fixed it by kzalloc.

The call chain is:
__do_sys_getdents64()

    -> iterate_dir()
        ...
            -> ext4_readdir()
                -> fscrypt_fname_alloc_buffer() // alloc
                -> dir_emit()
                    -> ovl_fill_real() // use by strcmp()
Reported-by [email protected]

Close: https://syzkaller.appspot.com/bug?extid=d130f98b2c265fae5297

---
 fs/crypto/fname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/crypto/fname.c +0 -0
index a9a4432d12ba..ba8282b96a2e 100644

fs/crypto/fname.c int fscrypt_fname_alloc_buffer(u32 max_encrypted_len, · line 223

Quoted 5 lines
fs/crypto/fname.c +1 -1
@@ -220,7 +220,7 @@ int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
 	u32 max_presented_len = max_t(u32, FSCRYPT_NOKEY_NAME_MAX_ENCODED,
 				      max_encrypted_len);
 
-	crypto_str->name = kmalloc(max_presented_len + 1, GFP_NOFS);
+	crypto_str->name = kzalloc(max_presented_len + 1, GFP_NOFS);

For KMSAN issues, it's important to root-cause them. Zero-initialization isn't necessarily the right fix.

In this case, it looks like ovl_fill_real() is incorrectly assuming that the name is NUL-terminated.

Yet, the name passed to dir_context::actor isn't normally NUL-terminated. Even for a regular directory, ext4 just passes a pointer to the filename in the ext4_dir_entry_2 in the buffer cache.

The encrypted directory case doesn't seem to be fundamentally different. Just KMSAN is able to report the issue because the memory is in a slab buffer rather than the buffer cache.

Can you consider fixing ovl_fill_real()? Instead of strcmp(".."), it should check whether namelen is 2 and the first two chars are '.'.

- Eric

author
#2 lore raw

PATCH v2 1/1 fscrypt: 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.

Reported-by [email protected]
Fixes 4edb83bb1041 ("ovl: constant d_ino for non-merge dirs")
---
 fs/overlayfs/readdir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/readdir.c +1 -1
index 160960bb0ad0..e852b38949b6 100644
@@ -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) == 0) {
 		ino = rdt->parent_ino;
 	} else if (rdt->cache) {
 		struct ovl_cache_entry *p;
Signature · 1 line
2.34.1
maintainer
#3 lore raw
Quoted 3 lines · Qing Wang
---
 fs/overlayfs/readdir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This is an overlayfs patch, so please title it appropriately and use get_maintainer.pl to get the correct recipients. You can leave [email protected] on Cc.

- Eric

author
#4 lore raw
Quoted 3 lines · Eric Biggers

This is an overlayfs patch, so please title it appropriately and use get_maintainer.pl to get the correct recipients. You can leave [email protected] on Cc.

Thank you again for your guidance.

--
Best regards,
Qing

author
#5 lore raw
In reply to Eric Biggers
Quoted 10 lines · Eric Biggers

For KMSAN issues, it's important to root-cause them. Zero-initialization isn't necessarily the right fix.

In this case, it looks like ovl_fill_real() is incorrectly assuming that the name is NUL-terminated.

Yet, the name passed to dir_context::actor isn't normally NUL-terminated. Even for a regular directory, ext4 just passes a pointer to the filename in the ext4_dir_entry_2 in the buffer cache.

Quoted 6 lines

The encrypted directory case doesn't seem to be fundamentally different. Just KMSAN is able to report the issue because the memory is in a slab buffer rather than the buffer cache.

Can you consider fixing ovl_fill_real()? Instead of strcmp(".."), it should check whether namelen is 2 and the first two chars are '.'.

Hi Eric,
Thanks for your reply. I agreed with your idea and resend a new patch.

https://lore.kernel.org/all/[email protected]/

Looking forward to your next review.

Signature · 2 lines
Best Regards,
Qing