* [PATCH] ext2: validate directory entries before rename
@ 2026-11-28 4:52 shaos
2026-11-28 5:08 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: shaos @ 2026-11-28 4:52 UTC (permalink / raw)
To: jack; +Cc: linux-ext4
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 <shawnq1@163.com>
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) {
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] ext2: validate directory entries before rename
2026-11-28 4:52 [PATCH] ext2: validate directory entries before rename shaos
@ 2026-11-28 5:08 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-28 5:08 UTC (permalink / raw)
To: shaos; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] ext2, ufs: i_nlink underflow in ext2_rmdir() and ufs_rmdir()
[1]
--
commit de08d578a699530700ffcced108cd1f48e146ca0
Author: shaos <shawnq1@163.com>
ext2: validate directory entries before rename
This commit validates the '.' and '..' entries in ext2_dotdot() and checks
that '..' points to old_dir in ext2_rename() before updating directory links.
> 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) {
[Severity: High]
This problem wasn't introduced by this patch, but can new_inode->i_nlink
underflow when overwriting an existing directory in ext2_rename()
[1]?
In the if (new_inode) block just below, overwriting a directory calls
drop_nlink(new_inode) followed immediately by
inode_dec_link_count(new_inode) (and ext2_rmdir() similarly calls
ext2_unlink() followed by inode_dec_link_count()):
fs/ext2/namei.c:ext2_rename() {
...
if (new_inode) {
...
if (old_is_dir)
drop_nlink(new_inode);
inode_dec_link_count(new_inode);
} else {
...
}
On a corrupted filesystem image where a directory inode has an on-disk
i_nlink of 0 or 1, these two unconditional decrements wrap
new_inode->i_nlink to UINT_MAX, triggering kernel warnings and corrupting
inode lifetime tracking.
[1] https://sashiko.dev/#/bug/linux-218ca4cb-3bec-4504-bb15-b7247a83953c
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261128045214.2649994-1-shawnq1@163.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-28 5:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 4:52 [PATCH] ext2: validate directory entries before rename shaos
2026-11-28 5:08 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox