// https://syzkaller.appspot.com/bug?id=85ac29f6b0b5e6075422c40dd0b207e11393f300 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef FIFREEZE #define FIFREEZE _IOWR('X', 119, int) #endif #ifndef FITHAW #define FITHAW _IOWR('X', 120, int) #endif static char g_devname1[32]; static volatile int g_helper_cmd = 0; static int g_backing_mnt_fd = -1; static char g_saved_fair_runtime[64] = ""; static char g_saved_rt_runtime[64] = ""; static char g_saved_soft_watchdog[64] = ""; static void read_and_save(const char *path, char *buf, size_t buf_sz) { buf[0] = '\0'; int fd = open(path, O_RDONLY); if (fd >= 0) { ssize_t n = read(fd, buf, buf_sz - 1); if (n > 0) buf[n] = '\0'; close(fd); } } static void write_file(const char *path, const char *val) { int fd = open(path, O_WRONLY); if (fd >= 0) { write(fd, val, strlen(val)); close(fd); } } /* * Build Image 1 (backing.img: 8 MB = 8192 blocks, block size 1024). * Single block group, consistent metadata. * Free block count is 8057 (8192 total - 134 metadata/root dir blocks - 1 padding bit = 8057). */ static void build_backing_img(uint8_t *img) { /* Block 1: Superblock at offset 1024 */ uint8_t *sb = img + 1024; *(uint32_t *)(sb + 0x00) = 1024; /* s_inodes_count */ *(uint32_t *)(sb + 0x04) = 8192; /* s_blocks_count_lo */ *(uint32_t *)(sb + 0x08) = 0; /* s_r_blocks_count_lo */ *(uint32_t *)(sb + 0x0C) = 8057; /* s_free_blocks_count_lo (8192 - 135) */ *(uint32_t *)(sb + 0x10) = 1013; /* s_free_inodes_count */ *(uint32_t *)(sb + 0x14) = 1; /* s_first_data_block */ *(uint32_t *)(sb + 0x18) = 0; /* s_log_block_size (1024 bytes) */ *(uint32_t *)(sb + 0x1C) = 0; /* s_log_cluster_size */ *(uint32_t *)(sb + 0x20) = 8192; /* s_blocks_per_group */ *(uint32_t *)(sb + 0x24) = 8192; /* s_clusters_per_group */ *(uint32_t *)(sb + 0x28) = 1024; /* s_inodes_per_group */ *(uint32_t *)(sb + 0x2C) = 0; /* s_mtime */ *(uint32_t *)(sb + 0x30) = 0; /* s_wtime */ *(uint16_t *)(sb + 0x34) = 1; /* s_mnt_count */ *(uint16_t *)(sb + 0x36) = 65535; /* s_max_mnt_count */ *(uint16_t *)(sb + 0x38) = 0xEF53; /* s_magic */ *(uint16_t *)(sb + 0x3A) = 1; /* s_state (EXT4_VALID_FS) */ *(uint16_t *)(sb + 0x3C) = 1; /* s_errors (EXT4_ERRORS_CONTINUE) */ *(uint16_t *)(sb + 0x3E) = 0; /* s_minor_rev_level */ *(uint32_t *)(sb + 0x40) = 0; /* s_lastcheck */ *(uint32_t *)(sb + 0x44) = 0; /* s_checkinterval */ *(uint32_t *)(sb + 0x48) = 0; /* s_creator_os (Linux) */ *(uint32_t *)(sb + 0x4C) = 1; /* s_rev_level (EXT4_DYNAMIC_REV) */ *(uint16_t *)(sb + 0x50) = 0; /* s_def_resuid */ *(uint16_t *)(sb + 0x52) = 0; /* s_def_resgid */ *(uint32_t *)(sb + 0x54) = 11; /* s_first_ino */ *(uint16_t *)(sb + 0x58) = 128; /* s_inode_size */ *(uint16_t *)(sb + 0x5A) = 0; /* s_block_group_nr */ *(uint32_t *)(sb + 0x5C) = 0; /* s_feature_compat */ *(uint32_t *)(sb + 0x60) = 0x0002; /* s_feature_incompat (FILETYPE) */ *(uint32_t *)(sb + 0x64) = 0x0001; /* s_feature_ro_compat (SPARSE_SUPER) */ for (int i = 0; i < 16; i++) { sb[0x68 + i] = (uint8_t)(i + 1); /* s_uuid */ } *(uint16_t *)(sb + 0xFE) = 32; /* s_desc_size */ /* Block 2: Block Group Descriptor at offset 2048 */ uint8_t *gd = img + 2048; *(uint32_t *)(gd + 0x00) = 3; /* bg_block_bitmap_lo */ *(uint32_t *)(gd + 0x04) = 4; /* bg_inode_bitmap_lo */ *(uint32_t *)(gd + 0x08) = 5; /* bg_inode_table_lo */ *(uint16_t *)(gd + 0x0C) = 8057; /* bg_free_blocks_count_lo (8192 - 135) */ *(uint16_t *)(gd + 0x0E) = 1013; /* bg_free_inodes_count_lo */ *(uint16_t *)(gd + 0x10) = 1; /* bg_used_dirs_count_lo */ *(uint16_t *)(gd + 0x12) = 0; /* bg_flags */ /* Block 3: Block Bitmap at offset 3072 */ /* Bits 0..127 used (bytes 0..15 = 0xFF) */ uint8_t *bb = img + 3072; memset(bb, 0xFF, 16); /* Bits 128..133 used (byte 16 = 0x3F) for ITB and root dir block 133 */ bb[16] = 0x3F; /* Bit 8191 set to satisfy ext4_valid_block_bitmap_padding() */ bb[1023] |= 0x80; /* Block 4: Inode Bitmap at offset 4096 */ uint8_t *ib = img + 4096; ib[0] = 0xFF; /* inodes 1..8 */ ib[1] = 0x07; /* inodes 9..11 */ /* Blocks 5..132: Inode Table at offset 5120. Inode 2 at offset 5248 */ uint8_t *ino2 = img + 5248; *(uint16_t *)(ino2 + 0x00) = 0040755; /* i_mode: S_IFDIR | 0755 */ *(uint16_t *)(ino2 + 0x02) = 0; /* i_uid */ *(uint32_t *)(ino2 + 0x04) = 1024; /* i_size_lo */ *(uint32_t *)(ino2 + 0x08) = 1600000000; /* i_atime */ *(uint32_t *)(ino2 + 0x0C) = 1600000000; /* i_ctime */ *(uint32_t *)(ino2 + 0x10) = 1600000000; /* i_mtime */ *(uint32_t *)(ino2 + 0x14) = 0; /* i_dtime */ *(uint16_t *)(ino2 + 0x18) = 0; /* i_gid */ *(uint16_t *)(ino2 + 0x1A) = 2; /* i_links_count */ *(uint32_t *)(ino2 + 0x1C) = 2; /* i_blocks_lo */ *(uint32_t *)(ino2 + 0x20) = 0; /* i_flags */ *(uint32_t *)(ino2 + 0x28) = 133; /* i_block[0] -> block 133 */ /* Block 133: Root Directory Data Block at offset 133 * 1024 = 136192 */ uint8_t *dir = img + 133 * 1024; /* Entry 1: "." */ *(uint32_t *)(dir + 0) = 2; *(uint16_t *)(dir + 4) = 12; *(uint8_t *)(dir + 6) = 1; *(uint8_t *)(dir + 7) = 2; /* EXT4_FT_DIR */ dir[8] = '.'; /* Entry 2: ".." */ *(uint32_t *)(dir + 12) = 2; *(uint16_t *)(dir + 16) = 1012; *(uint8_t *)(dir + 18) = 2; *(uint8_t *)(dir + 19) = 2; /* EXT4_FT_DIR */ dir[20] = '.'; dir[21] = '.'; } /* * Build Image 2 (target.img: 4 MB = 4096 blocks, block size 1024). * Single block group, no journal (sbi->s_journal == NULL). * Intentional metadata mismatch: bg_free_blocks = 3000 vs actual zero bits = 4026. */ static void build_target_img(uint8_t *img) { /* Block 1: Superblock at offset 1024 */ uint8_t *sb = img + 1024; *(uint32_t *)(sb + 0x00) = 512; /* s_inodes_count */ *(uint32_t *)(sb + 0x04) = 4096; /* s_blocks_count_lo */ *(uint32_t *)(sb + 0x08) = 0; /* s_r_blocks_count_lo */ *(uint32_t *)(sb + 0x0C) = 3000; /* s_free_blocks_count_lo (mismatch with 4026) */ *(uint32_t *)(sb + 0x10) = 501; /* s_free_inodes_count */ *(uint32_t *)(sb + 0x14) = 1; /* s_first_data_block */ *(uint32_t *)(sb + 0x18) = 0; /* s_log_block_size (1024 bytes) */ *(uint32_t *)(sb + 0x1C) = 0; /* s_log_cluster_size */ *(uint32_t *)(sb + 0x20) = 4096; /* s_blocks_per_group */ *(uint32_t *)(sb + 0x24) = 4096; /* s_clusters_per_group */ *(uint32_t *)(sb + 0x28) = 512; /* s_inodes_per_group */ *(uint32_t *)(sb + 0x2C) = 0; /* s_mtime */ *(uint32_t *)(sb + 0x30) = 0; /* s_wtime */ *(uint16_t *)(sb + 0x34) = 1; /* s_mnt_count */ *(uint16_t *)(sb + 0x36) = 65535; /* s_max_mnt_count */ *(uint16_t *)(sb + 0x38) = 0xEF53; /* s_magic */ *(uint16_t *)(sb + 0x3A) = 1; /* s_state (EXT4_VALID_FS) */ *(uint16_t *)(sb + 0x3C) = 1; /* s_errors (EXT4_ERRORS_CONTINUE) */ *(uint16_t *)(sb + 0x3E) = 0; /* s_minor_rev_level */ *(uint32_t *)(sb + 0x40) = 0; /* s_lastcheck */ *(uint32_t *)(sb + 0x44) = 0; /* s_checkinterval */ *(uint32_t *)(sb + 0x48) = 0; /* s_creator_os (Linux) */ *(uint32_t *)(sb + 0x4C) = 1; /* s_rev_level (EXT4_DYNAMIC_REV) */ *(uint16_t *)(sb + 0x50) = 0; /* s_def_resuid */ *(uint16_t *)(sb + 0x52) = 0; /* s_def_resgid */ *(uint32_t *)(sb + 0x54) = 11; /* s_first_ino */ *(uint16_t *)(sb + 0x58) = 128; /* s_inode_size */ *(uint16_t *)(sb + 0x5A) = 0; /* s_block_group_nr */ *(uint32_t *)(sb + 0x5C) = 0; /* s_feature_compat (NO JOURNAL) */ *(uint32_t *)(sb + 0x60) = 0x0002; /* s_feature_incompat (FILETYPE) */ *(uint32_t *)(sb + 0x64) = 0x0001; /* s_feature_ro_compat (SPARSE_SUPER) */ for (int i = 0; i < 16; i++) { sb[0x68 + i] = (uint8_t)(i + 17); /* s_uuid */ } *(uint16_t *)(sb + 0xFE) = 32; /* s_desc_size */ /* Block 2: Block Group Descriptor at offset 2048 */ uint8_t *gd = img + 2048; *(uint32_t *)(gd + 0x00) = 3; /* bg_block_bitmap_lo */ *(uint32_t *)(gd + 0x04) = 4; /* bg_inode_bitmap_lo */ *(uint32_t *)(gd + 0x08) = 5; /* bg_inode_table_lo */ *(uint16_t *)(gd + 0x0C) = 3000; /* bg_free_blocks_count_lo (mismatch with 4026) */ *(uint16_t *)(gd + 0x0E) = 501; /* bg_free_inodes_count_lo */ *(uint16_t *)(gd + 0x10) = 1; /* bg_used_dirs_count_lo */ *(uint16_t *)(gd + 0x12) = 0; /* bg_flags */ /* Block 3: Block Bitmap at offset 3072 */ /* Bits 0..63 used (bytes 0..7 = 0xFF) */ uint8_t *bb = img + 3072; memset(bb, 0xFF, 8); /* Bits 64..68 used (byte 8 = 0x1F) for ITB and root dir block 69 */ bb[8] = 0x1F; /* Padding: set all bits from 4095 up to 8191 to 1 */ for (int i = 4095; i < 8192; i++) { bb[i / 8] |= (uint8_t)(1 << (i % 8)); } /* Block 4: Inode Bitmap at offset 4096 */ uint8_t *ib = img + 4096; ib[0] = 0xFF; /* inodes 1..8 */ ib[1] = 0x07; /* inodes 9..11 */ /* Blocks 5..68: Inode Table at offset 5120. Inode 2 at offset 5248 */ uint8_t *ino2 = img + 5248; *(uint16_t *)(ino2 + 0x00) = 0040755; /* i_mode: S_IFDIR | 0755 */ *(uint16_t *)(ino2 + 0x02) = 0; /* i_uid */ *(uint32_t *)(ino2 + 0x04) = 1024; /* i_size_lo */ *(uint32_t *)(ino2 + 0x08) = 1600000000; /* i_atime */ *(uint32_t *)(ino2 + 0x0C) = 1600000000; /* i_ctime */ *(uint32_t *)(ino2 + 0x10) = 1600000000; /* i_mtime */ *(uint32_t *)(ino2 + 0x14) = 0; /* i_dtime */ *(uint16_t *)(ino2 + 0x18) = 0; /* i_gid */ *(uint16_t *)(ino2 + 0x1A) = 2; /* i_links_count */ *(uint32_t *)(ino2 + 0x1C) = 2; /* i_blocks_lo */ *(uint32_t *)(ino2 + 0x20) = 0; /* i_flags */ *(uint32_t *)(ino2 + 0x28) = 69; /* i_block[0] -> block 69 */ /* Block 69: Root Directory Data Block at offset 69 * 1024 = 70656 */ uint8_t *dir = img + 69 * 1024; /* Entry 1: "." */ *(uint32_t *)(dir + 0) = 2; *(uint16_t *)(dir + 4) = 12; *(uint8_t *)(dir + 6) = 1; *(uint8_t *)(dir + 7) = 2; /* EXT4_FT_DIR */ dir[8] = '.'; /* Entry 2: ".." */ *(uint32_t *)(dir + 12) = 2; *(uint16_t *)(dir + 16) = 1012; *(uint8_t *)(dir + 18) = 2; *(uint8_t *)(dir + 19) = 2; /* EXT4_FT_DIR */ dir[20] = '.'; dir[21] = '.'; } /* * Helper thread pinned to CPU 1: * 1. Arms wq_watchdog_timer on CPU 1 with 3s threshold (preventing starvation on CPU 0). * 2. Waits 100ms for main thread to enter SCHED_FIFO spin loop. * 3. Thaws backing fs: wakes loop1 writeback -> unlock_buffer -> kworker/0:X becomes TASK_RUNNING. * 4. Waits 100ms for try_to_wake_up() to place kworker/0:X into TASK_RUNNING on CPU 0. * 5. Writes to /proc/sys/vm/stat_refresh, queuing work onto CPU 0 pool 2 worklist and blocking. */ static void *helper_fn(void *arg) { int target_cpu = (int)(intptr_t)arg; cpu_set_t cs; CPU_ZERO(&cs); CPU_SET(target_cpu, &cs); int res = pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs); if (res != 0) { printf("[-] [helper] Failed to pthread_setaffinity_np to CPU %d: %s\n", target_cpu, strerror(res)); } else { printf("[+] [helper] Pinned to CPU %d successfully.\n", target_cpu); } while (__atomic_load_n(&g_helper_cmd, __ATOMIC_ACQUIRE) == 0) usleep(1000); printf("[*] [helper] Command received, setting watchdog_thresh on CPU %d...\n", target_cpu); /* 1. Arm watchdog timer on CPU 1 (NOT CPU 0) with 3s threshold */ int wfd = open("/sys/module/workqueue/parameters/watchdog_thresh", O_WRONLY); if (wfd >= 0) { ssize_t w = write(wfd, "3\n", 2); if (w < 0) { printf("[-] [helper] Failed write to watchdog_thresh: %s\n", strerror(errno)); } else { printf("[+] [helper] Configured watchdog_thresh to 3s.\n"); } close(wfd); } else { printf("[-] [helper] Failed to open watchdog_thresh: %s\n", strerror(errno)); } /* 2. Wait 100ms to ensure main thread on CPU 0 is spinning in SCHED_FIFO */ usleep(100000); /* 3. Thaw backing fs: wakes loop1 writeback -> unlock_buffer -> kworker becomes TASK_RUNNING */ printf("[*] [helper] Calling ioctl(FITHAW) on backing fs...\n"); res = ioctl(g_backing_mnt_fd, FITHAW, 0); if (res < 0) { printf("[-] [helper] Failed to ioctl(FITHAW): %s\n", strerror(errno)); } else { printf("[+] [helper] ioctl(FITHAW) successful, buffer unlocked.\n"); } /* 4. Wait 100ms for try_to_wake_up() to place kworker into TASK_RUNNING on CPU 0 */ usleep(100000); /* 5. Queue work onto CPU 0 pool 2 worklist and block */ printf("[*] [helper] Writing to /proc/sys/vm/stat_refresh to queue work on CPU 0...\n"); int sfd = open("/proc/sys/vm/stat_refresh", O_WRONLY); if (sfd >= 0) { ssize_t w = write(sfd, "1\n", 2); if (w < 0) { printf("[-] [helper] Failed write to /proc/sys/vm/stat_refresh: %s\n", strerror(errno)); } else { printf("[+] [helper] write to /proc/sys/vm/stat_refresh completed.\n"); } close(sfd); } else { printf("[-] [helper] Failed to open /proc/sys/vm/stat_refresh: %s\n", strerror(errno)); } return NULL; } static int open_loop_dev(int dev_nr) { char path[32]; snprintf(path, sizeof(path), "/dev/loop%d", dev_nr); int fd = open(path, O_RDWR); if (fd < 0 && errno == ENOENT) { mknod(path, S_IFBLK | 0660, makedev(7, dev_nr)); fd = open(path, O_RDWR); } return fd; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Initializing reproducer...\n"); /* Clean up existing mounts if present */ umount2("/tmp/mnt_target", MNT_DETACH); umount2("/tmp/mnt_backing", MNT_DETACH); /* 1. Pin main thread strictly to CPU 0 */ cpu_set_t cpuset0; CPU_ZERO(&cpuset0); CPU_SET(0, &cpuset0); int res = sched_setaffinity(0, sizeof(cpuset0), &cpuset0); if (res < 0) { printf("[-] Failed to sched_setaffinity to CPU 0: %s\n", strerror(errno)); exit(1); } printf("[+] Pin main process to CPU 0 successful.\n"); /* 2. Mount debugfs if not already mounted */ mkdir("/sys/kernel/debug", 0755); res = mount("nodev", "/sys/kernel/debug", "debugfs", 0, NULL); if (res < 0 && errno != EBUSY) { printf("[-] Note: mount debugfs at /sys/kernel/debug: %s\n", strerror(errno)); } else { printf("[+] debugfs available at /sys/kernel/debug.\n"); } /* 3. Save original scheduler and watchdog configurations, then apply overrides */ read_and_save("/sys/kernel/debug/sched/fair_server/cpu0/runtime", g_saved_fair_runtime, sizeof(g_saved_fair_runtime)); write_file("/sys/kernel/debug/sched/fair_server/cpu0/runtime", "0\n"); read_and_save("/proc/sys/kernel/sched_rt_runtime_us", g_saved_rt_runtime, sizeof(g_saved_rt_runtime)); write_file("/proc/sys/kernel/sched_rt_runtime_us", "-1\n"); read_and_save("/proc/sys/kernel/soft_watchdog", g_saved_soft_watchdog, sizeof(g_saved_soft_watchdog)); write_file("/proc/sys/kernel/soft_watchdog", "0\n"); printf("[+] Saved configuration and applied scheduler tuning.\n"); /* 4. Allocate loop device 0 via /dev/loop-control */ int loop_ctl_fd = open("/dev/loop-control", O_RDWR); if (loop_ctl_fd < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); exit(1); } printf("[+] open /dev/loop-control successful.\n"); int dev_nr0 = ioctl(loop_ctl_fd, LOOP_CTL_GET_FREE); if (dev_nr0 < 0) { printf("[-] Failed to ioctl(LOOP_CTL_GET_FREE) for dev0: %s\n", strerror(errno)); exit(1); } printf("[+] Allocated loop device index %d\n", dev_nr0); /* 5. Build Image 1 (backing.img: 8 MB = 8192 blocks, block size 1024) */ size_t img1_size = 8192 * 1024; uint8_t *img1 = calloc(1, img1_size); if (!img1) { printf("[-] Failed to allocate memory for img1: %s\n", strerror(errno)); exit(1); } build_backing_img(img1); printf("[+] Image 1 (backing) constructed in memory.\n"); /* 6. Write Image 1 to /tmp/backing.img */ int backing_fd = open("/tmp/backing.img", O_CREAT | O_RDWR | O_TRUNC, 0644); if (backing_fd < 0) { printf("[-] Failed to open /tmp/backing.img: %s\n", strerror(errno)); exit(1); } printf("[+] open /tmp/backing.img successful.\n"); ssize_t written = write(backing_fd, img1, img1_size); if (written != (ssize_t)img1_size) { printf("[-] Failed to write img1 to /tmp/backing.img: %s\n", strerror(errno)); exit(1); } printf("[+] write /tmp/backing.img successful (%zu bytes).\n", img1_size); free(img1); res = fsync(backing_fd); if (res < 0) { printf("[-] Failed to fsync /tmp/backing.img: %s\n", strerror(errno)); exit(1); } printf("[+] fsync /tmp/backing.img successful.\n"); /* 7. Bind /tmp/backing.img to loop0 */ char loop0_path[32]; snprintf(loop0_path, sizeof(loop0_path), "/dev/loop%d", dev_nr0); int loop0_fd = open_loop_dev(dev_nr0); if (loop0_fd < 0) { printf("[-] Failed to open %s: %s\n", loop0_path, strerror(errno)); exit(1); } printf("[+] open %s successful.\n", loop0_path); ioctl(loop0_fd, LOOP_CLR_FD, 0); res = ioctl(loop0_fd, LOOP_SET_FD, backing_fd); if (res < 0) { printf("[-] Failed to ioctl(LOOP_SET_FD) on %s: %s\n", loop0_path, strerror(errno)); exit(1); } printf("[+] LOOP_SET_FD on %s successful.\n", loop0_path); close(backing_fd); close(loop0_fd); /* 8. Mount loop0 to /tmp/mnt_backing */ res = mkdir("/tmp/mnt_backing", 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir /tmp/mnt_backing: %s\n", strerror(errno)); exit(1); } printf("[+] mkdir /tmp/mnt_backing successful.\n"); res = mount(loop0_path, "/tmp/mnt_backing", "ext4", 0, NULL); if (res < 0) { printf("[-] Failed to mount %s on /tmp/mnt_backing: %s\n", loop0_path, strerror(errno)); exit(1); } printf("[+] mount %s on /tmp/mnt_backing successful.\n", loop0_path); /* 9. Build Image 2 (target.img: 4 MB = 4096 blocks, intentional free blocks mismatch) */ size_t img2_size = 4096 * 1024; uint8_t *img2 = calloc(1, img2_size); if (!img2) { printf("[-] Failed to allocate memory for img2: %s\n", strerror(errno)); exit(1); } build_target_img(img2); printf("[+] Image 2 (target) constructed in memory.\n"); /* 10. Write Image 2 inside mounted backing filesystem at /tmp/mnt_backing/target.img */ int target_fd = open("/tmp/mnt_backing/target.img", O_CREAT | O_RDWR | O_TRUNC, 0644); if (target_fd < 0) { printf("[-] Failed to open /tmp/mnt_backing/target.img: %s\n", strerror(errno)); exit(1); } printf("[+] open /tmp/mnt_backing/target.img successful.\n"); written = write(target_fd, img2, img2_size); if (written != (ssize_t)img2_size) { printf("[-] Failed to write img2 to /tmp/mnt_backing/target.img: %s\n", strerror(errno)); exit(1); } printf("[+] write /tmp/mnt_backing/target.img successful (%zu bytes).\n", img2_size); free(img2); res = fsync(target_fd); if (res < 0) { printf("[-] Failed to fsync /tmp/mnt_backing/target.img: %s\n", strerror(errno)); exit(1); } printf("[+] fsync /tmp/mnt_backing/target.img successful.\n"); /* 11. Sequential allocation of loop device 1 */ int dev_nr1 = ioctl(loop_ctl_fd, LOOP_CTL_GET_FREE); if (dev_nr1 < 0 || dev_nr1 == dev_nr0) { dev_nr1 = dev_nr0 + 1; } close(loop_ctl_fd); printf("[+] Allocated loop device index %d\n", dev_nr1); char loop1_path[32]; snprintf(loop1_path, sizeof(loop1_path), "/dev/loop%d", dev_nr1); snprintf(g_devname1, sizeof(g_devname1), "%s", loop1_path); int loop1_fd = open_loop_dev(dev_nr1); if (loop1_fd < 0) { printf("[-] Failed to open %s: %s\n", loop1_path, strerror(errno)); exit(1); } printf("[+] open %s successful.\n", loop1_path); ioctl(loop1_fd, LOOP_CLR_FD, 0); res = ioctl(loop1_fd, LOOP_SET_FD, target_fd); if (res < 0) { printf("[-] Failed to ioctl(LOOP_SET_FD) on %s: %s\n", loop1_path, strerror(errno)); exit(1); } printf("[+] LOOP_SET_FD on %s successful.\n", loop1_path); close(target_fd); close(loop1_fd); /* 12. Mount loop1 on /tmp/mnt_target with errors=continue,nodelalloc */ res = mkdir("/tmp/mnt_target", 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir /tmp/mnt_target: %s\n", strerror(errno)); exit(1); } printf("[+] mkdir /tmp/mnt_target successful.\n"); res = mount(loop1_path, "/tmp/mnt_target", "ext4", 0, "errors=continue,nodelalloc"); if (res < 0) { printf("[-] Failed to mount %s on /tmp/mnt_target: %s\n", loop1_path, strerror(errno)); exit(1); } printf("[+] mount %s on /tmp/mnt_target successful.\n", loop1_path); /* 13. Create helper thread pinned to CPU 1 */ int ncpus = sysconf(_SC_NPROCESSORS_ONLN); int helper_cpu = (ncpus > 1) ? 1 : 0; printf("[+] Online CPU count: %d, targeting CPU %d for helper thread.\n", ncpus, helper_cpu); pthread_t helper_th; res = pthread_create(&helper_th, NULL, helper_fn, (void *)(intptr_t)helper_cpu); if (res != 0) { printf("[-] Failed to create helper thread: %s\n", strerror(res)); exit(1); } printf("[+] Helper thread created and waiting for trigger.\n"); /* 14. Open /tmp/mnt_backing and freeze it with FIFREEZE */ g_backing_mnt_fd = open("/tmp/mnt_backing", O_RDONLY); if (g_backing_mnt_fd < 0) { printf("[-] Failed to open /tmp/mnt_backing for FIFREEZE: %s\n", strerror(errno)); exit(1); } printf("[+] open /tmp/mnt_backing for FIFREEZE successful.\n"); res = ioctl(g_backing_mnt_fd, FIFREEZE, 0); if (res < 0) { printf("[-] Failed to ioctl(FIFREEZE) on /tmp/mnt_backing: %s\n", strerror(errno)); exit(1); } printf("[+] ioctl(FIFREEZE) on /tmp/mnt_backing successful.\n"); /* * 15. On CPU 0, trigger buddy generation corruption on loop1. * Reading mb_groups triggers ext4_mb_generate_buddy() which detects the * intentional block bitmap vs descriptor count mismatch and calls * schedule_work(&sbi->s_sb_upd_work), queueing work to CPU 0's pool. * CRITICAL: Do NOT perform any file operations on /tmp/mnt_target. */ char mb_path[64]; const char *devpart = strrchr(g_devname1, '/'); if (devpart) devpart++; else devpart = g_devname1; snprintf(mb_path, sizeof(mb_path), "/proc/fs/ext4/%s/mb_groups", devpart); printf("[*] Opening %s to trigger filesystem error...\n", mb_path); int mbfd = open(mb_path, O_RDONLY); if (mbfd >= 0) { char b[256]; ssize_t nr = read(mbfd, b, sizeof(b) - 1); if (nr < 0) { printf("[-] Failed read from %s: %s\n", mb_path, strerror(errno)); } else { printf("[+] Read %zd bytes from %s successfully.\n", nr, mb_path); } close(mbfd); } else { printf("[-] Failed to open %s: %s\n", mb_path, strerror(errno)); } /* 16. Sleep 500ms to allow background work to execute and block in wait_on_buffer */ printf("[*] Sleeping 500ms to allow background work to enter wait_on_buffer...\n"); usleep(500000); /* 17. Elevate main thread on CPU 0 to SCHED_FIFO with priority 90 */ struct sched_param sp = { .sched_priority = 90 }; res = sched_setscheduler(0, SCHED_FIFO, &sp); if (res < 0) { printf("[-] Failed to sched_setscheduler SCHED_FIFO 90: %s\n", strerror(errno)); } else { printf("[+] sched_setscheduler SCHED_FIFO (prio 90) successful on CPU 0.\n"); } /* 18. Signal helper thread on CPU 1 to thaw backing fs and queue work */ printf("[*] Signaling helper thread on CPU %d...\n", helper_cpu); __atomic_store_n(&g_helper_cmd, 1, __ATOMIC_RELEASE); /* 19. Monopolize CPU 0 with an uninterrupted userspace spin loop for 15 seconds */ printf("[*] Main thread spinning on CPU 0 with SCHED_FIFO for 15s...\n"); struct timespec ts_start, ts_cur; clock_gettime(CLOCK_MONOTONIC, &ts_start); while (1) { clock_gettime(CLOCK_MONOTONIC, &ts_cur); if (ts_cur.tv_sec - ts_start.tv_sec >= 15) break; __asm__ volatile("" : : : "memory"); } printf("[+] Finished 15s spin on CPU 0.\n"); /* 20. Restore normal SCHED_OTHER scheduling */ struct sched_param norm_param = { .sched_priority = 0 }; res = sched_setscheduler(0, SCHED_OTHER, &norm_param); if (res < 0) { printf("[-] Failed to restore SCHED_OTHER: %s\n", strerror(errno)); } else { printf("[+] Restored normal SCHED_OTHER scheduling.\n"); } /* 21. Restore scheduler and watchdog configurations */ if (g_saved_fair_runtime[0]) write_file("/sys/kernel/debug/sched/fair_server/cpu0/runtime", g_saved_fair_runtime); if (g_saved_rt_runtime[0]) write_file("/proc/sys/kernel/sched_rt_runtime_us", g_saved_rt_runtime); if (g_saved_soft_watchdog[0]) write_file("/proc/sys/kernel/soft_watchdog", g_saved_soft_watchdog); printf("[+] Restored configuration values.\n"); /* Wait for helper thread to finish */ pthread_join(helper_th, NULL); close(g_backing_mnt_fd); /* Sleep briefly to let pending work items drain */ sleep(1); printf("[+] Reproducer finished wait cycle.\n"); return 0; }