Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3467294imu; Sun, 11 Nov 2018 15:46:36 -0800 (PST) X-Google-Smtp-Source: AJdET5fY16WbKwqTGVlL4r1Tms2c8BIiK0cZCh/2Fi52NW1LrhKVWnCKS7rrTm5XuyGofhZZX8BH X-Received: by 2002:a62:4587:: with SMTP id n7mr840888pfi.118.1541979996343; Sun, 11 Nov 2018 15:46:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541979996; cv=none; d=google.com; s=arc-20160816; b=iXFWJT6uyaYZPaPdDtjIWz6/5oOLKiuEeSlASggSxvvO09Rr8VzPdQfDs7HZlHiCH6 madIOIVk6TPh+uiSJAAzsJ7jMoBEvTpGFthaVrvHn2sXFq2H7lPGfa/STb4gKgmfbKW9 ocE7npUl2rTx4Y3RrL/sT5ChKr24Sjba5Afellg+yxAWTu6z4uJr184plvMUyvyKvIKE ICTicX5NM9zmb88v9xyXzg72KCHRmY70JI5lfSHhj4rjPEEsKUklGXiqCGwoa+AHG+/Z GqZHLDKwJbfwgXc35OnBga3H1C2hmMUzSLmKx8ckN7lbe1uMnHhWkvkYVihWXVdiiiKr zBWg== 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=gVRdfSqVwBBzRgD+gDU/iJmmiBECYkbIuI85RPJd6QU=; b=ue+X6Enq8dA5dK7gYvCKqwqs/GhCSTt+80V80ZYtPUUCKdWJoMis78Fhmpm+YV/JiQ lcNZM4VQA8I4CWBDqc1Vgn1T1fn47xqFV3FOM9a6lHYCRbuhKd8iMg9FTzxwFAwtDZ4L STFySBCvwlLusJWAy3Ib71Djc/O9JbDxIKE5GSEQ81z0ybAnOaFvCzVzUV2Lu9NTwAVw ssAwG6H0kY93pgR7KvMjC/IMo0SOVRWJk0+9UWlPdXun0gWRgiomrcN5Ovhyb9AhV6Xb TPzgrkQ3syB+1ZrXSKo3LZVIYPAaM5EvURATjS5ibhvu6kglXp9NmNYlI6uVf1jBGGiT wWuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=yhkzzAsg; 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 k8-v6si9527971plt.144.2018.11.11.15.46.21; Sun, 11 Nov 2018 15:46:36 -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=yhkzzAsg; 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 S1733311AbeKLITD (ORCPT + 99 others); Mon, 12 Nov 2018 03:19:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:39736 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730142AbeKLITC (ORCPT ); Mon, 12 Nov 2018 03:19:02 -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 0E8F721104; Sun, 11 Nov 2018 22:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975346; bh=Bt0yqZ36HQX76yJjGY6jU+QWdldtxrsk3pZVb3UlPq0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yhkzzAsgJpeOiwlws4CYTj5BNs8Czi3B02awa9Y7JRMwOqb1s9Muhz6n1kfTXM2yR CoUuCvsbzSxcEFNmgaavRUhKgb1LE63Gkjn+tLPwSm/16OmEhTbUBjtB2bccMyQdxp kNK115K7Jz0Fv/nf6Nn11bmZwhzPsjnlYvaeSDy0= 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.19 327/361] btrfs: Handle owner mismatch gracefully when walking up tree Date: Sun, 11 Nov 2018 14:21:14 -0800 Message-Id: <20181111221659.552472690@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@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.19-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 @@ -8763,15 +8763,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); @@ -8779,6 +8778,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, @@ -8832,6 +8836,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 @@ -1281,7 +1281,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);