1 /* SPDX-License-Identifier: MIT */
2 // autogenerated by syzkaller (https://github.com/google/syzkaller)
3
4 #include <dirent.h>
5 #include <endian.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/prctl.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/mman.h>
20 #include <time.h>
21 #include <unistd.h>
22
23 #include "liburing.h"
24 #include "helpers.h"
25 #include "../src/syscall.h"
26
27 #ifndef CONFIG_USE_SANITIZER
sleep_ms(uint64_t ms)28 static void sleep_ms(uint64_t ms)
29 {
30 usleep(ms * 1000);
31 }
32
current_time_ms(void)33 static uint64_t current_time_ms(void)
34 {
35 struct timespec ts;
36 if (clock_gettime(CLOCK_MONOTONIC, &ts))
37 exit(1);
38 return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
39 }
40
write_file(const char * file,const char * what,...)41 static bool write_file(const char* file, const char* what, ...)
42 {
43 char buf[1024];
44 va_list args;
45 va_start(args, what);
46 vsnprintf(buf, sizeof(buf), what, args);
47 va_end(args);
48 buf[sizeof(buf) - 1] = 0;
49 int len = strlen(buf);
50 int fd = open(file, O_WRONLY | O_CLOEXEC);
51 if (fd == -1)
52 return false;
53 if (write(fd, buf, len) != len) {
54 int err = errno;
55 close(fd);
56 errno = err;
57 return false;
58 }
59 close(fd);
60 return true;
61 }
62
kill_and_wait(int pid,int * status)63 static void kill_and_wait(int pid, int* status)
64 {
65 kill(-pid, SIGKILL);
66 kill(pid, SIGKILL);
67 int i;
68 for (i = 0; i < 100; i++) {
69 if (waitpid(-1, status, WNOHANG | __WALL) == pid)
70 return;
71 usleep(1000);
72 }
73 DIR* dir = opendir("/sys/fs/fuse/connections");
74 if (dir) {
75 for (;;) {
76 struct dirent* ent = readdir(dir);
77 if (!ent)
78 break;
79 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
80 continue;
81 char abort[300];
82 snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
83 ent->d_name);
84 int fd = open(abort, O_WRONLY);
85 if (fd == -1) {
86 continue;
87 }
88 if (write(fd, abort, 1) < 0) {
89 }
90 close(fd);
91 }
92 closedir(dir);
93 } else {
94 }
95 while (waitpid(-1, status, __WALL) != pid) {
96 }
97 }
98
setup_test(void)99 static void setup_test(void)
100 {
101 prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
102 setpgrp();
103 write_file("/proc/self/oom_score_adj", "1000");
104 }
105
106 static void execute_one(void);
107
108 #define WAIT_FLAGS __WALL
109
loop(void)110 static void loop(void)
111 {
112 int iter;
113 for (iter = 0; iter < 50; iter++) {
114 int pid = fork();
115 if (pid < 0)
116 exit(1);
117 if (pid == 0) {
118 setup_test();
119 execute_one();
120 exit(0);
121 }
122 int status = 0;
123 uint64_t start = current_time_ms();
124 for (;;) {
125 if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
126 break;
127 sleep_ms(1);
128 if (current_time_ms() - start < 5 * 1000)
129 continue;
130 kill_and_wait(pid, &status);
131 break;
132 }
133 }
134 }
135
execute_one(void)136 void execute_one(void)
137 {
138 *(uint32_t*)0x20000080 = 0;
139 *(uint32_t*)0x20000084 = 0;
140 *(uint32_t*)0x20000088 = 3;
141 *(uint32_t*)0x2000008c = 3;
142 *(uint32_t*)0x20000090 = 0x175;
143 *(uint32_t*)0x20000094 = 0;
144 *(uint32_t*)0x20000098 = 0;
145 *(uint32_t*)0x2000009c = 0;
146 *(uint32_t*)0x200000a0 = 0;
147 *(uint32_t*)0x200000a4 = 0;
148 *(uint32_t*)0x200000a8 = 0;
149 *(uint32_t*)0x200000ac = 0;
150 *(uint32_t*)0x200000b0 = 0;
151 *(uint32_t*)0x200000b4 = 0;
152 *(uint32_t*)0x200000b8 = 0;
153 *(uint32_t*)0x200000bc = 0;
154 *(uint32_t*)0x200000c0 = 0;
155 *(uint32_t*)0x200000c4 = 0;
156 *(uint64_t*)0x200000c8 = 0;
157 *(uint32_t*)0x200000d0 = 0;
158 *(uint32_t*)0x200000d4 = 0;
159 *(uint32_t*)0x200000d8 = 0;
160 *(uint32_t*)0x200000dc = 0;
161 *(uint32_t*)0x200000e0 = 0;
162 *(uint32_t*)0x200000e4 = 0;
163 *(uint32_t*)0x200000e8 = 0;
164 *(uint32_t*)0x200000ec = 0;
165 *(uint64_t*)0x200000f0 = 0;
166 __sys_io_uring_setup(0x983, (struct io_uring_params *) 0x20000080);
167 }
168
sig_int(int sig)169 static void sig_int(int sig)
170 {
171 exit(0);
172 }
173
main(int argc,char * argv[])174 int main(int argc, char *argv[])
175 {
176 if (argc > 1)
177 return T_EXIT_SKIP;
178 signal(SIGINT, sig_int);
179 mmap((void *) 0x20000000, 0x1000000, 3, MAP_ANON|MAP_PRIVATE, -1, 0);
180 loop();
181 return T_EXIT_PASS;
182 }
183 #else
main(int argc,char * argv[])184 int main(int argc, char *argv[])
185 {
186 return T_EXIT_SKIP;
187 }
188 #endif
189