From 1769813948ddc82f6276d4addf415b328719bf24 Mon Sep 17 00:00:00 2001 From: jua Date: Thu, 2 Nov 2017 18:54:12 +0100 Subject: [PATCH] x86-64: handle stack fault for non-canonical address access * With the amd64 architecture, the stack fault exception got a new meaning: it is raised when an instruction tries to access a non-canonical address, and the stack is referenced in the instruction (e.g. by its addressing mode). So unlike on x86, this is not a fatal exception and shouldn't trigger a KDL -- instead, it is to be treated like a general protection fault, terminating the team which caused it. * Fixes #13744 --- src/system/kernel/arch/x86/64/descriptors.cpp | 11 ++++++++++- src/system/kernel/arch/x86/arch_int.cpp | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/system/kernel/arch/x86/64/descriptors.cpp b/src/system/kernel/arch/x86/64/descriptors.cpp index 993dfdf212..12f51c64e2 100644 --- a/src/system/kernel/arch/x86/64/descriptors.cpp +++ b/src/system/kernel/arch/x86/64/descriptors.cpp @@ -329,6 +329,15 @@ x86_64_general_protection_fault(iframe* frame) } +static void +x86_64_stack_fault_exception(iframe* frame) +{ + // Non-canonical address accesses which reference the stack cause a stack + // fault exception instead of GPF. However, we can treat it like a GPF. + x86_64_general_protection_fault(frame); +} + + // #pragma mark - @@ -381,7 +390,7 @@ x86_descriptors_init(kernel_args* args) table[9] = x86_fatal_exception; // Coprocessor Segment Overrun table[10] = x86_fatal_exception; // Invalid TSS Exception (#TS) table[11] = x86_fatal_exception; // Segment Not Present (#NP) - table[12] = x86_fatal_exception; // Stack Fault Exception (#SS) + table[12] = x86_64_stack_fault_exception; // Stack Fault Exception (#SS) table[13] = x86_64_general_protection_fault; // General Protection Exception (#GP) table[14] = x86_page_fault_exception; // Page-Fault Exception (#PF) table[16] = x86_unexpected_exception; // x87 FPU Floating-Point Error (#MF) diff --git a/src/system/kernel/arch/x86/arch_int.cpp b/src/system/kernel/arch/x86/arch_int.cpp index 126e1f37a1..090c3eb861 100644 --- a/src/system/kernel/arch/x86/arch_int.cpp +++ b/src/system/kernel/arch/x86/arch_int.cpp @@ -139,6 +139,13 @@ x86_unexpected_exception(iframe* frame) signalAddress = frame->ip; break; + case 12: // Stack Fault (#SS) + type = B_STACK_FAULT; + signalNumber = SIGBUS; + signalCode = BUS_ADRERR; + signalAddress = frame->ip; + break; + case 13: // General Protection Exception (#GP) type = B_GENERAL_PROTECTION_FAULT; signalNumber = SIGILL;