sqpoll-cancel-hang.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/mman.h>
  9. #include <sys/wait.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. #include "../src/syscall.h"
  15. /*
  16. * This syzbot test is known broken on some archs, just allow the ones that
  17. * are regularly tested.
  18. */
  19. #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || \
  20. defined(__aarch64__)
  21. static uint64_t current_time_ms(void)
  22. {
  23. struct timespec ts;
  24. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  25. exit(1);
  26. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  27. }
  28. #define SIZEOF_IO_URING_SQE 64
  29. #define SIZEOF_IO_URING_CQE 16
  30. #define SQ_TAIL_OFFSET 64
  31. #define SQ_RING_MASK_OFFSET 256
  32. #define SQ_RING_ENTRIES_OFFSET 264
  33. #define CQ_RING_ENTRIES_OFFSET 268
  34. #define CQ_CQES_OFFSET 320
  35. #define IORING_OFF_SQES 0x10000000ULL
  36. static void kill_and_wait(int pid, int* status)
  37. {
  38. kill(-pid, SIGKILL);
  39. kill(pid, SIGKILL);
  40. while (waitpid(-1, status, __WALL) != pid) {
  41. }
  42. }
  43. #define WAIT_FLAGS __WALL
  44. static uint64_t r[3] = {0xffffffffffffffff, 0x0, 0x0};
  45. static long syz_io_uring_setup(volatile long a0, volatile long a1,
  46. volatile long a2, volatile long a3, volatile long a4, volatile long
  47. a5)
  48. {
  49. uint32_t entries = (uint32_t)a0;
  50. struct io_uring_params* setup_params = (struct io_uring_params*)a1;
  51. void* vma1 = (void*)a2;
  52. void* vma2 = (void*)a3;
  53. void** ring_ptr_out = (void**)a4;
  54. void** sqes_ptr_out = (void**)a5;
  55. uint32_t fd_io_uring = __sys_io_uring_setup(entries, setup_params);
  56. uint32_t sq_ring_sz = setup_params->sq_off.array +
  57. setup_params->sq_entries * sizeof(uint32_t);
  58. uint32_t cq_ring_sz = setup_params->cq_off.cqes +
  59. setup_params->cq_entries * SIZEOF_IO_URING_CQE;
  60. uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz;
  61. *ring_ptr_out = mmap(vma1, ring_sz, PROT_READ | PROT_WRITE,
  62. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring,
  63. IORING_OFF_SQ_RING);
  64. uint32_t sqes_sz = setup_params->sq_entries * SIZEOF_IO_URING_SQE;
  65. *sqes_ptr_out = mmap(vma2, sqes_sz, PROT_READ | PROT_WRITE,
  66. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring, IORING_OFF_SQES);
  67. return fd_io_uring;
  68. }
  69. static long syz_io_uring_submit(volatile long a0, volatile long a1,
  70. volatile long a2, volatile long a3)
  71. {
  72. char* ring_ptr = (char*)a0;
  73. char* sqes_ptr = (char*)a1;
  74. char* sqe = (char*)a2;
  75. uint32_t sqes_index = (uint32_t)a3;
  76. uint32_t sq_ring_entries = *(uint32_t*)(ring_ptr + SQ_RING_ENTRIES_OFFSET);
  77. uint32_t cq_ring_entries = *(uint32_t*)(ring_ptr + CQ_RING_ENTRIES_OFFSET);
  78. uint32_t sq_array_off = (CQ_CQES_OFFSET + cq_ring_entries *
  79. SIZEOF_IO_URING_CQE + 63) & ~63;
  80. if (sq_ring_entries)
  81. sqes_index %= sq_ring_entries;
  82. char* sqe_dest = sqes_ptr + sqes_index * SIZEOF_IO_URING_SQE;
  83. memcpy(sqe_dest, sqe, SIZEOF_IO_URING_SQE);
  84. uint32_t sq_ring_mask = *(uint32_t*)(ring_ptr + SQ_RING_MASK_OFFSET);
  85. uint32_t* sq_tail_ptr = (uint32_t*)(ring_ptr + SQ_TAIL_OFFSET);
  86. uint32_t sq_tail = *sq_tail_ptr & sq_ring_mask;
  87. uint32_t sq_tail_next = *sq_tail_ptr + 1;
  88. uint32_t* sq_array = (uint32_t*)(ring_ptr + sq_array_off);
  89. *(sq_array + sq_tail) = sqes_index;
  90. __atomic_store_n(sq_tail_ptr, sq_tail_next, __ATOMIC_RELEASE);
  91. return 0;
  92. }
  93. static void trigger_bug(void)
  94. {
  95. intptr_t res = 0;
  96. *(uint32_t*)0x20000204 = 0;
  97. *(uint32_t*)0x20000208 = 2;
  98. *(uint32_t*)0x2000020c = 0;
  99. *(uint32_t*)0x20000210 = 0;
  100. *(uint32_t*)0x20000218 = -1;
  101. memset((void*)0x2000021c, 0, 12);
  102. res = -1;
  103. res = syz_io_uring_setup(0x7987, 0x20000200, 0x20400000, 0x20ffd000, 0x200000c0, 0x200001c0);
  104. if (res != -1) {
  105. r[0] = res;
  106. r[1] = *(uint64_t*)0x200000c0;
  107. r[2] = *(uint64_t*)0x200001c0;
  108. }
  109. *(uint8_t*)0x20000180 = 0xb;
  110. *(uint8_t*)0x20000181 = 1;
  111. *(uint16_t*)0x20000182 = 0;
  112. *(uint32_t*)0x20000184 = 0;
  113. *(uint64_t*)0x20000188 = 4;
  114. *(uint64_t*)0x20000190 = 0x20000140;
  115. *(uint64_t*)0x20000140 = 0x77359400;
  116. *(uint64_t*)0x20000148 = 0;
  117. *(uint32_t*)0x20000198 = 1;
  118. *(uint32_t*)0x2000019c = 0;
  119. *(uint64_t*)0x200001a0 = 0;
  120. *(uint16_t*)0x200001a8 = 0;
  121. *(uint16_t*)0x200001aa = 0;
  122. memset((void*)0x200001ac, 0, 20);
  123. syz_io_uring_submit(r[1], r[2], 0x20000180, 1);
  124. *(uint32_t*)0x20000544 = 0;
  125. *(uint32_t*)0x20000548 = 0x36;
  126. *(uint32_t*)0x2000054c = 0;
  127. *(uint32_t*)0x20000550 = 0;
  128. *(uint32_t*)0x20000558 = r[0];
  129. memset((void*)0x2000055c, 0, 12);
  130. }
  131. int main(void)
  132. {
  133. mmap((void *)0x20000000ul, 0x1000000ul, 7ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  134. int pid = fork();
  135. if (pid < 0)
  136. exit(1);
  137. if (pid == 0) {
  138. trigger_bug();
  139. exit(0);
  140. }
  141. int status = 0;
  142. uint64_t start = current_time_ms();
  143. for (;;) {
  144. if (current_time_ms() - start < 1000) {
  145. continue;
  146. }
  147. kill_and_wait(pid, &status);
  148. break;
  149. }
  150. return 0;
  151. }
  152. #else
  153. int main(void)
  154. {
  155. return T_EXIT_SKIP;
  156. }
  157. #endif