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