Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1018707ybe; Wed, 4 Sep 2019 11:13:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqwASgtzXGEaP46scgUCIHE07yOZI4v0/c3w2tEIsFMu35UQwoBP6xII2h2p6NBRsZzaHFCl X-Received: by 2002:a63:c006:: with SMTP id h6mr35932086pgg.290.1567620807150; Wed, 04 Sep 2019 11:13:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567620807; cv=none; d=google.com; s=arc-20160816; b=FtDGqz7zXHHEN0nB+io9eXXpkcYo4239t7vQRudUWCnDEifoLMEnMww/8JTCs6defS +xG0aXortQO2I2tZfPVRLyCIYEWQGhORLdV9jFngm6qk53mL9zLqYHWBywNWZBu69HwW dRlWI5I8F3+YJYwX4uhZdpQ4t/sHZjFhC0coI6ekgWtcWRTNYcGYCitErBnIFZu37Vns w4nLKgKJjBkVHv4fQKttT2qeFD8twZjwfLFWGqyFwYUPe0f51JAzyJQoUeFqbENb4+VB tT7Fg29Ue1W5UTuiYfWFO0hwz2sx8mgkItTEu0d4+MQPFOuOnFBx0SdPq1VyfMUQXIxv r+EQ== 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=CIGsxJWpUQ8gRMQVGY8HspfrCZKqNjiYM9rw5n6gXq0=; b=czQEw+hEX6o67PC7p89ovyKZ7x7awcXWVx4xlE3JoltOwBHaARwcgpjZDxQHKopN0a iygmZuXa3AXymJc//eNCZ2ivZXVy/BODLBJqdQtryHjmKV3fdPRZq1vByV34RDHDr+7/ VnRsYcsVJXnMHftUn0tT1I3zE3PMhx84utYhO+QneRaqLCtRYPnrSwHjnc0z5F/+nBcU cKHqN5R9lriKJuI4Q2a7bg0OIhYUrj2vc/K7DB+PgHMZcbWkoCpe8gVCudqBkhaB2zah cJqNRQgBN46dYndgaPOnAbl5PG1cykrgwwsX8tZnzCGoGNka2Lb9rh/c2nnxJf71Vl3O fYkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mpPP1RMB; 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 z9si17785188pgk.46.2019.09.04.11.13.11; Wed, 04 Sep 2019 11:13:27 -0700 (PDT) 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=mpPP1RMB; 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 S2390318AbfIDSLl (ORCPT + 99 others); Wed, 4 Sep 2019 14:11:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:55706 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389606AbfIDSLh (ORCPT ); Wed, 4 Sep 2019 14:11:37 -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 85F702339E; Wed, 4 Sep 2019 18:11:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620697; bh=3rwJ7ku9/cStwdw7IJRzQ1SEy8H0NB+TKPHJlrIlwJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mpPP1RMBO+Qmaz9I6t5vKO2E4SjObtdrwp9o1LJQGulapOZbMOdUGPxOH+MGxBHNp QQOIRE+TjVDWJxYBnnAF43pzdnjCbLa1GujFSiLS2uECGuzi5hQUGxe4j7Jdati6SR ItSs2c+jvmKwq7WLbjB2ynpYOtGBJdXEfXbFvZ1Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , Anand Jain , Qu Wenruo , David Sterba , Sasha Levin Subject: [PATCH 5.2 020/143] btrfs: trim: Check the range passed into to prevent overflow Date: Wed, 4 Sep 2019 19:52:43 +0200 Message-Id: <20190904175314.862538430@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175314.206239922@linuxfoundation.org> References: <20190904175314.206239922@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 [ Upstream commit 07301df7d2fc220d3de5f7ad804dcb941400cb00 ] Normally the range->len is set to default value (U64_MAX), but when it's not default value, we should check if the range overflows. And if it overflows, return -EINVAL before doing anything. Reviewed-by: Nikolay Borisov Reviewed-by: Anand Jain Signed-off-by: Qu Wenruo Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/extent-tree.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 5faf057f6f37f..b8f4720879021 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -11226,6 +11226,7 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) struct btrfs_device *device; struct list_head *devices; u64 group_trimmed; + u64 range_end = U64_MAX; u64 start; u64 end; u64 trimmed = 0; @@ -11235,16 +11236,23 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) int dev_ret = 0; int ret = 0; + /* + * Check range overflow if range->len is set. + * The default range->len is U64_MAX. + */ + if (range->len != U64_MAX && + check_add_overflow(range->start, range->len, &range_end)) + return -EINVAL; + cache = btrfs_lookup_first_block_group(fs_info, range->start); for (; cache; cache = next_block_group(cache)) { - if (cache->key.objectid >= (range->start + range->len)) { + if (cache->key.objectid >= range_end) { btrfs_put_block_group(cache); break; } start = max(range->start, cache->key.objectid); - end = min(range->start + range->len, - cache->key.objectid + cache->key.offset); + end = min(range_end, cache->key.objectid + cache->key.offset); if (end - start >= range->minlen) { if (!block_group_cache_done(cache)) { -- 2.20.1