From patchwork Tue Sep 30 02:39:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5000021 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 8D92E9F1D4 for ; Tue, 30 Sep 2014 02:39:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BBF94201EC for ; Tue, 30 Sep 2014 02:39:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BC891201B4 for ; Tue, 30 Sep 2014 02:39:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754838AbaI3Cj1 (ORCPT ); Mon, 29 Sep 2014 22:39:27 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:2660 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754755AbaI3Cj0 (ORCPT ); Mon, 29 Sep 2014 22:39:26 -0400 X-IronPort-AV: E=Sophos;i="5.04,624,1406563200"; d="scan'208";a="36652112" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 30 Sep 2014 10:36:22 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s8U2dQhs011252 for ; Tue, 30 Sep 2014 10:39:26 +0800 Received: from adam-work.lan (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Tue, 30 Sep 2014 10:39:27 +0800 From: Qu Wenruo To: Subject: [PATCH] btrfs-progs: Don't return any fs_info that contain NULL tree_root or fs_root. Date: Tue, 30 Sep 2014 10:39:22 +0800 Message-ID: <1412044762-2480-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.1.1 MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [BUG] btrfsck will segfault if it fails to open the fs tree or tree root. [REPRODUCER] Execute btrfsck on a highly damaged btrfs image. fsfuzz can be used to make a junk btrfs image. [REASON] Current open_ctree() in btrfs-progs support OPEN_CTREE_PARTIAL flag to allow return fs_info even some of the trees is missing. However it is too loose and even allows fs_info containing no tree to be returned. And when it happens, fs_info->fs_root is NULL, close_ctree(fs_info->fs_root) will cause the access to NULL pointer and segfault. [FIX] This patch will add checks for fs_info->tree_root and fs_info->fs_root before return fs_info. Signed-off-by: Qu Wenruo --- disk-io.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/disk-io.c b/disk-io.c index 26a532e..21a3083 100644 --- a/disk-io.c +++ b/disk-io.c @@ -1134,7 +1134,8 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path, return fs_info; out_failed: - if (flags & OPEN_CTREE_PARTIAL) + if (flags & OPEN_CTREE_PARTIAL && + fs_info->tree_root && fs_info->fs_root) return fs_info; out_chunk: btrfs_release_all_roots(fs_info);