]> git.hungrycats.org Git - linux/commitdiff
btrfs: reject existing chunks that don't match
authorQu Wenruo <wqu@suse.com>
Fri, 16 Oct 2026 00:48:19 +0000 (11:18 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:16 +0000 (21:36 +0100)
During read_one_chunk() we can hit an existing chunk map.

There are several situations that this can happen:

- System chunk maps
  This is the most common case, where the system chunks are added from
  super block during bootstrap.

  But in that case, the system chunks in the super block should match
  the one in the chunk tree, if not there is definitely something wrong.

- Overlapping chunk maps
  This is rare, but potentially malicious.

  Crafted images or even some bitflips in the chunk tree can lead to
  this.
  In that case we should reject such cases.

To detect and reject those cases:

- Introduce a new helper, is_same_chunk_map()
  This checks most important members, so we can detect such problem
  without fully checking the stripes.

  If the helper returns false, then we output an error message and
  reject the mount.

- Use the full chunk map range for btrfs_find_chunk_map()
  The current search range is [@logical, @logical], which is only one
  bytenr in length.

  If we have the following case, btrfs_find_chunk_map() returns NULL but
  we still have an overlapping chunk map:

                   Logical
                   |<--- New chunk map --->|
                            |<--- Existing chunk map --->|

  Using the full range to do the search would return the existing chunk
  map and get the new one rejected.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/volumes.c

index e3886c9035de13228bd57aa8fe82a536c8dc34a6..6574939e1295b561c5bcfd900c8d2b15dec619b9 100644 (file)
@@ -7718,6 +7718,15 @@ static struct btrfs_device *handle_missing_device(struct btrfs_fs_info *fs_info,
        return dev;
 }
 
+static bool is_same_chunk_map(const struct btrfs_chunk_map *map, u64 logical,
+                             u64 length, u64 type, u16 num_stripes)
+{
+       if (map->start != logical || map->chunk_len != length ||
+           map->on_disk_type != type || map->num_stripes != num_stripes)
+               return false;
+       return true;
+}
+
 static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
                          struct btrfs_chunk *chunk)
 {
@@ -7747,14 +7756,21 @@ static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
        warn_32bit_meta_chunk(fs_info, logical, length, type);
 #endif
 
-       map = btrfs_find_chunk_map(fs_info, logical, 1);
+       map = btrfs_find_chunk_map(fs_info, logical, length);
 
-       /* already mapped? */
-       if (map && map->start <= logical && map->start + map->chunk_len > logical) {
-               btrfs_free_chunk_map(map);
-               return 0;
-       } else if (map) {
+       /* Already mapped? Check if the existing one matches. */
+       if (map) {
+               if (unlikely(!is_same_chunk_map(map, logical, length,
+                                               type, num_stripes))) {
+                       btrfs_err(fs_info,
+               "existing chunk %llu doesn't match the new one at %llu",
+                                 map->start, logical);
+                       ret = -EUCLEAN;
+               } else {
+                       ret = 0;
+               }
                btrfs_free_chunk_map(map);
+               return ret;
        }
 
        map = btrfs_alloc_chunk_map(num_stripes, GFP_NOFS);