Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3427732imu; Sun, 11 Nov 2018 14:52:21 -0800 (PST) X-Google-Smtp-Source: AJdET5exZaqAxi5b4sr6ZrJwQVohAQL2UMLKC8xqYNKY1AnLxsbzy7jOzMwbfTFQUjfPQ/Yefwsd X-Received: by 2002:a17:902:aa4b:: with SMTP id c11-v6mr16628589plr.303.1541976741490; Sun, 11 Nov 2018 14:52:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541976741; cv=none; d=google.com; s=arc-20160816; b=lU6zYJNlciT/ZxpJKhqv6n5KZKqaE6z1f7DKWVNUfo43NDU4Puahxa15LJGMQg6yBm RVEA7nyUg59JkcJSiv3ZZUiMSua5yVZjnlVNjnD6juNOi8iVy5us9MMpK9Ug8THB942b zWzP7Y+dtz3txaUDIeJQQQl1r1h9OQqOq/gcJcE7FJdwhCbYDa3LfvxigOYETcKKGp1t ZE/u/b99wRDGYiovOpMcJ3MSI4nYimOaOW/1CjUZOZJYqUw69C1Fun9x5GodEamohzR6 /qdgANJrVWY2WhBdRseZP1US4zOHMLvutGu/a4fFFkI80r+L21JDaBn3MB1FdHWiW/uV SDOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aClKMm690c2Cp/Til1rDtpHKiRGhn8p8D6fkX/XRv0g=; b=clegyZrnbwNZtcHdchQ0722DI+aAYXiAHWL+MXZ4vOJ14nLXdt5FlXlLRGURH9g8f0 737lE3hSCu0JsNHFAKullM3zY+CUPezYQ/i5I8sb/qLq0Gmg8xMyiropz95dlV+yxfO9 XwfeXRYVPoYd5HwKXMIaMKAJyLAda7B6Pxbv4gGIC81sobmPOy6yc4AHqjRe5TLDV1xU L5nlSbHABQJlakxw2xeFjcj/TMJle8nH1gzXiOLF+FWSZCBGze+PliAilv375MAKbS4C jKrICS0Ut0UVivBOOsZb8zMuCvX3rSBjYS4CWFUR7DVFzoVCeX0/6XVjhi9JXc+jzXUC FsyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=PMkdNe7J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t3-v6si16405770pfb.247.2018.11.11.14.52.06; Sun, 11 Nov 2018 14:52:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=PMkdNe7J; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404166AbeKLIlp (ORCPT + 99 others); Mon, 12 Nov 2018 03:41:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:54620 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404154AbeKLIXG (ORCPT ); Mon, 12 Nov 2018 03:23:06 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D9C1421780; Sun, 11 Nov 2018 22:33:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975588; bh=FLdygPX7ypcFDLD/kL5uVnIyR/0wwsGBK7MEy+6ca/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PMkdNe7J35mQwITs4FEtyY1p20wGh+GeuI0rs51jTn9h16SrGiO2CDiIjTB2LytDm jsyocxfnzGfGlIlCD2Mv9EkYWfGm5ZgckcQrIgrFHstBghxojKA6V1QRdwRD5n5aMy laWQ8nugEtnYooqgeCAx4wsyHMOvE8xmIcr2zckY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xu Wen , Qu Wenruo , David Sterba Subject: [PATCH 4.14 197/222] btrfs: Handle owner mismatch gracefully when walking up tree Date: Sun, 11 Nov 2018 14:24:54 -0800 Message-Id: <20181111221704.415752862@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221647.665769131@linuxfoundation.org> References: <20181111221647.665769131@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo commit 65c6e82becec33731f48786e5a30f98662c86b16 upstream. [BUG] When mounting certain crafted image, btrfs will trigger kernel BUG_ON() when trying to recover balance: kernel BUG at fs/btrfs/extent-tree.c:8956! invalid opcode: 0000 [#1] PREEMPT SMP NOPTI CPU: 1 PID: 662 Comm: mount Not tainted 4.18.0-rc1-custom+ #10 RIP: 0010:walk_up_proc+0x336/0x480 [btrfs] RSP: 0018:ffffb53540c9b890 EFLAGS: 00010202 Call Trace: walk_up_tree+0x172/0x1f0 [btrfs] btrfs_drop_snapshot+0x3a4/0x830 [btrfs] merge_reloc_roots+0xe1/0x1d0 [btrfs] btrfs_recover_relocation+0x3ea/0x420 [btrfs] open_ctree+0x1af3/0x1dd0 [btrfs] btrfs_mount_root+0x66b/0x740 [btrfs] mount_fs+0x3b/0x16a vfs_kern_mount.part.9+0x54/0x140 btrfs_mount+0x16d/0x890 [btrfs] mount_fs+0x3b/0x16a vfs_kern_mount.part.9+0x54/0x140 do_mount+0x1fd/0xda0 ksys_mount+0xba/0xd0 __x64_sys_mount+0x21/0x30 do_syscall_64+0x60/0x210 entry_SYSCALL_64_after_hwframe+0x49/0xbe [CAUSE] Extent tree corruption. In this particular case, reloc tree root's owner is DATA_RELOC_TREE (should be TREE_RELOC), thus its backref is corrupted and we failed the owner check in walk_up_tree(). [FIX] It's pretty hard to take care of every extent tree corruption, but at least we can remove such BUG_ON() and exit more gracefully. And since in this particular image, DATA_RELOC_TREE and TREE_RELOC share the same root (which is obviously invalid), we needs to make __del_reloc_root() more robust to detect such invalid sharing to avoid possible NULL dereference as root->node can be NULL in this case. Link: https://bugzilla.kernel.org/show_bug.cgi?id=200411 Reported-by: Xu Wen CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 18 ++++++++++++------ fs/btrfs/relocation.c | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -9028,15 +9028,14 @@ static noinline int walk_up_proc(struct if (eb == root->node) { if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) parent = eb->start; - else - BUG_ON(root->root_key.objectid != - btrfs_header_owner(eb)); + else if (root->root_key.objectid != btrfs_header_owner(eb)) + goto owner_mismatch; } else { if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) parent = path->nodes[level + 1]->start; - else - BUG_ON(root->root_key.objectid != - btrfs_header_owner(path->nodes[level + 1])); + else if (root->root_key.objectid != + btrfs_header_owner(path->nodes[level + 1])) + goto owner_mismatch; } btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); @@ -9044,6 +9043,11 @@ out: wc->refs[level] = 0; wc->flags[level] = 0; return 0; + +owner_mismatch: + btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu", + btrfs_header_owner(eb), root->root_key.objectid); + return -EUCLEAN; } static noinline int walk_down_tree(struct btrfs_trans_handle *trans, @@ -9097,6 +9101,8 @@ static noinline int walk_up_tree(struct ret = walk_up_proc(trans, root, path, wc); if (ret > 0) return 0; + if (ret < 0) + return ret; if (path->locks[level]) { btrfs_tree_unlock_rw(path->nodes[level], --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -1334,7 +1334,7 @@ static void __del_reloc_root(struct btrf struct mapping_node *node = NULL; struct reloc_control *rc = fs_info->reloc_ctl; - if (rc) { + if (rc && root->node) { spin_lock(&rc->reloc_root_tree.lock); rb_node = tree_search(&rc->reloc_root_tree.rb_root, root->node->start);