Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8121826imu; Tue, 4 Dec 2018 03:22:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/XjakU9KdYVRE8DQPMMGbDICUkUkNGh94Dl1XM3HE46iG5DK97eL2dbTQSokKosCezyjCqt X-Received: by 2002:a62:9419:: with SMTP id m25mr20593722pfe.147.1543922562821; Tue, 04 Dec 2018 03:22:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543922562; cv=none; d=google.com; s=arc-20160816; b=tRPKcOLjDVprWqjWykVycvDc0sKAT2HlkfBmaG5pXu2O9StkmMisJdKh8KYBbW9ODo S9Dj5Qx+fneRDlKxMqftIneDv6Y+jW4Jwa6/V/+RCkzFcqkV9aeCBjCgpPfxyIak0P9u jMc+w75VqzPTWy6L2pD0VIX+7zvDPVE9fVX76YHt2VQJlUY4z+vMP1xGoN8PEdg7xKh5 YXeJMSowvQO13kwvq+tW4eG7I25g7sL6V/woUcjB1FKwxhxLsmrkkkik2LvHCkHtVolA LuVPuHpnE7TGbg3mov32oqZzXwP1LAlbt7dETw3zHltN1/riY3J753rhM3PhI6OfiOUO ErQg== 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=4FXIDPRNYbxs7DlmhZjj9RaYw44J/u2/zsQBd7JWG44=; b=0l15J3ZP/yWFazM5b1nxbiPWBbaTWMNowRYcdUmsx+Tm32Zqou6FkNUkmuaMbJkO66 QULI+VSl5aTNjoX8qCIzXMecm3LqtMUNXQQetBahcUuz4nrYN5LZekNEOHPNHTtMW3Es BFbdn0c77t4MLx3zQETB7BtALGEOXUABCH8t2zo50+MrriIYcIWEzrfVTzyxXUGIcBj4 cBLytTYneaisQjHuUGE7tSqOTq0+RQj/1AgZuPdizjYTXHBGEyvFqeNZrIowMRoy9vqL nMZrXqc4UZM00nyrwAtUCeeR5EWiAw7aPwq3TL09b3w+rU9feffW1xo4mY8+QnzLGkjQ ilvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BsUrc95y; 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 p189si9719542pfb.0.2018.12.04.03.22.27; Tue, 04 Dec 2018 03:22:42 -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=BsUrc95y; 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 S1727661AbeLDLCl (ORCPT + 99 others); Tue, 4 Dec 2018 06:02:41 -0500 Received: from mail.kernel.org ([198.145.29.99]:48776 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726596AbeLDLCj (ORCPT ); Tue, 4 Dec 2018 06:02:39 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 60CA92087F; Tue, 4 Dec 2018 11:02:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921358; bh=TjzVIOUuR3gAOeYdcvxJ5fICl9hAizOKd99TGtnor1M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BsUrc95yZf1EV0fWePI3oq7UErATRH3aN16CwS7XqsCvQTcHBjYmWnjGI1AlzmCFX m4q8UQGkQunykzJ/dzhuLQgHAFQomhImgcnyOr2KN7UJykT50JDD93qiJQD0HG0tKn auca65O0+UDB32VaGTEaUQm5XMrDb2MgWhVimROM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xu Wen , Qu Wenruo , Su Yue , David Sterba , Ben Hutchings , Sasha Levin Subject: [PATCH 4.14 041/146] btrfs: Check that each block group has corresponding chunk at mount time Date: Tue, 4 Dec 2018 11:48:47 +0100 Message-Id: <20181204103728.515606170@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103726.750894136@linuxfoundation.org> References: <20181204103726.750894136@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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. ------------------ commit 514c7dca85a0bf40be984dab0b477403a6db901f upstream. A crafted btrfs image with incorrect chunk<->block group mapping will trigger a lot of unexpected things as the mapping is essential. Although the problem can be caught by block group item checker added in "btrfs: tree-checker: Verify block_group_item", it's still not sufficient. A sufficiently valid block group item can pass the check added by the mentioned patch but could fail to match the existing chunk. This patch will add extra block group -> chunk mapping check, to ensure we have a completely matching (start, len, flags) chunk for each block group at mount time. Here we reuse the original helper find_first_block_group(), which is already doing the basic bg -> chunk checks, adding further checks of the start/len and type flags. Link: https://bugzilla.kernel.org/show_bug.cgi?id=199837 Reported-by: Xu Wen Signed-off-by: Qu Wenruo Reviewed-by: Su Yue Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Ben Hutchings Signed-off-by: Sasha Levin --- fs/btrfs/extent-tree.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index fdc42eddccc2..83791d13c204 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -9828,6 +9828,8 @@ static int find_first_block_group(struct btrfs_fs_info *fs_info, int ret = 0; struct btrfs_key found_key; struct extent_buffer *leaf; + struct btrfs_block_group_item bg; + u64 flags; int slot; ret = btrfs_search_slot(NULL, root, key, path, 0, 0); @@ -9862,8 +9864,32 @@ static int find_first_block_group(struct btrfs_fs_info *fs_info, "logical %llu len %llu found bg but no related chunk", found_key.objectid, found_key.offset); ret = -ENOENT; + } else if (em->start != found_key.objectid || + em->len != found_key.offset) { + btrfs_err(fs_info, + "block group %llu len %llu mismatch with chunk %llu len %llu", + found_key.objectid, found_key.offset, + em->start, em->len); + ret = -EUCLEAN; } else { - ret = 0; + read_extent_buffer(leaf, &bg, + btrfs_item_ptr_offset(leaf, slot), + sizeof(bg)); + flags = btrfs_block_group_flags(&bg) & + BTRFS_BLOCK_GROUP_TYPE_MASK; + + if (flags != (em->map_lookup->type & + BTRFS_BLOCK_GROUP_TYPE_MASK)) { + btrfs_err(fs_info, +"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", + found_key.objectid, + found_key.offset, flags, + (BTRFS_BLOCK_GROUP_TYPE_MASK & + em->map_lookup->type)); + ret = -EUCLEAN; + } else { + ret = 0; + } } free_extent_map(em); goto out; -- 2.17.1