Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3932365pxj; Mon, 21 Jun 2021 09:39:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyU0uRzizHySYEKxfpXlfkA/WiGbU8iOqC42gQBx2adbRmKgGwateT93HAbjWzw+hJD+JFz X-Received: by 2002:a17:906:f141:: with SMTP id gw1mr11414601ejb.435.1624293540184; Mon, 21 Jun 2021 09:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293540; cv=none; d=google.com; s=arc-20160816; b=YIC3VOWVsH7isqCgso10xwAUej2bxJ21W0q0Pz9ZSlllyqvmCRiAOWWvlWD+3zkbSk JOLXyz9sAoXjyNEV87HYsxIToQ10G9xHmzTvcO+CxaucpF67hpr0kMSKIYaPcmsfPiBJ wo0n/zZHdGe09sUqAl8aGJClUoholtPSsY9HlwxexTCX+fM4WFv+unr2djcUZlI3tShk scPmHPTBdS01F9HfN3H83hzbj8SDvgejcVJ0BmOyE8cGfznu4J31bhMNlKKXrxhRAwm0 dSw7mUx+tiMcs+8N4F9TOtq3SWOrB7qrx5L0AwNLw6NgZq40H4hyGDXvVCd9aFKOuJWK qHpw== 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=8AdrzlNGLlNzHsCoVMtmDLYP6cG8DAQWuFxqqZgWC60=; b=b7LNkcVU+oL84KlN7SZYF6acQTx8PnhQ+bmTfvX3yzPki+PYbYHA/2FWxNlptuzUFY KPKC+C/3On+9y6JLJuMkTrDfoQIpOkaKR5hIAEbpVr3sJ3RYGJ4VB/5TcrC0aBoKanot s1l2ajcJPG4+VngvhZmmhoj4qVLY9o9APiCCOH90zf9jfz/eKI5yFSvwQBpm2YQDBs+7 flPHLQrjW/ORqN5mm0iNGox2CZtQgQV0xa62yzGRiybxVRqLKNsPDdwdIxfGQWP/Q7er YH1FWNNhWD0X2O7/Bg8qBxb1Kg7iQr5sKa15yb6Dc5CbTghNGPT3X9Q4OGD6M0hF2+Ff uWdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=dbwyu4A4; 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 f13si17038771edw.44.2021.06.21.09.38.36; Mon, 21 Jun 2021 09:39:00 -0700 (PDT) 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=dbwyu4A4; 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 S231929AbhFUQjK (ORCPT + 99 others); Mon, 21 Jun 2021 12:39:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:56118 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232885AbhFUQew (ORCPT ); Mon, 21 Jun 2021 12:34:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A3D84611CE; Mon, 21 Jun 2021 16:27:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292843; bh=MZjtSJzO+qywH8efNw6SjL2tbIzjlSHbRH0jKMh3a+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dbwyu4A4i2bPozQzhtbrakK1AYk0oPS7cU1DZrvK+bcjJv0Yg4tGp89aCY9E5NOnx 7RQm0gw8jDmfEYlqbB9l0L7IVpcuqyK1j/Opm4tkm/vH0VABo/dao/BSgnC9ug5Bw8 191/bfn4N7MccD2Jj+2DKFI7lZGKWRLaNkMtnMYQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+2067e764dbcd10721e2e@syzkaller.appspotmail.com, Thomas Gleixner , Borislav Petkov , Dave Hansen , Rik van Riel Subject: [PATCH 5.10 122/146] x86/fpu: Prevent state corruption in __fpu__restore_sig() Date: Mon, 21 Jun 2021 18:15:52 +0200 Message-Id: <20210621154919.138664123@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@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: Thomas Gleixner commit 484cea4f362e1eeb5c869abbfb5f90eae6421b38 upstream. The non-compacted slowpath uses __copy_from_user() and copies the entire user buffer into the kernel buffer, verbatim. This means that the kernel buffer may now contain entirely invalid state on which XRSTOR will #GP. validate_user_xstate_header() can detect some of that corruption, but that leaves the onus on callers to clear the buffer. Prior to XSAVES support, it was possible just to reinitialize the buffer, completely, but with supervisor states that is not longer possible as the buffer clearing code split got it backwards. Fixing that is possible but not corrupting the state in the first place is more robust. Avoid corruption of the kernel XSAVE buffer by using copy_user_to_xstate() which validates the XSAVE header contents before copying the actual states to the kernel. copy_user_to_xstate() was previously only called for compacted-format kernel buffers, but it works for both compacted and non-compacted forms. Using it for the non-compacted form is slower because of multiple __copy_from_user() operations, but that cost is less important than robust code in an already slow path. [ Changelog polished by Dave Hansen ] Fixes: b860eb8dce59 ("x86/fpu/xstate: Define new functions for clearing fpregs and xstates") Reported-by: syzbot+2067e764dbcd10721e2e@syzkaller.appspotmail.com Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Reviewed-by: Borislav Petkov Acked-by: Dave Hansen Acked-by: Rik van Riel Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20210608144345.611833074@linutronix.de Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/fpu/signal.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -405,14 +405,7 @@ static int __fpu__restore_sig(void __use if (use_xsave() && !fx_only) { u64 init_bv = xfeatures_mask_user() & ~user_xfeatures; - if (using_compacted_format()) { - ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx); - } else { - ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size); - - if (!ret && state_size > offsetof(struct xregs_state, header)) - ret = validate_user_xstate_header(&fpu->state.xsave.header); - } + ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx); if (ret) goto err_out;