Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2165413ybl; Thu, 19 Dec 2019 09:04:52 -0800 (PST) X-Google-Smtp-Source: APXvYqxEqQqXFtt/B/CVzS9DEBw59mXt0Vd8k7T4Pc8tzjjLOmZfxgbX5pqpLgM08tdjyxMwdey8 X-Received: by 2002:a05:6830:451:: with SMTP id d17mr9411096otc.53.1576775091937; Thu, 19 Dec 2019 09:04:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576775091; cv=none; d=google.com; s=arc-20160816; b=oQVWxUTVh4tthfQr7/g83yN/Q/no51MyZKszgRUKeIxsR9++EVg3scKAx8lINKX5oa 8ZffDVhF71/TnxY+Blaks7kavsobT5tl/96VGKweS+CM+4C5i93/hYv1MT8C+dq3bSnb s1pHTFrPh2AxvzbC3Icl9XjK5dzHE+8noRDaL5sxJHBqe/rRyh78TnvTbNNJmfZfIrLi SPquNvlyTIY+/ghTucaVaE++y+7CKxD0G0RL5IVRyGBmwb3BicmNvAVh9chAOVfx88Zd FXdl0eTshdh8hhUYAIq5N748miYgqWIzEN3m4z5stYW9BJ7vjZE++ms6upVPhcnIcAy/ 6shA== 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:date:cc:to:from:subject :message-id; bh=YJGJGhczoEvK3NqzhpqUobTuJxzPFBgTpPPSAThcneQ=; b=KiqCfu+DfGAk/dcUGAB7k3iXmL43TUnk30Q97n4zl+h7Ns/t10pWFhASmkYY3hkw6/ CpxN3sjL15Vv72c8/dAWUu2Xp2OepceFnOUFw26NlGRzlyfEZL2PmIpj9V78n2XivkZW B5bGf4+uyytSsn7de5/Ext2A4KTwzo9SPHdv07fVW3py2Xoc1qvZTU01AeayHP5dLhkI iCRU/7GgzdWmiUuj0oyf9NE8kItpe5lAkQa9blXXFOf09hiktrmo4HHKdMdjBz4C6wX6 /ZAgKlPRe/eWEWF4n+6lhHqD6zTOAHcVq7+eOod0WI4+9seukmN67j7u+5utMp+PWGHw aT4g== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si2121179oiy.9.2019.12.19.09.04.26; Thu, 19 Dec 2019 09:04:51 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726960AbfLSRCx (ORCPT + 99 others); Thu, 19 Dec 2019 12:02:53 -0500 Received: from mga09.intel.com ([134.134.136.24]:2712 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726760AbfLSRCx (ORCPT ); Thu, 19 Dec 2019 12:02:53 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Dec 2019 09:02:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,332,1571727600"; d="scan'208";a="218228545" Received: from yyu32-desk.sc.intel.com ([143.183.136.51]) by orsmga006.jf.intel.com with ESMTP; 19 Dec 2019 09:02:52 -0800 Message-ID: <19a94f88f1bc66bb81dbf5dd72083d03ca5090e9.camel@intel.com> Subject: Re: [PATCH v2 3/3] x86/fpu/xstate: Invalidate fpregs when __fpu_restore_sig() fails From: Yu-cheng Yu To: Sebastian Andrzej Siewior Cc: linux-kernel@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Dave Hansen , Tony Luck , Andy Lutomirski , Borislav Petkov , Rik van Riel , "Ravi V. Shankar" , Fenghua Yu , Peter Zijlstra Date: Thu, 19 Dec 2019 08:44:08 -0800 In-Reply-To: <20191219142217.axgpqlb7zzluoxnf@linutronix.de> References: <20191212210855.19260-1-yu-cheng.yu@intel.com> <20191212210855.19260-4-yu-cheng.yu@intel.com> <20191218155449.sk4gjabtynh67jqb@linutronix.de> <587463c4e5fa82dff8748e5f753890ac390e993e.camel@intel.com> <20191219142217.axgpqlb7zzluoxnf@linutronix.de> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.4 (3.32.4-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2019-12-19 at 15:22 +0100, Sebastian Andrzej Siewior wrote: > On 2019-12-18 12:53:59 [-0800], Yu-cheng Yu wrote: > > I could have explained this better, sorry! I will explain the first > > case below; other cases are similar. > > > > In copy_user_to_fpregs_zeroing(), we have: > > > > if (user_xsave()) { > > ... > > if (unlikely(init_bv)) > > copy_kernel_to_xregs(&init_fpstate.xsave, init_bv); > > return copy_user_to_xregs(buf, xbv); > > ... > > } > > > > The copy_user_to_xregs() may fail, and when that happens, before going to > > the slow path, there is fpregs_unlock() and context switches may happen. > > The context switch may only happen after fpregs_unlock(). > > > However, at this point, fpu_fpregs_owner_ctx has not been changed; it could > > still be another task's FPU. > > TIF_NEED_FPU_LOAD is set for the task in __fpu__restore_sig() and its > context (__fpu_invalidate_fpregs_state()) has been invalidated. So the > FPU register may contain another task's content and > fpu_fpregs_owner_ctx points to another context. > > > For this to happen and to be detected, the user > > stack page needs to be non-present, fpu_fpregs_owner_ctx need to be another task, > > and that other task needs to be able to detect its registers are modified. > > The last factor is not easy to reproduce, and a CET control-protection fault > > helps. > > So far everything is legal. However. If there is a context switch before > fpregs_lock() then this is bad before we don't account for that. > So that: > > diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c > --- a/arch/x86/kernel/fpu/signal.c > +++ b/arch/x86/kernel/fpu/signal.c > @@ -352,6 +352,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) > fpregs_unlock(); > return 0; > } > + fpregs_deactivate(fpu); > fpregs_unlock(); > } > > @@ -403,6 +404,8 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) > } > if (!ret) > fpregs_mark_activate(); > + else > + fpregs_deactivate(fpu); > fpregs_unlock(); > > err_out: > > > Should be enough. Yes, this works. But then everywhere that calls copy_*_to_xregs_*() etc. needs to be checked. Are there other alternatives? Yu-cheng