* [PATCH] ext2: validate directory entries before rename
@ 2026-11-28 4:52 shaos
2026-11-28 5:08 ` sashiko-bot
2026-11-30 12:58 ` Jan Kara
0 siblings, 2 replies; 6+ 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] 6+ 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 2026-11-30 12:58 ` Jan Kara 1 sibling, 0 replies; 6+ 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] 6+ 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 @ 2026-11-30 12:58 ` Jan Kara 2026-12-01 14:18 ` [PATCH v2] " Shawn Shao 1 sibling, 1 reply; 6+ messages in thread From: Jan Kara @ 2026-11-30 12:58 UTC (permalink / raw) To: shaos; +Cc: jack, linux-ext4 On Sat 28-11-26 04:52:14, shaos wrote: > 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. Hum, rather than adding the validation to ext2_dotdot(), I'd prefer to modify ext2_check_folio() to validate . and .. entries (if this is the first directory folio) so that we have all the validation in one place. Honza > 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) { > -- Jan Kara <jack@suse.com> SUSE Labs, CR ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] ext2: validate directory entries before rename 2026-11-30 12:58 ` Jan Kara @ 2026-12-01 14:18 ` Shawn Shao 2026-12-01 14:35 ` sashiko-bot 2026-12-01 14:42 ` Jan Kara 0 siblings, 2 replies; 6+ messages in thread From: Shawn Shao @ 2026-12-01 14:18 UTC (permalink / raw) To: Jan Kara; +Cc: linux-ext4 Hello Jan, Thanks for the suggestion. I moved validation of the `.` and `..` records to `ext2_check_folio()` for the first directory folio, after the existing record-boundary checks. `ext2_get_folio()` runs this validation before a caller can use a directory-entry pointer. The `..` inode comparison stays in `ext2_rename()`, where `old_dir` is available. The malformed image still makes cross-directory rename return `-EIO`; the valid-image rename control succeeds. I also rebuilt the full x86-64 kernel and ran both cases in QEMU. Tested on Linux 7.4-rc4, commit 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0. The reproducer, image builder, kernel configuration, and updated console logs are collected here: https://gist.github.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8 Changes in v2: - Validate the first two directory entries in `ext2_check_folio()`. - Keep the expected-parent check in `ext2_rename()`. Thanks, Shawn Shao Signed-off-by: Shawn Shao <shawnq1@163.com> --- fs/ext2/dir.c | 28 +++++++++++++++++++++++++++- fs/ext2/namei.c | 6 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c index 5de32c221a83b..10f33a2de0acc 100644 --- a/fs/ext2/dir.c +++ b/fs/ext2/dir.c @@ -112,9 +112,11 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) limit = offset_in_folio(folio, dir->i_size); if (limit & (chunk_size - 1)) goto Ebadsize; - if (!limit) + if (!limit && folio_pos(folio)) goto out; } + if (!limit) + goto bad_dot; for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { p = (ext2_dirent *)(kaddr + offs); rec_len = ext2_rec_len_from_disk(p->rec_len); @@ -132,6 +134,24 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) } if (offs != limit) goto Eend; + if (!folio_pos(folio)) { + unsigned int dot_len, dotdot_len; + + p = (ext2_dirent *)kaddr; + dot_len = ext2_rec_len_from_disk(p->rec_len); + if (p->name_len != 1 || p->name[0] != '.' || + le32_to_cpu(p->inode) != dir->i_ino || + dot_len > chunk_size - EXT2_DIR_REC_LEN(2)) + goto bad_dot; + + p = (ext2_dirent *)(kaddr + dot_len); + dotdot_len = ext2_rec_len_from_disk(p->rec_len); + if (p->name_len != 2 || p->name[0] != '.' || p->name[1] != '.' || + !le32_to_cpu(p->inode) || + dotdot_len < EXT2_DIR_REC_LEN(2) || + dotdot_len > chunk_size - dot_len) + goto bad_dot; + } out: folio_set_checked(folio); return true; @@ -175,6 +195,12 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) dir->i_ino, folio_pos(folio) + offs, (unsigned long) le32_to_cpu(p->inode)); } + goto fail; +bad_dot: + if (!quiet) + ext2_error(sb, __func__, + "directory #%llu has invalid '.' or '..' entry", + dir->i_ino); fail: return false; } 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] 6+ messages in thread
* Re: [PATCH v2] ext2: validate directory entries before rename 2026-12-01 14:18 ` [PATCH v2] " Shawn Shao @ 2026-12-01 14:35 ` sashiko-bot 2026-12-01 14:42 ` Jan Kara 1 sibling, 0 replies; 6+ messages in thread From: sashiko-bot @ 2026-12-01 14:35 UTC (permalink / raw) To: Shawn Shao; +Cc: linux-ext4, tytso Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ext2: lax dot_len check in ext2_check_folio() lets ext2_add_link() split '.' - [Low] ext2: commit message formatted as email reply without problem description -- commit 9941f46a8139b486ee5aa49e86c4ccf959cce7a4 Author: Shawn Shao <shawnq1@163.com> > ext2: validate directory entries before rename > > Hello Jan, > > Thanks for the suggestion. I moved validation of the `.` and `..` records > to `ext2_check_folio()` for the first directory folio, after the existing > record-boundary checks. `ext2_get_folio()` runs this validation before a > caller can use a directory-entry pointer. The `..` inode comparison stays > in `ext2_rename()`, where `old_dir` is available. > > The malformed image still makes cross-directory rename return `-EIO`; the > valid-image rename control succeeds. I also rebuilt the full x86-64 kernel > and ran both cases in QEMU. > > Tested on Linux 7.4-rc4, commit > 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree > 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0. > The reproducer, image builder, kernel configuration, and updated console > logs are collected here: > https://gist.github.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8 > > Changes in v2: > - Validate the first two directory entries in `ext2_check_folio()`. > - Keep the expected-parent check in `ext2_rename()`. > > Thanks, > Shawn Shao > > Signed-off-by: Shawn Shao <shawnq1@163.com> [Severity: Low] The commit message is formatted as an email reply with v2 changelog notes rather than a standalone description of the bug being fixed. Conversational notes and version history belong below the --- separator, while the commit log should explain why missing or invalid "." and ".." entries cause a problem in ext2_dotdot() and ext2_rename(). > diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c > index 5de32c221a83b..10f33a2de0acc 100644 > --- a/fs/ext2/dir.c > +++ b/fs/ext2/dir.c > @@ -112,9 +112,11 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) > limit = offset_in_folio(folio, dir->i_size); > if (limit & (chunk_size - 1)) > goto Ebadsize; > - if (!limit) > + if (!limit && folio_pos(folio)) > goto out; > } > + if (!limit) > + goto bad_dot; > for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { > p = (ext2_dirent *)(kaddr + offs); > rec_len = ext2_rec_len_from_disk(p->rec_len); > @@ -132,6 +134,24 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) > } > if (offs != limit) > goto Eend; > + if (!folio_pos(folio)) { > + unsigned int dot_len, dotdot_len; > + > + p = (ext2_dirent *)kaddr; > + dot_len = ext2_rec_len_from_disk(p->rec_len); > + if (p->name_len != 1 || p->name[0] != '.' || > + le32_to_cpu(p->inode) != dir->i_ino || > + dot_len > chunk_size - EXT2_DIR_REC_LEN(2)) > + goto bad_dot; [Severity: Medium] Should dot_len be required to equal EXT2_DIR_REC_LEN(1) instead of only checking dot_len > chunk_size - EXT2_DIR_REC_LEN(2)? If a crafted image has "." at offset 0 with dot_len >= 24 and ".." at kaddr + dot_len, ext2_check_folio() marks the folio checked. When a new entry is later added to that directory, ext2_add_link() starts scanning at offset 0, sees rec_len >= name_len + reclen on ".", and splits "." to insert the new entry at offset 12 ahead of "..". After that, ext2_dotdot() returns the new entry instead of "..", causing a later cross-directory ext2_rename() or folio re-read to trigger ext2_error(). -- Sashiko AI review · https://sashiko.dev/#/patchset/179613469562.187341.6558824516172704292.1796134695.4-1-shawnq1@163.com?part=1 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] ext2: validate directory entries before rename 2026-12-01 14:18 ` [PATCH v2] " Shawn Shao 2026-12-01 14:35 ` sashiko-bot @ 2026-12-01 14:42 ` Jan Kara 1 sibling, 0 replies; 6+ messages in thread From: Jan Kara @ 2026-12-01 14:42 UTC (permalink / raw) To: Shawn Shao; +Cc: Jan Kara, linux-ext4 On Tue 01-12-26 14:18:15, Shawn Shao wrote: > Hello Jan, > > Thanks for the suggestion. I moved validation of the `.` and `..` records > to `ext2_check_folio()` for the first directory folio, after the existing > record-boundary checks. `ext2_get_folio()` runs this validation before a > caller can use a directory-entry pointer. The `..` inode comparison stays > in `ext2_rename()`, where `old_dir` is available. > > The malformed image still makes cross-directory rename return `-EIO`; the > valid-image rename control succeeds. I also rebuilt the full x86-64 kernel > and ran both cases in QEMU. > > Tested on Linux 7.4-rc4, commit > 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree > 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU 6.2.0. > The reproducer, image builder, kernel configuration, and updated console > logs are collected here: > https://gist.github.com/shaos26/afcee45e1ee5b8da6bbfdb8b642093a8 > > Changes in v2: > - Validate the first two directory entries in `ext2_check_folio()`. > - Keep the expected-parent check in `ext2_rename()`. > > Thanks, > Shawn Shao > > Signed-off-by: Shawn Shao <shawnq1@163.com> Thanks. I've merged your patch. Just the changelog wasn't really provided, it was more a description email so I had to write one myself. Honza > > --- > fs/ext2/dir.c | 28 +++++++++++++++++++++++++++- > fs/ext2/namei.c | 6 ++++++ > 2 files changed, 33 insertions(+), 1 deletion(-) > > diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c > index 5de32c221a83b..10f33a2de0acc 100644 > --- a/fs/ext2/dir.c > +++ b/fs/ext2/dir.c > @@ -112,9 +112,11 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) > limit = offset_in_folio(folio, dir->i_size); > if (limit & (chunk_size - 1)) > goto Ebadsize; > - if (!limit) > + if (!limit && folio_pos(folio)) > goto out; > } > + if (!limit) > + goto bad_dot; > for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) { > p = (ext2_dirent *)(kaddr + offs); > rec_len = ext2_rec_len_from_disk(p->rec_len); > @@ -132,6 +134,24 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) > } > if (offs != limit) > goto Eend; > + if (!folio_pos(folio)) { > + unsigned int dot_len, dotdot_len; > + > + p = (ext2_dirent *)kaddr; > + dot_len = ext2_rec_len_from_disk(p->rec_len); > + if (p->name_len != 1 || p->name[0] != '.' || > + le32_to_cpu(p->inode) != dir->i_ino || > + dot_len > chunk_size - EXT2_DIR_REC_LEN(2)) > + goto bad_dot; > + > + p = (ext2_dirent *)(kaddr + dot_len); > + dotdot_len = ext2_rec_len_from_disk(p->rec_len); > + if (p->name_len != 2 || p->name[0] != '.' || p->name[1] != '.' || > + !le32_to_cpu(p->inode) || > + dotdot_len < EXT2_DIR_REC_LEN(2) || > + dotdot_len > chunk_size - dot_len) > + goto bad_dot; > + } > out: > folio_set_checked(folio); > return true; > @@ -175,6 +195,12 @@ static bool ext2_check_folio(struct folio *folio, int quiet, char *kaddr) > dir->i_ino, folio_pos(folio) + offs, > (unsigned long) le32_to_cpu(p->inode)); > } > + goto fail; > +bad_dot: > + if (!quiet) > + ext2_error(sb, __func__, > + "directory #%llu has invalid '.' or '..' entry", > + dir->i_ino); > fail: > return false; > } > 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) { > -- Jan Kara <jack@suse.com> SUSE Labs, CR ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-01 14:42 UTC | newest] Thread overview: 6+ 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 2026-11-30 12:58 ` Jan Kara 2026-12-01 14:18 ` [PATCH v2] " Shawn Shao 2026-12-01 14:35 ` sashiko-bot 2026-12-01 14:42 ` Jan Kara
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox