All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86, fpu: fix 32-bit signal frame handling
@ 2015-11-11  0:23 Dave Hansen
  2015-11-12 13:28 ` [tip:x86/urgent] x86/fpu: Fix " tip-bot for Dave Hansen
  2015-11-12 22:50 ` [PATCH] x86, fpu: fix " Dave Hansen
  0 siblings, 2 replies; 4+ messages in thread
From: Dave Hansen @ 2015-11-11  0:23 UTC (permalink / raw)
  To: linux-kernel; +Cc: dave, dave.hansen, x86, luto, fenghua.yu, yu-cheng.yu


From: Dave Hansen <dave.hansen@linux.intel.com>

(This should have gone to LKML originally. Sorry for the extra
 noise, folks on the cc.)

Background:

Signal frames on x86 have two formats:
1. For 32-bit executables (whether on a real 32-bit kernel or
   under ia32 emulation on a 64-bit kernel) we have a
  'fpregset_t' that includes the "FSAVE" registers.
2. For 64-bit executables (on 64-bit kernels obviously), the
   'fpregset_t' is smaller and does not contain the "FSAVE"
   state.

When creating the signal frame, we have to be aware of whether
we are running a 32 or 64-bit executable so we create the correct
format signal frame.

Problem:

save_xstate_epilog() uses 'fx_sw_reserved_ia32' whenever it is
called for a 32-bit executable.  This is for real 32-bit and
ia32 emulation.

But, fpu__init_prepare_fx_sw_frame() only initializes
'fx_sw_reserved_ia32' when emulation is enabled, *NOT* for real
32-bit kernels.

This leads to really wierd situations where 32-bit programs
lose their extended state when returning from a signal handler.
The kernel copies the uninitialized (zero) 'fx_sw_reserved_ia32'
out to userspace in save_xstate_epilog().  But when returning
from the signal, the kernel errors out in check_for_xstate()
when it does not see FP_XSTATE_MAGIC1 present (because it was
zeroed).  This leads to the FPU/XSAVE state being initialized.

For MPX, this leads to the most permissive state and means we
silently lose bounds violations.  I think this would also mean
that we could lose *ANY* FPU/SSE/AVX state.  I'm not sure why
no one has spotted this bug.

I believe this was broken by:

	72a671ced: x86, fpu: Unify signal handling code paths for x86 and x86_64 kernels

way back in 2012.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: x86@kernel.org
Cc: luto@kernel.org
Cc: fenghua.yu@intel.com
Cc: yu-cheng.yu@intel.com
---

 b/arch/x86/kernel/fpu/signal.c |   11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff -puN arch/x86/kernel/fpu/signal.c~only-fix-signal-bug arch/x86/kernel/fpu/signal.c
