Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: "Miquel Sabaté Solà" <mssola@mssola.com>
To: Weiming Shi <bestswngs@gmail.com>
Cc: linux-btrfs@vger.kernel.org,  dsterba@suse.com,
	 josef@toxicpanda.com, clm@fb.com,  xmei5@asu.edu
Subject: Re: [PATCH] btrfs: reject names longer than BTRFS_NAME_LEN in btrfs_get_name()
Date: Sun, 07 Jun 2026 22:23:09 +0200	[thread overview]
Message-ID: <87fr2yw15e.fsf@> (raw)
In-Reply-To: <20260607121429.2091845-1-bestswngs@gmail.com> (Weiming Shi's message of "Sun, 7 Jun 2026 05:14:29 -0700")

[-- Attachment #1: Type: text/plain, Size: 2429 bytes --]

Hi,

Weiming Shi @ 2026-06-07 05:14 -07:

> btrfs_get_name() reads the name length straight from the on-disk
> inode_ref (or root_ref) and copies that many bytes into the caller's
> buffer with no upper bound. The caller (exportfs_get_name()) supplies a
> fixed NAME_MAX + 1 byte stack buffer, but name_len is a __le16 read from
> the leaf and the tree-checker only bounds it to the item size, not to
> BTRFS_NAME_LEN. A crafted leaf with name_len = 4096 therefore overflows
> the 256-byte buffer with attacker-controlled bytes. It is reachable from
> a mounted untrusted btrfs image via open_by_handle_at(), and on btrfs
> filesystems exported over NFS.
>
>  BUG: KASAN: stack-out-of-bounds in read_extent_buffer (fs/btrfs/extent_io.c:3742)
>  Write of size 633 at addr ffffc90006c9fc40 by task exploit/5192
>   read_extent_buffer (fs/btrfs/extent_io.c:3742)
>   btrfs_get_name (fs/btrfs/export.c:289)
>   reconnect_path (fs/exportfs/expfs.c:222)
>   exportfs_decode_fh_raw (fs/exportfs/expfs.c:473)
>   do_handle_open (fs/fhandle.c:230)
>   do_syscall_64 (arch/x86/entry/syscall_64.c:94)
>  Kernel panic - not syncing: stack-protector: Kernel stack is corrupted
>
> Reject any name_len greater than BTRFS_NAME_LEN before the copy. Such a
> name is never valid on disk, so this only rejects corrupted leaves and
> leaves valid names unchanged.
>
> Fixes: 2ede0daf0154 ("Btrfs: handle NFS lookups properly")
> Reported-by: Xiang Mei <xmei5@asu.edu>
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> ---
>  fs/btrfs/export.c | 10 ++++++++++
>  1 file changed, 10 insertions(+)
>
> diff --git a/fs/btrfs/export.c b/fs/btrfs/export.c
> index c403117ac..a54c6e8b3 100644
> --- a/fs/btrfs/export.c
> +++ b/fs/btrfs/export.c
> @@ -285,6 +285,16 @@ static int btrfs_get_name(struct dentry *parent, char *name,
>  		name_len = btrfs_inode_ref_name_len(leaf, iref);
>  	}
>
> +	/* The caller's buffer is only NAME_MAX + 1 bytes. */
> +	if (name_len > BTRFS_NAME_LEN) {
> +		btrfs_err(fs_info,
> +			  "corrupt name length %d for inode %llu in root %llu, max %d",
> +			  name_len, btrfs_ino(BTRFS_I(inode)),
> +			  btrfs_root_id(BTRFS_I(inode)->root), BTRFS_NAME_LEN);
> +		btrfs_free_path(path);

Nitpicking: this is not necessary as 'path' is declared with a cleanup
attribute.

> +		return -EUCLEAN;
> +	}
> +
>  	read_extent_buffer(leaf, name, name_ptr, name_len);
>  	btrfs_free_path(path);

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 897 bytes --]

  reply	other threads:[~2026-06-07 20:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-07 12:14 [PATCH] btrfs: reject names longer than BTRFS_NAME_LEN in btrfs_get_name() Weiming Shi
2026-06-07 20:23 ` Miquel Sabaté Solà [this message]
2026-06-07 22:21 ` Qu Wenruo
2026-06-08  9:58   ` Weiming Shi

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=87fr2yw15e.fsf@ \
    --to=mssola@mssola.com \
    --cc=bestswngs@gmail.com \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=xmei5@asu.edu \
    /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