Skip to content

Commit f2d10ff

Browse files
author
Daniel Thompson
committed
kgdb: Honour the kprobe blocklist when setting breakpoints
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 <[email protected]> Reviewed-by: Douglas Anderson <[email protected]> Reviewed-by: Masami Hiramatsu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Daniel Thompson <[email protected]>
1 parent e16c33e commit f2d10ff

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

include/linux/kgdb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/linkage.h>
1717
#include <linux/init.h>
1818
#include <linux/atomic.h>
19+
#include <linux/kprobes.h>
1920
#ifdef CONFIG_HAVE_ARCH_KGDB
2021
#include <asm/kgdb.h>
2122
#endif
@@ -335,6 +336,23 @@ extern int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
335336
atomic_t *snd_rdy);
336337
extern void gdbstub_exit(int status);
337338

339+
/*
340+
* kgdb and kprobes both use the same (kprobe) blocklist (which makes sense
341+
* given they are both typically hooked up to the same trap meaning on most
342+
* architectures one cannot be used to debug the other)
343+
*
344+
* However on architectures where kprobes is not (yet) implemented we permit
345+
* breakpoints everywhere rather than blocking everything by default.
346+
*/
347+
static inline bool kgdb_within_blocklist(unsigned long addr)
348+
{
349+
#ifdef CONFIG_KGDB_HONOUR_BLOCKLIST
350+
return within_kprobe_blacklist(addr);
351+
#else
352+
return false;
353+
#endif
354+
}
355+
338356
extern int kgdb_single_step;
339357
extern atomic_t kgdb_active;
340358
#define in_dbg_master() \

kernel/debug/debug_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ int __weak kgdb_validate_break_address(unsigned long addr)
180180
{
181181
struct kgdb_bkpt tmp;
182182
int err;
183+
184+
if (kgdb_within_blocklist(addr))
185+
return -EINVAL;
186+
183187
/* Validate setting the breakpoint and then removing it. If the
184188
* remove fails, the kernel needs to emit a bad message because we
185189
* are deep trouble not being able to put things back the way we

kernel/debug/kdb/kdb_bp.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ static int kdb_bp(int argc, const char **argv)
306306
if (!template.bp_addr)
307307
return KDB_BADINT;
308308

309+
/*
310+
* This check is redundant (since the breakpoint machinery should
311+
* be doing the same check during kdb_bp_install) but gives the
312+
* user immediate feedback.
313+
*/
314+
diag = kgdb_validate_break_address(template.bp_addr);
315+
if (diag)
316+
return diag;
317+
309318
/*
310319
* Find an empty bp structure to allocate
311320
*/

lib/Kconfig.kgdb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ menuconfig KGDB
2424

2525
if KGDB
2626

27+
config KGDB_HONOUR_BLOCKLIST
28+
bool "KGDB: use kprobe blocklist to prohibit unsafe breakpoints"
29+
depends on HAVE_KPROBES
30+
depends on MODULES
31+
select KPROBES
32+
default y
33+
help
34+
If set to Y the debug core will use the kprobe blocklist to
35+
identify symbols where it is unsafe to set breakpoints.
36+
In particular this disallows instrumentation of functions
37+
called during debug trap handling and thus makes it very
38+
difficult to inadvertently provoke recursive trap handling.
39+
40+
If unsure, say Y.
41+
2742
config KGDB_SERIAL_CONSOLE
2843
tristate "KGDB: use kgdb over the serial console"
2944
select CONSOLE_POLL

0 commit comments

Comments
 (0)