Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <string.h>
  6.  
  7. #include <sys/mman.h>
  8. #include <sys/ptrace.h>
  9.  
  10. struct region
  11. {
  12. int count;
  13. char buf[10000];
  14. int id;
  15. };
  16.  
  17. int main ( int argc, char *argv[] )
  18. {
  19. struct sigaction act;
  20. struct region *rptr;
  21. pid_t child;
  22. int fd;
  23.  
  24. fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  25. if (ftruncate(fd, 0) == -1) return -1;
  26. rptr = mmap(NULL, sizeof(struct region), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  27. if (rptr == MAP_FAILED) return -1;
  28.  
  29. child = fork();
  30. if(child == 0)
  31. {
  32. ptrace(PTRACE_TRACEME, 0, NULL, NULL);
  33. fprintf(stderr, "rptr: %d\n", rptr->count);
  34. fprintf(stderr, "rptr: %d\n", rptr->count);
  35. fprintf(stderr, "rptr: %d %s\n", rptr->count, rptr->buf);
  36. fprintf(stderr, "rptr: %d %s\n", rptr->count, rptr->buf);
  37. fprintf(stderr, "rptr: %d\n", rptr->count);
  38. fprintf(stderr, "rptr: %d 0x%x\n", rptr->count, rptr->id);
  39. fprintf(stderr, "rptr: %d\n", rptr->count);
  40. }
  41. else
  42. {
  43. struct region mydata = { 0, "test tmp", 0xdeadbeef };
  44. int status = ~0;
  45. size_t i;
  46.  
  47. while(!WIFEXITED(status))
  48. {
  49. if(!WIFSTOPPED(status) || WSTOPSIG(status) != SIGBUS)
  50. {
  51. ptrace(PTRACE_CONT, child, NULL, NULL);
  52. wait(&status);
  53. continue;
  54. }
  55.  
  56. mydata.count++;
  57. lseek(fd, 0, SEEK_SET);
  58. write(fd, &mydata, sizeof(mydata));
  59. ptrace(PTRACE_SINGLESTEP, child, NULL, NULL);
  60. wait(&status);
  61. if (ftruncate(fd, 0) == -1) return -1;
  62.  
  63. ptrace(PTRACE_CONT, child, NULL, NULL);
  64. wait(&status);
  65. }
  66. }
  67.  
  68. shm_unlink("/myregion");
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement