public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>,
	syzbot+4376a9a073770c173269@syzkaller.appspotmail.com,
	Sasha Levin <sashal@kernel.org>,
	clm@fb.com, josef@toxicpanda.com, linux-btrfs@vger.kernel.org
Subject: [PATCH AUTOSEL 5.15 07/12] btrfs: send: limit number of clones and allocated memory size
Date: Wed, 15 Feb 2023 15:46:29 -0500	[thread overview]
Message-ID: <20230215204637.2761073-7-sashal@kernel.org> (raw)
In-Reply-To: <20230215204637.2761073-1-sashal@kernel.org>

From: David Sterba <dsterba@suse.com>

[ Upstream commit 33e17b3f5ab74af12aca58c515bc8424ff69a343 ]

The arg->clone_sources_count is u64 and can trigger a warning when a
huge value is passed from user space and a huge array is allocated.
Limit the allocated memory to 8MiB (can be increased if needed), which
in turn limits the number of clone sources to 8M / sizeof(struct
clone_root) = 8M / 40 = 209715.  Real world number of clones is from
tens to hundreds, so this is future proof.

Reported-by: syzbot+4376a9a073770c173269@syzkaller.appspotmail.com
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 fs/btrfs/send.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 9250a17731bdb..692ae2e2f8cc5 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -7549,10 +7549,10 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
 	/*
 	 * Check that we don't overflow at later allocations, we request
 	 * clone_sources_count + 1 items, and compare to unsigned long inside
-	 * access_ok.
+	 * access_ok. Also set an upper limit for allocation size so this can't
+	 * easily exhaust memory. Max number of clone sources is about 200K.
 	 */
-	if (arg->clone_sources_count >
-	    ULONG_MAX / sizeof(struct clone_root) - 1) {
+	if (arg->clone_sources_count > SZ_8M / sizeof(struct clone_root)) {
 		ret = -EINVAL;
 		goto out;
 	}
-- 
2.39.0


  parent reply	other threads:[~2023-02-15 20:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-15 20:46 [PATCH AUTOSEL 5.15 01/12] ionic: refactor use of ionic_rx_fill() Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 02/12] Fix XFRM-I support for nested ESP tunnels Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 03/12] arm64: dts: rockchip: drop unused LED mode property from rk3328-roc-cc Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 04/12] ARM: dts: rockchip: add power-domains property to dp node on rk3288 Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 05/12] HID: elecom: add support for TrackBall 056E:011C Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 06/12] ACPI: NFIT: fix a potential deadlock during NFIT teardown Sasha Levin
2023-02-15 20:46 ` Sasha Levin [this message]
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 08/12] ASoC: rt715-sdca: fix clock stop prepare timeout issue Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 09/12] IB/hfi1: Assign npages earlier Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 10/12] neigh: make sure used and confirmed times are valid Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 11/12] HID: core: Fix deadloop in hid_apply_multiplier Sasha Levin
2023-02-15 20:46 ` [PATCH AUTOSEL 5.15 12/12] x86/cpu: Add Lunar Lake M Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230215204637.2761073-7-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+4376a9a073770c173269@syzkaller.appspotmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox