// https://syzkaller.appspot.com/bug?id=4157935be2e32fae6b62aa95bb0a08b07160b986 // 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) { 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; } 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*)0x20001480, "ntfs3\000", 6); memcpy((void*)0x200014c0, "./file0\000", 8); *(uint64_t*)0x20002940 = 0; *(uint64_t*)0x20002948 = 0; *(uint64_t*)0x20002950 = 0xffffffff; *(uint64_t*)0x20002958 = 0; *(uint64_t*)0x20002960 = 0; *(uint64_t*)0x20002968 = 0x40; *(uint64_t*)0x20002970 = 0; *(uint64_t*)0x20002978 = 0; *(uint64_t*)0x20002980 = 9; *(uint64_t*)0x20002988 = 0; *(uint64_t*)0x20002990 = 0; *(uint64_t*)0x20002998 = 1; *(uint64_t*)0x200029a0 = 0; *(uint64_t*)0x200029a8 = 0; *(uint64_t*)0x200029b0 = 0; *(uint64_t*)0x200029b8 = 0; *(uint64_t*)0x200029c0 = 0; *(uint64_t*)0x200029c8 = 0xff; *(uint64_t*)0x200029d0 = 0; *(uint64_t*)0x200029d8 = 0; *(uint64_t*)0x200029e0 = 0x9fd; *(uint64_t*)0x200029e8 = 0; *(uint64_t*)0x200029f0 = 0; *(uint64_t*)0x200029f8 = 0x81; memcpy((void*)0x20001280, "\x69\x6f\x63\x68\x61\x72\x73\x65\x74\x3d\x63\x70\x38\x36\x33\x2c\x73" "\x79\x73\x5f\x69\x6d\x6d\x75\x74\x61\x62\x6c\x65\x2c\x69\x6f\x63\x68" "\x76\x72\x73\x65\x74\x3d\x6d\x61\x63\x69\x63\x65\x6c\x61\x6e\x64\x2c" "\x70\x6c\x6c\x6f\x63\x2c\x64\x69\x73\x63\x61\x72\x64\x2c\x73\x6d\x61" "\x63\x6b\x66\x73\x66\x6c\x6f\x6f\x72\x3d\x2c\x00\x98\x2e\x55\x26\xb7" "\x24\x22\xff\x44\xd6\x46\x5d\x61\x0f\xb5\xcc\xa1\x5e\xdb\x1a\xef\xb1" "\xc3\x0e\x49\xb1\xb1\x39\x99\x16\x07\x34\xd2\x17\x26\xcb\x13", 117); syz_mount_image(0x20001480, 0x200014c0, 0x101, 8, 0x20002940, 8, 0x20001280); return 0; }