Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH] fscrypt: Defensively clear dangling crypto_buf pointer
@ 2026-12-06  5:50 Eric Biggers
  0 siblings, 0 replies; only message in thread
From: Eric Biggers @ 2026-12-06  5:50 UTC (permalink / raw)
  To: linux-fscrypt; +Cc: linux-btrfs, Eric Biggers

After fscrypt_name::crypto_buf.name is freed by fscrypt_free_filename()
or by the error path of fscrypt_setup_filename(), set it to NULL.

This doesn't matter for any current code.  However, this prevents double
frees from accidentally being introduced in the future, such as if
fscrypt_free_filename() is called after fscrypt_setup_filename() returns
an error.  In fact, btrfs_link() does exactly that; the only reason it's
benign currently is that btrfs encryption is not finished yet, so the
IS_ENCRYPTED() path never gets taken on btrfs.

Note: most calls to fscrypt_free_filename() are on a stack variable
that's going out of scope.  The NULL assignment gets optimized out from
those anyway.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 fs/crypto/fname.c       | 2 +-
 include/linux/fscrypt.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c
index d8c9d442c7fbb..be2a7e2b7e86d 100644
--- a/fs/crypto/fname.c
+++ b/fs/crypto/fname.c
@@ -407,7 +407,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
 	return 0;
 
 errout:
-	kfree(fname->crypto_buf.name);
+	fscrypt_free_filename(fname);
 	return ret;
 }
 EXPORT_SYMBOL(fscrypt_setup_filename);
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index f6c462e70f01a..ecf825cafa6a7 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -421,6 +421,7 @@ int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
 {
 	kfree(fname->crypto_buf.name);
+	fname->crypto_buf.name = NULL;
 }
 
 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,

base-commit: 481f4e477b715a5c4e3f4fe81551e0bb68007679
-- 
2.56.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-06  5:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  5:50 [PATCH] fscrypt: Defensively clear dangling crypto_buf pointer Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox