public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: Qu Wenruo <wqu@suse.com>, linux-btrfs@vger.kernel.org
Cc: Nikolay Borisov <nborisov@suse.com>
Subject: Re: [PATCH v3 5/5] btrfs: ctree: Checking key orders before merged tree blocks
Date: Tue, 11 Aug 2020 14:53:48 -0400	[thread overview]
Message-ID: <ff8fc934-2fc1-91af-84f0-ea50382fe6e2@toxicpanda.com> (raw)
In-Reply-To: <20200809120919.85271-6-wqu@suse.com>

On 8/9/20 8:09 AM, Qu Wenruo wrote:
> [BUG]
> With crafted image, btrfs can panic at btrfs_del_csums().
>    kernel BUG at fs/btrfs/ctree.c:3188!
>    invalid opcode: 0000 [#1] SMP PTI
>    CPU: 0 PID: 1156 Comm: btrfs-transacti Not tainted 5.0.0-rc8+ #9
>    RIP: 0010:btrfs_set_item_key_safe+0x16c/0x180
>    Code: b7 48 8d 7d bf 4c 89 fe 48 89 45 c8 0f b6 45 b6 88 45 c7 48 8b 45 ae 48 89 45 bf e8 ce f2 ff ff 85 c0 0f 8f 48 ff ff ff 0f 0b <0f> 0b e8 dd 8d be ff 0f 1f 00 66 2e 0f 1f 84 00 00 00 00 00 66 66
>    RSP: 0018:ffff976141257ab8 EFLAGS: 00010202
>    RAX: 0000000000000001 RBX: ffff898a6b890930 RCX: 0000000004b70000
>    RDX: 0000000000000000 RSI: ffff976141257bae RDI: ffff976141257acf
>    RBP: ffff976141257b10 R08: 0000000000001000 R09: ffff9761412579a8
>    R10: 0000000000000000 R11: 0000000000000000 R12: ffff976141257abe
>    R13: 0000000000000003 R14: ffff898a6a8be578 R15: ffff976141257bae
>    FS: 0000000000000000(0000) GS:ffff898a77a00000(0000) knlGS:0000000000000000
>    CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
>    CR2: 00007f779d9cd624 CR3: 000000022b2b4006 CR4: 00000000000206f0
>    Call Trace:
>    truncate_one_csum+0xac/0xf0
>    btrfs_del_csums+0x24f/0x3a0
>    __btrfs_free_extent.isra.72+0x5a7/0xbe0
>    __btrfs_run_delayed_refs+0x539/0x1120
>    btrfs_run_delayed_refs+0xdb/0x1b0
>    btrfs_commit_transaction+0x52/0x950
>    ? start_transaction+0x94/0x450
>    transaction_kthread+0x163/0x190
>    kthread+0x105/0x140
>    ? btrfs_cleanup_transaction+0x560/0x560
>    ? kthread_destroy_worker+0x50/0x50
>    ret_from_fork+0x35/0x40
>    Modules linked in:
>    ---[ end trace 93bf9db00e6c374e ]---
> 
> [CAUSE]
> This crafted image has a very tricky key order corruption:
> 
>    checksum tree key (CSUM_TREE ROOT_ITEM 0)
>    node 29741056 level 1 items 14 free 107 generation 19 owner CSUM_TREE
>            ...
>            key (EXTENT_CSUM EXTENT_CSUM 73785344) block 29757440 gen 19
>            key (EXTENT_CSUM EXTENT_CSUM 77594624) block 29753344 gen 19
>            ...
> 
>    leaf 29757440 items 5 free space 150 generation 19 owner CSUM_TREE
>            item 0 key (EXTENT_CSUM EXTENT_CSUM 73785344) itemoff 2323 itemsize 1672
>                    range start 73785344 end 75497472 length 1712128
>            item 1 key (EXTENT_CSUM EXTENT_CSUM 75497472) itemoff 2319 itemsize 4
>                    range start 75497472 end 75501568 length 4096
>            item 2 key (EXTENT_CSUM EXTENT_CSUM 75501568) itemoff 579 itemsize 1740
>                    range start 75501568 end 77283328 length 1781760
>            item 3 key (EXTENT_CSUM EXTENT_CSUM 77283328) itemoff 575 itemsize 4
>                    range start 77283328 end 77287424 length 4096
>            item 4 key (EXTENT_CSUM EXTENT_CSUM 4120596480) itemoff 275 itemsize 300 <<<
>                    range start 4120596480 end 4120903680 length 307200
>    leaf 29753344 items 3 free space 1936 generation 19 owner CSUM_TREE
>            item 0 key (18446744073457893366 EXTENT_CSUM 77594624) itemoff 2323 itemsize 1672
>                    range start 77594624 end 79306752 length 1712128
>            ...
> 
> Note the item 4 key of leaf 29757440, which is obviously too large, and
> even larger than the first key of the next leaf.
> 
> However it still follows the key order in that tree block, thus tree
> checker is unable to detect it at read time, since tree checker can only
> work inside a leaf, thus such complex corruption can't be rejected in
> advance.
> 
> [FIX]
> The next timing to detect such problem is at tree block merge time,
> which is in push_node_left(), balance_node_right(), push_leaf_left() and
> push_leaf_right().
> 
> Now we check if the key order of the right most key of the left node is
> larger than the left most key of the right node.
> 
> By this we don't need to call the full tree-check, while still keeps the
> key order correct as key order in each node is already checked by tree
> checker thus we only need to check the above two slots.
> 
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=202833
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> Reviewed-by: Nikolay Borisov <nborisov@suse.com>

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

      reply	other threads:[~2020-08-11 18:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-09 12:09 [PATCH v3 0/5] btrfs: Enhanced runtime defence against fuzzed images Qu Wenruo
2020-08-09 12:09 ` [PATCH v3 1/5] btrfs: extent_io: Do extra check for extent buffer read write functions Qu Wenruo
2020-08-11 18:42   ` Josef Bacik
2020-08-09 12:09 ` [PATCH v3 2/5] btrfs: extent-tree: Kill BUG_ON() in __btrfs_free_extent() and do better comment Qu Wenruo
2020-08-11 18:45   ` Josef Bacik
2020-08-09 12:09 ` [PATCH v3 3/5] btrfs: Detect unbalanced tree with empty leaf before crashing btree operations Qu Wenruo
2020-08-11 18:48   ` Josef Bacik
2020-08-11 23:04     ` Qu Wenruo
2020-08-12  0:23       ` Josef Bacik
2020-08-12  0:29         ` Qu Wenruo
2020-08-12  1:50           ` Josef Bacik
2020-08-12  1:53             ` Qu Wenruo
2020-08-09 12:09 ` [PATCH v3 4/5] btrfs: extent-tree: Kill the BUG_ON() in insert_inline_extent_backref() Qu Wenruo
2020-08-11 18:50   ` Josef Bacik
2020-08-09 12:09 ` [PATCH v3 5/5] btrfs: ctree: Checking key orders before merged tree blocks Qu Wenruo
2020-08-11 18:53   ` Josef Bacik [this message]

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=ff8fc934-2fc1-91af-84f0-ea50382fe6e2@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=nborisov@suse.com \
    --cc=wqu@suse.com \
    /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