From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7EFDC48BC2 for ; Wed, 23 Jun 2021 12:24:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF1866100B for ; Wed, 23 Jun 2021 12:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231265AbhFWM0S (ORCPT ); Wed, 23 Jun 2021 08:26:18 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:36454 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230526AbhFWM0F (ORCPT ); Wed, 23 Jun 2021 08:26:05 -0400 Message-Id: <20210623121452.308388343@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1624451027; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=l1bb+7d6N8jvg0pgI138TNfNQ4TeqyNc9Yg6ZRE0VNI=; b=uWE6/ita4xbkPCr1m5gK20m3VPJh9JpRYchiQE751Gy7v4w+lPR/2JQRF44mCpR9AbWrc5 uwMsxN15nJAscG/vPYrUiUvD4/juwBELR/6C4jOJMId7GtB74ODwPXatIm/jVyU82ED7ai 8/DRMg6Ll7que5/pFVxOtYVuiGf7PPmymdxNLhrAstfjM+pn7MfPNdiOaBoHxd/eUmpIU5 myBlwIKjN+9KdLXw0Q8xyESzDem9iOoNXeopZ6cBlU9O299XTcevQjyRvxA7PPalpAS0KX SzmL98I2Xy6t5Y7sRskpsSCmnmFeLJEQgekfqB26Bvo83+xC0Ojzvu5ueMKe7w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1624451027; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: references:references; bh=l1bb+7d6N8jvg0pgI138TNfNQ4TeqyNc9Yg6ZRE0VNI=; b=ppnwkId0zwtug5agfCsAk0ih6/snPGI8CaPWyNVFKKvBHyGN5EyPPVrH8dEDY55Y5KvFw9 OPh4cErwUSE2q+CA== Date: Wed, 23 Jun 2021 14:01:37 +0200 From: Thomas Gleixner To: LKML Cc: Andy Lutomirski , Dave Hansen , Fenghua Yu , Tony Luck , Yu-cheng Yu , Sebastian Andrzej Siewior , Borislav Petkov , Peter Zijlstra , Kan Liang , "Chang Seok Bae" , Megha Dey , Oliver Sang Subject: [patch V4 10/65] x86/fpu: Reject invalid MXCSR values in copy_kernel_to_xstate() References: <20210623120127.327154589@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-transfer-encoding: 8-bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Instead of masking out reserved bits, check them and reject the provided state as invalid if not zero. Suggested-by: Andy Lutomirski Signed-off-by: Thomas Gleixner Reviewed-by: Borislav Petkov --- V3: Validate MXCSR when FP|SSE|YMM are set. The quirk check is only correct for the copy function. V2: New patch --- arch/x86/kernel/fpu/xstate.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1154,6 +1154,19 @@ void copy_xstate_to_kernel(struct membuf membuf_zero(&to, to.left); } +static inline bool mxcsr_valid(struct xstate_header *hdr, const u32 *mxcsr) +{ + u64 mask = XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM; + + /* Only check if it is in use */ + if (hdr->xfeatures & mask) { + /* Reserved bits in MXCSR must be zero. */ + if (*mxcsr & ~mxcsr_feature_mask) + return false; + } + return true; +} + /* * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] format * and copy to the target thread. This is called from xstateregs_set(). @@ -1172,6 +1185,9 @@ int copy_kernel_to_xstate(struct xregs_s if (validate_user_xstate_header(&hdr)) return -EINVAL; + if (!mxcsr_valid(&hdr, kbuf + offsetof(struct fxregs_state, mxcsr))) + return -EINVAL; + for (i = 0; i < XFEATURE_MAX; i++) { u64 mask = ((u64)1 << i); @@ -1202,9 +1218,6 @@ int copy_kernel_to_xstate(struct xregs_s */ xsave->header.xfeatures |= hdr.xfeatures; - /* mxcsr reserved bits must be masked to zero for historical reasons. */ - xsave->i387.mxcsr &= mxcsr_feature_mask; - return 0; }