--- a/arch/x86/kernel/fpu/signal.c~only-fix-signal-bug	2015-11-10 14:30:19.910135621 -0800
+++ b/arch/x86/kernel/fpu/signal.c	2015-11-10 14:30:19.913135757 -0800
@@ -385,20 +385,19 @@ fpu__alloc_mathframe(unsigned long sp, i
  */
 void fpu__init_prepare_fx_sw_frame(void)
 {
-	int fsave_header_size = sizeof(struct fregs_state);
 	int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
 
-	if (config_enabled(CONFIG_X86_32))
-		size += fsave_header_size;
-
 	fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
 	fx_sw_reserved.extended_size = size;
 	fx_sw_reserved.xfeatures = xfeatures_mask;
 	fx_sw_reserved.xstate_size = xstate_size;
 
-	if (config_enabled(CONFIG_IA32_EMULATION)) {
+	if (config_enabled(CONFIG_IA32_EMULATION) ||
+	    config_enabled(CONFIG_X86_32)) {
+		int fsave_header_size = sizeof(struct fregs_state);
+
 		fx_sw_reserved_ia32 = fx_sw_reserved;
-		fx_sw_reserved_ia32.extended_size += fsave_header_size;
+		fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
 	}
 }
 
_

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip:x86/urgent] x86/fpu: Fix 32-bit signal frame handling
  2015-11-11  0:23 [PATCH] x86, fpu: fix 32-bit signal frame handling Dave Hansen
@ 2015-11-12 13:28 ` tip-bot for Dave Hansen
  2015-11-12 22:50 ` [PATCH] x86, fpu: fix " Dave Hansen
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot for Dave Hansen @ 2015-11-12 13:28 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: mingo, linux-kernel, bp, torvalds, peterz, dvlasenk, brgerst,
	luto, tglx, dave.hansen, stable, hpa

Commit-ID:  ab6b52947545a5355154f64f449f97af9d05845f
Gitweb:     http://git.kernel.org/tip/ab6b52947545a5355154f64f449f97af9d05845f
Author:     Dave Hansen <dave.hansen@linux.intel.com>
AuthorDate: Tue, 10 Nov 2015 16:23:54 -0800
Committer:  Ingo Molnar <mingo@kernel.org>
CommitDate: Thu, 12 Nov 2015 09:23:45 +0100

x86/fpu: Fix 32-bit signal frame handling

(This should have gone to LKML originally. Sorry for the extra
 noise, folks on the cc.)

Background:

Signal frames on x86 have two formats:

  1. For 32-bit executables (whether on a real 32-bit kernel or
     under 32-bit emulation on a 64-bit kernel) we have a
    'fpregset_t' that includes the "FSAVE" registers.

  2. For 64-bit executables (on 64-bit kernels obviously), the
     'fpregset_t' is smaller and does not contain the "FSAVE"
     state.

When creating the signal frame, we have to be aware of whether
we are running a 32 or 64-bit executable so we create the
correct format signal frame.

Problem:

save_xstate_epilog() uses 'fx_sw_reserved_ia32' whenever it is
called for a 32-bit executable.  This is for real 32-bit and
ia32 emulation.

But, fpu__init_prepare_fx_sw_frame() only initializes
'fx_sw_reserved_ia32' when emulation is enabled, *NOT* for real
32-bit kernels.

This leads to really wierd situations where 32-bit programs
lose their extended state when returning from a signal handler.
The kernel copies the uninitialized (zero) 'fx_sw_reserved_ia32'
out to userspace in save_xstate_epilog().  But when returning
from the signal, the kernel errors out in check_for_xstate()
when it does not see FP_XSTATE_MAGIC1 present (because it was
zeroed).  This leads to the FPU/XSAVE state being initialized.

For MPX, this leads to the most permissive state and means we
silently lose bounds violations.  I think this would also mean
that we could lose *ANY* FPU/SSE/AVX state.  I'm not sure why
no one has spotted this bug.

I believe this was broken by:

	72a671ced66d ("x86, fpu: Unify signal handling code paths for x86 and x86_64 kernels")

way back in 2012.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: <stable@vger.kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: dave@sr71.net
Cc: fenghua.yu@intel.com
Cc: yu-cheng.yu@intel.com
Link: http://lkml.kernel.org/r/20151111002354.A0799571@viggo.jf.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
---
 arch/x86/kernel/fpu/signal.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index ef29b74..31c6a60 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -385,20 +385,19 @@ fpu__alloc_mathframe(unsigned long sp, int ia32_frame,
  */
 void fpu__init_prepare_fx_sw_frame(void)
 {
-	int fsave_header_size = sizeof(struct fregs_state);
 	int size = xstate_size + FP_XSTATE_MAGIC2_SIZE;
 
-	if (config_enabled(CONFIG_X86_32))
-		size += fsave_header_size;
-
 	fx_sw_reserved.magic1 = FP_XSTATE_MAGIC1;
 	fx_sw_reserved.extended_size = size;
 	fx_sw_reserved.xfeatures = xfeatures_mask;
 	fx_sw_reserved.xstate_size = xstate_size;
 
-	if (config_enabled(CONFIG_IA32_EMULATION)) {
+	if (config_enabled(CONFIG_IA32_EMULATION) ||
+	    config_enabled(CONFIG_X86_32)) {
+		int fsave_header_size = sizeof(struct fregs_state);
+
 		fx_sw_reserved_ia32 = fx_sw_reserved;
-		fx_sw_reserved_ia32.extended_size += fsave_header_size;
+		fx_sw_reserved_ia32.extended_size = size + fsave_header_size;
 	}
 }
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86, fpu: fix 32-bit signal frame handling
  2015-11-11  0:23 [PATCH] x86, fpu: fix 32-bit signal frame handling Dave Hansen
  2015-11-12 13:28 ` [tip:x86/urgent] x86/fpu: Fix " tip-bot for Dave Hansen
@ 2015-11-12 22:50 ` Dave Hansen
  2015-11-27 10:06   ` Ingo Molnar
  1 sibling, 1 reply; 4+ messages in thread
From: Dave Hansen @ 2015-11-12 22:50 UTC (permalink / raw)
  To: linux-kernel; +Cc: dave.hansen, x86, luto, fenghua.yu, yu-cheng.yu

On 11/10/2015 04:23 PM, Dave Hansen wrote:
> For MPX, this leads to the most permissive state and means we
> silently lose bounds violations.  I think this would also mean
> that we could lose *ANY* FPU/SSE/AVX state.  I'm not sure why
> no one has spotted this bug.

FWIW, I looked at this a little more today.

We lose all extended state for our "extended xfeatures", also known as
state component numbers >=2 (AVX, MPX, AVX-512, PKEYs)...  But we retain
the state for FP/SSE state.  So we lose the top half of the AVX
registers (the bottom half are SSE state).

I also did a little objdump'ing and grep'ing in a 32-bit distro.
There's no sign of actual use of the ymm registers.

Basically, it appears nobody has taken a 64-bit Sandybridge or later
CPU, put a 32-bit distro on it that had a >=3.7 kernel on it and tried
to use AVX instructions.  Or, if they did, they got random corruption
and gave up before actually diagnosing the problem. :)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86, fpu: fix 32-bit signal frame handling
  2015-11-12 22:50 ` [PATCH] x86, fpu: fix " Dave Hansen
@ 2015-11-27 10:06   ` Ingo Molnar
  0 siblings, 0 replies; 4+ messages in thread
From: Ingo Molnar @ 2015-11-27 10:06 UTC (permalink / raw)
  To: Dave Hansen; +Cc: linux-kernel, dave.hansen, x86, luto, fenghua.yu, yu-cheng.yu


* Dave Hansen <dave@sr71.net> wrote:

> On 11/10/2015 04:23 PM, Dave Hansen wrote:
> > For MPX, this leads to the most permissive state and means we
> > silently lose bounds violations.  I think this would also mean
> > that we could lose *ANY* FPU/SSE/AVX state.  I'm not sure why
> > no one has spotted this bug.
> 
> FWIW, I looked at this a little more today.
> 
> We lose all extended state for our "extended xfeatures", also known as
> state component numbers >=2 (AVX, MPX, AVX-512, PKEYs)...  But we retain
> the state for FP/SSE state.  So we lose the top half of the AVX
> registers (the bottom half are SSE state).
> 
> I also did a little objdump'ing and grep'ing in a 32-bit distro.
> There's no sign of actual use of the ymm registers.
> 
> Basically, it appears nobody has taken a 64-bit Sandybridge or later
> CPU, put a 32-bit distro on it that had a >=3.7 kernel on it and tried
> to use AVX instructions.  Or, if they did, they got random corruption
> and gave up before actually diagnosing the problem. :)

Weird: putting a 32-bit distro on such a fine piece of 64-bit hardware is pure 
masochism - and such masochism would also imply the willingness to track down 
random corruptions! ;-)

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-11-27 10:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-11  0:23 [PATCH] x86, fpu: fix 32-bit signal frame handling Dave Hansen
2015-11-12 13:28 ` [tip:x86/urgent] x86/fpu: Fix " tip-bot for Dave Hansen
2015-11-12 22:50 ` [PATCH] x86, fpu: fix " Dave Hansen
2015-11-27 10:06   ` Ingo Molnar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.