Hello, ext2_dotdot() advances from the first directory entry using its on-disk rec_len without checking that the resulting '..' entry fits in the directory block. A malformed directory can make ext2_rename() pass an out-of-block pointer to ext2_set_link(), which hits a kernel BUG in __block_write_begin_int(). The function also trusts the inode in '..' without checking that it still names old_dir. Validate the '.' and '..' records before returning the pointer, and reject a mismatched parent before changing the directory. The malformed image now makes rename return -EIO; valid cross-directory rename controls still succeed. HEAD commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc git tree: fe970b05333fb8e41da21e9e289f652abcc7cba7 source version: v7.3-23088-g8d7fe9885c62f (Linux 7.4.0-rc4) compiler: gcc 11.4.0, GNU ld 2.38 console output: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/console-output.txt kernel config: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/kernel-config.txt reproducer: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/poc.c image builder: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/mkimage.py build script: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/build.sh QEMU runner: https://gist.githubusercontent.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8/raw/9b9627e2a59163890b551da6521d211dc6eb5e2d/run.sh Download the Gist files into one working directory: gist_id=afcee45e1ee5b8da6bbfdb8b642093a8 gist_revision=9b9627e2a59163890b551da6521d211dc6eb5e2d gist_base=https://gist.githubusercontent.com/shaos26 gist=$gist_base/$gist_id/raw/$gist_revision for file in console-output.txt kernel-config.txt poc.c build.sh \ mkimage.py run.sh; do curl -fL "$gist/$file" -o "$file" done Clone the kernel and build the test kernel from the specified revision: git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git cd linux git checkout 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc mkdir build cp ../kernel-config.txt build/.config make O=build olddefconfig make O=build -j"$(nproc)" bzImage cd .. From the working directory holding the downloaded scripts, run: ./build.sh KERNEL=linux/build/arch/x86/boot/bzImage OUT=out \ WORK=/tmp/ext2-positive ./run.sh holenopm KERNEL=linux/build/arch/x86/boot/bzImage OUT=out \ WORK=/tmp/ext2-control ./run.sh holenopmcontrol The malformed image triggers the BUG in ext2_set_link() on the unfixed kernel. With the patch, rename returns -EIO; the valid-image control rename succeeds. The image and QEMU commands are included in the Gist scripts. The trigger requires mounting a crafted ext2 image and renaming a directory across parents. Mounting the image requires CAP_SYS_ADMIN. This reproduces a kernel BUG; it does not demonstrate privilege escalation or memory disclosure. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: shaos Thanks, shaos --- diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c index 5de32c221a83b..f0a23cbceed5e 100644 --- a/fs/ext2/dir.c +++ b/fs/ext2/dir.c @@ -412,10 +412,36 @@ struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir, */ struct ext2_dir_entry_2 *ext2_dotdot(struct inode *dir, struct folio **foliop) { + unsigned int chunk_size = ext2_chunk_size(dir); ext2_dirent *de = ext2_get_folio(dir, 0, 0, foliop); + unsigned int dot_len, dotdot_len; - if (!IS_ERR(de)) - return ext2_next_entry(de); + if (IS_ERR(de)) + return NULL; + + if (de->name_len != 1 || de->name[0] != '.' || + le32_to_cpu(de->inode) != dir->i_ino) + goto bad; + + dot_len = ext2_rec_len_from_disk(de->rec_len); + if (dot_len < EXT2_DIR_REC_LEN(1) || + dot_len > chunk_size - EXT2_DIR_REC_LEN(2)) + goto bad; + + de = ext2_next_entry(de); + dotdot_len = ext2_rec_len_from_disk(de->rec_len); + if (de->name_len != 2 || de->name[0] != '.' || de->name[1] != '.' || + !le32_to_cpu(de->inode) || dotdot_len < EXT2_DIR_REC_LEN(2) || + dotdot_len > chunk_size - dot_len) + goto bad; + + return de; + +bad: + ext2_error(dir->i_sb, __func__, + "directory #%llu has invalid '.' or '..' entry", + (unsigned long long)dir->i_ino); + folio_release_kmap(*foliop, de); return NULL; } diff --git a/fs/ext2/namei.c b/fs/ext2/namei.c index bfb6a463a95e0..a5b5aff1cfb45 100644 --- a/fs/ext2/namei.c +++ b/fs/ext2/namei.c @@ -350,6 +350,12 @@ static int ext2_rename (const struct mnt_idmap * idmap, dir_de = ext2_dotdot(old_inode, &dir_folio); if (!dir_de) goto out_old; + if (le32_to_cpu(dir_de->inode) != old_dir->i_ino) { + ext2_error(old_inode->i_sb, __func__, + "directory #%llu has incorrect '..' entry", + (unsigned long long)old_inode->i_ino); + goto out_dir; + } } if (new_inode) {