Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3940729pxj; Mon, 21 Jun 2021 09:50:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy0ri1iwWzbC02ZUpvknSjpWash9HRYBQkqJTxPBjHarUPCVwQjIWYkHDU69sl/L/5/dOvz X-Received: by 2002:a02:908a:: with SMTP id x10mr18148277jaf.30.1624294200482; Mon, 21 Jun 2021 09:50:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294200; cv=none; d=google.com; s=arc-20160816; b=ZcLdDzUU6yErWY0wIiqouiR5B823HrU3zg9lbc0hYbL2C05AwPUoEOcA/yOz6PQ24B UjQV8HeNkgO2OT1EOgCpoD0kCHeR9BmgUdoclMZlM7AAJSlHD/rzDPO9RAFvzTCb74vy K5d0+QcdnK7RmLNgy0XQfqHGaiFTerTEbHaD6iCohKv6EhY2jU7OMrSn5C3IoJs3ARws iX8cQWKi0EsQ39qqhPq9/4Nd27APb31Uph4Lh0A0VkP2lgtV5VaOQz+Q1dLaWG8eclo8 fJr7L5+3dSf7qpmBJ2EK7Qa+HlFQ3B7j8Dgf5CGwwUlmT5QVzeBGKkRjuSdOcKFvlegb Hqlg== 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=pCDy6cIZyGD64rX2DNqIqxn/FG8v+DKv5sUngV2ymHo=; b=qThxHJKP6Mab2R8/C0e6G4U6HSYVIiOEn7cSAlExVc31y+SDREsdyho91AH21B77Fk lUgb0AwdZAckIC6qbhQRdCIVJ90DFnk6PwHuGd8EAoS9FWfQbuKAHtKQJU48moARAKum +TKSPO00+DbE7SqfPFnuzjUQMt36W3g7UMbwVYKqJtTdlgdRpXEHB3+4ttsFeoxAqdEQ RWByL960rdtErz1N6xB57ulIKbvcOx9iPz9X1o3TDRiHaKO+fQ0i+5YkFpqSN12X9ICd AldIC0JMxSxwBCpm/Y8S9HUGeaWpKSIji+xJ3GuHJTQzwelQtVtgvz/iQOH8y+mC7BFg nl3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BXgS3yEJ; 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 w4si22065155iov.14.2021.06.21.09.49.48; Mon, 21 Jun 2021 09:50: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=BXgS3yEJ; 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 S230170AbhFUQv3 (ORCPT + 99 others); Mon, 21 Jun 2021 12:51:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:38112 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231164AbhFUQrv (ORCPT ); Mon, 21 Jun 2021 12:47:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id BB9A86120D; Mon, 21 Jun 2021 16:33:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293225; bh=SLdZNVlc3QuvAH3Rngujdew2yvmtAj+wl+Hap5ihrjU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BXgS3yEJz/PtVIchon+65QdVQexEe3SchNmFxVrm48BDljZLp1hDHj+6q1MR2EBRh hG5qeO9WoNIE7APvElWFo8Wl7JwE3fxkkJKtMIhdQfZIdL6p59pXKLW7T/aFsb/kXn ViNzNXCtLzMXpV8IwcjTZ0dobs0XqfHoEFT6ZJbI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Lutomirski , Thomas Gleixner , Borislav Petkov , Dave Hansen , Rik van Riel Subject: [PATCH 5.12 145/178] x86/fpu: Invalidate FPU state after a failed XRSTOR from a user buffer Date: Mon, 21 Jun 2021 18:15:59 +0200 Message-Id: <20210621154927.708870613@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: Andy Lutomirski commit d8778e393afa421f1f117471144f8ce6deb6953a upstream. Both Intel and AMD consider it to be architecturally valid for XRSTOR to fail with #PF but nonetheless change the register state. The actual conditions under which this might occur are unclear [1], but it seems plausible that this might be triggered if one sibling thread unmaps a page and invalidates the shared TLB while another sibling thread is executing XRSTOR on the page in question. __fpu__restore_sig() can execute XRSTOR while the hardware registers are preserved on behalf of a different victim task (using the fpu_fpregs_owner_ctx mechanism), and, in theory, XRSTOR could fail but modify the registers. If this happens, then there is a window in which __fpu__restore_sig() could schedule out and the victim task could schedule back in without reloading its own FPU registers. This would result in part of the FPU state that __fpu__restore_sig() was attempting to load leaking into the victim task's user-visible state. Invalidate preserved FPU registers on XRSTOR failure to prevent this situation from corrupting any state. [1] Frequent readers of the errata lists might imagine "complex microarchitectural conditions". Fixes: 1d731e731c4c ("x86/fpu: Add a fastpath to __fpu__restore_sig()") Signed-off-by: Andy Lutomirski Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Acked-by: Dave Hansen Acked-by: Rik van Riel Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20210608144345.758116583@linutronix.de Signed-off-by: Greg Kroah-Hartman --- arch/x86/kernel/fpu/signal.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -369,6 +369,25 @@ static int __fpu__restore_sig(void __use fpregs_unlock(); return 0; } + + /* + * The above did an FPU restore operation, restricted to + * the user portion of the registers, and failed, but the + * microcode might have modified the FPU registers + * nevertheless. + * + * If the FPU registers do not belong to current, then + * invalidate the FPU register state otherwise the task might + * preempt current and return to user space with corrupted + * FPU registers. + * + * In case current owns the FPU registers then no further + * action is required. The fixup below will handle it + * correctly. + */ + if (test_thread_flag(TIF_NEED_FPU_LOAD)) + __cpu_invalidate_fpregs_state(); + fpregs_unlock(); } else { /*