btrfs: reject names longer than BTRFS_NAME_LEN in btrfs_get_name()
PATCH 1/1 btrfs: reject names longer than BTRFS_NAME_LEN in btrfs_get_name()
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.
--- fs/btrfs/export.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
fs/btrfs/export.c +10 -0
@@ -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); +return -EUCLEAN ; + } +read_extent_buffer (leaf, name, name_ptr, name_len);btrfs_free_path (path);
Signature · 1 line
2.43.0
Hi,
Weiming Shi @ 2026-06-07 05:14 -07:
Quoted 37 lines
btrfs_get_name()reads the name length straight from the on-diskinode_ref(orroot_ref) and copies that many bytes into the caller's buffer with no upper bound. The caller (exportfs_get_name()) supplies a fixedNAME_MAX+ 1 byte stack buffer, butname_lenis a__le16read from the leaf and the tree-checker only bounds it to the item size, not toBTRFS_NAME_LEN. A crafted leaf withname_len= 4096 therefore overflows the 256-byte buffer with attacker-controlled bytes. It is reachable from a mounted untrusted btrfs image viaopen_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 corruptedReject any
name_lengreater thanBTRFS_NAME_LENbefore 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 <[email protected]>Assisted-by Claude:claude-opus-4-8Signed-off-by Weiming Shi <[email protected]>--- fs/btrfs/export.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/export.c +0
-0
fs/btrfs/export.c static int btrfs_get_name(struct dentry *parent, char *name, · line 294
Quoted 10 lines
fs/btrfs/export.c +7
-0@@ -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.
Quoted 5 lines
fs/btrfs/export.c +3
-0+return -EUCLEAN ; + } +read_extent_buffer (leaf, name, name_ptr, name_len);btrfs_free_path (path);
Quoted 37 lines · Weiming Shi
btrfs_get_name()reads the name length straight from the on-diskinode_ref(orroot_ref) and copies that many bytes into the caller's buffer with no upper bound. The caller (exportfs_get_name()) supplies a fixedNAME_MAX+ 1 byte stack buffer, butname_lenis a__le16read from the leaf and the tree-checker only bounds it to the item size, not toBTRFS_NAME_LEN. A crafted leaf withname_len= 4096 therefore overflows the 256-byte buffer with attacker-controlled bytes. It is reachable from a mounted untrusted btrfs image viaopen_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 corruptedReject any
name_lengreater thanBTRFS_NAME_LENbefore 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 <[email protected]>Assisted-by Claude:claude-opus-4-8Signed-off-by Weiming Shi <[email protected]>--- fs/btrfs/export.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/export.c +0
-0
fs/btrfs/export.c static int btrfs_get_name(struct dentry *parent, char *name, · line 289
Quoted 5 lines
fs/btrfs/export.c +2
-0@@ -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 ) {
We do not play the whac-a-mole game by add random checks ad-hoc.
Do the proper ROOT_REF_KEY and INODE_REF_KEY verification inside tree-checker.
Quoted 11 lines
fs/btrfs/export.c +8
-0+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); +return -EUCLEAN ; + } +read_extent_buffer (leaf, name, name_ptr, name_len);btrfs_free_path (path);
Quoted 50 lines · Qu Wenruo
Quoted 37 lines · Weiming Shi
btrfs_get_name()reads the name length straight from the on-diskinode_ref(orroot_ref) and copies that many bytes into the caller's buffer with no upper bound. The caller (exportfs_get_name()) supplies a fixedNAME_MAX+ 1 byte stack buffer, butname_lenis a__le16read from the leaf and the tree-checker only bounds it to the item size, not toBTRFS_NAME_LEN. A crafted leaf withname_len= 4096 therefore overflows the 256-byte buffer with attacker-controlled bytes. It is reachable from a mounted untrusted btrfs image viaopen_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 corruptedReject any
name_lengreater thanBTRFS_NAME_LENbefore 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 <[email protected]>Assisted-by Claude:claude-opus-4-8Signed-off-by Weiming Shi <[email protected]>--- fs/btrfs/export.c | 10 ++++++++++ 1 file changed, 10 insertions(+)fs/btrfs/export.c +0
-0fs/btrfs/export.c static int btrfs_get_name(struct dentry *parent, char *name, · line 288
Quoted 4 lines
fs/btrfs/export.c +2
-0@@ -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 ) {We do not play the whac-a-mole game by add random checks ad-hoc.
Do the proper
ROOT_REF_KEYandINODE_REF_KEYverification inside tree-checker.
Quoted 11 lines
fs/btrfs/export.c static int btrfs_get_name(struct dentry *parent, char *name, · line 299
Quoted 10 lines
fs/btrfs/export.c +8
-0+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); +return -EUCLEAN ; + } +read_extent_buffer (leaf, name, name_ptr, name_len);btrfs_free_path (path);
Thanks for the review. v2 sent.