Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-fscrypt@vger.kernel.org
Cc: linux-btrfs@vger.kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH] fscrypt: Defensively clear dangling crypto_buf pointer
Date: Sat,  5 Dec 2026 21:50:45 -0800	[thread overview]
Message-ID: <20261206055045.320196-1-ebiggers@kernel.org> (raw)

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


                 reply	other threads:[~2026-12-06  5:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206055045.320196-1-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fscrypt@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox