]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
btrfs: tree-checker: reject BTRFS_FT_UNKNOWN dir type
authorQu Wenruo <wqu@suse.com>
Sun, 11 Aug 2024 23:22:44 +0000 (08:52 +0930)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 29 Aug 2024 15:35:42 +0000 (17:35 +0200)
commit 31723c9542dba1681cc3720571fdf12ffe0eddd9 upstream.

[REPORT]
There is a bug report that kernel is rejecting a mismatching inode mode
and its dir item:

  [ 1881.553937] BTRFS critical (device dm-0): inode mode mismatch with
  dir: inode mode=040700 btrfs type=2 dir type=0

[CAUSE]
It looks like the inode mode is correct, while the dir item type
0 is BTRFS_FT_UNKNOWN, which should not be generated by btrfs at all.

This may be caused by a memory bit flip.

[ENHANCEMENT]
Although tree-checker is not able to do any cross-leaf verification, for
this particular case we can at least reject any dir type with
BTRFS_FT_UNKNOWN.

So here we enhance the dir type check from [0, BTRFS_FT_MAX), to
(0, BTRFS_FT_MAX).
Although the existing corruption can not be fixed just by such enhanced
checking, it should prevent the same 0x2->0x0 bitflip for dir type to
reach disk in the future.

Reported-by: Kota <nospam@kota.moe>
Link: https://lore.kernel.org/linux-btrfs/CACsxjPYnQF9ZF-0OhH16dAx50=BXXOcP74MxBc3BG+xae4vTTw@mail.gmail.com/
CC: stable@vger.kernel.org # 5.4+
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/btrfs/tree-checker.c

index a2c3651a3d8fced4ce958d145c0f0e57b34ce12c..ed49d5c08e7118f29a7a27994ba82fbc5c8e04fa 100644 (file)
@@ -551,9 +551,10 @@ static int check_dir_item(struct extent_buffer *leaf,
 
                /* dir type check */
                dir_type = btrfs_dir_ftype(leaf, di);
-               if (unlikely(dir_type >= BTRFS_FT_MAX)) {
+               if (unlikely(dir_type <= BTRFS_FT_UNKNOWN ||
+                            dir_type >= BTRFS_FT_MAX)) {
                        dir_item_err(leaf, slot,
-                       "invalid dir item type, have %u expect [0, %u)",
+                       "invalid dir item type, have %u expect (0, %u)",
                                dir_type, BTRFS_FT_MAX);
                        return -EUCLEAN;
                }