Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3941427pxj; Mon, 21 Jun 2021 09:50:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqBRHdxO+31uUnpYojsCpnal+48/7HQJDRysRvX8HlokbGXZ9oSIvSinbDJl/kSAMLRcaN X-Received: by 2002:a05:6e02:12ac:: with SMTP id f12mr18781010ilr.207.1624294256875; Mon, 21 Jun 2021 09:50:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294256; cv=none; d=google.com; s=arc-20160816; b=zJ25Of9o6utYkh8gM8bap9mSDynkqBr8AnyBLhsDs/3V3Mt+y7bw7osq8mpbjWM27l C2cif4GTanJAppKU5FVgHdLTEI7vxNj0KOkE9eNfsreqJ47cS/ksMTI+y/hEln894pLo jeen7Ox8Uyc8rAUd08V1bP0nejfB8OMgmVCe7p4JH8qqnpQuM0j5NF40TwuE5M8gx82r ExCgokeSUVqo6ITen1jy9uLSw1TR8z4wnbzcvimhmCKC/3VMEJMAW78E5ffOpCCVWrbY OWzT31u51fS1kdbs97iJyfbztV6begvhWn1vatVC6mrZ8jyIjNfcJl3elHXzRLg9XiPS ABeg== 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=k/2wT1si+NRXvD4fmTDHxTDOETnmcMDWBDzMNAtaATIIqSyBYifrDpd9yUz1zk6r8G U5DiXfIyDNk1stHn5Z3Tc5A1aHkRTxxmN/KlyAiiHbCsetoRkCMHzbjFQBLJZ6uUPkBo BBHUNollDigKeacMAh1zV4Z7sm/CeJ1cKxIZoQ9/AtywKrS73QJWEQ6mWNqD7uE0qR35 7nXcuqR9kS9tHCs8wPIyRv6H2YcBe3+fqhwz4C6yjEvcXuMeAR1aNkGm/0OnSH/aRs+X Ii2KlvJEyVCghlUTYOFzyFiyDMo/+BwXFXdzYINNakJA0NoOiezZ3cdomB+tPYL93SEm F3Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lij4Hh2k; 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 h5si18441607ioz.62.2021.06.21.09.50.44; Mon, 21 Jun 2021 09:50:56 -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=lij4Hh2k; 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 S232758AbhFUQvV (ORCPT + 99 others); Mon, 21 Jun 2021 12:51:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:38080 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232546AbhFUQrt (ORCPT ); Mon, 21 Jun 2021 12:47:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 293BC61456; Mon, 21 Jun 2021 16:33:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293222; bh=MZjtSJzO+qywH8efNw6SjL2tbIzjlSHbRH0jKMh3a+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lij4Hh2kJ3fEVUc7E1FCTjTWwRL2cejCgwIXxtFCiu6L4WasgfcdfqMblb4oDShcy tdxPEyt7isotneDA4sQbUpp6t9ep/+CHZM4ez7giyhk5KrBHILJNdwpic0IRel2UBU ZCbfOh/NGLM8e11yr5JPWXcwyU170/6sHR6dkPnM= 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.12 144/178] x86/fpu: Prevent state corruption in __fpu__restore_sig() Date: Mon, 21 Jun 2021 18:15:58 +0200 Message-Id: <20210621154927.675535697@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@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;