Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp2343093ybz; Thu, 23 Apr 2020 16:23:34 -0700 (PDT) X-Google-Smtp-Source: APiQypI+sANjO6SG4STI/9r44ajXhKtNLgTmaDVOD0Sz2DyQezbE1ByqC1Ig+I7H2j6TosQOYLZQ X-Received: by 2002:a17:906:1584:: with SMTP id k4mr4922721ejd.355.1587684214220; Thu, 23 Apr 2020 16:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587684214; cv=none; d=google.com; s=arc-20160816; b=dCG3YAANd1ysl+KSIvcnmQacdWST8/IojUhaSS3A02eKEM0CcGXAjK2jahNKjwhFH2 6z966sYlWSHv+6qfoyAUL4UFXpIObRmGY+lyD9YKSe3r42PVBx2rspO4IU9vsEoO5JV3 3xEumJ86d43ecH6oswhGR+l4dLievFNL3WIqPVYCUgPwk0DNxE5k0PPyy8QWdW5BLFgD TRSjjMYxQiGIKbQxi5tZWX9QK54p8U/n87U0ExI6DrWRRbxqyisc+KFIW+Pu9eJzBn/J DX/c29SW/xN4YHlwetosHetwOBp/GJ9mR0zzfBGHWNT8abhQV2Tnr7+PrwnghO4K/VSS dm8w== 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=c7xVLMf39OMMaGYEKkjbmw8B0PEQ+bUANSPvVjnZ+HQ=; b=j0cxj03pBqqK6psBDiwWpD/X9UsDjNptTr3mZWjAi1fs9IqQhQzbBdQIMW+TScAK/e XTyX/SATiaykz5AfoYHRMJ+RxPyTKYeEiuRw/NlNP2nBkwWw2qmA2arOgwA3Y+egjEmK zXXkkSYHrqMu+le3HuxtAa/citrKSyvvCxLukVTV/lxolhhWls55bIkyXd1VVGVgUmy9 M4Umu6zpYFmb/ImzW9S5YW6aqV5H3gKyjjSQLg6ckEdOkAqbLMG3Pbs2aBRV8fkYSptj Kwft8gK25g3M6VHfLZwv3Rsik5OPszGqWJQ7KUYHAn7nt0MWEl7jCbJxJMCsQZqvTbXp EBtg== 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 x68si1989649ede.282.2020.04.23.16.23.11; Thu, 23 Apr 2020 16:23:34 -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 S1729622AbgDWXVi (ORCPT + 99 others); Thu, 23 Apr 2020 19:21:38 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:48516 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728195AbgDWXGc (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-0004bI-1i; Fri, 24 Apr 2020 00:06:27 +0100 Received: from ben by deadeye with local (Exim 4.93) (envelope-from ) id 1jRkvJ-00E6i8-Kv; 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 , "Ben Hutchings" , "Qu Wenruo" , "Xu Wen" , "David Sterba" , "Gu Jinxiang" , "Greg Kroah-Hartman" Date: Fri, 24 Apr 2020 00:04:37 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) X-Patchwork-Hint: ignore Subject: [PATCH 3.16 050/245] btrfs: validate type when reading a chunk 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: Gu Jinxiang commit 315409b0098fb2651d86553f0436b70502b29bb2 upstream. Reported in https://bugzilla.kernel.org/show_bug.cgi?id=199839, with an image that has an invalid chunk type but does not return an error. Add chunk type check in btrfs_check_chunk_valid, to detect the wrong type combinations. Link: https://bugzilla.kernel.org/show_bug.cgi?id=199839 Reported-by: Xu Wen Reviewed-by: Qu Wenruo Signed-off-by: Gu Jinxiang Signed-off-by: David Sterba [bwh: Backported to 4.4: Use root->fs_info instead of fs_info] Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman Signed-off-by: Ben Hutchings --- fs/btrfs/volumes.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -5815,6 +5815,8 @@ static int btrfs_check_chunk_valid(struc u16 num_stripes; u16 sub_stripes; u64 type; + u64 features; + bool mixed = false; length = btrfs_chunk_length(leaf, chunk); stripe_len = btrfs_chunk_stripe_len(leaf, chunk); @@ -5855,6 +5857,32 @@ static int btrfs_check_chunk_valid(struc btrfs_chunk_type(leaf, chunk)); return -EIO; } + + if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { + btrfs_err(root->fs_info, "missing chunk type flag: 0x%llx", type); + return -EIO; + } + + if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && + (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { + btrfs_err(root->fs_info, + "system chunk with data or metadata type: 0x%llx", type); + return -EIO; + } + + features = btrfs_super_incompat_flags(root->fs_info->super_copy); + if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) + mixed = true; + + if (!mixed) { + if ((type & BTRFS_BLOCK_GROUP_METADATA) && + (type & BTRFS_BLOCK_GROUP_DATA)) { + btrfs_err(root->fs_info, + "mixed chunk type in non-mixed mode: 0x%llx", type); + return -EIO; + } + } + if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||