From a731d4e3d7cb1cae46899af30f12cabe851c191d Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Wed, 6 Aug 2025 00:16:18 -0400 Subject: kernel/amd64: Add better checks on 'rbp' and 'rip' Improve handling within the stacktrace logic used during system panics and critical events. - Perform checks on 'rbp' *before* logging - Ensure RBP is 8-byte aligned Signed-off-by: Ian Moffett --- sys/arch/amd64/amd64/machdep.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index 19dcf44..52ad64c 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -259,7 +259,7 @@ void md_backtrace(void) { uintptr_t *rbp; - uintptr_t rip; + uintptr_t rip, tmp; off_t off; const char *name; char line[256]; @@ -268,13 +268,26 @@ md_backtrace(void) while (1) { rip = rbp[1]; rbp = (uintptr_t *)rbp[0]; - name = backtrace_addr_to_name(rip, &off); - if (rbp == NULL) + /* + * RBP should be aligned on an 8-byte + * boundary... Don't trust this state + * anymore if it is not. + */ + tmp = (uintptr_t)rbp; + if ((tmp & (8 - 1)) != 0) { + break; + } + + /* + * This is not a valid value, get out + * of this loop!! + */ + if (rbp == NULL || rip == 0) { break; - if (name == NULL) - name = "???"; + } + 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)); } -- cgit v1.2.3