// https://syzkaller.appspot.com/bug?id=68b3528dc42805d2128b6c5620d74fe87c3bfbdf // 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 #ifndef __NR_memfd_create #define __NR_memfd_create 319 #endif static unsigned long long procid; struct fs_image_segment { void* data; uintptr_t size; uintptr_t offset; }; #define IMAGE_MAX_SEGMENTS 4096 #define IMAGE_MAX_SIZE (129 << 20) static unsigned long fs_image_segment_check(unsigned long size, unsigned long nsegs, struct fs_image_segment* segs) { if (nsegs > IMAGE_MAX_SEGMENTS) nsegs = IMAGE_MAX_SEGMENTS; for (size_t i = 0; i < nsegs; i++) { if (segs[i].size > IMAGE_MAX_SIZE) segs[i].size = IMAGE_MAX_SIZE; segs[i].offset %= IMAGE_MAX_SIZE; if (segs[i].offset > IMAGE_MAX_SIZE - segs[i].size) segs[i].offset = IMAGE_MAX_SIZE - segs[i].size; if (size < segs[i].offset + segs[i].offset) size = segs[i].offset + segs[i].offset; } if (size > IMAGE_MAX_SIZE) size = IMAGE_MAX_SIZE; return size; } 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; size = fs_image_segment_check(size, nsegs, segs); 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; } 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); memcpy((void*)0x20000000, "gfs2\000", 5); memcpy((void*)0x20000100, "./file0\000", 8); *(uint64_t*)0x20000040 = 0x20000340; memcpy( (void*)0x20000340, "\x01\x16\x19\x70\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x64\x00\x00\x00\x00\x00\x00\x07\x09\x00\x00\x07\x6c\x00\x00\x00\x00" "\x00\x00\x10\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x02\x00\x00\x00\x00\x00\x00\x08\xd0\xb3\x60\x7b\x4f\x84\x5e\xdd\xf4" "\x2d\xe8\x69\xba\xa5\x59\x42\xf1\xdc\xca\x83\x30\x10\xa8\x2d\xfa\x5a\xe1" "\x4f\xbe\xe4\x8c\xde\xff\x1c\x7f\x8d\xf8\x8c\xb4\x99\xdc\xf2\x1e\x93\xe9" "\xa6\x98\xff\x5a\x9f\x58\x80\x05\xf3\x8d\x62\xf0\x7d\x40\x1c\x6a\xce\x70" "\xac\xfd\xf6\x2a\xba\xd0\xe2\xf8\xcf\x94\x4e\x9c\x3b\x37\xd6\xd0\x14\x1a" "\x88\x7a\xa4\x3f\x1b\xdf\x38\x8f\x69\xd8\xfd\x3f\x74\x5d\x14\x6a\xdd\xd8" "\x60\xdb\x0f\xd7\x4f\xc4\xae\x55\xa5\x2c\x21\x2e\x92\x10\x5a\x89\xc0\x09" "\x27\xe3\x42\x79\x9b\x57\x37\x32\xc7\x1e\xaa\x7f\xe4\xd2\x20\xf5\x54\xe0" "\x6c\xc0\x79\xa2\x8e\x6e\x25\x73\x27\x3a\xdc\x0b\x27\xde\x47\xe0\xad\x3d" "\x2c\x98\x2f\xd5\xc6\xa0\xcf\x9d\xaf\xc1\xf6\xe3\x8c\xc3\x3d\x1b\x8b\xe8" "\x4e\xe0\x1a\x42\x7e\x55\x87\x42\x16\xda\x6b\x65\x62\x7c\x0c\xaf\xc2\x4b" "\x52\xf9\x31\x96\xd9\x4c\x00\x24\xce\xdd\x9c\x25\x5b\x64\x22\x82\x27\x63" "\x03\x5b\x4a\x37\x36\x05\x81\x9f\xf9\xe8\x7a\xe2\x0e\x46\x7c\x5b\xdb\xe3" "\xd9\x68\xed\x98\xe6\x8c\xe6\x7e\xfd\x37\x94\x8e\xe2\x04\xf0\xdb\xbf\xf1" "\x29\x01\xef\x23\x97\x23\xf9\x5a\x51\xd1\x07\x50\xea\x89\xf1\x97\x2f\xed" "\xeb\x02\x6a\x82\x7a\xca\xa3\x7f\x0a\xcd\x08\x3b\xff\xf2\xeb\x1a\xf9\xde" "\x7d", 343); *(uint64_t*)0x20000048 = 0x157; *(uint64_t*)0x20000050 = 0x10000; *(uint64_t*)0x20000058 = 0x20000140; memcpy((void*)0x20000140, "\x60\x22\xa7\xf3\x72\xec\x34\xf6\x34\x52\xc2\x6b\xb0\xbf\xce\x99\xad" "\x3e\x09\x83\x25\xdf\xc0\xa4\x97\x4f\x37\xe3\x6e\xf4\x15\xf4\x41\x5b" "\x6f\xde\xff\xb3\x27\xe6\x63\x75\x43\xdd\xc1\x87\x02\xf5\x37\x02\xb3" "\x00\xea\x66\xa8\x55\x5f\xd5\x2b\x6f\x80\x99\xc8\x4e\x4e\x96\x03\x46" "\xea\xd1\xd3\x67\x7f\x38\xce\x9e\x9c\xee\x9b\x77\x1e\xf9\x6d\x85\xcc" "\x74\x6e\x90\x28\xc6\x65\x3c\x77\xcc\x4f\x7b\x22\xd2\x3d\x69\x7d\x22" "\x60\x8f\xfe\x30\x01\x1f\x82\xac\xf1\xae\x10\x34\x61\xe7\x8b\x43\xa3" "\xdd\x68\x84\x80\x0f\x82\x29\xda\xb8\xd4\x6c", 130); *(uint64_t*)0x20000060 = 0x82; *(uint64_t*)0x20000068 = 9; syz_mount_image(0x20000000, 0x20000100, 4, 2, 0x20000040, 8, 0x20000240, 0); return 0; }