From patchwork Mon Nov 19 16:04:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sebastian Andrzej Siewior X-Patchwork-Id: 10688843 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 57E5B6C5 for ; Mon, 19 Nov 2018 16:04:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 484CA29B8B for ; Mon, 19 Nov 2018 16:04:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3BB0B29D80; Mon, 19 Nov 2018 16:04:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D2FAB29B8B for ; Mon, 19 Nov 2018 16:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729907AbeKTC2Q convert rfc822-to-8bit (ORCPT ); Mon, 19 Nov 2018 21:28:16 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:38931 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729796AbeKTC2Q (ORCPT ); Mon, 19 Nov 2018 21:28:16 -0500 Received: from bigeasy by Galois.linutronix.de with local (Exim 4.80) (envelope-from ) id 1gOm1y-0001lo-Vn; Mon, 19 Nov 2018 17:04:11 +0100 Date: Mon, 19 Nov 2018 17:04:10 +0100 From: Sebastian Andrzej Siewior To: Borislav Petkov , x86@kernel.org Cc: Ingo Molnar , linux-kernel@vger.kernel.org, Andy Lutomirski , Paolo Bonzini , Radim =?utf-8?b?S3LEjW3DocWZ?= , kvm@vger.kernel.org, "Jason A. Donenfeld" , Rik van Riel , Dave Hansen Subject: [PATCH v2] x86/fpu: Disable BH while while loading FPU registers in __fpu__restore_sig() Message-ID: <20181119160410.ne7oiq2gkwt6jiqg@linutronix.de> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20180716 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The sequence fpu->initialized = 1; /* step A */ preempt_disable(); /* step B */ fpu__restore(fpu); preempt_enable(); is racy in regard to a context switch. For 32bit frames __fpu__restore_sig() prepares the FPU state within fpu->state. To ensure that a context switch (switch_fpu_prepare() in particular) does not modify fpu->state it uses fpu__drop() which sets fpu->initializes to 0. With this change the CPU's FPU state is not saved to fpu->state during a context switch. It then loads the state to fpu->state from userland and ensures it sane. The new state is loaded via fpu__restore(). The code sets then fpu->initializes to 1 in order to avoid fpu__initialize() doing anything (overwrite the new state) which is part of fpu__restore(). A context switch between step A and B would save CPU's current FPU registers to fpu->state and overwrite the newly prepared state. This looks like tiny race window but the Kernel Test Robot reported this back in 2016 while we had lazy FPU support. Borislav Petkov made the link between that report and another patch that has been posted. Since the removal of the lazy FPU support, this race goes unnoticed because the warning has been removed. Use local_bh_disable() around the restore sequence to avoid the race. BH needs to be disabled because BH is allowed to run (even with preemption disabled) and might invoke kernel_fpu_begin(). Link: https://lkml.kernel.org/r/20160226074940.GA28911@pd.tnic Cc: stable@vger.kernel.org Signed-off-by: Sebastian Andrzej Siewior --- v1…v2: A more verbose commit as message. arch/x86/kernel/fpu/signal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 61a949d84dfa5..d99a8ee9e185e 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -344,10 +344,10 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) sanitize_restored_xstate(tsk, &env, xfeatures, fx_only); } + local_bh_disable(); fpu->initialized = 1; - preempt_disable(); fpu__restore(fpu); - preempt_enable(); + local_bh_enable(); return err; } else {