Guest User

Untitled

a guest
Jun 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sys/types.h>
  3. #include <ucontext.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6.  
  7. static void hello(void)
  8. {
  9. printf("hello, signal\n");
  10. }
  11.  
  12. static void trampoline(void)
  13. {
  14. printf("trampoline\n");
  15. }
  16.  
  17. static void sigsegv_handler(int sig, siginfo_t *si, void *ctx)
  18. {
  19. ucontext_t *uc = ctx;
  20. unsigned long prev_rip;
  21. unsigned long *rsp;
  22.  
  23. prev_rip = uc->uc_mcontext.gregs[REG_RIP];
  24.  
  25. rsp = (void *) uc->uc_mcontext.gregs[REG_RSP];
  26.  
  27. *(--rsp) = (unsigned long) prev_rip;
  28. *(--rsp) = (unsigned long) hello;
  29.  
  30. printf("%p\n", rsp);
  31.  
  32. uc->uc_mcontext.gregs[REG_RSP] = (unsigned long) rsp;
  33. uc->uc_mcontext.gregs[REG_RIP] = (unsigned long) trampoline;
  34. }
  35.  
  36. void touch_stack(void)
  37. {
  38. char array[4096];
  39. int i;
  40.  
  41. for (i = 0; i < sizeof(array); i++) {
  42. array[i] = 0xff;
  43. }
  44.  
  45. printf("%p\n", array+i);
  46. }
  47.  
  48. static void make_kill(void)
  49. {
  50. char use_some_stack[8];
  51.  
  52. use_some_stack[0] = 0xff;
  53.  
  54. if (kill(0, SIGSEGV) < 0)
  55. perror("kill");
  56. }
  57.  
  58. void setup(void)
  59. {
  60. struct sigaction sa;
  61.  
  62. sigemptyset(&sa.sa_mask);
  63. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  64.  
  65. sa.sa_sigaction = sigsegv_handler;
  66. sigaction(SIGSEGV, &sa, NULL);
  67. }
  68.  
  69. int main(int argc, char *argv[])
  70. {
  71. setup();
  72.  
  73. make_kill();
  74.  
  75. printf("main\n");
  76.  
  77. touch_stack();
  78.  
  79. return 0;
  80. }
Add Comment
Please, Sign In to add comment