mprotect_file_fork.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <err.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <sys/mman.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #define FNAME "/tmp/test"
  11. #define VAL 0xff
  12. int main(void) {
  13. int fd;
  14. void *ptr;
  15. if (mkdir("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
  16. err(1, "mkdir");
  17. }
  18. if (unlink(FNAME) < 0 && errno != ENOENT) {
  19. err(1, "unlink");
  20. }
  21. fd = open(FNAME, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
  22. if (fd < 0) {
  23. err(1, "open");
  24. }
  25. if (ftruncate(fd, 0x10) < 0) {
  26. err(1, "ftruncate");
  27. }
  28. ptr = mmap(NULL, 0x1000, PROT_READ, MAP_PRIVATE, fd, 0);
  29. if (ptr == MAP_FAILED) {
  30. err(1, "mmap");
  31. }
  32. if (close(fd) < 0) {
  33. err(1, "close");
  34. }
  35. if (mprotect(ptr, 0x1000, PROT_READ | PROT_WRITE) < 0) {
  36. err(1, "mprotect");
  37. }
  38. *(int*)ptr = VAL;
  39. pid_t p = fork();
  40. if (p < 0) {
  41. err(1, "fork");
  42. }
  43. if (p == 0) {
  44. // child
  45. if (*(int*)ptr != VAL) {
  46. printf("EXPECTED: 0x%x\nGOT : 0x%x\n", VAL, *(int*)ptr);
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. // parent
  52. int st = 0;
  53. if (wait(&st) < 0) {
  54. err(1, "wait");
  55. }
  56. if (unlink(FNAME) < 0) {
  57. err(1, "unlink");
  58. }
  59. if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) {
  60. printf("abnormal child termination: %d\n", st);
  61. return 1;
  62. }
  63. puts("Test successful!");
  64. return 0;
  65. }