| 1 |
|
| 2 |
|
| 3 |
#include <arch/breakpoint.h> |
| 4 |
#include <arch/stack_canary_breakpoint.h> |
| 5 |
#include <arch/symbols.h> |
| 6 |
#include <bootstate.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <types.h> |
| 9 |
#include <symbols.h> |
| 10 |
|
| 11 |
static struct breakpoint_handle stack_canary_bp; |
| 12 |
|
| 13 |
static int handle_stack_canary_written(struct breakpoint_handle handle, struct eregs *regs) |
| 14 |
{ |
| 15 |
#if ENV_X86_64 |
| 16 |
printk(BIOS_ERR, "Stack corruption detected at rip: 0x%llx\n", regs->rip); |
| 17 |
#else |
| 18 |
printk(BIOS_ERR, "Stack corruption detected at eip: 0x%x\n", regs->eip); |
| 19 |
#endif |
| 20 |
return 0; |
| 21 |
} |
| 22 |
|
| 23 |
static void create_stack_canary_breakpoint(uintptr_t *addr) |
| 24 |
{ |
| 25 |
enum breakpoint_result res = |
| 26 |
breakpoint_create_data(&stack_canary_bp, addr, sizeof(uintptr_t), true); |
| 27 |
|
| 28 |
if (res != BREAKPOINT_RES_OK) { |
| 29 |
printk(BIOS_ERR, "Failed to create stack canary breakpoint\n"); |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
breakpoint_set_handler(stack_canary_bp, &handle_stack_canary_written); |
| 34 |
breakpoint_enable(stack_canary_bp, true); |
| 35 |
} |
| 36 |
|
| 37 |
void stack_canary_breakpoint_init(void) |
| 38 |
{ |
| 39 |
uintptr_t *addr; |
| 40 |
|
| 41 |
if (CONFIG(RESET_VECTOR_IN_RAM)) { |
| 42 |
addr = (uintptr_t *)_earlyram_stack; |
| 43 |
} else if (ENV_CACHE_AS_RAM) { |
| 44 |
addr = (uintptr_t *)_car_stack; |
| 45 |
} else { |
| 46 |
addr = (uintptr_t *)_stack; |
| 47 |
} |
| 48 |
|
| 49 |
create_stack_canary_breakpoint(addr); |
| 50 |
} |
| 51 |
|
| 52 |
void stack_canary_breakpoint_remove(void) |
| 53 |
{ |
| 54 |
breakpoint_remove(stack_canary_bp); |
| 55 |
} |
| 56 |
|
| 57 |
static void stack_canary_breakpoint_remove_hook(void *unused) |
| 58 |
{ |
| 59 |
stack_canary_breakpoint_remove(); |
| 60 |
} |
| 61 |
|
| 62 |
BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, stack_canary_breakpoint_remove_hook, NULL); |
| 63 |
BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, stack_canary_breakpoint_remove_hook, NULL); |