Three fixes all related to #DB:

- Handle the BTF bit correctly so it doesn't get lost due to a kernel #DB
 
  - Only clear and set the virtual DR6 value used by ptrace on user space
    triggered #DB. A kernel #DB must leave it alone to ensure data
    consistency for ptrace.
 
  - Make the bitmasking of the virtual DR6 storage correct so it does not
    lose DR_STEP.
 -----BEGIN PGP SIGNATURE-----
 
 iQJHBAABCgAxFiEEQp8+kY+LLUocC4bMphj1TA10mKEFAl+evlcTHHRnbHhAbGlu
 dXRyb25peC5kZQAKCRCmGPVMDXSYofaAD/4gzRDGRZiorwX60o+RuJjgBN/iS1BN
 SEpprlC2TGtdiOvGNKxMxwMZFbpSExgGtwku6xya1VDDLJS+NwartL0AEnCsei2e
 O4KqUiU6HuBsKi+M0LARVm8bHRzb1s3YjrFFMwrarTm40joOvxNiw2w+nvTtamfJ
 tlkQMp5/iX0KNO5FBLMPDDwVyE3xY+yAyiI0Z7bPmrhNSlNWW6x8QKoBpp8T5v2a
 ATIhAnOAClZVH/ig9A/fbUgsBqxyyIZRSW2wudAtHZg8NhDox+TitUIWG+IEoRDL
 uW2hKSqxrBsapTWxv2+tN5Kk0ORUSqEMqWaog80pjP3o0ezUKZxQsYuAZ430bzDr
 qqVWGpX42UjYet6V4S9P+I/gN3lCZmYoc24zfWLT8T8KRaDOOebYmC7EiJjudaXd
 sEYCKRv10ysBlFqXyzz2LDOyjFOiXodyFbhdBQVCPUuis99mHdYIJHoUxpYhgZSH
 IIbYn9RUsij6mMaCfPhNVoJwRVtY+AGGisfEnk+v5dfFYQruliFWgyjDYWT5+IxQ
 ZsJOJVCqR8JT9bL0xiTNbcZ3SvRfMQ2pLfd6MWX9fQZ9o6LvVSg32gPjgneKpQ/y
 QLRtiyVW3oBw2rJZeoYVIHrD8r3SUOI8iQLcCjXwxQoBiei6G+4GNdFtkUmqAFB3
 VvtPZYyjj05gCw==
 =Hfek
 -----END PGP SIGNATURE-----

Merge tag 'x86-urgent-2020-11-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 fixes from Thomas Gleixner:
 "Three fixes all related to #DB:

   - Handle the BTF bit correctly so it doesn't get lost due to a kernel
     #DB

   - Only clear and set the virtual DR6 value used by ptrace on user
     space triggered #DB. A kernel #DB must leave it alone to ensure
     data consistency for ptrace.

   - Make the bitmasking of the virtual DR6 storage correct so it does
     not lose DR_STEP"

* tag 'x86-urgent-2020-11-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  x86/debug: Fix DR_STEP vs ptrace_get_debugreg(6)
  x86/debug: Only clear/set ->virtual_dr6 for userspace #DB
  x86/debug: Fix BTF handling
This commit is contained in:
Linus Torvalds 2020-11-01 11:21:26 -08:00
commit 7b56fbd83e

View File

@ -793,19 +793,6 @@ static __always_inline unsigned long debug_read_clear_dr6(void)
set_debugreg(DR6_RESERVED, 6);
dr6 ^= DR6_RESERVED; /* Flip to positive polarity */
/*
* Clear the virtual DR6 value, ptrace routines will set bits here for
* things we want signals for.
*/
current->thread.virtual_dr6 = 0;
/*
* The SDM says "The processor clears the BTF flag when it
* generates a debug exception." Clear TIF_BLOCKSTEP to keep
* TIF_BLOCKSTEP in sync with the hardware BTF flag.
*/
clear_thread_flag(TIF_BLOCKSTEP);
return dr6;
}
@ -873,6 +860,20 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs,
*/
WARN_ON_ONCE(user_mode(regs));
if (test_thread_flag(TIF_BLOCKSTEP)) {
/*
* The SDM says "The processor clears the BTF flag when it
* generates a debug exception." but PTRACE_BLOCKSTEP requested
* it for userspace, but we just took a kernel #DB, so re-set
* BTF.
*/
unsigned long debugctl;
rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
debugctl |= DEBUGCTLMSR_BTF;
wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
}
/*
* Catch SYSENTER with TF set and clear DR_STEP. If this hit a
* watchpoint at the same time then that will still be handled.
@ -935,6 +936,22 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,
irqentry_enter_from_user_mode(regs);
instrumentation_begin();
/*
* Start the virtual/ptrace DR6 value with just the DR_STEP mask
* of the real DR6. ptrace_triggered() will set the DR_TRAPn bits.
*
* Userspace expects DR_STEP to be visible in ptrace_get_debugreg(6)
* even if it is not the result of PTRACE_SINGLESTEP.
*/
current->thread.virtual_dr6 = (dr6 & DR_STEP);
/*
* The SDM says "The processor clears the BTF flag when it
* generates a debug exception." Clear TIF_BLOCKSTEP to keep
* TIF_BLOCKSTEP in sync with the hardware BTF flag.
*/
clear_thread_flag(TIF_BLOCKSTEP);
/*
* If dr6 has no reason to give us about the origin of this trap,
* then it's very likely the result of an icebp/int01 trap.