Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1709772pxb; Mon, 8 Mar 2021 04:37:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFqA/AUUmcsFoBjCCg0KHaNyq2KU7yslBAO6CX9fhSP6KI7LaMbVUPagsbqmqx1vQkTvLm X-Received: by 2002:a17:906:4a50:: with SMTP id a16mr15088028ejv.256.1615207037759; Mon, 08 Mar 2021 04:37:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615207037; cv=none; d=google.com; s=arc-20160816; b=IezFT1Dmz0cSMgLvdfiCqVY6vWEyx0eUz7zwPQ0aHup1lrJuxRRyIPr7LjE90wcKWT gbKYgaZSRLDjgPXFiTqenLt/2TnySyOAH+h/rUMp+9l8gDnQiLPjD0LRT0BcsCGA1e10 rur/XS1jYXP9EF6rYMBoXJUskAf7nFfrTvWvYihQUA9d8Opx495ccxTIfJjGP6+JVxku Wm0JuWARqwdXZg/CbLC+yR7VqoYeYubzQd0UzpCkHF4Yu3GPKrsk0Yu1fuoBQF6Efk3x KW6FZuTi34tlmfb79xS3ErHuS+44RUWLYmq1cw/lCDAbbYKyjguE5YzoQEyPLyA2nbNE t55Q== 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=TubOUXux4AQxiZPZ0yYSI1diZ2xQK/NYQjZmmNX2L/8=; b=WfNdWbqaJXW79PGZ7nHsVqILj5h7twQisSman++0Gd2WrqxdwUQuSd9IbBRPWOTx3F 0ZGL6xEW+17gs59SjF/Uoveq7T2w863sRVTE2LagncPZ9lRpCXSr4qcwb8yf1PwB6moD wwQ2Pbobqn6RWrB4ILXgvsSWQkjN2IRdn3aYMyz283e+d1ocPD8QpmWMOSIvwet0YqmI Pbx9K/xQxnunP8JO++EF+NnPezSDgCne6nq7sOB7Sn5LPoRG3hfXz+1DpLae9yJ1kE52 jiAr6zyoXCEBftxyZUG9eLcY45LzKzKYI84ZfNnfHJgQROE+qCh/7AoYy7BNeMfwtGbv dkIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UQRe60Z0; 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 n1si7132922edo.504.2021.03.08.04.36.54; Mon, 08 Mar 2021 04:37:17 -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=UQRe60Z0; 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 S231303AbhCHMdp (ORCPT + 99 others); Mon, 8 Mar 2021 07:33:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:42370 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229904AbhCHMdZ (ORCPT ); Mon, 8 Mar 2021 07:33:25 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C313364EBC; Mon, 8 Mar 2021 12:33:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206805; bh=wzhuPkANCXCaekNYRQaxmpc/vxCAdUc0q3IvCWdgKbk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UQRe60Z0n2M2BJ8KYlxnJFi7lBNtiHdcFAttnpSSwNY+nXRVSoTtH3UHv2GD6Rqe3 Yb6KOvPm9GGpKRdqW1V2KR3IBJTvzMLgY3EOz/pYuY4mLsk43zmfcEx9n2lW1AB2A7 4kmJO71zuej91T2R8xwnTosXs9ZOHxByM+OfREMw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , David Sterba Subject: [PATCH 5.10 12/42] btrfs: validate qgroup inherit for SNAP_CREATE_V2 ioctl Date: Mon, 8 Mar 2021 13:30:38 +0100 Message-Id: <20210308122718.731384148@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122718.120213856@linuxfoundation.org> References: <20210308122718.120213856@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: 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 @@ -1914,7 +1914,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; } @@ -1923,6 +1926,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(file, vol_args->name, vol_args->fd,