Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1708379pxb; Mon, 8 Mar 2021 04:35:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9vTI93L7Law8gBfddLrAvWaVjs8ycYl9s0BjIv02HszvtZ8VbPB3YKE9T+ZM7tNofKkh8 X-Received: by 2002:aa7:cf16:: with SMTP id a22mr21301970edy.288.1615206919691; Mon, 08 Mar 2021 04:35:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615206919; cv=none; d=google.com; s=arc-20160816; b=yjgEs8p24NAtcv9i/Iyh3U6B6FL6eurxb4uXTmSw8DoxJbUMpt2Wp0LL9hmS39NhIC DAKiZz4aZlzCyAK8fZbvzzvj0rAT/13+8xGj6N2jRd7E5tWvAunOq1YqFm8ub+56XnCl oDBglgRIRJtvo6s1B0U82dmbssnv866HHIYKqNknnujYFEmYgAKHpSheB/P9iNxnk2Ad gtQqzQg4voZf3qUCVJg+Wb8/xNSMMFgGosNGqQ9BllQxwrplHi2nC43/eq0w9a3E0LTd 7i+szXRhEPwohNuljNego1gyOT1/6tvLxImrCrkf5aDCjY4g7eraiXPBbf18i2O4O9uZ a7oQ== 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=O+EG9RVyZgnNXnc9NINCdlwqx1aIQgV1NGYOePVaFTs=; b=r9ATrRNAPEtHT/I2Snxx9yDO6lqJD1WSDEBLUb+GOJCOBXrsDxGczBsqroprsuZUjX +omWWIwXkM34JIOZrDA6j+eGpdjTSjw452nohBNl8mFQiX3+SyAbp9yM5xA52/FTpxZr 3K9eMzG2DmURGNeTD984YDlEVlhA7flGU7PFOejJAQFpLLLu36vTpjRqCiU72DXLZ7iD YVFzC0rs0UZyvOGBWVMGxAMk+wJMI8vJLY+KwUM74ObVOPTG5Ba+9k7uD6rZhCeXqqOl uTQ1F0A3UlUhg4mnS8kHiqSsinwbAXYRqjGxG7hTIN9RWScr2ZWYRxeQXXzJd19t8e6t 5FcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=d0Fyo21B; 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 g12si7662400edj.22.2021.03.08.04.34.56; Mon, 08 Mar 2021 04:35:19 -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=d0Fyo21B; 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 S231822AbhCHMcF (ORCPT + 99 others); Mon, 8 Mar 2021 07:32:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:40812 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbhCHMbe (ORCPT ); Mon, 8 Mar 2021 07:31:34 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7D2E064EBC; Mon, 8 Mar 2021 12:31:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206694; bh=GoS1EONNrChvEgo5uCbEG0sXQw0iWTaE1lA20hbqlbU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d0Fyo21B//tkz0OfUjuZiAFPnBF5ZpFnoJ+LaSDwPQOPsk3OmCCfy/WO7qMEYp2m9 kkcZtCDQwBFuUnth4SjCQ752qdeNk+//K/043TOBzY2Lnw6Nw1LX+qb4Ztukct8iUM vnxvEHPow+G9KhUEY1Cw4DTxVQWzgwHfcY5JzMcg= 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.4 05/22] btrfs: validate qgroup inherit for SNAP_CREATE_V2 ioctl Date: Mon, 8 Mar 2021 13:30:22 +0100 Message-Id: <20210308122714.654095897@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122714.391917404@linuxfoundation.org> References: <20210308122714.391917404@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 @@ -1907,7 +1907,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; } @@ -1916,6 +1919,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,