Skip to contentSkip to conversation

ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup}

PATCH Applied Luís Henriques 3 participants 3 messages Aug 3, 2023 – Aug 5, 2023 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup}

If the filename casefolding fails, we'll be leaking memory from the fscrypt_name struct, namely from the 'crypto_buf.name' member.

Make sure we free it in the error path on both ext4_fname_setup_filename() and ext4_fname_prepare_lookup() functions.

Fixes 1ae98e295fa2 ("ext4: optimize match for casefolded encrypted dirs")

---
Changes since v2:
- Rephrased commit log to (hopefully!) make it more clear.
- Use ext4_fname_free_filename() instead of fscrypt_free_filename(). Although not strictly needed, it cleans up the fname struct, removing a dangling pointer to freed memory.

Changes since v1:
- Include fix to ext4_fname_prepare_lookup() as well
- Add 'Fixes:' tag

 fs/ext4/crypto.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ext4/crypto.c +4 -0
index e20ac0654b3f..453d4da5de52 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)
+		ext4_fname_free_filename(fname);
 #endif
 	return err;
 }
@@ -51,6 +53,8 @@ int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
 
 #if IS_ENABLED(CONFIG_UNICODE)
 	err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname);
+	if (err)
+		ext4_fname_free_filename(fname);
 #endif
 	return err;
 }
Reviewed-by on v3 1/1
#1 lore raw
Quoted 36 lines · Luís Henriques

If the filename casefolding fails, we'll be leaking memory from the fscrypt_name struct, namely from the 'crypto_buf.name' member.

Make sure we free it in the error path on both ext4_fname_setup_filename() and ext4_fname_prepare_lookup() functions.

Fixes 1ae98e295fa2 ("ext4: optimize match for casefolded encrypted dirs")

---
Changes since v2:
- Rephrased commit log to (hopefully!) make it more clear.
- Use ext4_fname_free_filename() instead of fscrypt_free_filename(). Although not strictly needed, it cleans up the fname struct, removing a dangling pointer to freed memory.

Changes since v1:
- Include fix to ext4_fname_prepare_lookup() as well
- Add 'Fixes:' tag

 fs/ext4/crypto.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ext4/crypto.c +2 -0
index e20ac0654b3f..453d4da5de52 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)
+		ext4_fname_free_filename(fname);
 #endif
 	return err;
 }

fs/ext4/crypto.c int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry, · line 60

Quoted 8 lines
fs/ext4/crypto.c +2 -0
@@ -51,6 +53,8 @@ int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
 
 #if IS_ENABLED(CONFIG_UNICODE)
 	err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname);
+	if (err)
+		ext4_fname_free_filename(fname);
 #endif
 	return err;
 }

- Eric

maintainer Applied
#2 lore raw
In reply to Luís Henriques
Quoted 8 lines · Luís Henriques

If the filename casefolding fails, we'll be leaking memory from the fscrypt_name struct, namely from the 'crypto_buf.name' member.

Make sure we free it in the error path on both ext4_fname_setup_filename() and ext4_fname_prepare_lookup() functions.

[...]

Applied, thanks!

[1/1] ext4: fix memory leaks in ext4_fname_{setup_filename,prepare_lookup}

      commit: 7ca4b085f430f3774c3838b3da569ceccd6a0177

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>