]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
kgdb: Honour the kprobe blocklist when setting breakpoints
authorDaniel Thompson <daniel.thompson@linaro.org>
Sun, 27 Sep 2020 21:15:29 +0000 (22:15 +0100)
committerDaniel Thompson <daniel.thompson@linaro.org>
Mon, 28 Sep 2020 11:14:08 +0000 (12:14 +0100)
Currently kgdb has absolutely no safety rails in place to discourage or
prevent a user from placing a breakpoint in dangerous places such as
the debugger's own trap entry/exit and other places where it is not safe
to take synchronous traps.

Introduce a new config symbol KGDB_HONOUR_BLOCKLIST and modify the
default implementation of kgdb_validate_break_address() so that we use
the kprobe blocklist to prohibit instrumentation of critical functions
if the config symbol is set. The config symbol dependencies are set to
ensure that the blocklist will be enabled by default if we enable KGDB
and are compiling for an architecture where we HAVE_KPROBES.

Suggested-by: Peter Zijlstra <peterz@infradead.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Link: https://lore.kernel.org/r/20200927211531.1380577-2-daniel.thompson@linaro.org
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
include/linux/kgdb.h
kernel/debug/debug_core.c
kernel/debug/kdb/kdb_bp.c
lib/Kconfig.kgdb

index 477b8b7c908f8cdbae88139a9b239fbb8def95de..0d6cf64c8bb12aa283c72f133489885fc7127f54 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/linkage.h>
 #include <linux/init.h>
 #include <linux/atomic.h>
+#include <linux/kprobes.h>
 #ifdef CONFIG_HAVE_ARCH_KGDB
 #include <asm/kgdb.h>
 #endif
@@ -335,6 +336,23 @@ extern int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
                          atomic_t *snd_rdy);
 extern void gdbstub_exit(int status);
 
+/*
+ * kgdb and kprobes both use the same (kprobe) blocklist (which makes sense
+ * given they are both typically hooked up to the same trap meaning on most
+ * architectures one cannot be used to debug the other)
+ *
+ * However on architectures where kprobes is not (yet) implemented we permit
+ * breakpoints everywhere rather than blocking everything by default.
+ */
+static inline bool kgdb_within_blocklist(unsigned long addr)
+{
+#ifdef CONFIG_KGDB_HONOUR_BLOCKLIST
+       return within_kprobe_blacklist(addr);
+#else
+       return false;
+#endif
+}
+
 extern int                     kgdb_single_step;
 extern atomic_t                        kgdb_active;
 #define in_dbg_master() \
index 165e5b0c2083522c144c03d6971e7946ce7a1ad7..6b9383fa827803a3cdc0c236a2cd72112c74d75f 100644 (file)
@@ -180,6 +180,10 @@ int __weak kgdb_validate_break_address(unsigned long addr)
 {
        struct kgdb_bkpt tmp;
        int err;
+
+       if (kgdb_within_blocklist(addr))
+               return -EINVAL;
+
        /* Validate setting the breakpoint and then removing it.  If the
         * remove fails, the kernel needs to emit a bad message because we
         * are deep trouble not being able to put things back the way we
index d7ebb2c79cb867793bc0aa6832142c401e71917a..ec49401466123fcee1ddc7de6a67845927b742e1 100644 (file)
@@ -306,6 +306,15 @@ static int kdb_bp(int argc, const char **argv)
        if (!template.bp_addr)
                return KDB_BADINT;
 
+       /*
+        * This check is redundant (since the breakpoint machinery should
+        * be doing the same check during kdb_bp_install) but gives the
+        * user immediate feedback.
+        */
+       diag = kgdb_validate_break_address(template.bp_addr);
+       if (diag)
+               return diag;
+
        /*
         * Find an empty bp structure to allocate
         */
index 256f2486f9bd2f4d0dd0ef603df4955465836417..05dae05b6cc9ef84e2443b0ea713ec59940d6963 100644 (file)
@@ -24,6 +24,21 @@ menuconfig KGDB
 
 if KGDB
 
+config KGDB_HONOUR_BLOCKLIST
+       bool "KGDB: use kprobe blocklist to prohibit unsafe breakpoints"
+       depends on HAVE_KPROBES
+       depends on MODULES
+       select KPROBES
+       default y
+       help
+         If set to Y the debug core will use the kprobe blocklist to
+         identify symbols where it is unsafe to set breakpoints.
+         In particular this disallows instrumentation of functions
+         called during debug trap handling and thus makes it very
+         difficult to inadvertently provoke recursive trap handling.
+
+         If unsure, say Y.
+
 config KGDB_SERIAL_CONSOLE
        tristate "KGDB: use kgdb over the serial console"
        select CONSOLE_POLL