Re: [PATCH] x86/split_lock: Sanitize userspace and guest error output

From: Prarit Bhargava
Date: Fri Jun 05 2020 - 12:42:58 EST




On 6/5/20 11:29 AM, Xiaoyao Li wrote:
> On 6/5/2020 7:44 PM, Prarit Bhargava wrote:
>> 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.
>>
>> Signed-off-by: Prarit Bhargava <prarit@xxxxxxxxxx>
>> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
>> Cc: Borislav Petkov <bp@xxxxxxxxx>
>> Cc: x86@xxxxxxxxxx
>> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
>> Cc: Tony Luck <tony.luck@xxxxxxxxx>
>> Cc: "Peter Zijlstra (Intel)" <peterz@xxxxxxxxxxxxx>
>> Cc: Sean Christopherson <sean.j.christopherson@xxxxxxxxx>
>> Cc: Rahul Tanwar <rahul.tanwar@xxxxxxxxxxxxxxx>
>> Cc: Xiaoyao Li <xiaoyao.li@xxxxxxxxx>
>> Cc: Ricardo Neri <ricardo.neri-calderon@xxxxxxxxxxxxxxx>
>> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
>> ---
>> Â arch/x86/kernel/cpu/intel.c | 24 ++++++++++--------------
>> Â 1 file changed, 10 insertions(+), 14 deletions(-)
>>
>> diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c
>> index 166d7c355896..463022aa9b7a 100644
>> --- a/arch/x86/kernel/cpu/intel.c
>> +++ b/arch/x86/kernel/cpu/intel.c
>> @@ -1074,10 +1074,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 split_lock_warn(unsigned long ip, int fatal)
>> Â {
>> -ÂÂÂ pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ current->comm, current->pid, ip);
>> +ÂÂÂ 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 || fatal)
>> +ÂÂÂÂÂÂÂ return false;
>> Â ÂÂÂÂÂ /*
>> ÂÂÂÂÂÂ * Disable the split lock detection for this task so it can make
>> @@ -1086,18 +1090,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 +1107,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);
>
> It's incorrect. You change the behavior that it will print the split lock
> warning even when CPL 3 Alignment Check is turned on.

Do you want the message to be displayed in the fatal case of CPL 3 Alignment check?

P.