Skip to contentSkip to conversation

xen/x86: don't corrupt %eip when returning from a signal handler

PATCH Applied David Vrabel 3 participants 5 messages Oct 17, 2012 – Oct 19, 2012 x86

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/x86: don't corrupt %eip when returning from a signal handler

From: David Vrabel <[email protected]>

In 32 bit guests, if a userspace process has %eax == -ERESTARTSYS (-512) or -ERESTARTNOINTR (-513) when it is interrupted by an event /and/ the process has a pending signal then %eip (and %eax) are corrupted when returning to the main process after handling the signal. The application may then crash with SIGSEGV or a SIGILL or it may have subtly incorrect behaviour (depending on what instruction it returned to).

The occurs because handle_signal() is incorrectly thinking that there is a system call that needs to restarted so it adjusts %eip and %eax to re-execute the system call instruction (even though user space had not done a system call).

If %eax == -514 (-ERESTARTNOHAND (-514) or -ERESTART_RESTARTBLOCK (-516) then handle_signal() only corrupted %eax (by setting it to -EINTR). This may cause the application to crash or have incorrect behaviour.

handle_signal() assumes that regs->orig_ax >= 0 means a system call so any kernel entry point that is not for a system call must push a negative value for orig_ax. For example, for physical interrupts on bare metal the inverse of the vector is pushed and page_fault() sets regs->orig_ax to -1, overwriting the hardware provided error code.

xen_hypervisor_callback() was incorrectly pushing 0 for orig_ax instead of -1.

Classic Xen kernels pushed %eax which works as %eax cannot be both non-negative and -RESTARTSYS (etc.), but using -1 is consistent with other non-system call entry points.

There were similar bugs in xen_failsafe_callback(), if the fault was corrected and normal return path was used. 64 bit guests would push 0 which is broken. 32 bit guests would push %eax which is safe (see previous paragraph), but for consistency this is also changed to -1.

---
 arch/x86/kernel/entry_32.S |    4 ++--
 arch/x86/kernel/entry_64.S |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_32.S +2 -2
index 2c63407..6a19e66 100644
@@ -1042,7 +1042,7 @@ ENTRY(xen_sysenter_target)
 
 ENTRY(xen_hypervisor_callback)
 	CFI_STARTPROC
-	pushl_cfi $0
+	pushl_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	TRACE_IRQS_OFF
 
@@ -1078,7 +1078,7 @@ ENDPROC(xen_hypervisor_callback)
 # We distinguish between categories by maintaining a status value in EAX.
 ENTRY(xen_failsafe_callback)
 	CFI_STARTPROC
-	pushl_cfi %eax
+	pushl_cfi $-1  /* orig_ax = -1 => not a system call */
 	movl $1,%eax
 1:	mov 4(%esp),%ds
 2:	mov 8(%esp),%es
arch/x86/kernel/entry_64.S +1 -1
index cdc790c..430b1fc 100644
@@ -1451,7 +1451,7 @@ ENTRY(xen_failsafe_callback)
 	CFI_RESTORE r11
 	addq $0x30,%rsp
 	CFI_ADJUST_CFA_OFFSET -0x30
-	pushq_cfi $0
+	pushq_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	jmp error_exit
 	CFI_ENDPROC
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 38 lines · David Vrabel

From: David Vrabel <[email protected]>

In 32 bit guests, if a userspace process has %eax == -ERESTARTSYS (-512) or -ERESTARTNOINTR (-513) when it is interrupted by an event /and/ the process has a pending signal then %eip (and %eax) are corrupted when returning to the main process after handling the signal. The application may then crash with SIGSEGV or a SIGILL or it may have subtly incorrect behaviour (depending on what instruction it returned to).

The occurs because handle_signal() is incorrectly thinking that there is a system call that needs to restarted so it adjusts %eip and %eax to re-execute the system call instruction (even though user space had not done a system call).

If %eax == -514 (-ERESTARTNOHAND (-514) or -ERESTART_RESTARTBLOCK (-516) then handle_signal() only corrupted %eax (by setting it to -EINTR). This may cause the application to crash or have incorrect behaviour.

handle_signal() assumes that regs->orig_ax >= 0 means a system call so any kernel entry point that is not for a system call must push a negative value for orig_ax. For example, for physical interrupts on bare metal the inverse of the vector is pushed and page_fault() sets regs->orig_ax to -1, overwriting the hardware provided error code.

xen_hypervisor_callback() was incorrectly pushing 0 for orig_ax instead of -1.

Classic Xen kernels pushed %eax which works as %eax cannot be both non-negative and -RESTARTSYS (etc.), but using -1 is consistent with other non-system call entry points.

There were similar bugs in xen_failsafe_callback(), if the fault was corrected and normal return path was used. 64 bit guests would push 0 which is broken. 32 bit guests would push %eax which is safe (see previous paragraph), but for consistency this is also changed to -1.

Quoted 4 lines

Applied.

Quoted 42 lines
---
 arch/x86/kernel/entry_32.S |    4 ++--
 arch/x86/kernel/entry_64.S |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_32.S +2 -2
index 2c63407..6a19e66 100644
@@ -1042,7 +1042,7 @@ ENTRY(xen_sysenter_target)
 
 ENTRY(xen_hypervisor_callback)
 	CFI_STARTPROC
-	pushl_cfi $0
+	pushl_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	TRACE_IRQS_OFF
 
@@ -1078,7 +1078,7 @@ ENDPROC(xen_hypervisor_callback)
 # We distinguish between categories by maintaining a status value in EAX.
 ENTRY(xen_failsafe_callback)
 	CFI_STARTPROC
-	pushl_cfi %eax
+	pushl_cfi $-1  /* orig_ax = -1 => not a system call */
 	movl $1,%eax
 1:	mov 4(%esp),%ds
 2:	mov 8(%esp),%es
arch/x86/kernel/entry_64.S +1 -1
index cdc790c..430b1fc 100644
@@ -1451,7 +1451,7 @@ ENTRY(xen_failsafe_callback)
 	CFI_RESTORE r11
 	addq $0x30,%rsp
 	CFI_ADJUST_CFA_OFFSET -0x30
-	pushq_cfi $0
+	pushq_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	jmp error_exit
 	CFI_ENDPROC
Signature · 1 line
1.7.2.5
#2 lore raw
In reply to David Vrabel
Quoted 62 lines
Quoted 1 line
Quoted 1 line · David Vrabel

From: David Vrabel <[email protected]>

In 32 bit guests, if a userspace process has %eax == -ERESTARTSYS (-512) or -ERESTARTNOINTR (-513) when it is interrupted by an event /and/ the process has a pending signal then %eip (and %eax) are corrupted when returning to the main process after handling the signal. The application may then crash with SIGSEGV or a SIGILL or it may have subtly incorrect behaviour (depending on what instruction it returned to).

The occurs because handle_signal() is incorrectly thinking that there is a system call that needs to restarted so it adjusts %eip and %eax to re-execute the system call instruction (even though user space had not done a system call).

If %eax == -514 (-ERESTARTNOHAND (-514) or -ERESTART_RESTARTBLOCK (-516) then handle_signal() only corrupted %eax (by setting it to -EINTR). This may cause the application to crash or have incorrect behaviour.

handle_signal() assumes that regs->orig_ax >= 0 means a system call so any kernel entry point that is not for a system call must push a negative value for orig_ax. For example, for physical interrupts on bare metal the inverse of the vector is pushed and page_fault() sets regs->orig_ax to -1, overwriting the hardware provided error code.

xen_hypervisor_callback() was incorrectly pushing 0 for orig_ax instead of -1.

Classic Xen kernels pushed %eax which works as %eax cannot be both non-negative and -RESTARTSYS (etc.), but using -1 is consistent with other non-system call entry points.

There were similar bugs in xen_failsafe_callback(), if the fault was corrected and normal return path was used. 64 bit guests would push 0 which is broken. 32 bit guests would push %eax which is safe (see previous paragraph), but for consistency this is also changed to -1.

---
 arch/x86/kernel/entry_32.S |    4 ++--
 arch/x86/kernel/entry_64.S |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/entry_32.S +1 -1
index 2c63407..6a19e66 100644
@@ -1042,7 +1042,7 @@ ENTRY(xen_sysenter_target)
 
 ENTRY(xen_hypervisor_callback)
 	CFI_STARTPROC
-	pushl_cfi $0
+	pushl_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	TRACE_IRQS_OFF
 

arch/x86/kernel/entry_32.S ENDPROC(xen_hypervisor_callback) · line 1081

Quoted 5 lines
arch/x86/kernel/entry_32.S +1 -1
@@ -1078,7 +1078,7 @@ ENDPROC(xen_hypervisor_callback)
 # We distinguish between categories by maintaining a status value in EAX.
 ENTRY(xen_failsafe_callback)
 	CFI_STARTPROC
-	pushl_cfi %eax
+	pushl_cfi $-1  /* orig_ax = -1 => not a system call */

While making this apply to the 2.6.18 tree, I noticed that you replaced the wrong push here, thus causing register corruption. Just like on the 64-bit side, the one that needs fixing is the one right before the SAVE_ALL (and hence it's again not just for consistency, as zero is being pushed there too).

Jan

Quoted 24 lines
arch/x86/kernel/entry_32.S +0 -0
 	movl $1,%eax
 1:	mov 4(%esp),%ds
 2:	mov 8(%esp),%es
arch/x86/kernel/entry_64.S +1 -1
index cdc790c..430b1fc 100644
@@ -1451,7 +1451,7 @@ ENTRY(xen_failsafe_callback)
 	CFI_RESTORE r11
 	addq $0x30,%rsp
 	CFI_ADJUST_CFA_OFFSET -0x30
-	pushq_cfi $0
+	pushq_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	jmp error_exit
 	CFI_ENDPROC
Signature · 7 lines
1.7.2.5


_______________________________________________
Xen-devel mailing list
[email protected] 
http://lists.xen.org/xen-devel 
#3 lore raw
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · David Vrabel

From: David Vrabel <[email protected]>

In 32 bit guests, if a userspace process has %eax == -ERESTARTSYS (-512) or -ERESTARTNOINTR (-513) when it is interrupted by an event /and/ the process has a pending signal then %eip (and %eax) are corrupted when returning to the main process after handling the signal. The application may then crash with SIGSEGV or a SIGILL or it may have subtly incorrect behaviour (depending on what instruction it returned to).

[...]

Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/entry_32.S +1 -1
index 2c63407..6a19e66 100644
@@ -1042,7 +1042,7 @@ ENTRY(xen_sysenter_target)
 
 ENTRY(xen_hypervisor_callback)
 	CFI_STARTPROC
-	pushl_cfi $0
+	pushl_cfi $-1 /* orig_ax = -1 => not a system call */
 	SAVE_ALL
 	TRACE_IRQS_OFF
 
Quoted 12 lines

arch/x86/kernel/entry_32.S ENDPROC(xen_hypervisor_callback) · line 1081

Quoted 6 lines
arch/x86/kernel/entry_32.S +1 -1
@@ -1078,7 +1078,7 @@ ENDPROC(xen_hypervisor_callback)
 # We distinguish between categories by maintaining a status value in EAX.
 ENTRY(xen_failsafe_callback)
 	CFI_STARTPROC
-	pushl_cfi %eax
+	pushl_cfi $-1  /* orig_ax = -1 => not a system call */

While making this apply to the 2.6.18 tree, I noticed that you replaced the wrong push here, thus causing register corruption. Just like on the 64-bit side, the one that needs fixing is the one right before the SAVE_ALL (and hence it's again not just for consistency, as zero is being pushed there too).

Oops.

We would have liked to test this path but could not see how to. Do you have any ideas?

David

#4 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · David Vrabel
Quoted 13 lines · Jan Beulich

arch/x86/kernel/entry_32.S ENDPROC(xen_hypervisor_callback) · line 1081

Quoted 7 lines
Quoted 1 line
Quoted 1 line · David Vrabel
arch/x86/kernel/entry_32.S +1 -1
@@ -1078,7 +1078,7 @@ ENDPROC(xen_hypervisor_callback)
 # We distinguish between categories by maintaining a status value in EAX.
 ENTRY(xen_failsafe_callback)
 	CFI_STARTPROC
-	pushl_cfi %eax
+	pushl_cfi $-1  /* orig_ax = -1 => not a system call */

While making this apply to the 2.6.18 tree, I noticed that you replaced the wrong push here, thus causing register corruption. Just like on the 64-bit side, the one that needs fixing is the one right before the SAVE_ALL (and hence it's again not just for consistency, as zero is being pushed there too).

Quoted 4 lines

Oops.

We would have liked to test this path but could not see how to. Do you have any ideas?

I'm not aware of a way to reliably trigger this without adding assisting code to the kernel.

Jan