• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 #include <fcntl.h>
3 #include <signal.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/mman.h>
8 #include <sys/wait.h>
9 #include <time.h>
10 #include <unistd.h>
11 #include "liburing.h"
12 #include "../src/syscall.h"
13 
current_time_ms(void)14 static uint64_t current_time_ms(void)
15 {
16     struct timespec ts;
17     if (clock_gettime(CLOCK_MONOTONIC, &ts))
18         exit(1);
19     return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
20 }
21 
22 #define SIZEOF_IO_URING_SQE 64
23 #define SIZEOF_IO_URING_CQE 16
24 #define SQ_TAIL_OFFSET 64
25 #define SQ_RING_MASK_OFFSET 256
26 #define SQ_RING_ENTRIES_OFFSET 264
27 #define CQ_RING_ENTRIES_OFFSET 268
28 #define CQ_CQES_OFFSET 320
29 
30 #define IORING_OFF_SQES 0x10000000ULL
31 
kill_and_wait(int pid,int * status)32 static void kill_and_wait(int pid, int* status)
33 {
34     kill(-pid, SIGKILL);
35     kill(pid, SIGKILL);
36     while (waitpid(-1, status, __WALL) != pid) {
37     }
38 }
39 
40 #define WAIT_FLAGS __WALL
41 
42 uint64_t r[3] = {0xffffffffffffffff, 0x0, 0x0};
43 
syz_io_uring_setup(volatile long a0,volatile long a1,volatile long a2,volatile long a3,volatile long a4,volatile long a5)44 static long syz_io_uring_setup(volatile long a0, volatile long a1,
45 volatile long a2, volatile long a3, volatile long a4, volatile long
46 a5)
47 {
48     uint32_t entries = (uint32_t)a0;
49     struct io_uring_params* setup_params = (struct io_uring_params*)a1;
50     void* vma1 = (void*)a2;
51     void* vma2 = (void*)a3;
52     void** ring_ptr_out = (void**)a4;
53     void** sqes_ptr_out = (void**)a5;
54     uint32_t fd_io_uring = __sys_io_uring_setup(entries, setup_params);
55     uint32_t sq_ring_sz = setup_params->sq_off.array +
56 setup_params->sq_entries * sizeof(uint32_t);
57     uint32_t cq_ring_sz = setup_params->cq_off.cqes +
58 setup_params->cq_entries * SIZEOF_IO_URING_CQE;
59     uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz;
60     *ring_ptr_out = mmap(vma1, ring_sz, PROT_READ | PROT_WRITE,
61 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring,
62 IORING_OFF_SQ_RING);
63     uint32_t sqes_sz = setup_params->sq_entries * SIZEOF_IO_URING_SQE;
64     *sqes_ptr_out = mmap(vma2, sqes_sz, PROT_READ | PROT_WRITE,
65 MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring, IORING_OFF_SQES);
66     return fd_io_uring;
67 }
68 
syz_io_uring_submit(volatile long a0,volatile long a1,volatile long a2,volatile long a3)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 
94 
trigger_bug(void)95 void trigger_bug(void)
96 {
97     intptr_t res = 0;
98     *(uint32_t*)0x20000204 = 0;
99     *(uint32_t*)0x20000208 = 2;
100     *(uint32_t*)0x2000020c = 0;
101     *(uint32_t*)0x20000210 = 0;
102     *(uint32_t*)0x20000218 = -1;
103     memset((void*)0x2000021c, 0, 12);
104     res = -1;
105     res = syz_io_uring_setup(0x7987, 0x20000200, 0x20400000, 0x20ffd000, 0x200000c0, 0x200001c0);
106     if (res != -1) {
107         r[0] = res;
108         r[1] = *(uint64_t*)0x200000c0;
109         r[2] = *(uint64_t*)0x200001c0;
110     }
111     *(uint8_t*)0x20000180 = 0xb;
112     *(uint8_t*)0x20000181 = 1;
113     *(uint16_t*)0x20000182 = 0;
114     *(uint32_t*)0x20000184 = 0;
115     *(uint64_t*)0x20000188 = 4;
116     *(uint64_t*)0x20000190 = 0x20000140;
117     *(uint64_t*)0x20000140 = 0x77359400;
118     *(uint64_t*)0x20000148 = 0;
119     *(uint32_t*)0x20000198 = 1;
120     *(uint32_t*)0x2000019c = 0;
121     *(uint64_t*)0x200001a0 = 0;
122     *(uint16_t*)0x200001a8 = 0;
123     *(uint16_t*)0x200001aa = 0;
124     memset((void*)0x200001ac, 0, 20);
125     syz_io_uring_submit(r[1], r[2], 0x20000180, 1);
126     *(uint32_t*)0x20000544 = 0;
127     *(uint32_t*)0x20000548 = 0x36;
128     *(uint32_t*)0x2000054c = 0;
129     *(uint32_t*)0x20000550 = 0;
130     *(uint32_t*)0x20000558 = r[0];
131     memset((void*)0x2000055c, 0, 12);
132 
133 }
main(void)134 int main(void)
135 {
136     mmap((void *)0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
137     int pid = fork();
138     if (pid < 0)
139         exit(1);
140     if (pid == 0) {
141         trigger_bug();
142         exit(0);
143     }
144     int status = 0;
145     uint64_t start = current_time_ms();
146     for (;;) {
147         if (current_time_ms() - start < 1000) {
148             continue;
149         }
150         kill_and_wait(pid, &status);
151         break;
152     }
153     return 0;
154 }
155 
156 
157 
158