Skip to content

Instantly share code, notes, and snippets.

@mvanotti
Last active January 26, 2021 05:13
Show Gist options
  • Save mvanotti/735b388f53a64fa23d2a8aeae9c4877a to your computer and use it in GitHub Desktop.
Save mvanotti/735b388f53a64fa23d2a8aeae9c4877a to your computer and use it in GitHub Desktop.
kasan exchange stacks
/**
This code is useful for moments when the stack might be corrupted.
On the first asan panic, asan will disable further panics and switch
to a new stack.
*/
// ktl::atomic<bool> g_asan_panic = false;
// uint8_t kasan_panic_stack[4096];
if (g_asan_panic.exchange(true)) {
return;
}
uint8_t* newstack = &kasan_panic_stack[4096];
asm volatile("movq %[Address], %%rdi\n"
"movq %[Bytes], %%rsi\n"
"movb %[IsWrite], %%dl\n"
"movq %[Caller], %%rcx\n"
"movq %[PoisonedAddr], %%r8\n"
"leaq %[AsanFail], %%rax\n"
"movq %[NewStack], %%rsp\n"
"callq *%%rax\n" :
/* outputs */ :
/* inputs */
[NewStack] "m" (newstack),
[AsanFail] "m" (asan_fail),
[Address] "m" (address),
[Bytes] "m" (bytes),
[IsWrite] "m" (is_write),
[Caller] "m" (caller),
[PoisonedAddr] "m" (poisoned_addr) :
/* clobbers */
"rsp", "rax", "rdi", "rsi", "rdx", "rcx", "r8");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment