Skip to contentSkip to conversation

x86/mpx: Correctly report do_mpx_bt_fault() failures to user-space

PATCH In review Joerg Roedel 4 participants 7 messages Apr 6, 2017 – Apr 21, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mpx: Correctly report do_mpx_bt_fault() failures to user-space

From: Joerg Roedel <[email protected]>

When this function fails it just sends a SIGSEGV signal to user-space using force_sig(). This signal is missing essential information about the cause, e.g. the trap_nr or an error code.

Fix this by propagating the error to the only caller of mpx_handle_bd_fault(), do_bounds(), which sends the correct SIGSEGV signal to the process.

Fixes fe3d197f84319 ('x86, mpx: On-demand kernel allocation of bounds tables')
---
 arch/x86/mm/mpx.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
arch/x86/mm/mpx.c +1 -9
index cd44ae7..1c34b76 100644
@@ -526,15 +526,7 @@ int mpx_handle_bd_fault(void)
 	if (!kernel_managing_mpx_tables(current->mm))
 		return -EINVAL;
 
-	if (do_mpx_bt_fault()) {
-		force_sig(SIGSEGV, current);
-		/*
-		 * The force_sig() is essentially "handling" this
-		 * exception, so we do not pass up the error
-		 * from do_mpx_bt_fault().
-		 */
-	}
-	return 0;
+	return do_mpx_bt_fault();
 }
 
 /*
Signature · 1 line
1.9.1
#1 lore raw
x86/mpx: Correctly report do_mpx_bt_fault() failures to user-space
Commit-ID:  5ed386ec09a5d75bcf073967e55e895c2607a5c3
Gitweb:     http://git.kernel.org/tip/5ed386ec09a5d75bcf073967e55e895c2607a5c3
Author:     Joerg Roedel <[email protected]>
AuthorDate: Thu, 6 Apr 2017 16:19:22 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Apr 2017 08:40:58 +0200

x86/mpx: Correctly report do_mpx_bt_fault() failures to user-space

When this function fails it just sends a SIGSEGV signal to user-space using force_sig(). This signal is missing essential information about the cause, e.g. the trap_nr or an error code.

Fix this by propagating the error to the only caller of mpx_handle_bd_fault(), do_bounds(), which sends the correct SIGSEGV signal to the process.

Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Fixes fe3d197f84319 ('x86, mpx: On-demand kernel allocation of bounds tables')
---
 arch/x86/mm/mpx.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
arch/x86/mm/mpx.c +1 -9
index cd44ae7..1c34b76 100644
@@ -526,15 +526,7 @@ int mpx_handle_bd_fault(void)
 	if (!kernel_managing_mpx_tables(current->mm))
 		return -EINVAL;
 
-	if (do_mpx_bt_fault()) {
-		force_sig(SIGSEGV, current);
-		/*
-		 * The force_sig() is essentially "handling" this
-		 * exception, so we do not pass up the error
-		 * from do_mpx_bt_fault().
-		 */
-	}
-	return 0;
+	return do_mpx_bt_fault();
 }
 
 /*
maintainer
#2 lore raw
In reply to Joerg Roedel

Hi Joerg,

Quoted 8 lines

When this function fails it just sends a SIGSEGV signal to user-space using force_sig(). This signal is missing essential information about the cause, e.g. the trap_nr or an error code.

Fix this by propagating the error to the only caller of mpx_handle_bd_fault(), do_bounds(), which sends the correct SIGSEGV signal to the process.

Just to be clear, the thing you're calling "correct" is this do_trap(), right?

        do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);
Quoted 1 line
Fixes fe3d197f84319 ('x86, mpx: On-demand kernel allocation of bounds

tables')

Quoted 14 lines
---
 arch/x86/mm/mpx.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
arch/x86/mm/mpx.c +0 -1
index cd44ae7..1c34b76 100644
@@ -526,15 +526,7 @@ int mpx_handle_bd_fault(void)
 	if (!kernel_managing_mpx_tables(current->mm))
 		return -EINVAL;
 
-	if (do_mpx_bt_fault()) {

arch/x86/mm/mpx.c int mpx_handle_bd_fault(void) · line 530

Quoted 10 lines
arch/x86/mm/mpx.c +1 -8
-		force_sig(SIGSEGV, current);
-		/*
-		 * The force_sig() is essentially "handling" this
-		 * exception, so we do not pass up the error
-		 * from do_mpx_bt_fault().
-		 */
-	}
-	return 0;
+	return do_mpx_bt_fault();
 }

do_mpx_bt_fault() can fail for a bunch of reasons:
* unexpected or invalid value in BNDCSR
* out of memory (physical or virtual)
* unresolvable fault walking/filling bounds tables
* !valid and non-empty bad entry in the bounds tables

This will end up sending a signal that *looks* like a X86_TRAP_BR for all of those, including those that are not really bounds-related, like unresolvable faults. We also don't populate enough information in the siginfo that gets delivered for userspace to resolve the fault.

I'm not sure this patch is the right thing.

#3 lore raw

Hi Dave,

Quoted 4 lines · Dave Hansen

Just to be clear, the thing you're calling "correct" is this do_trap(), right?

        do_trap(X86_TRAP_BR, SIGSEGV, "bounds", regs, error_code, NULL);

Yes, because it signals the right trap_nr and error_code to user-space.

Quoted 6 lines

do_mpx_bt_fault() can fail for a bunch of reasons:
* unexpected or invalid value in BNDCSR
* out of memory (physical or virtual)
* unresolvable fault walking/filling bounds tables
* !valid and non-empty bad entry in the bounds tables

Quoted 6 lines

This will end up sending a signal that *looks* like a X86_TRAP_BR for all of those, including those that are not really bounds-related, like unresolvable faults. We also don't populate enough information in the siginfo that gets delivered for userspace to resolve the fault.

I'm not sure this patch is the right thing.

The problem is, without this patch the trap_nr reported to user-space is 0, which maps to divide-by-zero. I think this is wrong, and since all failure cases from do_mpx_bt_fault() can only happen in the #BR exception handler, I think that reporting X86_TRAP_BR for all failure cases is the right thing to do.

I don't know whether user-space (with this patch) already gets enough information from do_trap() to handle all of the above cases, but it is a step in the right direction.

Joerg

maintainer
#4 lore raw
Quoted 11 lines · Joerg Roedel
Quoted 11 lines

do_mpx_bt_fault() can fail for a bunch of reasons:
* unexpected or invalid value in BNDCSR
* out of memory (physical or virtual)
* unresolvable fault walking/filling bounds tables
* !valid and non-empty bad entry in the bounds tables

This will end up sending a signal that *looks* like a X86_TRAP_BR for all of those, including those that are not really bounds-related, like unresolvable faults. We also don't populate enough information in the siginfo that gets delivered for userspace to resolve the fault.

Quoted 7 lines
Quoted 1 line

I'm not sure this patch is the right thing.

The problem is, without this patch the trap_nr reported to user-space is 0, which maps to divide-by-zero. I think this is wrong, and since all failure cases from do_mpx_bt_fault() can only happen in the #BR exception handler, I think that reporting X86_TRAP_BR for all failure cases is the right thing to do.

Urg, that does sound bogus.

How about doing X86_TRAP_PF? That would at least be consistent with SIGBUS, which is probably the closest thing to a generic error code that we have.

#5 lore raw
Quoted 3 lines · Dave Hansen

How about doing X86_TRAP_PF? That would at least be consistent with SIGBUS, which is probably the closest thing to a generic error code that we have.

Correct me if I am wrong, but for SIGBUS this only happens in the page-fault path, right? And this path is indeed entered on a #PF exception.

I see no reason to lie to user-space about the trap_nr that caused the SIGSEGV, especially since user-space software needs to be modified to make use of MPX, including the signal handler. So there is no risk of introducing any incompatibility or regression, no?

Joerg

maintainer
#6 lore raw
Quoted 7 lines · Joerg Roedel
Quoted 3 lines · Dave Hansen

How about doing X86_TRAP_PF? That would at least be consistent with SIGBUS, which is probably the closest thing to a generic error code that we have.

Correct me if I am wrong, but for SIGBUS this only happens in the page-fault path, right? And this path is indeed entered on a #PF exception.

It can happen to programs for tons of reasons. It definitely happens outside page faults.

Quoted 4 lines

I see no reason to lie to user-space about the trap_nr that caused the SIGSEGV, especially since user-space software needs to be modified to make use of MPX, including the signal handler. So there is no risk of introducing any incompatibility or regression, no?

I think it's pretty safe to change.