Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2342672ybz; Thu, 23 Apr 2020 16:23:02 -0700 (PDT) X-Google-Smtp-Source: APiQypKJX8++serT5+L6O/d7rC/1Wt7IBlSXeweW85aeULlJLTkiTzlq//ST7gfE/jLmrhzmbq0a X-Received: by 2002:a17:906:7804:: with SMTP id u4mr4919608ejm.328.1587684182435; Thu, 23 Apr 2020 16:23:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587684182; cv=none; d=google.com; s=arc-20160816; b=TF6o3VKevRqlBJwGS8mI5wWo42kZl9TzXl0vpLsrEQbE3kE5YhPgnyNapZCjpa7EGn UIYSU+fgekSHnmF4dDobfcWSVHlboanyM/bpsvg7uu8nuLA9DG/deC6Tjif2EQMJvtIk bAq/7LDWRGVP2q+bR4WgIKejcnEg61E3xGHVpV2nqgUnZXWeMnWVHBzdc9ahc/yuM3l4 rZDoEnPJ62bEjqq5abCKAboQoFX2BCUrK/K34MM5SGIy5qLwhxW8aMe3NMbPd6yM+eKF 9M57RCvSkn+X6AHP1qUmfdm7ePUVTYCgOVDRGh0x2XcmaoqTPRKDOXkj1D04Z+NnzXmZ aRQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=SXS5oRsEM0NDkYQQuMDKFLXN2amPyQXIXWc753rfxww=; b=MY128xYi21HS4ZkoxLUhFCMvOutvwbvfxKP+tGK732w7ll8UBSWGwApUmhl7jtuP0o pdozwTcZt24CKX8YNhCd3YNaWlZr8Gx6EH0hAhIkfmTSQSeYkqelK+5L+N9prVS/pcoi G4Yil/kVztl7CvACpqcj7mFtRv6/ZT0yXHj4BnxOkr0/WjHPZtRrNVKkSRRsilHC0Rbc A+unFjE3gjdTKi2p5CbEQen4Iw/rsSvAFk0nFiOP+s3cZU7860oipafH/SEMBOHVhmSG WfqXF+6KMeVHgBVbxYTpf4QQUcKw77RtNE5MhG5fuvQMFBIHtMBjwuBvvBrgAyG/VmoL GL1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g15si1923626ejh.346.2020.04.23.16.22.39; Thu, 23 Apr 2020 16:23:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728748AbgDWXVZ (ORCPT + 99 others); Thu, 23 Apr 2020 19:21:25 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:48544 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728199AbgDWXGc (ORCPT ); Thu, 23 Apr 2020 19:06:32 -0400 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1jRkvL-0004bY-2N; Fri, 24 Apr 2020 00:06:27 +0100 Received: from ben by deadeye with local (Exim 4.93) (envelope-from ) id 1jRkvJ-00E6iH-Mh; Fri, 24 Apr 2020 00:06:25 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, Denis Kirjanov , "Greg Kroah-Hartman" , "David Sterba" , "Gu Jinxiang" , "Xu Wen" , "Qu Wenruo" , "Ben Hutchings" Date: Fri, 24 Apr 2020 00:04:39 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 052/245] btrfs: Verify that every chunk has corresponding block group at mount time In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.83-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo commit 7ef49515fa6727cb4b6f2f5b0ffbc5fc20a9f8c6 upstream. If a crafted image has missing block group items, it could cause unexpected behavior and breaks the assumption of 1:1 chunk<->block group mapping. Although we have the block group -> chunk mapping check, we still need chunk -> block group mapping check. This patch will do extra check to ensure each chunk has its corresponding block group. Link: https://bugzilla.kernel.org/show_bug.cgi?id=199847 Reported-by: Xu Wen Signed-off-by: Qu Wenruo Reviewed-by: Gu Jinxiang Reviewed-by: David Sterba Signed-off-by: David Sterba [bwh: Backported to 4.4: adjust context] Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- fs/btrfs/extent-tree.c | 58 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -8819,6 +8819,62 @@ btrfs_create_block_group_cache(struct bt return cache; } + +/* + * Iterate all chunks and verify that each of them has the corresponding block + * group + */ +static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) +{ + struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; + struct extent_map *em; + struct btrfs_block_group_cache *bg; + u64 start = 0; + int ret = 0; + + while (1) { + read_lock(&map_tree->map_tree.lock); + /* + * lookup_extent_mapping will return the first extent map + * intersecting the range, so setting @len to 1 is enough to + * get the first chunk. + */ + em = lookup_extent_mapping(&map_tree->map_tree, start, 1); + read_unlock(&map_tree->map_tree.lock); + if (!em) + break; + + bg = btrfs_lookup_block_group(fs_info, em->start); + if (!bg) { + btrfs_err(fs_info, + "chunk start=%llu len=%llu doesn't have corresponding block group", + em->start, em->len); + ret = -EUCLEAN; + free_extent_map(em); + break; + } + if (bg->key.objectid != em->start || + bg->key.offset != em->len || + (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != + (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { + btrfs_err(fs_info, +"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", + em->start, em->len, + em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, + bg->key.objectid, bg->key.offset, + bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); + ret = -EUCLEAN; + free_extent_map(em); + btrfs_put_block_group(bg); + break; + } + start = em->start + em->len; + free_extent_map(em); + btrfs_put_block_group(bg); + } + return ret; +} + int btrfs_read_block_groups(struct btrfs_root *root) { struct btrfs_path *path; @@ -8981,7 +9037,7 @@ int btrfs_read_block_groups(struct btrfs } init_global_block_rsv(info); - ret = 0; + ret = check_chunk_block_group_mappings(info); error: btrfs_free_path(path); return ret;