From patchwork Wed Nov 22 09:03:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10069937 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 52ADA60375 for ; Wed, 22 Nov 2017 09:04:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4758A29B94 for ; Wed, 22 Nov 2017 09:04:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3BF6629BFA; Wed, 22 Nov 2017 09:04:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E47F529B94 for ; Wed, 22 Nov 2017 09:04:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751886AbdKVJEE (ORCPT ); Wed, 22 Nov 2017 04:04:04 -0500 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:47604 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751825AbdKVJEC (ORCPT ); Wed, 22 Nov 2017 04:04:02 -0500 Received: from adam-pc.lan (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Wed, 22 Nov 2017 02:03:46 -0700 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH 02/11] btrfs-progs: lowmem check: Fix NULL pointer access caused by large tree reloc tree Date: Wed, 22 Nov 2017 17:03:16 +0800 Message-Id: <20171122090325.29458-3-wqu@suse.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171122090325.29458-1-wqu@suse.com> References: <20171122090325.29458-1-wqu@suse.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [BUG] v4.14 btrfs-progs can't pass new self test image with large tree reloc trees. It will fail with later "shared_block_ref_only.raw.xz" test image with NULL pointer access. [CAUSE] For image with higher (level >= 2) tree reloc tree, for function need_check() its ulist will be empty as tree reloc tree won't be accounted in btrfs_find_all_roots(). Then accessing ulist->roots with rb_first() will return NULL pointer. [FIX] For need_check() function, if @roots is empty, meaning it's a tree reloc tree, always check them. Although this can be slow, but at least it's safe that we won't skip any possible wrong tree block. Fixes: 5e2dc770471b ("btrfs-progs: check: skip shared node or leaf check for low_memory mode") Signed-off-by: Qu Wenruo --- cmds-check.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmds-check.c b/cmds-check.c index 644ee084cb8e..03ff89a4221c 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -2149,7 +2149,12 @@ static int need_check(struct btrfs_root *root, struct ulist *roots) struct rb_node *node; struct ulist_node *u; - if (roots->nnodes == 1) + /* + * @roots can be empty if it belongs to tree reloc tree + * In that case, we should always check the leaf, as we can't use + * the tree owner to ensure some other root will check it. + */ + if (roots->nnodes == 1 || roots->nnodes == 0) return 1; node = rb_first(&roots->root);