// https://syzkaller.appspot.com/bug?id=6a35ebf9b87aff4076464774493363563a779a5d #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SNAP_MAGIC 0x70416e53 #define SNAPSHOT_DISK_VERSION 1 #define TABLE_CHUNK_SECTORS 64U /* 32 KB in 512-byte sectors */ #define ALLOC_BLOCK_SIZE (32 * 1024 * 1024) #define MAX_CHUNKS 2048 static void *held_chunks[MAX_CHUNKS]; static int num_held_chunks = 0; struct disk_header { uint32_t magic; uint32_t valid; uint32_t version; uint32_t chunk_size; } __attribute__((packed)); static unsigned long get_mem_available_kb(void) { FILE *f = fopen("/proc/meminfo", "r"); if (!f) { printf("[-] Failed to fopen /proc/meminfo: %s\n", strerror(errno)); exit(1); } char line[256]; unsigned long mem_avail = 0; while (fgets(line, sizeof(line), f)) { if (sscanf(line, "MemAvailable: %lu kB", &mem_avail) == 1) break; } int res = fclose(f); if (res != 0) { printf("[-] Failed to fclose /proc/meminfo: %s\n", strerror(errno)); exit(1); } return mem_avail; } static void consume_memory_to_target_kb(unsigned long target_kb) { printf("[*] Consuming memory to reach target residual ~%lu MB...\n", target_kb / 1024); while (num_held_chunks < MAX_CHUNKS) { unsigned long cur_avail = get_mem_available_kb(); if (cur_avail <= target_kb || cur_avail - target_kb < (ALLOC_BLOCK_SIZE / 1024)) break; void *p = mmap(NULL, ALLOC_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { printf("[-] mmap failed: %s\n", strerror(errno)); break; } /* Dirty the allocated pages to force physical page allocation */ volatile char *cp = (volatile char *)p; for (size_t off = 0; off < ALLOC_BLOCK_SIZE; off += 4096) cp[off] = 1; held_chunks[num_held_chunks++] = p; } printf("[+] Consumed %d chunks (%d MB). Current MemAvailable: %lu MB\n", num_held_chunks, (int)(num_held_chunks * 32), get_mem_available_kb() / 1024); } static void consume_additional_chunks(int count) { printf("[*] Consuming %d additional %d MB chunk(s)...\n", count, (int)(ALLOC_BLOCK_SIZE / (1024 * 1024))); for (int i = 0; i < count && num_held_chunks < MAX_CHUNKS; i++) { void *p = mmap(NULL, ALLOC_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { printf("[-] Additional mmap failed: %s\n", strerror(errno)); break; } volatile char *cp = (volatile char *)p; for (size_t off = 0; off < ALLOC_BLOCK_SIZE; off += 4096) cp[off] = 1; held_chunks[num_held_chunks++] = p; } printf("[+] Current MemAvailable: %lu MB\n", get_mem_available_kb() / 1024); } static int get_and_prepare_loop_dev(int loop_ctl_fd, char *path_out, size_t path_out_size) { int nr = ioctl(loop_ctl_fd, LOOP_CTL_GET_FREE); if (nr < 0) { printf("[-] Failed ioctl LOOP_CTL_GET_FREE: %s\n", strerror(errno)); exit(1); } snprintf(path_out, path_out_size, "/dev/loop%d", nr); if (access(path_out, F_OK) < 0) { int res = mknod(path_out, S_IFBLK | 0660, makedev(LOOP_MAJOR, nr)); if (res < 0 && errno != EEXIST) { printf("[-] Failed mknod for %s: %s\n", path_out, strerror(errno)); exit(1); } } printf("[+] Successfully prepared loop device %s (nr=%d)\n", path_out, nr); return nr; } static void write_cow_header(int fd, uint32_t chunk_sectors) { char sector[512]; memset(sector, 0, sizeof(sector)); struct disk_header *dh = (struct disk_header *)sector; dh->magic = htole32(SNAP_MAGIC); /* Setting valid=0 marks snapshot invalidated so read_exceptions is bypassed if alloc_area succeeds */ dh->valid = htole32(0); dh->version = htole32(SNAPSHOT_DISK_VERSION); dh->chunk_size = htole32(chunk_sectors); ssize_t written = pwrite(fd, sector, sizeof(sector), 0); if (written < 0) { printf("[-] Failed pwrite COW header: %s\n", strerror(errno)); exit(1); } int res = fsync(fd); if (res < 0) { printf("[-] Failed fsync COW header: %s\n", strerror(errno)); exit(1); } printf("[+] Written COW header with chunk_size=%u sectors (%u MB)\n", chunk_sectors, (chunk_sectors * 512) / (1024 * 1024)); } static int dm_dev_remove(int dm_fd, const char *name) { struct dm_ioctl dmi; memset(&dmi, 0, sizeof(dmi)); dmi.version[0] = DM_VERSION_MAJOR; dmi.version[1] = 0; dmi.version[2] = 0; dmi.data_size = sizeof(dmi); dmi.data_start = sizeof(dmi); strncpy(dmi.name, name, sizeof(dmi.name) - 1); int res = ioctl(dm_fd, DM_DEV_REMOVE, &dmi); if (res < 0) { /* Non-fatal if device did not exist */ return -1; } printf("[+] ioctl DM_DEV_REMOVE succeeded for '%s'\n", name); return 0; } static int dm_dev_create(int dm_fd, const char *name) { struct dm_ioctl dmi; memset(&dmi, 0, sizeof(dmi)); dmi.version[0] = DM_VERSION_MAJOR; dmi.version[1] = 0; dmi.version[2] = 0; dmi.data_size = sizeof(dmi); dmi.data_start = sizeof(dmi); strncpy(dmi.name, name, sizeof(dmi.name) - 1); int res = ioctl(dm_fd, DM_DEV_CREATE, &dmi); if (res < 0) { printf("[-] Failed ioctl DM_DEV_CREATE for '%s': %s\n", name, strerror(errno)); exit(1); } printf("[+] ioctl DM_DEV_CREATE succeeded for '%s'\n", name); return 0; } static int dm_table_load_snapshot(int dm_fd, const char *name, const char *origin_dev, const char *cow_dev, uint32_t table_chunk_sectors) { char buf[4096]; memset(buf, 0, sizeof(buf)); struct dm_ioctl *dmi = (struct dm_ioctl *)buf; dmi->version[0] = DM_VERSION_MAJOR; dmi->version[1] = 0; dmi->version[2] = 0; dmi->data_size = sizeof(buf); dmi->data_start = sizeof(struct dm_ioctl); dmi->target_count = 1; strncpy(dmi->name, name, sizeof(dmi->name) - 1); struct dm_target_spec *spec = (struct dm_target_spec *)(buf + sizeof(struct dm_ioctl)); spec->sector_start = 0; spec->length = 2048; /* 1 MB */ spec->status = 0; spec->next = 0; strncpy(spec->target_type, "snapshot", sizeof(spec->target_type) - 1); char *target_params = (char *)(spec + 1); snprintf(target_params, sizeof(buf) - sizeof(struct dm_ioctl) - sizeof(struct dm_target_spec), "%s %s P %u", origin_dev, cow_dev, table_chunk_sectors); printf("[*] ioctl DM_TABLE_LOAD: loading snapshot params '%s'\n", target_params); int res = ioctl(dm_fd, DM_TABLE_LOAD, dmi); if (res < 0) { printf("[*] ioctl DM_TABLE_LOAD returned %d: %s\n", res, strerror(errno)); return -1; } printf("[+] ioctl DM_TABLE_LOAD succeeded\n"); return 0; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[+] Starting snapshot vfree double-free reproducer...\n"); /* Parse initial available memory to choose optimal target chunk size and threshold */ unsigned long mem_avail_kb = get_mem_available_kb(); unsigned long avail_mb = mem_avail_kb / 1024; printf("[+] Initial MemAvailable: %lu kB (~%lu MB)\n", mem_avail_kb, avail_mb); uint32_t large_chunk_mb = 512; unsigned long target_residual_mb = 880; if (avail_mb < 600) { large_chunk_mb = 128; target_residual_mb = 220; } else if (avail_mb < 1200) { large_chunk_mb = 256; target_residual_mb = 440; } uint32_t large_chunk_sectors = large_chunk_mb * 2048; printf("[+] Target on-disk chunk size: %u MB (%u sectors), target residual memory: %lu MB\n", large_chunk_mb, large_chunk_sectors, target_residual_mb); /* Open DM control device */ int dm_fd = open("/dev/mapper/control", O_RDWR); if (dm_fd < 0) { printf("[-] Failed to open /dev/mapper/control: %s\n", strerror(errno)); exit(1); } printf("[+] Successfully opened /dev/mapper/control (fd=%d)\n", dm_fd); /* Open Loop control device */ 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("[+] Successfully opened /dev/loop-control (fd=%d)\n", loop_ctl_fd); /* Create 4 GB sparse origin backing file */ int orig_file_fd = open("/tmp/snap_orig.img", O_RDWR | O_CREAT | O_TRUNC, 0666); if (orig_file_fd < 0) { printf("[-] Failed to open /tmp/snap_orig.img: %s\n", strerror(errno)); exit(1); } int res = ftruncate(orig_file_fd, 4ULL * 1024 * 1024 * 1024); if (res < 0) { printf("[-] Failed ftruncate on /tmp/snap_orig.img: %s\n", strerror(errno)); exit(1); } printf("[+] Created /tmp/snap_orig.img (4 GB sparse)\n"); /* Setup loop device for origin */ char loop0_path[32]; get_and_prepare_loop_dev(loop_ctl_fd, loop0_path, sizeof(loop0_path)); int loop0_fd = open(loop0_path, O_RDWR); if (loop0_fd < 0) { printf("[-] Failed to open %s: %s\n", loop0_path, strerror(errno)); exit(1); } ioctl(loop0_fd, LOOP_CLR_FD, 0); res = ioctl(loop0_fd, LOOP_SET_FD, orig_file_fd); if (res < 0) { printf("[-] Failed ioctl LOOP_SET_FD on %s: %s\n", loop0_path, strerror(errno)); exit(1); } printf("[+] Successfully bound %s to /tmp/snap_orig.img\n", loop0_path); /* Create 4 GB sparse COW backing file */ int cow_file_fd = open("/tmp/snap_cow.img", O_RDWR | O_CREAT | O_TRUNC, 0666); if (cow_file_fd < 0) { printf("[-] Failed to open /tmp/snap_cow.img: %s\n", strerror(errno)); exit(1); } res = ftruncate(cow_file_fd, 4ULL * 1024 * 1024 * 1024); if (res < 0) { printf("[-] Failed ftruncate on /tmp/snap_cow.img: %s\n", strerror(errno)); exit(1); } printf("[+] Created /tmp/snap_cow.img (4 GB sparse)\n"); /* Write on-disk snapshot header with the target large chunk size */ write_cow_header(cow_file_fd, large_chunk_sectors); /* Setup loop device for COW */ char loop1_path[32]; get_and_prepare_loop_dev(loop_ctl_fd, loop1_path, sizeof(loop1_path)); int loop1_fd = open(loop1_path, O_RDWR); if (loop1_fd < 0) { printf("[-] Failed to open %s: %s\n", loop1_path, strerror(errno)); exit(1); } ioctl(loop1_fd, LOOP_CLR_FD, 0); res = ioctl(loop1_fd, LOOP_SET_FD, cow_file_fd); if (res < 0) { printf("[-] Failed ioctl LOOP_SET_FD on %s: %s\n", loop1_path, strerror(errno)); exit(1); } printf("[+] Successfully bound %s to /tmp/snap_cow.img\n", loop1_path); /* Ensure loop1 also has the COW header written */ write_cow_header(loop1_fd, large_chunk_sectors); /* * Consume memory down to target residual level: * Target residual memory is chosen so that: * 1. The first allocation in alloc_area() (ps->area = vmalloc(large_chunk_mb)) * succeeds because large_chunk_mb < target_residual_mb. * 2. Available memory is reduced to target_residual_mb - large_chunk_mb. * 3. The second allocation (ps->zero_area = vzalloc(large_chunk_mb)) * fails because (target_residual_mb - large_chunk_mb) < large_chunk_mb. * 4. alloc_area() enters the err_zero_area unwinding path, calling vfree(ps->area) * without setting ps->area = NULL. * 5. snapshot_ctr() aborts and invokes dm_exception_store_destroy() -> persistent_dtr(), * which calls free_area() -> vfree(ps->area), triggering the double vfree warning! */ consume_memory_to_target_kb(target_residual_mb * 1024); const char *dm_dev_name = "test_snap"; for (int attempt = 0; attempt < 8; attempt++) { printf("\n[*] === Iteration %d: Current MemAvailable: %lu MB ===\n", attempt + 1, get_mem_available_kb() / 1024); /* Ensure clean state for DM target */ dm_dev_remove(dm_fd, dm_dev_name); /* Create DM device */ dm_dev_create(dm_fd, dm_dev_name); /* * Load snapshot table with TABLE_CHUNK_SECTORS (64 = 32 KB). * read_header() initial read is only 32 KB, avoiding -EIO. * It discovers dh->chunk_size is large_chunk_sectors, frees the 32 KB area, * and calls alloc_area() for the large chunk size. */ int load_res = dm_table_load_snapshot(dm_fd, dm_dev_name, loop0_path, loop1_path, TABLE_CHUNK_SECTORS); if (load_res < 0) { printf("[+] DM_TABLE_LOAD failed with %s as expected!\n", strerror(errno)); printf("[+] vzalloc() failed after vmalloc() succeeded: double vfree() triggered!\n"); /* Allow kernel to print the warning before removing */ sleep(2); dm_dev_remove(dm_fd, dm_dev_name); break; } /* * If DM_TABLE_LOAD succeeded, memory was slightly too high. * Clean up and consume 1 additional 32 MB chunk to tighten memory. */ printf("[*] DM_TABLE_LOAD succeeded (both allocations fit); tightening memory...\n"); dm_dev_remove(dm_fd, dm_dev_name); consume_additional_chunks(1); } /* Cleanup loop devices and backing files */ printf("\n[+] Cleaning up loop devices, files, and held memory...\n"); ioctl(loop0_fd, LOOP_CLR_FD, 0); close(loop0_fd); ioctl(loop1_fd, LOOP_CLR_FD, 0); close(loop1_fd); close(cow_file_fd); close(orig_file_fd); close(loop_ctl_fd); close(dm_fd); unlink("/tmp/snap_orig.img"); unlink("/tmp/snap_cow.img"); for (int i = 0; i < num_held_chunks; i++) munmap(held_chunks[i], ALLOC_BLOCK_SIZE); sleep(2); printf("[+] Reproducer completed successfully.\n"); return 0; }