From d097c4299109f056a425684955fc118e84f94fa1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 6 Aug 2025 00:30:29 -0400 Subject: kernel/amd64: Limit frame depth in backtrace A backtrace usually occurs during critcal system events like panics. When the system in a error state, we cannot trust the correctness of the stack. This commit aims to improve checks done in a731d4e by adding a max frame depth that limits how many frames can be walked. Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'sys/arch/amd64') diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 52ad64c..9ff96e1 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -47,6 +47,13 @@ #include #include +/* + * This defines the max number of frames + * we will pass while walking the callstack + * in md_backtrace() + */ +#define MAX_FRAME_DEPTH 16 + #define pr_trace(fmt, ...) kprintf("cpu: " fmt, ##__VA_ARGS__) #define pr_error(...) pr_trace(__VA_ARGS__) #define pr_trace_bsp(...) \ @@ -263,9 +270,14 @@ md_backtrace(void) off_t off; const char *name; char line[256]; + uint8_t n = 0; __ASMV("mov %%rbp, %0" : "=r" (rbp) :: "memory"); while (1) { + if (n >= MAX_FRAME_DEPTH) { + break; + } + rip = rbp[1]; rbp = (uintptr_t *)rbp[0]; @@ -290,6 +302,7 @@ md_backtrace(void) name = backtrace_addr_to_name(rip, &off); snprintf(line, sizeof(line), "%p @ <%s+0x%x>\n", rip, name, off); cons_putstr(&g_root_scr, line, strlen(line)); + ++n; } } -- cgit v1.2.3