Guest User

Untitled

a guest
Jun 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sys/types.h>
  3. #include <ucontext.h>
  4. #include <pthread.h>
  5. #include <signal.h>
  6. #include <stdio.h>
  7.  
  8. static void hello(void)
  9. {
  10. printf("hello, signal\n");
  11. }
  12.  
  13. static void trampoline(void)
  14. {
  15. printf("trampoline\n");
  16. }
  17.  
  18. static void sigsegv_handler(int sig, siginfo_t *si, void *ctx)
  19. {
  20. ucontext_t *uc = ctx;
  21. unsigned long prev_rip;
  22. unsigned long *rsp;
  23.  
  24. prev_rip = uc->uc_mcontext.gregs[REG_RIP];
  25.  
  26. rsp = (void *) uc->uc_mcontext.gregs[REG_RSP];
  27.  
  28. *(--rsp) = (unsigned long) hello;
  29.  
  30. printf("rsp=%p, ctx=%p\n", rsp, ctx);
  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. static void *start_thread(void *arg)
  70. {
  71. for (;;) {
  72. make_kill();
  73.  
  74. printf("main\n");
  75.  
  76. touch_stack();
  77. }
  78.  
  79. return NULL;
  80. }
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84. pthread_t id;
  85.  
  86. setup();
  87.  
  88. if (pthread_create(&id, NULL, start_thread, NULL) != 0)
  89. perror("pthread_create");
  90.  
  91. if (pthread_join(id, NULL) != 0)
  92. perror("pthread_join");
  93.  
  94. return 0;
  95. }
Add Comment
Please, Sign In to add comment