nanosleep-race-outbuf-munmap.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2020, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <pthread.h>
  27. #include <assert.h>
  28. #include <signal.h>
  29. #include <stdio.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. static void signal_printer(int)
  33. {
  34. // no-op
  35. }
  36. typedef struct yank_shared_t {
  37. timespec* remaining_sleep;
  38. // TODO: Be nice and use thread ID
  39. //pthread_t sleeper_thread;
  40. } yank_shared_t;
  41. static void* yanker_fn(void* shared_)
  42. {
  43. yank_shared_t* shared = static_cast<yank_shared_t*>(shared_);
  44. timespec requested_sleep = { 1, 0 };
  45. int rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, nullptr);
  46. if (rc != 0) {
  47. printf("Yanker: Failed during sleep: %d\n", rc);
  48. return nullptr;
  49. }
  50. delete shared->remaining_sleep; // T2
  51. // Send SIGUSR1.
  52. // Use pthread:
  53. // pthread_kill(somewhere, SIGUSR1);
  54. // But wait! pthread_kill isn't implemented yet, and therefore causes
  55. // a linker error. It also looks like the corresponding syscall is missing.
  56. // Use normal IPC syscall:
  57. // kill(getpid(), SIGUSR1);
  58. // But wait! If destination_pid == own_pid, then the signal is sent
  59. // to the calling thread, *no matter what*.
  60. // So, we have to go the very ugly route of fork():
  61. // (Thank goodness this is only a demo of a kernel bug!)
  62. pid_t pid_to_kill = getpid();
  63. pid_t child_pid = fork();
  64. if (child_pid < 0) {
  65. printf("Yanker: Fork failed: %d\n", child_pid);
  66. pthread_exit(nullptr); // See below
  67. return nullptr;
  68. }
  69. if (child_pid > 0) {
  70. // Success. Terminate quickly. T3
  71. // FIXME: LibPthread bug: returning during normal operation causes nullptr deref.
  72. // Workaround: Exit manually.
  73. pthread_exit(nullptr);
  74. return nullptr;
  75. }
  76. // Give parent *thread* a moment to die.
  77. requested_sleep = { 1, 0 };
  78. rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, nullptr);
  79. if (rc != 0) {
  80. printf("Yanker-child: Failed during sleep: %d\n", rc);
  81. return nullptr;
  82. }
  83. // Prod the parent *process*
  84. kill(pid_to_kill, SIGUSR1); // T4
  85. // Wait a moment, to ensure the log output is as well-separated as possible.
  86. requested_sleep = { 2, 0 };
  87. rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, nullptr);
  88. if (rc != 0) {
  89. printf("Yanker-child: Failed during after-sleep: %d\n", rc);
  90. return nullptr;
  91. }
  92. pthread_exit(nullptr);
  93. assert(false);
  94. // FIXME: return nullptr;
  95. }
  96. int main()
  97. {
  98. // Chronological order:
  99. // T0: Main thread allocates region for the outvalue of clock_nanosleep
  100. // T1: Main thread enters clock_nanosleep
  101. // T2: Side thread deallocates that region
  102. // T3: Side thread dies
  103. // T4: A different process sends SIGUSR1, waking up the main thread,
  104. // forcing the kernel to write to the deallocated Region.
  105. // I'm sorry that both a side *thread* and a side *process* are necessary.
  106. // Maybe in the future this test can be simplified, see above.
  107. yank_shared_t shared = { nullptr };
  108. shared.remaining_sleep = new timespec({ 0xbad, 0xf00d }); // T0
  109. pthread_t yanker_thread;
  110. int rc = pthread_create(&yanker_thread, nullptr, yanker_fn, &shared);
  111. if (rc != 0) {
  112. perror("pthread");
  113. printf("FAIL\n");
  114. return 1;
  115. }
  116. // Set an action for SIGUSR1, so that the sleep can be interrupted:
  117. signal(SIGUSR1, signal_printer);
  118. // T1: Go to sleep.
  119. const timespec requested_sleep = { 3, 0 };
  120. rc = clock_nanosleep(CLOCK_MONOTONIC, 0, &requested_sleep, shared.remaining_sleep);
  121. // Now we are beyond T4.
  122. if (rc == 0) {
  123. // We somehow weren't interrupted. Bad.
  124. printf("Not interrupted.\n");
  125. printf("FAIL\n");
  126. return 1;
  127. }
  128. // nanosleep was interrupted and the kernel didn't crash. Good!
  129. printf("PASS\n");
  130. return 0;
  131. }