Skip to content

Instantly share code, notes, and snippets.

@moyix
Last active March 23, 2022 17:43
Show Gist options
  • Save moyix/f2e101348209ddeb7eba903147a867aa to your computer and use it in GitHub Desktop.
Save moyix/f2e101348209ddeb7eba903147a867aa to your computer and use it in GitHub Desktop.
Example of using a constructor to set up the shadow callstack and assign gs appropriately
#include <stdio.h>
#include <asm/prctl.h>
#include <sys/prctl.h>
#include <stdlib.h>
// Compile with: clang-8 -fsanitize=shadow-call-stack shadowstack.c -o shadowstack
int arch_prctl(int code, unsigned long *addr);
void __attribute__ ((constructor)) __attribute__((no_sanitize("shadow-call-stack"))) setupgs()
{
void *shadow = malloc(16384);
arch_prctl(ARCH_SET_GS, shadow);
}
int bar() {
return 42;
}
int foo() {
return bar() + 1;
}
int main(int argc, char **argv) {
printf("Hello, world %d!\n", foo());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment