Skip to contentSkip to conversation

ext4: fix memory leak in ext4_fname_setup_filename() error path

PATCH In review Luís Henriques 2 participants 3 messages Aug 1, 2023 – Aug 2, 2023 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: fix memory leak in ext4_fname_setup_filename() error path

If casefolding the filename fails, we'll be leaking fscrypt_buf name. Make sure we free it in the error path.

---
 fs/ext4/crypto.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/crypto.c +2 -0
index e20ac0654b3f..9e4503b051c4 100644
@@ -33,6 +33,8 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
 
 #if IS_ENABLED(CONFIG_UNICODE)
 	err = ext4_fname_setup_ci_filename(dir, iname, fname);
+	if (err)
+		fscrypt_free_filename(&name);
 #endif
 	return err;
 }
#1 lore raw
Quoted 13 lines · Luís Henriques

If casefolding the filename fails, we'll be leaking fscrypt_buf name. Make sure we free it in the error path.

---
 fs/ext4/crypto.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/crypto.c +0 -0
index e20ac0654b3f..9e4503b051c4 100644

fs/ext4/crypto.c int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, · line 40

Quoted 8 lines
fs/ext4/crypto.c +2 -0
@@ -33,6 +33,8 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
 
 #if IS_ENABLED(CONFIG_UNICODE)
 	err = ext4_fname_setup_ci_filename(dir, iname, fname);
+	if (err)
+		fscrypt_free_filename(&name);
 #endif
 	return err;
 }

Doesn't ext4_fname_prepare_lookup() have the same bug?

Also, please include a Fixes tag.

- Eric

#2 lore raw
Quoted 15 lines · Eric Biggers
Quoted 14 lines · Luís Henriques

If casefolding the filename fails, we'll be leaking fscrypt_buf name. Make sure we free it in the error path.

---
 fs/ext4/crypto.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ext4/crypto.c +0 -0
index e20ac0654b3f..9e4503b051c4 100644
@@ -33,6 +33,8 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
 
Quoted 9 lines

fs/ext4/crypto.c int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, · line 40

Quoted 7 lines
fs/ext4/crypto.c +2 -0
 #if IS_ENABLED(CONFIG_UNICODE)
 	err = ext4_fname_setup_ci_filename(dir, iname, fname);
+	if (err)
+		fscrypt_free_filename(&name);
 #endif
 	return err;
 }

Doesn't ext4_fname_prepare_lookup() have the same bug?

Doh! I looked into that function too and then completely forgot about it. It may also call fscrypt_setup_filename(), so it has to do the free in the error path as well. I'll send out v2 to include that fix too.

Quoted 1 line

Also, please include a Fixes tag.

Ok, makes sense. Thanks!

Cheers,

Signature · 1 line
Luís