Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10788931imu; Thu, 6 Dec 2018 06:51:28 -0800 (PST) X-Google-Smtp-Source: AFSGD/W3vRDmrR69AkWP5nWUnibqs/n8G0AbiY3xoAvdhpNw4urzh6rZrdLH/6VzNw3tmGafM/5Z X-Received: by 2002:a62:1f53:: with SMTP id f80mr28671821pff.92.1544107887978; Thu, 06 Dec 2018 06:51:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544107887; cv=none; d=google.com; s=arc-20160816; b=R+a/W1KLM7L7gwWiu+MCZkWIlWGktUgmQmLJpn+AKzy5JwF/FpIWbliHyZnj8zzdHr FtY8QgIrMQbLZzWVQyLeBB7yJMHyzhAuoT51R+bqJOkBtCmrOKKswKrSNL7wkLDvNsZc sjKCELLkdeqPeOTekMTT3eGjF9YszNGW6kLZbswYlCFuTLCFr+/HWMLKhueAa+D/y887 kZPQEpQpM1TdIexiil2CjNUChSxMDQtWJvunl99fx9Iv82cmlHBuhFoXVSc8e99kc5hK fnVXpKfGO7IeVwR/v9Q5Aw9//8kGvrFOpfbeFZTItGJNfFE+m//YKm7DmrFNsJ+CBIFN 8Tdw== 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=15sIWFltPxKVglXO+rs4iWP0uB3e00L6mgoCg5ZK5Kk=; b=zyl1AhzeDRUfIiYxCkuzPoRVvFJMmAym9DcFiZubUuEMmmXg8R12iRzHiFd+DzkLyw ktKjgP4qT5sAavcSqjMo+dTdQxCsHWgRNTu13pJTehtA5xz/RCPc/rojFh30VhMS61yF RjkUee3MRgS9CjwbYYypfCKeaCR6Q01o16D1c+auA8103s26v2iayZNOJvsJ3jCJSxa9 avNejINp6HTn83WHvb1wl2DPre9crBI31AoP98vgMqw62r7kAe2AtCeSyCaqCkrcvxc1 2s9XYQYn9cMKjZO5PmSSSTHcmedBA28Q5bVSpDfzVzzNfsiysYH5AXntG/I0x6885luv zTyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=cxt+0v3T; 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 n24si375342pgv.119.2018.12.06.06.51.12; Thu, 06 Dec 2018 06:51:27 -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=cxt+0v3T; 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 S1731571AbeLFOsZ (ORCPT + 99 others); Thu, 6 Dec 2018 09:48:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:53590 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731122AbeLFOsY (ORCPT ); Thu, 6 Dec 2018 09:48:24 -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 E389E2082B; Thu, 6 Dec 2018 14:48:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544107703; bh=eBA7gT3Rd+0e4dfnuM6juCumTJZnPnpfDIrWxFEtQM8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cxt+0v3TIqZ7n8rAebwz49G5iY8bF0gTnnH5MJYIyEdSjCYuzfwO8b39nBZENmHLv UDqiwNJyi4CjjtPotKCilYk0669kQ3DyDl5+Dc5b86FVkzJ9+Ts+s4dWDgL83E9CPg phw9oW4+4gi7O5/BBWt339PNIHolKdGCONG/Y+r0= 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 Subject: [PATCH 4.9 080/101] btrfs: Check that each block group has corresponding chunk at mount time Date: Thu, 6 Dec 2018 15:39:19 +0100 Message-Id: <20181206143016.797837482@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206143011.174892052@linuxfoundation.org> References: <20181206143011.174892052@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.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo 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 [bwh: Backported to 4.9: Use root->fs_info instead of fs_info] Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -9896,6 +9896,8 @@ static int find_first_block_group(struct 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); @@ -9930,8 +9932,32 @@ static int find_first_block_group(struct "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(root->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(root->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;