Skip to contentSkip to conversation

btrfs: reject zero num_stripes in the system chunk array

PATCH In review Cheng Yongkang 3 participants 7 messages Oct 29 – Nov 1 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: reject zero num_stripes in the system chunk array

validate_sys_chunk_array() reads the on-disk num_stripes and passes it to btrfs_chunk_item_size() to check that the chunk item fits in the system chunk array. btrfs_chunk_item_size() has ASSERT(num_stripes), and the only num_stripes == 0 rejection is in btrfs_check_chunk_valid(), which is called later. A crafted superblock with a BTRFS_CHUNK_ITEM_KEY entry whose btrfs_chunk has num_stripes == 0 therefore trips the assertion before the invalid chunk is rejected:

assertion failed: num_stripes, in fs/btrfs/volumes.h:714 kernel BUG at fs/btrfs/volumes.h:714!

  RIP: 0010:btrfs_validate_super+0x3ce7/0x4210
  Call Trace:
   open_ctree
   btrfs_get_tree
   vfs_get_tree
   do_new_mount

Commit 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()") moved the validation out of btrfs_read_sys_array(), which rejected num_stripes == 0 explicitly. Reject zero stripes in validate_sys_chunk_array() before calling btrfs_chunk_item_size(), so the crafted superblock is rejected with -EUCLEAN before the assertion is evaluated. open_ctree() turns the validation failure into -EINVAL for the mount.

Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
Assisted-by LLM
---
 fs/btrfs/disk-io.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/btrfs/disk-io.c +13 -0
index c628c50..920ca4b 100644
@@ -2389,6 +2389,19 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
 
 		chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur);
 		num_stripes = btrfs_stack_chunk_num_stripes(chunk);
+		/*
+		 * btrfs_chunk_item_size() cannot represent a chunk without
+		 * stripes; such a chunk is also invalid in the system chunk
+		 * array (system chunks are never remapped).  Check before
+		 * btrfs_chunk_item_size() so a crafted super block is rejected
+		 * with -EUCLEAN instead of tripping its ASSERT().
+		 */
+		if (unlikely(num_stripes == 0)) {
+			btrfs_err(fs_info,
+				  "invalid chunk num_stripes 0 in sys_array at offset %u",
+				  cur);
+			return -EUCLEAN;
+		}
 		if (unlikely(cur + btrfs_chunk_item_size(num_stripes) > sys_array_size))
 			goto short_read;
 		type = btrfs_stack_chunk_type(chunk);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 39 lines · Cheng Yongkang

validate_sys_chunk_array() reads the on-disk num_stripes and passes it to btrfs_chunk_item_size() to check that the chunk item fits in the system chunk array. btrfs_chunk_item_size() has ASSERT(num_stripes), and the only num_stripes == 0 rejection is in btrfs_check_chunk_valid(), which is called later. A crafted superblock with a BTRFS_CHUNK_ITEM_KEY entry whose btrfs_chunk has num_stripes == 0 therefore trips the assertion before the invalid chunk is rejected:

assertion failed: num_stripes, in fs/btrfs/volumes.h:714 kernel BUG at fs/btrfs/volumes.h:714!

  RIP: 0010:btrfs_validate_super+0x3ce7/0x4210
  Call Trace:
   open_ctree
   btrfs_get_tree
   vfs_get_tree
   do_new_mount

Commit 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()") moved the validation out of btrfs_read_sys_array(), which rejected num_stripes == 0 explicitly. Reject zero stripes in validate_sys_chunk_array() before calling btrfs_chunk_item_size(), so the crafted superblock is rejected with -EUCLEAN before the assertion is evaluated. open_ctree() turns the validation failure into -EINVAL for the mount.

Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
Assisted-by LLM
---
 fs/btrfs/disk-io.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/btrfs/disk-io.c +0 -0
index c628c50..920ca4b 100644

fs/btrfs/disk-io.c static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, · line 2393

