| // INFO: task hung in loop_control_ioctl |
| // https://syzkaller.appspot.com/bug?id=7baeaafd34fd4c80d274f9104873d018c9bf38a1 |
| // status:open |
| // autogenerated by syzkaller (http://github.com/google/syzkaller) |
| |
| #define _GNU_SOURCE |
| #include <endian.h> |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <linux/loop.h> |
| #include <stdint.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <sys/ioctl.h> |
| #include <sys/mount.h> |
| #include <sys/stat.h> |
| #include <sys/syscall.h> |
| #include <sys/types.h> |
| #include <unistd.h> |
| |
| extern 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 SYZ_memfd_create 319 |
| |
| static uintptr_t syz_read_part_table(uintptr_t size, uintptr_t nsegs, |
| uintptr_t segments) |
| { |
| char loopname[64], linkname[64]; |
| int loopfd, err = 0, res = -1; |
| uintptr_t i, j; |
| struct fs_image_segment* segs = (struct fs_image_segment*)segments; |
| |
| if (nsegs > IMAGE_MAX_SEGMENTS) |
| nsegs = IMAGE_MAX_SEGMENTS; |
| for (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; |
| int memfd = syscall(SYZ_memfd_create, "syz_read_part_table", 0); |
| if (memfd == -1) { |
| err = errno; |
| goto error; |
| } |
| if (ftruncate(memfd, size)) { |
| err = errno; |
| goto error_close_memfd; |
| } |
| for (i = 0; i < nsegs; i++) { |
| if (pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset) < 0) { |
| } |
| } |
| snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid); |
| 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; |
| } |
| } |
| struct loop_info64 info; |
| if (ioctl(loopfd, LOOP_GET_STATUS64, &info)) { |
| err = errno; |
| goto error_clear_loop; |
| } |
| info.lo_flags |= LO_FLAGS_PARTSCAN; |
| if (ioctl(loopfd, LOOP_SET_STATUS64, &info)) { |
| err = errno; |
| goto error_clear_loop; |
| } |
| res = 0; |
| for (i = 1, j = 0; i < 8; i++) { |
| snprintf(loopname, sizeof(loopname), "/dev/loop%llup%d", procid, (int)i); |
| struct stat statbuf; |
| if (stat(loopname, &statbuf) == 0) { |
| snprintf(linkname, sizeof(linkname), "./file%d", (int)j++); |
| if (symlink(loopname, linkname)) { |
| } |
| } |
| } |
| error_clear_loop: |
| ioctl(loopfd, LOOP_CLR_FD, 0); |
| error_close_loop: |
| close(loopfd); |
| error_close_memfd: |
| close(memfd); |
| error: |
| errno = err; |
| return res; |
| } |
| |
| static void execute_one(); |
| extern unsigned long long procid; |
| |
| void loop() |
| { |
| while (1) { |
| execute_one(); |
| } |
| } |
| |
| uint64_t r[2] = {0xffffffffffffffff, 0x0}; |
| unsigned long long procid; |
| void execute_one() |
| { |
| long res = 0; |
| memcpy((void*)0x20000040, "/dev/loop-control", 18); |
| res = syscall(__NR_openat, 0xffffffffffffff9c, 0x20000040, 0, 0); |
| if (res != -1) |
| r[0] = res; |
| res = syscall(__NR_ioctl, r[0], 0x4c82); |
| if (res != -1) |
| r[1] = res; |
| syscall(__NR_ioctl, r[0], 0x4c81, r[1]); |
| *(uint64_t*)0x20000240 = 0x20000200; |
| *(uint64_t*)0x20000248 = 0; |
| *(uint64_t*)0x20000250 = 0xca; |
| syz_read_part_table(0, 1, 0x20000240); |
| } |
| |
| int main() |
| { |
| syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0); |
| for (procid = 0; procid < 8; procid++) { |
| if (fork() == 0) { |
| for (;;) { |
| loop(); |
| } |
| } |
| } |
| sleep(1000000); |
| return 0; |
| } |