Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1264031pxa; Thu, 20 Aug 2020 07:05:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ3iXIaP0i4v7zmuRSW+NZCGXDx/8P4fM9R6euSHtPAxrvCU9Eb3AnrjtLfFLE8XcxRmQi X-Received: by 2002:a17:906:a9a:: with SMTP id y26mr3344737ejf.403.1597932301965; Thu, 20 Aug 2020 07:05:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597932301; cv=none; d=google.com; s=arc-20160816; b=S6OJrVI9VqQER05b1pXPpoyWl8HbLnd1C64VW0MAoUatlGoBjEMLSlHmvDk8IHwNou MZHkZICBp6Hva/piHJULN+iVKxyucse0wDkD4lDaLGimKbcNpTvVb2fJLpc3dpfnbF19 JtCIhJB9peZ7owAG8s27ph5ux7ISm6Q8OxXz+Rn2aJrHW2MDAADG/JpCuXlndkP+P7ze 703hQOLSxPbgGiUJ7SGIH/KyceVU94/AChH46zor5TxoIlXFaCTsg/KDxx9E3P4wgAvF 7g4GZLAJZTvrJHBK/ySobNrXeEeF6wHNFOBVNlnL6rStaawQI0BR1K7g7eXgIs4DZ7cJ V5Rw== 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=QhE2ZmPCq6PaoZC1MenJSW6uqh27jjV0O2Fxn95ym9c=; b=lfAZ85FLCxlHfO/55pBKEeWBg7Mf0e0GHhLLZg7fpVzI8u4J0ajN4KAnIy72c6OwFi 8kIQvNwHB1SYZHsko21vPMIgyI5y/c2XRyLhL//k6/wLId0onqNAdCkHlSezkanWAw0B +V+xUw0SY9tQ2V+YjaR6dTNELxIcLIT02vRi8J3naKPcR74SowTmHgFIlr6fMPfIQ/yt ceRLA+dj8l1sdaci4kDeSs0vPdIwo3vm+GQwV31jlNx559sagwIqQWy9+HglmbRi83Sc VVAJGFk2v2mrDti6DyGAOnTjvtP21m4xqZ4NuMpKIZXqZAIHGg4WqUBudB/Nu00jCAqU 2EQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dphiwbMA; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s6si1340118edy.74.2020.08.20.07.04.37; Thu, 20 Aug 2020 07:05:01 -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=dphiwbMA; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731259AbgHTN6u (ORCPT + 99 others); Thu, 20 Aug 2020 09:58:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:33422 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727011AbgHTJZH (ORCPT ); Thu, 20 Aug 2020 05:25:07 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 7D17722D03; Thu, 20 Aug 2020 09:25:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597915506; bh=avcBHyqrkzSdswKWmM8x1RCkJC0BbdbsDhQaxrT6diM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dphiwbMAF2/92NpIr2UkuBLM8ORktpUoXyidzbdKrII4BOJjbctXVt0q6HkZnkYYd LDhqsW+O9RoDxRBPmrozCsHbiF1c7YmurqXtlMxKdKYaMy2b03yPyRglK3gi4F2hRs de+3Xl7ITnlyJLOX2FW5jCKo2CXjL21xHCo1RcNk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , Filipe Manana , David Sterba Subject: [PATCH 5.8 038/232] btrfs: trim: fix underflow in trim length to prevent access beyond device boundary Date: Thu, 20 Aug 2020 11:18:09 +0200 Message-Id: <20200820091614.619601643@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200820091612.692383444@linuxfoundation.org> References: <20200820091612.692383444@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Qu Wenruo commit c57dd1f2f6a7cd1bb61802344f59ccdc5278c983 upstream. [BUG] The following script can lead to tons of beyond device boundary access: mkfs.btrfs -f $dev -b 10G mount $dev $mnt trimfs $mnt btrfs filesystem resize 1:-1G $mnt trimfs $mnt [CAUSE] Since commit 929be17a9b49 ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit"), we try to avoid trimming ranges that's already trimmed. So we check device->alloc_state by finding the first range which doesn't have CHUNK_TRIMMED and CHUNK_ALLOCATED not set. But if we shrunk the device, that bits are not cleared, thus we could easily got a range starts beyond the shrunk device size. This results the returned @start and @end are all beyond device size, then we call "end = min(end, device->total_bytes -1);" making @end smaller than device size. Then finally we goes "len = end - start + 1", totally underflow the result, and lead to the beyond-device-boundary access. [FIX] This patch will fix the problem in two ways: - Clear CHUNK_TRIMMED | CHUNK_ALLOCATED bits when shrinking device This is the root fix - Add extra safety check when trimming free device extents We check and warn if the returned range is already beyond current device. Link: https://github.com/kdave/btrfs-progs/issues/282 Fixes: 929be17a9b49 ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit") CC: stable@vger.kernel.org # 5.4+ Signed-off-by: Qu Wenruo Reviewed-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-io-tree.h | 2 ++ fs/btrfs/extent-tree.c | 14 ++++++++++++++ fs/btrfs/volumes.c | 4 ++++ 3 files changed, 20 insertions(+) --- a/fs/btrfs/extent-io-tree.h +++ b/fs/btrfs/extent-io-tree.h @@ -34,6 +34,8 @@ struct io_failure_record; */ #define CHUNK_ALLOCATED EXTENT_DIRTY #define CHUNK_TRIMMED EXTENT_DEFRAG +#define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \ + CHUNK_TRIMMED) enum { IO_TREE_FS_PINNED_EXTENTS, --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -33,6 +33,7 @@ #include "delalloc-space.h" #include "block-group.h" #include "discard.h" +#include "rcu-string.h" #undef SCRAMBLE_DELAYED_REFS @@ -5668,6 +5669,19 @@ static int btrfs_trim_free_extents(struc &start, &end, CHUNK_TRIMMED | CHUNK_ALLOCATED); + /* Check if there are any CHUNK_* bits left */ + if (start > device->total_bytes) { + WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG)); + btrfs_warn_in_rcu(fs_info, +"ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu", + start, end - start + 1, + rcu_str_deref(device->name), + device->total_bytes); + mutex_unlock(&fs_info->chunk_mutex); + ret = 0; + break; + } + /* Ensure we skip the reserved area in the first 1M */ start = max_t(u64, start, SZ_1M); --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -4720,6 +4720,10 @@ again: } mutex_lock(&fs_info->chunk_mutex); + /* Clear all state bits beyond the shrunk device size */ + clear_extent_bits(&device->alloc_state, new_size, (u64)-1, + CHUNK_STATE_MASK); + btrfs_device_set_disk_total_bytes(device, new_size); if (list_empty(&device->post_commit_list)) list_add_tail(&device->post_commit_list,