Quoted 5 lines
fs/btrfs/disk-io.c +2 -0
@@ -2389,6 +2389,19 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
 
                chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur);
                num_stripes = btrfs_stack_chunk_num_stripes(chunk);
+               /*
+                * btrfs_chunk_item_size() cannot represent a chunk without

I find this puzzling, using the "represent" word since btrfs_chunk_item_size() does not represent anything, it just does a calculation.

fs/btrfs/disk-io.c static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, · line 2395

Quoted 2 lines
fs/btrfs/disk-io.c +2 -0
+                * stripes; such a chunk is also invalid in the system chunk
+                * array (system chunks are never remapped).  Check before

Also the "remapped" detail is also confusing and I can't grasp the intention or why it matters.

fs/btrfs/disk-io.c static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, · line 2397

Quoted 2 lines
fs/btrfs/disk-io.c +2 -0
+                * btrfs_chunk_item_size() so a crafted super block is rejected
+                * with -EUCLEAN instead of tripping its ASSERT().

Something simpler and more clear for the whole comment:

"We can not have 0 stripes, and passing 0 to btrfs_chunk_item_size() triggers an ASSERT(), so fail immediately if we have 0 stripes."

Codewise it looks good, thanks.

Quoted 14 lines
fs/btrfs/disk-io.c +7 -1
+                */
+               if (unlikely(num_stripes == 0)) {
+                       btrfs_err(fs_info,
+                                 "invalid chunk num_stripes 0 in sys_array at offset %u",
+                                 cur);
+                       return -EUCLEAN;
+               }
                if (unlikely(cur + btrfs_chunk_item_size(num_stripes) > sys_array_size))
                        goto short_read;
                type = btrfs_stack_chunk_type(chunk);
--

2.34.1

#2 lore raw

Hi Filipe,

Thanks for the review. I'll simplify the comment as you suggested and drop the "represent"/"remapped" wording.

Thanks,
Cheng Yongkang

#3 lore raw
In reply to Cheng Yongkang
Quoted 7 lines · Cheng Yongkang

validate_sys_chunk_array() reads the on-disk num_stripes and passes it to btrfs_chunk_item_size() to check that the chunk item fits in the system chunk array. btrfs_chunk_item_size() has ASSERT(num_stripes), and the only num_stripes == 0 rejection is in btrfs_check_chunk_valid(), which is called later. A crafted superblock with a BTRFS_CHUNK_ITEM_KEY entry whose btrfs_chunk has num_stripes == 0 therefore trips the assertion before the invalid chunk is rejected:

Since it will be checked by btrfs_check_chunk_valid(), why not remove the ASSERT(num_stripes) and let it continue to btrfs_check_chunk_valid()?

The whole idea of tree-checker is to concentrate the checks, not distribute them again.

Quoted 31 lines

assertion failed: num_stripes, in fs/btrfs/volumes.h:714 kernel BUG at fs/btrfs/volumes.h:714!

   RIP: 0010:btrfs_validate_super+0x3ce7/0x4210
   Call Trace:
    open_ctree
    btrfs_get_tree
    vfs_get_tree
    do_new_mount

Commit 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()") moved the validation out of btrfs_read_sys_array(), which rejected num_stripes == 0 explicitly. Reject zero stripes in validate_sys_chunk_array() before calling btrfs_chunk_item_size(), so the crafted superblock is rejected with -EUCLEAN before the assertion is evaluated. open_ctree() turns the validation failure into -EINVAL for the mount.

Fixes 2a9bb78cfd36 ("btrfs: validate system chunk array at btrfs_validate_super()")
Assisted-by LLM
---
  fs/btrfs/disk-io.c | 13 +++++++++++++
  1 file changed, 13 insertions(+)
fs/btrfs/disk-io.c +0 -0
index c628c50..920ca4b 100644

fs/btrfs/disk-io.c static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info, · line 2397

Quoted 9 lines
fs/btrfs/disk-io.c +6 -0
@@ -2389,6 +2389,19 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
 
  		chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur);
  		num_stripes = btrfs_stack_chunk_num_stripes(chunk);
