x86/debug: Allow a single level of #DB recursion

Trying to clear DR7 around a #DB from usermode malfunctions if the tasks
schedules when delivering SIGTRAP.

Rather than trying to define a special no-recursion region, just allow a
single level of recursion. The same mechanism is used for NMI, and it
hasn't caused any problems yet.

Fixes: 9f58fdde95c9 ("x86/db: Split out dr6/7 handling")
Reported-by: Kyle Huey <me@kylehuey.com>
Debugged-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Daniel Thompson <daniel.thompson@linaro.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/8b9bd05f187231df008d48cf818a6a311cbd5c98.1597882384.git.luto@kernel.org
Link: https://lore.kernel.org/r/20200902133200.726584153@infradead.org

authored by Andy Lutomirski and committed by Thomas Gleixner d5c678ae 662a0221

Changed files
+31 -34
arch
x86
kernel
+31 -34
arch/x86/kernel/traps.c
··· 729 729 #endif 730 730 } 731 731 732 - static __always_inline void debug_enter(unsigned long *dr6, unsigned long *dr7) 732 + static __always_inline unsigned long debug_read_clear_dr6(void) 733 733 { 734 - /* 735 - * Disable breakpoints during exception handling; recursive exceptions 736 - * are exceedingly 'fun'. 737 - * 738 - * Since this function is NOKPROBE, and that also applies to 739 - * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a 740 - * HW_BREAKPOINT_W on our stack) 741 - * 742 - * Entry text is excluded for HW_BP_X and cpu_entry_area, which 743 - * includes the entry stack is excluded for everything. 744 - */ 745 - *dr7 = local_db_save(); 734 + unsigned long dr6; 746 735 747 736 /* 748 737 * The Intel SDM says: ··· 744 755 * 745 756 * Keep it simple: clear DR6 immediately. 746 757 */ 747 - get_debugreg(*dr6, 6); 758 + get_debugreg(dr6, 6); 748 759 set_debugreg(0, 6); 749 760 /* Filter out all the reserved bits which are preset to 1 */ 750 - *dr6 &= ~DR6_RESERVED; 751 - } 761 + dr6 &= ~DR6_RESERVED; 752 762 753 - static __always_inline void debug_exit(unsigned long dr7) 754 - { 755 - local_db_restore(dr7); 763 + return dr6; 756 764 } 757 765 758 766 /* ··· 849 863 static __always_inline void exc_debug_kernel(struct pt_regs *regs, 850 864 unsigned long dr6) 851 865 { 866 + /* 867 + * Disable breakpoints during exception handling; recursive exceptions 868 + * are exceedingly 'fun'. 869 + * 870 + * Since this function is NOKPROBE, and that also applies to 871 + * HW_BREAKPOINT_X, we can't hit a breakpoint before this (XXX except a 872 + * HW_BREAKPOINT_W on our stack) 873 + * 874 + * Entry text is excluded for HW_BP_X and cpu_entry_area, which 875 + * includes the entry stack is excluded for everything. 876 + */ 877 + unsigned long dr7 = local_db_save(); 852 878 bool irq_state = idtentry_enter_nmi(regs); 853 879 instrumentation_begin(); 854 880 ··· 881 883 882 884 instrumentation_end(); 883 885 idtentry_exit_nmi(regs, irq_state); 886 + 887 + local_db_restore(dr7); 884 888 } 885 889 886 890 static __always_inline void exc_debug_user(struct pt_regs *regs, ··· 893 893 * #DB, we will malfunction. 894 894 */ 895 895 WARN_ON_ONCE(!user_mode(regs)); 896 + 897 + /* 898 + * NB: We can't easily clear DR7 here because 899 + * idtentry_exit_to_usermode() can invoke ptrace, schedule, access 900 + * user memory, etc. This means that a recursive #DB is possible. If 901 + * this happens, that #DB will hit exc_debug_kernel() and clear DR7. 902 + * Since we're not on the IST stack right now, everything will be 903 + * fine. 904 + */ 896 905 897 906 irqentry_enter_from_user_mode(regs); 898 907 instrumentation_begin(); ··· 916 907 /* IST stack entry */ 917 908 DEFINE_IDTENTRY_DEBUG(exc_debug) 918 909 { 919 - unsigned long dr6, dr7; 920 - 921 - debug_enter(&dr6, &dr7); 922 - exc_debug_kernel(regs, dr6); 923 - debug_exit(dr7); 910 + exc_debug_kernel(regs, debug_read_clear_dr6()); 924 911 } 925 912 926 913 /* User entry, runs on regular task stack */ 927 914 DEFINE_IDTENTRY_DEBUG_USER(exc_debug) 928 915 { 929 - unsigned long dr6, dr7; 930 - 931 - debug_enter(&dr6, &dr7); 932 - exc_debug_user(regs, dr6); 933 - debug_exit(dr7); 916 + exc_debug_user(regs, debug_read_clear_dr6()); 934 917 } 935 918 #else 936 919 /* 32 bit does not have separate entry points. */ 937 920 DEFINE_IDTENTRY_RAW(exc_debug) 938 921 { 939 - unsigned long dr6, dr7; 940 - 941 - debug_enter(&dr6, &dr7); 922 + unsigned long dr6 = debug_read_clear_dr6(); 942 923 943 924 if (user_mode(regs)) 944 925 exc_debug_user(regs, dr6); 945 926 else 946 927 exc_debug_kernel(regs, dr6); 947 - 948 - debug_exit(dr7); 949 928 } 950 929 #endif 951 930