// https://syzkaller.appspot.com/bug?id=7db3a0ee48a805cd26f19586fd8b7f32f84f304b // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef __NR_memfd_create #define __NR_memfd_create 319 #endif static unsigned long long procid; static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static bool write_file(const char* file, const char* what, ...) { char buf[1024]; va_list args; va_start(args, what); vsnprintf(buf, sizeof(buf), what, args); va_end(args); buf[sizeof(buf) - 1] = 0; int len = strlen(buf); int fd = open(file, O_WRONLY | O_CLOEXEC); if (fd == -1) return false; if (write(fd, buf, len) != len) { int err = errno; close(fd); errno = err; return false; } close(fd); return true; } struct fs_image_segment { void* data; uintptr_t size; uintptr_t offset; }; static int setup_loop_device(long unsigned size, long unsigned nsegs, struct fs_image_segment* segs, const char* loopname, int* memfd_p, int* loopfd_p) { int err = 0, loopfd = -1; int memfd = syscall(__NR_memfd_create, "syzkaller", 0); if (memfd == -1) { err = errno; goto error; } if (ftruncate(memfd, size)) { err = errno; goto error_close_memfd; } for (size_t i = 0; i < nsegs; i++) { if (pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset) < 0) { } } loopfd = open(loopname, O_RDWR); if (loopfd == -1) { err = errno; goto error_close_memfd; } if (ioctl(loopfd, LOOP_SET_FD, memfd)) { if (errno != EBUSY) { err = errno; goto error_close_loop; } ioctl(loopfd, LOOP_CLR_FD, 0); usleep(1000); if (ioctl(loopfd, LOOP_SET_FD, memfd)) { err = errno; goto error_close_loop; } } *memfd_p = memfd; *loopfd_p = loopfd; return 0; error_close_loop: close(loopfd); error_close_memfd: close(memfd); error: errno = err; return -1; } static long syz_mount_image(volatile long fsarg, volatile long dir, volatile unsigned long size, volatile unsigned long nsegs, volatile long segments, volatile long flags, volatile long optsarg, volatile long change_dir) { struct fs_image_segment* segs = (struct fs_image_segment*)segments; int res = -1, err = 0, loopfd = -1, memfd = -1, need_loop_device = !!segs; char* mount_opts = (char*)optsarg; char* target = (char*)dir; char* fs = (char*)fsarg; char* source = NULL; char loopname[64]; if (need_loop_device) { memset(loopname, 0, sizeof(loopname)); snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid); if (setup_loop_device(size, nsegs, segs, loopname, &memfd, &loopfd) == -1) return -1; source = loopname; } mkdir(target, 0777); char opts[256]; memset(opts, 0, sizeof(opts)); if (strlen(mount_opts) > (sizeof(opts) - 32)) { } strncpy(opts, mount_opts, sizeof(opts) - 32); if (strcmp(fs, "iso9660") == 0) { flags |= MS_RDONLY; } else if (strncmp(fs, "ext", 3) == 0) { if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0) strcat(opts, ",errors=continue"); } else if (strcmp(fs, "xfs") == 0) { strcat(opts, ",nouuid"); } res = mount(source, target, fs, flags, opts); if (res == -1) { err = errno; goto error_clear_loop; } res = open(target, O_RDONLY | O_DIRECTORY); if (res == -1) { err = errno; goto error_clear_loop; } if (change_dir) { res = chdir(target); if (res == -1) { err = errno; } } error_clear_loop: if (need_loop_device) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); close(memfd); } errno = err; return res; } static void kill_and_wait(int pid, int* status) { kill(-pid, SIGKILL); kill(pid, SIGKILL); for (int i = 0; i < 100; i++) { if (waitpid(-1, status, WNOHANG | __WALL) == pid) return; usleep(1000); } DIR* dir = opendir("/sys/fs/fuse/connections"); if (dir) { for (;;) { struct dirent* ent = readdir(dir); if (!ent) break; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char abort[300]; snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name); int fd = open(abort, O_WRONLY); if (fd == -1) { continue; } if (write(fd, abort, 1) < 0) { } close(fd); } closedir(dir); } else { } while (waitpid(-1, status, __WALL) != pid) { } } static void reset_loop() { char buf[64]; snprintf(buf, sizeof(buf), "/dev/loop%llu", procid); int loopfd = open(buf, O_RDWR); if (loopfd != -1) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); } } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { reset_loop(); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } } } void execute_one(void) { memcpy((void*)0x20000000, "bfs\000", 4); memcpy((void*)0x200005c0, "./file0\000", 8); *(uint64_t*)0x20000200 = 0x20000100; memcpy((void*)0x20000100, "\xce\xfa\xad\x1b\x00\x0e\x00\x00\xff\x7f", 10); *(uint64_t*)0x20000208 = 0xa; *(uint64_t*)0x20000210 = 0; *(uint64_t*)0x20000218 = 0x20010040; memcpy((void*)0x20010040, "\x02\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00\x1f\x0e\x00\x00\x02", 17); *(uint64_t*)0x20000220 = 0x11; *(uint64_t*)0x20000228 = 0x200; *(uint64_t*)0x20000230 = 0x20000280; memcpy( (void*)0x20000280, "\x02\x00\x2e\x00\xc5\x00\x53\x00\x00\x00\x00\x00\x20\x00\xd6\x48\x5d\xc3" "\x7d\xf1\x06\x96\xbb\x54\xe5\x81\x67\x7d\xfa\x6c\x83\xbe\xf5\xea\xb1\xdf" "\x18\xcc\xdb\x2b\xc6\xd4\x1c\x71\x82\x17\x2d\x59\xa0\x02\xc5\x9b\x2a\x98" "\x2f\x9f\xcc\xe1\x28\x54\x36\x06\xf6\xdc\xfe\xe3\x98\x28\xc4\xa1\x84\xd3" "\xef\x43\x14\xa0\x8b\xcc\x77\xa1\x17\x87\x11\x00\x48\xfb\x25\x44\x03\xb7" "\xb9\xff\xff\x00\x00\x00\x00\x00\x00\x1a\x2c\xd2\x47\x7d\x9f\xe4\x66\x77" "\xcd\x5d\x93\xfb\x60\xe3\x17\x43\x6c\x5b\x8e\x78\xe1\x89\xd5\x7a\xce\x52" "\xd0\x34\x9c\x59\x90\x1f\x37\x8b\xd3\xec\x97\x83\x47\xe5\x02\x9d\xd0\x5b" "\x7c\x71\x0f\x84\x83\x99\x59\xd3\x8b\xeb\x6d\x7f\xde\x56\x06\xa9\xe9\xa7" "\xeb\x57\x4d\xf2\x06\xad\x46\xec\x67\x31\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x86\xcc\xaf\xa2\xa5\x0c\xda\x5e\x92\x00\x2c\x8d\x8b\x7b\xd5\xcf\x0f" "\x88\x19\x24\x34\x28\xeb\x68\x7e\x68\x96\x3f\x11\x15\xed\x9f\xa3\x18\x36" "\xab\x40\xc5\x55\x56\xf9\x1c\x4c\xc8\xe4\x2b\x5a\x41\x80\xeb\x81\x0e\xce" "\x3d\x4a\x4a\xf4\x8d\xa8\xc5\xdb\x9a\x38\xa5\xad\x41\xa6\xd7\x29\xf9\xdf" "\xd0\x3b\xbe\x73\x6d\x6e\x0a\xd7\xde\x90\xc9\x88\x5b\x29\xfc\x82\xbc\x72" "\xf0\x18\xbf\x3f\x37\x6d\x4c\x96\x83\xa5\x1a\x2f\x4e\xd6\x53\x92\xc3\x9e" "\x73\x18\x61\x5c\x65\xa7\x46\xbe\x38\x4f\xd9\xbd\x0f\x42\x84\xd6\x21\xe5" "\x5d\x2c\x7b\xbc\xc5\x8d\x40\x68\x78\x95\x49\xdd\xca\xb7\x51\xe7\xae\x1a" "\x54\x4c\xc9\x35\x57\x20\x08\x0a\x27\x28\x54\x13\x5e\xe0\xed\x90\x50\xd3" "\x97\xc0\x1a\xf8\x04\x5d\xc5\xc5\xa4\x84\x9b\x1e\x88\x79\xd5\x7a\x71\x36" "\x06\x71\x5b\x4f\x7c\xa2\xac\x8c\x01\x69\x54\x1f\x02\xe6\x91\xf3\x28\xea" "\x17\xf6\x97\x95\x84\xb9\x09\x69\x40\x62\xbe\x61\xeb\xa8\xe5\xd0\x36\xe7" "\x04\x84\x38\x8d\x9c\x07\xf2\x16\x4d\xfe\xbb\xb9\x9b\xb6\x1d\x7d\x4a\x81" "\xed\x63\xed\x62\x0d\x55\xe6\x71\x4e\x39\xf8\xeb\x0f\x9a\xc9\x1c\x21\x81" "\x70\x4a\xc8\x60\x00\x46\x35\x96\x75\x32\x6e\x4e\xcc\x79\xd1\x8f\x05\x29" "\x0a\x87\x5f\x08\xd6\xee\x3d\x21\x0e\x26\xf6\x06\x9f\x0a\xda\x74\x1e\x01" "\x6b\x64\x5b\xa3\x98\x57\xa8\x4c\x0b\xea\x6c\x49\x05\x28\xa7\xd1\x3e\x02" "\x32\x75\xcc\x1a\x28\xab\x41\x57\x3f\x37\x48\x43\x81\x56\x05\x2b\x35\xc8" "\xb6\xea\xc5\xe4\x18\x2b\xa2\x70\x84\xf9\xc6\x61\x6c\x43\x2a\x25\xfb\x86" "\x34\xef\x39\x54\x25\x2f\x96\x1c\x9d\x8f\x0c\x23\xdd\x8b\xef\x19\xc5\x3d" "\x78\xce\xbc\x69\x91\xb7\xdc\x6f\x54\x65\xe0\xa0\xa5\xc0\x3e\x4a\x95\xf5" "\x7d\xda\x28\x80\xc1\x20\x63\xce\xd9\x6a\x7d\x1b\x2f\xa2\x21\xbb\xc5\x0e" "\x32\x54\x77\x27\x5b\x18\x26\x17\x9c\xcd\xfd\x35\x7b\x66\x05\xa3\xf7\x53" "\x94\x08\x2f\x1c\x98\xf7\xcf\xfc\x62\x9c\x0d\x5d\x6a\xa5\x75\xfc\xf3\xba" "\xf2", 613); *(uint64_t*)0x20000238 = 0x265; *(uint64_t*)0x20000240 = 0xdfc; syz_mount_image(0x20000000, 0x200005c0, 0x8000, 3, 0x20000200, 0, 0x200100a0, 1); memcpy((void*)0x20000040, "./file0\000", 8); syscall(__NR_mknod, 0x20000040ul, 0ul, 0); } int main(void) { syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); loop(); return 0; }