Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp241809pxf; Wed, 10 Mar 2021 05:28:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJxNdcceZqUTL17iYE2mfFpOkxVnPLF5wmk+rKDHcCMeJDeaq1AAYxeogsGGobzUF0w4tPH1 X-Received: by 2002:a17:906:66cf:: with SMTP id k15mr3635070ejp.296.1615382918137; Wed, 10 Mar 2021 05:28:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615382918; cv=none; d=google.com; s=arc-20160816; b=ySkJ/AUKl6Kr7PJAPdakloc6pYtCncOeKZhXJrqhdZcnUPcg2vojbnz1JPOdZbY4Mb VVLUK7aMMiFdqAZFe8kCg/+4Km98UwrWGT1IuUufh3cuOOwLl59RdbcrUFSouXG0EdlS 82dppa0S/XEmaT8FPLwQbHH0/GDSi/jwAshCaXC1vb5hur+6K6dhIMNNiXpzF0F755gV yalIcqixh0z8KQ+eifVWICfbElfs3+t1ebGO9DxlT5b2ovDNn3HCo1g2zrM4MvWYoSSo vKGuG13K58NL05zYmKdICu7wr8xokuLAaWkGyBP00fpD/3tyHTeT0fF4x0lIZ8JlhlL0 u/oA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pmifnJQ2CW5X/0qNDKNyH02lwrx8eLM77MtmjSzt+Fs=; b=zRHkDWZPpcfr+UIKsVVnH4tr2YudOzTMiTwavdD3npAhMyM2efteFq3Ymn0smW3OUY yVJH0Baq8SV4OZY9RIANHTfiGe7UQYSzR9eWAvr0nMpQLdXzaPtGvVsCRm1Yljx97vS4 AqIbOzRSQmJVd8uxGm18BvGhbeffXeC5l9clyLl1jxycG18/vB4ZXr/z/P8ZXfDFRp89 JzFib7NqE4Yd4gh8SneUsQjEQ3L7PQsgsuj6aaSRqPXilY3u4UYkjmPnKFvFbh4wfTnL oB+V3qoW6UDzof2iTJ0QsQf6J9B/Ygq3A9IYzDHh3aXFHCORA+hnjmw2mEZGWzZaAAkz in2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wqXocW0r; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h7si11998922edt.553.2021.03.10.05.28.13; Wed, 10 Mar 2021 05:28:38 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=wqXocW0r; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232874AbhCJN1S (ORCPT + 99 others); Wed, 10 Mar 2021 08:27:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:46444 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233134AbhCJNY5 (ORCPT ); Wed, 10 Mar 2021 08:24:57 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5BD8A64FD7; Wed, 10 Mar 2021 13:24:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615382697; bh=a7SOAual/f/cgG+0y327eyUUjVQ+AkZiQCn2i5lx14I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wqXocW0rA3xacug/7W+zbnZ4vlxtrsXu3BWHIlZkphax5ZZinKWBflYuokv94cnUb NMt0YuOnGwYcYEHBKeL3SYzFFTBduQBZZSd9zLYP63p774ab4f1DcsvU1iO25YIHRc eMQex7LKUCSlChsdAGOnNv+X67XadrugrnzZu3lI= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , David Sterba Subject: [PATCH 4.19 03/39] btrfs: validate qgroup inherit for SNAP_CREATE_V2 ioctl Date: Wed, 10 Mar 2021 14:24:11 +0100 Message-Id: <20210310132319.834379173@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210310132319.708237392@linuxfoundation.org> References: <20210310132319.708237392@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Dan Carpenter commit 5011c5a663b9c6d6aff3d394f11049b371199627 upstream. The problem is we're copying "inherit" from user space but we don't necessarily know that we're copying enough data for a 64 byte struct. Then the next problem is that 'inherit' has a variable size array at the end, and we have to verify that array is the size we expected. Fixes: 6f72c7e20dba ("Btrfs: add qgroup inheritance") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Dan Carpenter Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/ioctl.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -1842,7 +1842,10 @@ static noinline int btrfs_ioctl_snap_cre if (vol_args->flags & BTRFS_SUBVOL_RDONLY) readonly = true; if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { - if (vol_args->size > PAGE_SIZE) { + u64 nums; + + if (vol_args->size < sizeof(*inherit) || + vol_args->size > PAGE_SIZE) { ret = -EINVAL; goto free_args; } @@ -1851,6 +1854,20 @@ static noinline int btrfs_ioctl_snap_cre ret = PTR_ERR(inherit); goto free_args; } + + if (inherit->num_qgroups > PAGE_SIZE || + inherit->num_ref_copies > PAGE_SIZE || + inherit->num_excl_copies > PAGE_SIZE) { + ret = -EINVAL; + goto free_inherit; + } + + nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + + 2 * inherit->num_excl_copies; + if (vol_args->size != struct_size(inherit, qgroups, nums)) { + ret = -EINVAL; + goto free_inherit; + } } ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,