Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp657978ybl; Tue, 7 Jan 2020 12:44:16 -0800 (PST) X-Google-Smtp-Source: APXvYqy3CWghqD1NbCCGusd4jYKwraRBkjCPsb4HOoZITHSgZsBsVamgUWxwK6ow6GfeggtTOALb X-Received: by 2002:aca:a9c5:: with SMTP id s188mr259690oie.154.1578429856747; Tue, 07 Jan 2020 12:44:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578429856; cv=none; d=google.com; s=arc-20160816; b=bLmxjqlGEBbKnbVPHs09qS/mEC9U685L75GqDdREJyxB/MNzK2qO5Iq1VDX+f5qoOq fN+NpXNFZT8wPxuhTm/UAwsfdF6M4bXHZTBrUOr/XU77oZcuc1OK+h1TJvQSJE9o0tEA aC0hRZ5SL9LoYLsfWxdoAfIC/IgJ/aL47Y2qSF6UmJCuovDkt4GhoVb8levkh0rFgiWl Nf9ptMHdtvJvULgo7KRLw5BBCXvYCDnHjTcCphiOMNSML8dRqcErd4FUqqx/57jXZkI/ 5cbaCkdILhZvSJewDE3ntEmmf4lxEBmA1rpMyr7i2bjhqe8lm7rq1djHP9dZiHqbTwPJ qwaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:in-reply-to:cc:references:message-id :date:subject:mime-version:from:content-transfer-encoding :dkim-signature; bh=elgI3vKe52VBb1amGoNs0niUYR4aSQmTxfCO3jOaCgQ=; b=e8NnxB+qc7HVZ76u0yucCbIM4uYoCF95+LweX/wEX5xTrEeOpYl6ODLp/fB6m58kLs hmVIpQoL8pf7CcwQIEStKTO/r6q4az7FfmukvxXOIJEQj3m0tJX2fCEKm23AJQ60Ahor WrXOZFxUMNrqgaNdZHMCx1ciGbCgFogJh/tB4T2lo4M/C0mWiM/PoFI5klLrGOut2XNp 77RRUTUpHow6LqtlPS9y6Mrnkff6oWlZiUoX0DsM0wMxcHqeDcSiSvPISi2QeddY2Mzp ws6IG7lie7rNswEZJWsDgJvztn6vL/zPM7M4tMlAj9ockjSLSxnVfBVlG92oolpNXC9V QRPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=pse7AIJX; 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 w5si537225otq.238.2020.01.07.12.44.04; Tue, 07 Jan 2020 12:44:16 -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=@amacapital-net.20150623.gappssmtp.com header.s=20150623 header.b=pse7AIJX; 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 S1727207AbgAGUl6 (ORCPT + 99 others); Tue, 7 Jan 2020 15:41:58 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:41729 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726111AbgAGUl5 (ORCPT ); Tue, 7 Jan 2020 15:41:57 -0500 Received: by mail-pf1-f195.google.com with SMTP id w62so433567pfw.8 for ; Tue, 07 Jan 2020 12:41:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amacapital-net.20150623.gappssmtp.com; s=20150623; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=elgI3vKe52VBb1amGoNs0niUYR4aSQmTxfCO3jOaCgQ=; b=pse7AIJXvWQEE1UrVGW8LJRquTGP8AhP+gp8sQ/BNXrJJHXyEyupgeEYOjvJqHIhEs PSGCWfQo2WPn485ZKM4yX7WYtk4J6xSk95atY/62GyVK95SX1OVTRBaqZavBrIq4lqfF WPq1xB6Kcv6KzUX2ZOPIbCXF0n3D+IsA6MQ/mOaTfoHOM+Dh6Gj1xb9pRB5I48FbOZ3T W4f3Uyk7fw0/WxpSbeioFR3mqbt2WPHs89j/WtgyN3VbpUEQiHla1UB+xXFGtdGRtsDq V50EqoaH2IyLzX4rXIfHZ2frmNfSmvUxLhgmR3KY0+ZzjLFeOJ4CZmw28hltvQwaz4uL siUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=elgI3vKe52VBb1amGoNs0niUYR4aSQmTxfCO3jOaCgQ=; b=LwZ4Tt0JJYiIuvbgIicADceaxBvwaoBFNyqkbYcy9WHJBfb2H6dmGYguXxTTKGkTqg 39I9Gx+P2wUESi58PH0zBIHwGYMLJ8Zj++GTuf+74lUf/i9fCrh/OJ6CYBOexYDeT16f iKhuJfEhJSneUre99p+XwcTYZyzI3nb/roG/HSXCYXomQNjT+xvd1IB/HCk3pYAfuPw5 k9u6fe+Uc3ldrI8sobPqsWr85jMsZirqkvYV8i49TZxEibH1CEj0UvmgSBIsFEYgFXqd Gca1FFLrpBcTNW0WctCrBnUImCZaLk5Y3GShXz/pLqkVMMlJePLDG33jh66VkbdudeYz 96Cg== X-Gm-Message-State: APjAAAVOdf8j6PSxQ+AsztjvEqElt9Zti8DRkQr3OgRMR8lFL/ao/gHo tpl/9wvVB+1iTO/M3g7w6V4IcQ== X-Received: by 2002:a63:eb15:: with SMTP id t21mr1414307pgh.365.1578429716604; Tue, 07 Jan 2020 12:41:56 -0800 (PST) Received: from [10.197.24.59] ([139.104.2.60]) by smtp.gmail.com with ESMTPSA id y62sm475499pfg.45.2020.01.07.12.41.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Jan 2020 12:41:55 -0800 (PST) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Andy Lutomirski Mime-Version: 1.0 (1.0) Subject: Re: [tip: x86/fpu] x86/fpu: Deactivate FPU state after failure during state load Date: Tue, 7 Jan 2020 10:41:52 -1000 Message-Id: References: <157840155965.30329.313988118654552721.tip-bot2@tip-bot2> Cc: linux-tip-commits@vger.kernel.org, Yu-cheng Yu , Sebastian Andrzej Siewior , Borislav Petkov , Andy Lutomirski , Dave Hansen , Fenghua Yu , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Peter Zijlstra , "Ravi V. Shankar" , Rik van Riel , Thomas Gleixner , Tony Luck , x86-ml In-Reply-To: <157840155965.30329.313988118654552721.tip-bot2@tip-bot2> To: linux-kernel@vger.kernel.org X-Mailer: iPhone Mail (17C54) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Jan 7, 2020, at 2:52 AM, tip-bot2 for Sebastian Andrzej Siewior wrote: >=20 > =EF=BB=BFThe following commit has been merged into the x86/fpu branch of t= ip: >=20 > Commit-ID: bbc55341b9c67645d1a5471506370caf7dd4a203 > Gitweb: https://git.kernel.org/tip/bbc55341b9c67645d1a5471506370caf= 7dd4a203 > Author: Sebastian Andrzej Siewior > AuthorDate: Fri, 20 Dec 2019 20:59:06 +01:00 > Committer: Borislav Petkov > CommitterDate: Tue, 07 Jan 2020 13:44:42 +01:00 >=20 > x86/fpu: Deactivate FPU state after failure during state load >=20 > In __fpu__restore_sig(), fpu_fpregs_owner_ctx needs to be reset if the > FPU state was not fully restored. Otherwise the following may happen (on > the same CPU): >=20 > Task A Task B fpu_fpregs_owner_ctx > *active* A.fpu > __fpu__restore_sig() > ctx switch load B.fpu > *active* B.fpu > fpregs_lock() > copy_user_to_fpregs_zeroing() > copy_kernel_to_xregs() *modify* > copy_user_to_xregs() *fails* > fpregs_unlock() > ctx switch skip loading B.fpu, > *active* B.fpu >=20 > In the success case, fpu_fpregs_owner_ctx is set to the current task. >=20 > In the failure case, the FPU state might have been modified by loading > the init state. >=20 > In this case, fpu_fpregs_owner_ctx needs to be reset in order to ensure > that the FPU state of the following task is loaded from saved state (and > not skipped because it was the previous state). >=20 > Reset fpu_fpregs_owner_ctx after a failure during restore occurred, to > ensure that the FPU state for the next task is always loaded. >=20 > The problem was debugged-by Yu-cheng Yu . Wow, __fpu__restore_sig is a mess. We have __copy_from... that is Obviously I= ncorrect (tm) even though it=E2=80=99s not obviously exploitable. (It=E2=80=99= s wrong because the *wrong pointer* is checked with access_ok().). We have a= fast path that will execute just enough of the time to make debugging the s= low path really annoying. (We should probably delete the fast path.) There a= re pagefault_disable() call in there mostly to confuse people. (So we take a= fault and sleep =E2=80=94 big deal. We have temporarily corrupt state, but= no one will ever read it. The retry after sleeping will clobber xstate, bu= t lazy save is long gone and this should be fine now. The real issue is tha= t, if we=E2=80=99re preempted after a successful a successful restore, then t= he new state will get lost.) So either we should delete the fast path or we should make it work reliably a= nd delete the slow path. And we should get rid of the __copy. And we should= have some test cases. BTW, how was the bug in here discovered? It looks like it only affects sign= al restore failure, which is usually not survivable unless the user program i= s really trying. >=20 > [ bp: Massage commit message. ] >=20 > Fixes: 5f409e20b7945 ("x86/fpu: Defer FPU state load until return to users= pace") > Reported-by: Yu-cheng Yu > Signed-off-by: Sebastian Andrzej Siewior > Signed-off-by: Borislav Petkov > Cc: Andy Lutomirski > Cc: Dave Hansen > Cc: Fenghua Yu > Cc: "H. Peter Anvin" > Cc: Ingo Molnar > Cc: Jann Horn > Cc: Peter Zijlstra > Cc: "Ravi V. Shankar" > Cc: Rik van Riel > Cc: Thomas Gleixner > Cc: Tony Luck > Cc: x86-ml > Link: https://lkml.kernel.org/r/20191220195906.plk6kpmsrikvbcfn@linutronix= .de > --- > arch/x86/kernel/fpu/signal.c | 3 +++ > 1 file changed, 3 insertions(+) >=20 > diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c > index 0071b79..400a05e 100644 > --- 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(); > } >=20 > @@ -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(); >=20 > err_out: