Skip to contentSkip to conversation

x86/split_lock: Sanitize userspace and guest error output

PATCH Superseded Prarit Bhargava 2 participants 5 messages Jun 8, 2020 – Jun 11, 2020 x86

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/split_lock: Sanitize userspace and guest error output

There are two problems with kernel messages in fatal mode that were found during testing of guests and userspace programs.

The first is that no kernel message is output when the split lock detector is triggered with a userspace program. As a result the userspace process dies from receiving SIGBUS with no indication to the user of what caused the process to die.

The second problem is that only the first triggering guest causes a kernel message to be output because the message is output with pr_warn_once(). This also results in a loss of information to the user.

While fixing these I noticed that the same message was being output three times so I'm cleaning that up too.

Fix fatal mode output, and use consistent messages for fatal and warn modes for both userspace and guests.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Tony Luck <[email protected]>
Cc "Peter Zijlstra (Intel)" <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Rahul Tanwar <[email protected]>
Cc Xiaoyao Li <[email protected]>
Cc Ricardo Neri <[email protected]>
Cc Dave Hansen <[email protected]>

---
v2: Do not output a message if CPL 3 Alignment Check is turned on (xiaoyao.li)

 arch/x86/kernel/cpu/intel.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/intel.c +13 -14
index 166d7c355896..e02ec81fe1eb 100644
@@ -1074,10 +1074,17 @@ static void split_lock_init(void)
 	split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool split_lock_warn(unsigned long ip, int fatal_no_warn)
 {
-	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
-			    current->comm, current->pid, ip);
+	if (fatal_no_warn)
+		return false;
+
+	pr_warn_ratelimited("#AC: %s/%d %ssplit_lock trap at address: 0x%lx\n",
+			    current->comm, current->pid,
+			    sld_state == sld_fatal ? "fatal " : "", ip);
+
+	if (sld_state == sld_fatal)
+		return false;
 
 	/*
 	 * Disable the split lock detection for this task so it can make
@@ -1086,18 +1093,13 @@ static void split_lock_warn(unsigned long ip)
 	 */
 	sld_update_msr(false);
 	set_tsk_thread_flag(current, TIF_SLD);
+	return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-	if (sld_state == sld_warn) {
-		split_lock_warn(ip);
+	if (split_lock_warn(ip, 0))
 		return true;
-	}
-
-	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-		     current->comm, current->pid,
-		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
 	current->thread.error_code = 0;
 	current->thread.trap_nr = X86_TRAP_AC;
@@ -1108,10 +1110,7 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
-		return false;
-	split_lock_warn(regs->ip);
-	return true;
+	return split_lock_warn(regs->ip, regs->flags & X86_EFLAGS_AC);
 }
 
 /*
Signature · 1 line
2.21.3
#1 lore raw
Quoted 5 lines · Prarit Bhargava
arch/x86/kernel/cpu/intel.c +0 -0
index 166d7c355896..e02ec81fe1eb 100644

arch/x86/kernel/cpu/intel.c static void split_lock_init(void) · line 1080

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +3 -3
@@ -1074,10 +1074,17 @@ static void split_lock_init(void)
 	split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool split_lock_warn(unsigned long ip, int fatal_no_warn)
 {
-	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
-			    current->comm, current->pid, ip);
+	if (fatal_no_warn)
+		return false;

This misses the point Xiaoyao was making. If EFLAGS.AC=1 then the #AC is a legacy alignment check fault and should not be treated as a split-lock #AC. The basic premise of the patch makes sense, but the end result is confusing because incorporating "fatal" and the EFLAGS.AC state into split_lock_warn() bastardizes both the "split_lock" and "warn" aspects of the function.

E.g. something like this yields the same net effect, it's just organized differently. If so desired, the "bogus" message could be dropped via Xiaoyao's prep patch[*] so that this change would only affect the sld_fatal messages.

[*] https://lkml.kernel.org/r/[email protected]

arch/x86/kernel/cpu/intel.c +9 -11
index 23fd5f319908..1aad0b8e394c 100644
@@ -1071,11 +1071,14 @@ static void split_lock_init(void)
        split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool handle_split_lock(unsigned long ip)
 {
        pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
                            current->comm, current->pid, ip);
 
+       if (sld_state != sld_warn)
+               return false;
+
        /*
         * Disable the split lock detection for this task so it can make
         * progress and set TIF_SLD so the detection is re-enabled via
@@ -1083,18 +1086,13 @@ static void split_lock_warn(unsigned long ip)
         */
        sld_update_msr(false);
        set_tsk_thread_flag(current, TIF_SLD);
+       return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-       if (sld_state == sld_warn) {
-               split_lock_warn(ip);
+       if (handle_split_lock(ip))
                return true;
-       }
-
-       pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-                    current->comm, current->pid,
-                    sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
        current->thread.error_code = 0;
        current->thread.trap_nr = X86_TRAP_AC;
@@ -1105,10 +1103,10 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-       if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
+       if (regs->flags & X86_EFLAGS_AC)
                return false;
-       split_lock_warn(regs->ip);
-       return true;
+
+       return handle_split_lock(regs->ip);
 }
 
 /*
Quoted 46 lines
arch/x86/kernel/cpu/intel.c +10 -11
+
+	pr_warn_ratelimited("#AC: %s/%d %ssplit_lock trap at address: 0x%lx\n",
+			    current->comm, current->pid,
+			    sld_state == sld_fatal ? "fatal " : "", ip);
+
+	if (sld_state == sld_fatal)
+		return false;
 
 	/*
 	 * Disable the split lock detection for this task so it can make
@@ -1086,18 +1093,13 @@ static void split_lock_warn(unsigned long ip)
 	 */
 	sld_update_msr(false);
 	set_tsk_thread_flag(current, TIF_SLD);
+	return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-	if (sld_state == sld_warn) {
-		split_lock_warn(ip);
+	if (split_lock_warn(ip, 0))
 		return true;
-	}
-
-	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-		     current->comm, current->pid,
-		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
 	current->thread.error_code = 0;
 	current->thread.trap_nr = X86_TRAP_AC;
@@ -1108,10 +1110,7 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
-		return false;
-	split_lock_warn(regs->ip);
-	return true;
+	return split_lock_warn(regs->ip, regs->flags & X86_EFLAGS_AC);
 }
 
 /*
Signature · 1 line
2.21.3
#2 lore raw
Quoted 23 lines · Sean Christopherson
Quoted 5 lines · Prarit Bhargava
arch/x86/kernel/cpu/intel.c +0 -0
index 166d7c355896..e02ec81fe1eb 100644

arch/x86/kernel/cpu/intel.c static void split_lock_init(void) · line 1080

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +3 -3
@@ -1074,10 +1074,17 @@ static void split_lock_init(void)
 	split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool split_lock_warn(unsigned long ip, int fatal_no_warn)
 {
-	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
-			    current->comm, current->pid, ip);
+	if (fatal_no_warn)
+		return false;

This misses the point Xiaoyao was making. If EFLAGS.AC=1 then the #AC is a legacy alignment check fault and should not be treated as a split-lock #AC. The basic premise of the patch makes sense, but the end result is confusing because incorporating "fatal" and the EFLAGS.AC state into split_lock_warn() bastardizes both the "split_lock" and "warn" aspects of the function.

Quoted 7 lines

E.g. something like this yields the same net effect, it's just organized differently. If so desired, the "bogus" message could be dropped via Xiaoyao's prep patch[*] so that this change would only affect the sld_fatal messages.

[*] https://lkml.kernel.org/r/[email protected]

Sean, I'll just go with your patch below. It's good enough. I'll add a Signed-off-by from you as well.

P.

Quoted 105 lines
arch/x86/kernel/cpu/intel.c +9 -11
index 23fd5f319908..1aad0b8e394c 100644
@@ -1071,11 +1071,14 @@ static void split_lock_init(void)
        split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool handle_split_lock(unsigned long ip)
 {
        pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
                            current->comm, current->pid, ip);
 
+       if (sld_state != sld_warn)
+               return false;
+
        /*
         * Disable the split lock detection for this task so it can make
         * progress and set TIF_SLD so the detection is re-enabled via
@@ -1083,18 +1086,13 @@ static void split_lock_warn(unsigned long ip)
         */
        sld_update_msr(false);
        set_tsk_thread_flag(current, TIF_SLD);
+       return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-       if (sld_state == sld_warn) {
-               split_lock_warn(ip);
+       if (handle_split_lock(ip))
                return true;
-       }
-
-       pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-                    current->comm, current->pid,
-                    sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
        current->thread.error_code = 0;
        current->thread.trap_nr = X86_TRAP_AC;
@@ -1105,10 +1103,10 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-       if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
+       if (regs->flags & X86_EFLAGS_AC)
                return false;
-       split_lock_warn(regs->ip);
-       return true;
+
+       return handle_split_lock(regs->ip);
 }
 
 /*
Quoted 48 lines
arch/x86/kernel/cpu/intel.c +10 -11
+
+	pr_warn_ratelimited("#AC: %s/%d %ssplit_lock trap at address: 0x%lx\n",
+			    current->comm, current->pid,
+			    sld_state == sld_fatal ? "fatal " : "", ip);
+
+	if (sld_state == sld_fatal)
+		return false;
 
 	/*
 	 * Disable the split lock detection for this task so it can make
@@ -1086,18 +1093,13 @@ static void split_lock_warn(unsigned long ip)
 	 */
 	sld_update_msr(false);
 	set_tsk_thread_flag(current, TIF_SLD);
+	return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-	if (sld_state == sld_warn) {
-		split_lock_warn(ip);
+	if (split_lock_warn(ip, 0))
 		return true;
-	}
-
-	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-		     current->comm, current->pid,
-		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
 	current->thread.error_code = 0;
 	current->thread.trap_nr = X86_TRAP_AC;
@@ -1108,10 +1110,7 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
-		return false;
-	split_lock_warn(regs->ip);
-	return true;
+	return split_lock_warn(regs->ip, regs->flags & X86_EFLAGS_AC);
 }
 
 /*
Signature · 1 line
2.21.3
#3 lore raw
In reply to Sean Christopherson
Quoted 23 lines · Sean Christopherson
Quoted 5 lines · Prarit Bhargava
arch/x86/kernel/cpu/intel.c +0 -0
index 166d7c355896..e02ec81fe1eb 100644

arch/x86/kernel/cpu/intel.c static void split_lock_init(void) · line 1080

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +3 -3
@@ -1074,10 +1074,17 @@ static void split_lock_init(void)
 	split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool split_lock_warn(unsigned long ip, int fatal_no_warn)
 {
-	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
-			    current->comm, current->pid, ip);
+	if (fatal_no_warn)
+		return false;

This misses the point Xiaoyao was making. If EFLAGS.AC=1 then the #AC is a legacy alignment check fault and should not be treated as a split-lock #AC. The basic premise of the patch makes sense, but the end result is confusing because incorporating "fatal" and the EFLAGS.AC state into split_lock_warn() bastardizes both the "split_lock" and "warn" aspects of the function.

Quoted 8 lines

E.g. something like this yields the same net effect, it's just organized differently. If so desired, the "bogus" message could be dropped via Xiaoyao's prep patch[*] so that this change would only affect the sld_fatal messages.

[*] https://lkml.kernel.org/r/[email protected]

Sean, I will just take your patch to make things easy. I will add you as a Signed-off-by.

/me is testing the patch right now

P.

Quoted 104 lines
arch/x86/kernel/cpu/intel.c +9 -11
index 23fd5f319908..1aad0b8e394c 100644
@@ -1071,11 +1071,14 @@ static void split_lock_init(void)
        split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool handle_split_lock(unsigned long ip)
 {
        pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
                            current->comm, current->pid, ip);
 
+       if (sld_state != sld_warn)
+               return false;
+
        /*
         * Disable the split lock detection for this task so it can make
         * progress and set TIF_SLD so the detection is re-enabled via
@@ -1083,18 +1086,13 @@ static void split_lock_warn(unsigned long ip)
         */
        sld_update_msr(false);
        set_tsk_thread_flag(current, TIF_SLD);
+       return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-       if (sld_state == sld_warn) {
-               split_lock_warn(ip);
+       if (handle_split_lock(ip))
                return true;
-       }
-
-       pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-                    current->comm, current->pid,
-                    sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
        current->thread.error_code = 0;
        current->thread.trap_nr = X86_TRAP_AC;
@@ -1105,10 +1103,10 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-       if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
+       if (regs->flags & X86_EFLAGS_AC)
                return false;
-       split_lock_warn(regs->ip);
-       return true;
+
+       return handle_split_lock(regs->ip);
 }
 
 /*
Quoted 48 lines
arch/x86/kernel/cpu/intel.c +10 -11
+
+	pr_warn_ratelimited("#AC: %s/%d %ssplit_lock trap at address: 0x%lx\n",
+			    current->comm, current->pid,
+			    sld_state == sld_fatal ? "fatal " : "", ip);
+
+	if (sld_state == sld_fatal)
+		return false;
 
 	/*
 	 * Disable the split lock detection for this task so it can make
@@ -1086,18 +1093,13 @@ static void split_lock_warn(unsigned long ip)
 	 */
 	sld_update_msr(false);
 	set_tsk_thread_flag(current, TIF_SLD);
+	return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-	if (sld_state == sld_warn) {
-		split_lock_warn(ip);
+	if (split_lock_warn(ip, 0))
 		return true;
-	}
-
-	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-		     current->comm, current->pid,
-		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
 	current->thread.error_code = 0;
 	current->thread.trap_nr = X86_TRAP_AC;
@@ -1108,10 +1110,7 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
-		return false;
-	split_lock_warn(regs->ip);
-	return true;
+	return split_lock_warn(regs->ip, regs->flags & X86_EFLAGS_AC);
 }
 
 /*
Signature · 1 line
2.21.3
#4 lore raw
Quoted 31 lines · Prarit Bhargava
Quoted 28 lines · Sean Christopherson
Quoted 5 lines · Prarit Bhargava
arch/x86/kernel/cpu/intel.c +0 -0
index 166d7c355896..e02ec81fe1eb 100644

arch/x86/kernel/cpu/intel.c static void split_lock_init(void) · line 1080

Quoted 10 lines
arch/x86/kernel/cpu/intel.c +3 -3
@@ -1074,10 +1074,17 @@ static void split_lock_init(void)
 	split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool split_lock_warn(unsigned long ip, int fatal_no_warn)
 {
-	pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
-			    current->comm, current->pid, ip);
+	if (fatal_no_warn)
+		return false;

This misses the point Xiaoyao was making. If EFLAGS.AC=1 then the #AC is a legacy alignment check fault and should not be treated as a split-lock #AC. The basic premise of the patch makes sense, but the end result is confusing because incorporating "fatal" and the EFLAGS.AC state into split_lock_warn() bastardizes both the "split_lock" and "warn" aspects of the function.

E.g. something like this yields the same net effect, it's just organized differently. If so desired, the "bogus" message could be dropped via Xiaoyao's prep patch[*] so that this change would only affect the sld_fatal messages.

Quoted 8 lines
Quoted 3 lines

[*] https://lkml.kernel.org/r/[email protected]

Sean, I will just take your patch to make things easy. I will add you as a Signed-off-by.

/me is testing the patch right now

Sure, here's an official SOB if it happens to work.

Quoted 105 lines
Quoted 104 lines
arch/x86/kernel/cpu/intel.c +9 -11
index 23fd5f319908..1aad0b8e394c 100644
@@ -1071,11 +1071,14 @@ static void split_lock_init(void)
        split_lock_verify_msr(sld_state != sld_off);
 }
 
-static void split_lock_warn(unsigned long ip)
+static bool handle_split_lock(unsigned long ip)
 {
        pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
                            current->comm, current->pid, ip);
 
+       if (sld_state != sld_warn)
+               return false;
+
        /*
         * Disable the split lock detection for this task so it can make
         * progress and set TIF_SLD so the detection is re-enabled via
@@ -1083,18 +1086,13 @@ static void split_lock_warn(unsigned long ip)
         */
        sld_update_msr(false);
        set_tsk_thread_flag(current, TIF_SLD);
+       return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-       if (sld_state == sld_warn) {
-               split_lock_warn(ip);
+       if (handle_split_lock(ip))
                return true;
-       }
-
-       pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-                    current->comm, current->pid,
-                    sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
        current->thread.error_code = 0;
        current->thread.trap_nr = X86_TRAP_AC;
@@ -1105,10 +1103,10 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-       if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
+       if (regs->flags & X86_EFLAGS_AC)
                return false;
-       split_lock_warn(regs->ip);
-       return true;
+
+       return handle_split_lock(regs->ip);
 }
 
 /*
Quoted 48 lines
arch/x86/kernel/cpu/intel.c +10 -11
+
+	pr_warn_ratelimited("#AC: %s/%d %ssplit_lock trap at address: 0x%lx\n",
+			    current->comm, current->pid,
+			    sld_state == sld_fatal ? "fatal " : "", ip);
+
+	if (sld_state == sld_fatal)
+		return false;
 
 	/*
 	 * Disable the split lock detection for this task so it can make
@@ -1086,18 +1093,13 @@ static void split_lock_warn(unsigned long ip)
 	 */
 	sld_update_msr(false);
 	set_tsk_thread_flag(current, TIF_SLD);
+	return true;
 }
 
 bool handle_guest_split_lock(unsigned long ip)
 {
-	if (sld_state == sld_warn) {
-		split_lock_warn(ip);
+	if (split_lock_warn(ip, 0))
 		return true;
-	}
-
-	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
-		     current->comm, current->pid,
-		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
 
 	current->thread.error_code = 0;
 	current->thread.trap_nr = X86_TRAP_AC;
@@ -1108,10 +1110,7 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);
 
 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
 {
-	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
-		return false;
-	split_lock_warn(regs->ip);
-	return true;
+	return split_lock_warn(regs->ip, regs->flags & X86_EFLAGS_AC);
 }
 
 /*
Signature · 1 line
2.21.3