+		/*
+		 * btrfs_chunk_item_size() cannot represent a chunk without
+		 * stripes; such a chunk is also invalid in the system chunk
+		 * array (system chunks are never remapped).  Check before
+		 * btrfs_chunk_item_size() so a crafted super block is rejected
+		 * with -EUCLEAN instead of tripping its ASSERT().

Please let your agent to drop unnecessary comments.

It only wastes code space without providing any real benefit.

Quoted 10 lines
fs/btrfs/disk-io.c +7 -0
+		 */
+		if (unlikely(num_stripes == 0)) {
+			btrfs_err(fs_info,
+				  "invalid chunk num_stripes 0 in sys_array at offset %u",
+				  cur);
+			return -EUCLEAN;
+		}
  		if (unlikely(cur + btrfs_chunk_item_size(num_stripes) > sys_array_size))
  			goto short_read;
  		type = btrfs_stack_chunk_type(chunk);
#4 lore raw

Hi Qu,

Removing the ASSERT alone is not sufficient. btrfs_chunk_item_size() computes

sizeof(struct btrfs_chunk) +
sizeof(struct btrfs_stripe) * (num_stripes - 1)

With num_stripes == 0, the (num_stripes - 1) term underflows in the unsigned multiplication and the function returns 16 bytes (48 - 32) instead of a valid item size. The system chunk array loop would then advance by the wrong size before btrfs_check_chunk_valid() runs.

So either btrfs_chunk_item_size() has to handle num_stripes == 0 first (for example, return sizeof(struct btrfs_chunk) and let btrfs_check_chunk_valid() reject it), or the early check has to stay so the size calculation is never reached with zero stripes. I kept the early check as the minimal fix; if you prefer centralizing the rejection in the tree-checker, I can change btrfs_chunk_item_size() and drop the check.

Thanks,
Cheng Yongkang

#5 lore raw
Quoted 13 lines · Cheng Yongkang

Hi Qu,

Removing the ASSERT alone is not sufficient. btrfs_chunk_item_size() computes

sizeof(struct btrfs_chunk) + sizeof(struct btrfs_stripe) * (num_stripes - 1)

With num_stripes == 0, the (num_stripes - 1) term underflows in the unsigned multiplication and the function returns 16 bytes (48 - 32) instead of a valid item size. The system chunk array loop would then advance by the wrong size before btrfs_check_chunk_valid() runs.

Quoted 7 lines

So either btrfs_chunk_item_size() has to handle num_stripes == 0 first (for example, return sizeof(struct btrfs_chunk) and let btrfs_check_chunk_valid() reject it), or the early check has to stay so the size calculation is never reached with zero stripes. I kept the early check as the minimal fix; if you prefer centralizing the rejection in the tree-checker, I can change btrfs_chunk_item_size() and drop the check.

Yes, drop the check and change btrfs_chunk_item_size() to handle zero num_stripes is not hard at all, and it's still smaller than your original fix:

fs/btrfs/volumes.h +2 -3
index d5fe5cba0c35..891bc2432870 100644
@@ -711,9 +711,8 @@ static inline enum btrfs_map_op btrfs_op(const struct bio *bio)
 
 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
 {
-       ASSERT(num_stripes);
-       return sizeof(struct btrfs_chunk) +
-               sizeof(struct btrfs_stripe) * (num_stripes - 1);
+       return offsetof(struct btrfs_chunk, stripe) +
+               sizeof(struct btrfs_stripe) * num_stripes;
 }
 
 /*
Quoted 3 lines

Thanks,
Cheng Yongkang

#6 lore raw

Hi Qu,

Thanks, I've sent v2 with your change:
https://lore.kernel.org/linux-btrfs/[email protected]/

I also need to correct my earlier reply: with the assertion disabled, the old expression gives 48 bytes for zero stripes, not 16, and btrfs_check_chunk_valid() runs before the loop advances over the chunk. So removing the assertion is enough, as you said. Sorry for the confusion.

Thanks,
Cheng Yongkang