Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp880584pxk; Thu, 17 Sep 2020 20:11:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyHQhPDz/TFOsLQ93TPJVZ/0e80KhkoTui2r5u/hxjJ9OaTqlCAjNXDD+KxN2iqiY7uR3Zj X-Received: by 2002:a17:906:16c8:: with SMTP id t8mr35094161ejd.272.1600398704484; Thu, 17 Sep 2020 20:11:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398704; cv=none; d=google.com; s=arc-20160816; b=D3fm/TfoIIfRkpIVqc1+7cQ5VvkbcLQG5GTubi47qL0f+SxQttA1XXmihL2oaOr44b g+b31WkszUsbTZU5Ok1VhcR+noVtv1LfxPzEalNN4LICiSJCUcf7i8MbAvno4okFKWFD mElTsiPA2SOV3YGnQg3VPfKdVb726byhxJZDz5sYU9asL4ehmzrBlkA9nOb0eQarT2Dk 6nlP8Eau5EYDbqGXtxAD5OcaFbo/blejoqLNCyHisIzqxmAmlxpwNQ8nqdQiXi7JWmyE hQDSuLnqApEHL/8gBVsrC69bbZZzxITfffDFBbYoVqE9FpJTd/k0LbAwLv1m74TLZmA3 SF5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SKyMbGIqG8cv039WrJqgmCOd1opeN2EE58Jc8hIjeQU=; b=TNBhE3oMVKYmPnKJyLyhV5af00QD5F1wYMZqZe9D4bbEe+EtRjOdGJ+JS4qOUP35EZ kNm0S7CdSK5NzMLPkD+K+hVsW40KgdXY4F29v6ERVEqe+LhGuSJ7duV6S7mEkJ+MWP2c ScPHRETSSxUWTUnQT0Zix4Q4tEdZuKisV7UjOWc9pOG67Uv5VRI513DMVOtj8p0MiN/G tUBW0Lxui8iyD+xOf77xmFEOXqKfBNZLzyz3Maomnf3uiwahaEjsKQ/fHa2yw1AnzQcR EtCKBjB8RPRYg/qBj0KyUczj1tN8jH5T0JWEmvz0rqshmRpNLvPhcnWZ8chEISVQsCON 4lnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=jqbbOov9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si1306046eda.448.2020.09.17.20.11.21; Thu, 17 Sep 2020 20:11:44 -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; dkim=pass header.i=@kernel.org header.s=default header.b=jqbbOov9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730675AbgIRDJR (ORCPT + 99 others); Thu, 17 Sep 2020 23:09:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:49088 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726979AbgIRCDJ (ORCPT ); Thu, 17 Sep 2020 22:03:09 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E38972376F; Fri, 18 Sep 2020 02:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394588; bh=gK1pCVrS3EWbvsNEZcGrV7NFBFqJjT6F0+m+yH146Hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jqbbOov9HCHoZwqDSi8pFi8iTvUT/E6EaUZtM4INr4UEFBAtwgyW651SZExb4YQNb nwFyDgr4/DLpJo9Q1aJW5x3ZB2tbHtZsaJvfezdNkTo+EfTG5Z++O7yDRJz5sULQqS 3gxDnChkgqHcE5F+/pKomTjqE9ensSmeaaoHYx7k= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , Josef Bacik , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 097/330] btrfs: tree-checker: Check leaf chunk item size Date: Thu, 17 Sep 2020 21:57:17 -0400 Message-Id: <20200918020110.2063155-97-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit f6d2a5c263afca84646cf3300dc13061bedbd99e ] Inspired by btrfs-progs github issue #208, where chunk item in chunk tree has invalid num_stripes (0). Although that can already be caught by current btrfs_check_chunk_valid(), that function doesn't really check item size as it needs to handle chunk item in super block sys_chunk_array(). This patch will add two extra checks for chunk items in chunk tree: - Basic chunk item size If the item is smaller than btrfs_chunk (which already contains one stripe), exit right now as reading num_stripes may even go beyond eb boundary. - Item size check against num_stripes If item size doesn't match with calculated chunk size, then either the item size or the num_stripes is corrupted. Error out anyway. Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/tree-checker.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 91ea38506fbb7..84b8d6ebf98f3 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -674,6 +674,44 @@ int btrfs_check_chunk_valid(struct extent_buffer *leaf, return 0; } +/* + * Enhanced version of chunk item checker. + * + * The common btrfs_check_chunk_valid() doesn't check item size since it needs + * to work on super block sys_chunk_array which doesn't have full item ptr. + */ +static int check_leaf_chunk_item(struct extent_buffer *leaf, + struct btrfs_chunk *chunk, + struct btrfs_key *key, int slot) +{ + int num_stripes; + + if (btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) { + chunk_err(leaf, chunk, key->offset, + "invalid chunk item size: have %u expect [%zu, %u)", + btrfs_item_size_nr(leaf, slot), + sizeof(struct btrfs_chunk), + BTRFS_LEAF_DATA_SIZE(leaf->fs_info)); + return -EUCLEAN; + } + + num_stripes = btrfs_chunk_num_stripes(leaf, chunk); + /* Let btrfs_check_chunk_valid() handle this error type */ + if (num_stripes == 0) + goto out; + + if (btrfs_chunk_item_size(num_stripes) != + btrfs_item_size_nr(leaf, slot)) { + chunk_err(leaf, chunk, key->offset, + "invalid chunk item size: have %u expect %lu", + btrfs_item_size_nr(leaf, slot), + btrfs_chunk_item_size(num_stripes)); + return -EUCLEAN; + } +out: + return btrfs_check_chunk_valid(leaf, chunk, key->offset); +} + __printf(3, 4) __cold static void dev_item_err(const struct extent_buffer *eb, int slot, @@ -1265,7 +1303,7 @@ static int check_leaf_item(struct extent_buffer *leaf, break; case BTRFS_CHUNK_ITEM_KEY: chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); - ret = btrfs_check_chunk_valid(leaf, chunk, key->offset); + ret = check_leaf_chunk_item(leaf, chunk, key, slot); break; case BTRFS_DEV_ITEM_KEY: ret = check_dev_item(leaf, key, slot); -- 2.25.1