Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1903987ybl; Tue, 3 Dec 2019 14:44:30 -0800 (PST) X-Google-Smtp-Source: APXvYqwHZbqe/smtpSFNjbCZjBGpT0IL0bp8CItoAbbmM2BXGLTDbHK4PrCMSxlF6GOfC58niqG4 X-Received: by 2002:a9d:624e:: with SMTP id i14mr170365otk.371.1575413070090; Tue, 03 Dec 2019 14:44:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575413070; cv=none; d=google.com; s=arc-20160816; b=mGDmw5pQrBRI+d9UzlSmO64rsFwaKh93ofK1gOnEhXsDVAGhF5DC8zEwlrU0v7KTNV JZ0NfMmNKlyb/r5hLE2fcx7UMgfBJkHmHoQvIlxGpmCjJ+5tLc0yDp/VPk1wFtjOnmi5 kUMCOVvMdk7IKvbIYvr8Hl3waLEh1MpRwdSMhPksr8iBUflyksur5hGz8K2GLqpA5ZlZ YGpy0FY+AEk/8YkDk2uUtMyAqBHAVQZSqd0libRX6zUKXKs/avxAfkn2vrsfltkY0hp4 f5u3FuN7dVVWJ7P3OfC/5zNfqjoOj4h56b1TbFOwHH3TeP/IHHcrM6nMcA6r7L2cUOd4 6Iuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Pa/NouIRGRhYE4bTEMyLNgWHintmUSyogU9r44016N0=; b=MdnFtfKIgcPNqybtsRmFjjNALKPst4ebTMQhRX8FE/Aw7MQN5K46M7xLtnisAO3XP1 3eAL20jXzuVri+fkTEnIHoyHHUwdxbgPrFufYDTofaviT1TP+GjXSskfN1ZI5+UFiy4v 2b31G0hClMLsVgY1e/a4fjH6e6kLESIjSag8no2XO71fjmgxVtECyAG5+FcDqyQQV2yn 4F8MSYzy1xZ3zfyja9Y/HYaB/8W+ylnDl26zjfltpfI8gjaGAKzslAAoRuOXGPVawh70 rQ/ReKTWpFuqyziO/rbNBklu9VT3boipFbAVcYhG8cCHfm6Ks4mbbqihBdXewoYmpyKR Fpdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zbaKe7Tv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w18si2225530otl.54.2019.12.03.14.44.17; Tue, 03 Dec 2019 14:44:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zbaKe7Tv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728864AbfLCWnv (ORCPT + 99 others); Tue, 3 Dec 2019 17:43:51 -0500 Received: from mail.kernel.org ([198.145.29.99]:59376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728853AbfLCWns (ORCPT ); Tue, 3 Dec 2019 17:43:48 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E5558206EC; Tue, 3 Dec 2019 22:43:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575413027; bh=3YuYJgHzPeqfnu9iRUvtg9XL7Q+BuK2tpsB3QoGyXMI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zbaKe7Tv4Ym/Tx314g/XXf6D0Nr/sFfTXC14ubBNq74wRtImzge39q8kZRhUyUZ9t oLBwelMKtMqDHpBmMqbKKcccxCqtly02TWG4JkzE/UdDShCqHPhiw3lPedMDpr1bao Mv9Ymm8EWJKOVLygjbehQapioT6DmP0F2ulUMOAI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sebastian Andrzej Siewior , Borislav Petkov , Rik van Riel , Aubrey Li , Austin Clements , Barret Rhoden , Dave Hansen , David Chase , "H. Peter Anvin" , ian@airs.com, Ingo Molnar , Josh Bleecher Snyder , Thomas Gleixner , x86-ml Subject: [PATCH 5.3 108/135] x86/fpu: Dont cache access to fpu_fpregs_owner_ctx Date: Tue, 3 Dec 2019 23:35:48 +0100 Message-Id: <20191203213041.013425390@linuxfoundation.org> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191203213005.828543156@linuxfoundation.org> References: <20191203213005.828543156@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sebastian Andrzej Siewior commit 59c4bd853abcea95eccc167a7d7fd5f1a5f47b98 upstream. The state/owner of the FPU is saved to fpu_fpregs_owner_ctx by pointing to the context that is currently loaded. It never changed during the lifetime of a task - it remained stable/constant. After deferred FPU registers loading until return to userland was implemented, the content of fpu_fpregs_owner_ctx may change during preemption and must not be cached. This went unnoticed for some time and was now noticed, in particular since gcc 9 is caching that load in copy_fpstate_to_sigframe() and reusing it in the retry loop: copy_fpstate_to_sigframe() load fpu_fpregs_owner_ctx and save on stack fpregs_lock() copy_fpregs_to_sigframe() /* failed */ fpregs_unlock() *** PREEMPTION, another uses FPU, changes fpu_fpregs_owner_ctx *** fault_in_pages_writeable() /* succeed, retry */ fpregs_lock() __fpregs_load_activate() fpregs_state_valid() /* uses fpu_fpregs_owner_ctx from stack */ copy_fpregs_to_sigframe() /* succeeds, random FPU content */ This is a comparison of the assembly produced by gcc 9, without vs with this patch: | # arch/x86/kernel/fpu/signal.c:173: if (!access_ok(buf, size)) | cmpq %rdx, %rax # tmp183, _4 | jb .L190 #, |-# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |-#APP |-# 512 "arch/x86/include/asm/fpu/internal.h" 1 |- movq %gs:fpu_fpregs_owner_ctx,%rax #, pfo_ret__ |-# 0 "" 2 |-#NO_APP |- movq %rax, -88(%rbp) # pfo_ret__, %sfp … |-# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |- movq -88(%rbp), %rcx # %sfp, pfo_ret__ |- cmpq %rcx, -64(%rbp) # pfo_ret__, %sfp |+# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |+#APP |+# 512 "arch/x86/include/asm/fpu/internal.h" 1 |+ movq %gs:fpu_fpregs_owner_ctx(%rip),%rax # fpu_fpregs_owner_ctx, pfo_ret__ |+# 0 "" 2 |+# arch/x86/include/asm/fpu/internal.h:512: return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; |+#NO_APP |+ cmpq %rax, -64(%rbp) # pfo_ret__, %sfp Use this_cpu_read() instead this_cpu_read_stable() to avoid caching of fpu_fpregs_owner_ctx during preemption points. The Fixes: tag points to the commit where deferred FPU loading was added. Since this commit, the compiler is no longer allowed to move the load of fpu_fpregs_owner_ctx somewhere else / outside of the locked section. A task preemption will change its value and stale content will be observed. [ bp: Massage. ] Debugged-by: Austin Clements Debugged-by: David Chase Debugged-by: Ian Lance Taylor Fixes: 5f409e20b7945 ("x86/fpu: Defer FPU state load until return to userspace") Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Borislav Petkov Reviewed-by: Rik van Riel Tested-by: Borislav Petkov Cc: Aubrey Li Cc: Austin Clements Cc: Barret Rhoden Cc: Dave Hansen Cc: David Chase Cc: "H. Peter Anvin" Cc: ian@airs.com Cc: Ingo Molnar Cc: Josh Bleecher Snyder Cc: Thomas Gleixner Cc: x86-ml Link: https://lkml.kernel.org/r/20191128085306.hxfa2o3knqtu4wfn@linutronix.de Link: https://bugzilla.kernel.org/show_bug.cgi?id=205663 Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/fpu/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -509,7 +509,7 @@ static inline void __fpu_invalidate_fpre static inline int fpregs_state_valid(struct fpu *fpu, unsigned int cpu) { - return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; + return fpu == this_cpu_read(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu; } /*