Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp734880imm; Thu, 13 Sep 2018 07:01:45 -0700 (PDT) X-Google-Smtp-Source: ANB0Vda/3c0k5+4sNcuf3Kv5THNCITloyABfOj3V6KLRSS35JsPfzo8G/skmL+vJC9dnCjb/dSt4 X-Received: by 2002:a63:881:: with SMTP id 123-v6mr7424861pgi.244.1536847305394; Thu, 13 Sep 2018 07:01:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536847305; cv=none; d=google.com; s=arc-20160816; b=lNF8fQAjj/H0/ERKD7TFpS+qMkK3KBoWUDwciZMUCG4Es0f63d3eL7LKzvscnQ4HPR ru0bdLFNul9eUSP5ZT7H/DMqagDhemH6Dq5W0XkjphjtQU4APorn5sniNXcCRtROoA7g JjAvk+Nu74GBZHEp9yo0+wzQSWDv93rrly1gzli68H9VPklWA+TG5ZU2vLnSwlKnK9i9 AP7EiYgrIPeKAGNL0cQNsXE5P9GZ133IZhQewCir1ZxWsjafvwPR4y2/owrNdfzM893Q P6FOhVis/e3mM+Sr51U7xBPjufDeIyNBjZyM2OJ2Khaz5Z8RoWP2KXGHyDXiF4OF3T26 KQJQ== 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; bh=J/qKtr816p0jmH8pFHxJtLiIlBikwMmaR8tdnW3SKuY=; b=UlgTZUT3RY8H6LJbw2PT3F+3yChRX033gp6c9J+tK9WRrATGcdo9wSqV/s8uobSBJU 9v8aqbMTL93WPBlZh3Q+Ozb7y27zlodmyquq8pnI3nMy8gzEOOS3P0YYm8HPbFiBqjcC kxNZXiWAIZc1EGyssduQXLUiQ7amV5kCzlK0YuOQIIlh1FPZ4umu86IHgmKlYxIxnGoK Q1PPnky659dgowxdgnXYb968MBhhUNxA15da9f4UkPt7bfGeFlmjNvJIpJTOR705nD8c CkSSgvrtkbqw8Z2AvwxLE9VS4E8R3Ja9IkHk8nEUF7M5Mb9PyoKPOTLS7/77W5v1vI1h MY7A== ARC-Authentication-Results: i=1; mx.google.com; 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 d7-v6si3521975pls.136.2018.09.13.07.01.28; Thu, 13 Sep 2018 07:01:45 -0700 (PDT) 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; 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 S1731719AbeIMTK6 (ORCPT + 99 others); Thu, 13 Sep 2018 15:10:58 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:35016 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730449AbeIMTK5 (ORCPT ); Thu, 13 Sep 2018 15:10:57 -0400 Received: from localhost (ip-213-127-77-73.ip.prioritytelecom.net [213.127.77.73]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D6F5CD1D; Thu, 13 Sep 2018 14:01:18 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xu Wen , Qu Wenruo , Gu Jinxiang , David Sterba , Sasha Levin Subject: [PATCH 4.18 137/197] btrfs: tree-checker: Detect invalid and empty essential trees Date: Thu, 13 Sep 2018 15:31:26 +0200 Message-Id: <20180913131847.035490862@linuxfoundation.org> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20180913131841.568116777@linuxfoundation.org> References: <20180913131841.568116777@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.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo [ Upstream commit ba480dd4db9f1798541eb2d1c423fc95feee8d36 ] A crafted image has empty root tree block, which will later cause NULL pointer dereference. The following trees should never be empty: 1) Tree root Must contain at least root items for extent tree, device tree and fs tree 2) Chunk tree Or we can't even bootstrap as it contains the mapping. 3) Fs tree At least inode item for top level inode (.). 4) Device tree Dev extents for chunks 5) Extent tree Must have corresponding extent for each chunk. If any of them is empty, we are sure the fs is corrupted and no need to mount it. Link: https://bugzilla.kernel.org/show_bug.cgi?id=199847 Reported-by: Xu Wen Signed-off-by: Qu Wenruo Tested-by: Gu Jinxiang Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/tree-checker.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -396,9 +396,22 @@ static int check_leaf(struct btrfs_fs_in * skip this check for relocation trees. */ if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { + u64 owner = btrfs_header_owner(leaf); struct btrfs_root *check_root; - key.objectid = btrfs_header_owner(leaf); + /* These trees must never be empty */ + if (owner == BTRFS_ROOT_TREE_OBJECTID || + owner == BTRFS_CHUNK_TREE_OBJECTID || + owner == BTRFS_EXTENT_TREE_OBJECTID || + owner == BTRFS_DEV_TREE_OBJECTID || + owner == BTRFS_FS_TREE_OBJECTID || + owner == BTRFS_DATA_RELOC_TREE_OBJECTID) { + generic_err(fs_info, leaf, 0, + "invalid root, root %llu must never be empty", + owner); + return -EUCLEAN; + } + key.objectid = owner; key.type = BTRFS_ROOT_ITEM_KEY; key.offset = (u64)-1;