fs/ntfs3: fix out-of-bounds page_lcns[] access on log replay
Subsystems (2)
v2 cover fs/ntfs3: fix out-of-bounds page_lcns[] access on log replay
While mounting a crafted NTFS image, log_replay() can index a dirty page entry's page_lcns[] array out of bounds. The array length is the on-disk lcns_follow field, which is never validated against the space actually available, so several replay paths read and write past the entry. There are two independent ways the index goes out of range, so the fix is split into two patches:
1/2 - The entry's own lcns_follow (from the on-disk dirty page table) is
not bounded against the entry size. check_dp_table() rejects any
such entry once, right after check_rstbl(), so every consumer can
trust lcns_follow as the real capacity. This closes the HotFix store
and the v0->v1 conversion memmove:
BUG: KASAN: slab-use-after-free in log_replay+0x55be/0xbbd0
Read of size 8 at addr ffff88800dbb28b8 by task mount
Call Trace:
kasan_report+0xce/0x100
log_replay+0x55be/0xbbd0
ntfs_loadlog_and_replay+0x4a4/0x5c0
ntfs_fill_super+0x2252/0x4570
get_tree_bdev_flags+0x373/0x5f0
vfs_get_tree+0x91/0x360
path_mount+0x75a/0x2120
__x64_sys_mount+0x298/0x310 2/2 - In the copy_lcns loop and the redo shorten loop the index is driven
by the *log record's* lcns_follow, which is checked only against the
record length, not the target entry. dp_range_ok() bounds the run
against the entry before each loop:
BUG: KASAN: slab-out-of-bounds in log_replay+0xb7e0/0xbbe0
Write of size 8 at addr ffff88800fff1518 by task mount
Call Trace:
kasan_report+0xce/0x100
log_replay+0xb7e0/0xbbe0
ntfs_loadlog_and_replay+0x4a4/0x5c0
ntfs_fill_super+0x2252/0x4570
get_tree_bdev_flags+0x373/0x5f0
vfs_get_tree+0x91/0x360
path_mount+0x75a/0x2120
__x64_sys_mount+0x298/0x310
The buggy address is 0 bytes to the right of a kmalloc-2k object.Both are present since ntfs3 was first added (v5.15) and are reachable from an unprivileged mount of a removable device on systems that allow it.
The two patches address distinct untrusted inputs (entry capacity vs. log record count): 1/2 alone does not cover the record-driven loops fixed in 2/2. Verified with two KASAN reproducers that mount a malformed image, one per patch:
reproducer without fix 1/2 only 1/2 + 2/2 ---------- ----------- -------- --------- hotfix crash fixed fixed copy_lcns crash crash fixed
The reproducers and crash logs will be sent as a follow up email.
Xiang Mei (2):
fs/ntfs3: validate dirty page table on log replay fs/ntfs3: bound page_lcns[] index by the log record
fs/ntfs3/fslog.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 fs/ntfs3: validate dirty page table on log replay
Each DIR_PAGE_ENTRY ends in a page_lcns[] array whose length is the on-disk lcns_follow field. check_rstbl() validates the table bookkeeping but never checks that this array fits in the entry, so a crafted lcns_follow lets the v0->v1 conversion memmove and later replay passes run off the entry.
Add check_dp_table() to reject, right after check_rstbl(), any entry larger than its size claims via struct_size() (the same expression used to allocate these entries, so the check is overflow-safe by construction). All consumers can then trust lcns_follow as the real capacity. This covers every page_lcns[] access whose index is bounded by the entry itself (the conversion memmove, the HotFix store via find_dp(), and the self-bounded scan loops). Accesses whose index comes from the log record need a separate bound and are handled in a follow-up patch.
---
v2: resend to public mailing list
fs/ntfs3/fslog.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
fs/ntfs3/fslog.c +19 -0
@@ -778,6 +778,20 @@ static bool check_rstbl(const struct RESTART_TABLE *rt, size_t bytes)return true; } +static bool check_dp_table (const struct RESTART_TABLE *dptbl) +{ +u32 rsize =le16_to_cpu (dptbl->size); +struct DIR_PAGE_ENTRY *dp =NULL ; + +while ((dp =enum_rstbl ((struct RESTART_TABLE *)dptbl, dp))) { +if (struct_size (dp, page_lcns,le32_to_cpu (dp->lcns_follow)) > + rsize) +return false; + } + +return true; +} +/* * free_rsttbl_idx - Free a previously allocated index a Restart Table. */
@@ -4209,6 +4223,11 @@ int log_replay(struct ntfs_inode *ni, bool *initialized) goto out; } +if (!check_dp_table (rt)) { + err = -EINVAL ; + goto out; + } + dptbl =kmemdup (rt, t32,GFP_NOFS );if (!dptbl) { err = -ENOMEM ;
Signature · 1 line
2.43.0
PATCH v2 2/2 fs/ntfs3: bound page_lcns[] index by the log record
In reply to Xiang MeiThe copy_lcns loop and the redo shorten loop index page_lcns[] at j + i, where i runs up to the log record's lcns_follow. That count is checked only against the record's own length, not the target entry, so check_dp_table() (which validates the entry's lcns_follow) does not cover it: the copy_lcns entry may even be freshly allocated after that check, and find_dp() bounds j but not i. A crafted record thus overflows page_lcns[] of an otherwise valid entry.
Add dp_range_ok() and reject, before each loop, any record whose run does not fit the entry. These are the only two page_lcns[] accesses indexed by the record rather than the entry, so together with the entry validation every access is now bounded.
---
v2: resend to public mailing list
fs/ntfs3/fslog.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
fs/ntfs3/fslog.c +21 -4
@@ -647,6 +647,14 @@ static inline void *enum_rstbl(struct RESTART_TABLE *t, void *c)return NULL ; } +/* +* dp_range_ok - true if [j, j + count) fits in a page_lcns[cap] array. +*/ +static inline bool dp_range_ok (size_t j,u32 count,u32 cap) +{ +return j < cap && count <= cap - j; +} +/* * find_dp - Search for a @vcn in Dirty Page Table. */
@@ -3801,6 +3809,7 @@ int log_replay(struct ntfs_inode *ni, bool *initialized)u64 t64;u16 t16;u32 t32; +size_t j; log = kzalloc_obj(struct ntfs_log, GFP_NOFS);if (!log)
@@ -4566,9 +4575,12 @@ int log_replay(struct ntfs_inode *ni, bool *initialized)* whole routine a loop, case Lcns do not fit below. */ t16 =le16_to_cpu (lrh->lcns_follow); + j =le64_to_cpu (lrh->target_vcn) -le64_to_cpu (dp->vcn); +if (!dp_range_ok (j, t16,le32_to_cpu (dp->lcns_follow))) { + err = -EINVAL ; + goto out; + }for (i =0 ; i < t16; i++) { -size_t j = (size_t )(le64_to_cpu (lrh->target_vcn) - -le64_to_cpu (dp->vcn)); dp->page_lcns[j + i] = lrh->page_lcns[i]; }
@@ -4985,8 +4997,14 @@ int log_replay(struct ntfs_inode *ni, bool *initialized)/* Shorten length by any Lcns which were deleted. */ saved_len = dlen; + j =le64_to_cpu (lrh->target_vcn) -le64_to_cpu (dp->vcn); +if (!dp_range_ok (j,le16_to_cpu (lrh->lcns_follow), +le32_to_cpu (dp->lcns_follow))) { + err = -EINVAL ; + goto out; + } +for (i =le16_to_cpu (lrh->lcns_follow); i; i--) { -size_t j;u32 alen, voff; voff = le16_to_cpu(lrh->record_off) +
@@ -4994,7 +5012,6 @@ int log_replay(struct ntfs_inode *ni, bool *initialized) voff +=le16_to_cpu (lrh->cluster_off) <<SECTOR_SHIFT ;/* If the Vcn question is allocated, we can just get out. */ - j =le64_to_cpu (lrh->target_vcn) -le64_to_cpu (dp->vcn);if (dp->page_lcns[j + i -1 ])break ;
Signature · 1 line
2.43.0
Considering the vulnerability is exploitable, we don't post the poc publicly. If you need the corresponding poc triggering the issues, we would like to DM and share the payload image, poc.c and steps to reproduce the bugs.
Thanks,
Xiang
Quoted 69 lines · Xiang Mei
While mounting a crafted NTFS image,
log_replay()can index a dirty page entry's page_lcns[] array out of bounds. The array length is the on-disklcns_followfield, which is never validated against the space actually available, so several replay paths read and write past the entry. There are two independent ways the index goes out of range, so the fix is split into two patches:1/2 - The entry's own
lcns_follow(from the on-disk dirty page table) isnot bounded against the entry size. check_dp_table() rejects any such entry once, right after check_rstbl(), so every consumer can trust lcns_follow as the real capacity. This closes the HotFix store and the v0->v1 conversion memmove: BUG: KASAN: slab-use-after-free in log_replay+0x55be/0xbbd0 Read of size 8 at addr ffff88800dbb28b8 by task mount Call Trace: kasan_report+0xce/0x100 log_replay+0x55be/0xbbd0 ntfs_loadlog_and_replay+0x4a4/0x5c0 ntfs_fill_super+0x2252/0x4570 get_tree_bdev_flags+0x373/0x5f0 vfs_get_tree+0x91/0x360 path_mount+0x75a/0x2120 __x64_sys_mount+0x298/0x3102/2 - In the
copy_lcnsloop and the redo shorten loop the index is drivenby the *log record's* lcns_follow, which is checked only against the record length, not the target entry. dp_range_ok() bounds the run against the entry before each loop: BUG: KASAN: slab-out-of-bounds in log_replay+0xb7e0/0xbbe0 Write of size 8 at addr ffff88800fff1518 by task mount Call Trace: kasan_report+0xce/0x100 log_replay+0xb7e0/0xbbe0 ntfs_loadlog_and_replay+0x4a4/0x5c0 ntfs_fill_super+0x2252/0x4570 get_tree_bdev_flags+0x373/0x5f0 vfs_get_tree+0x91/0x360 path_mount+0x75a/0x2120 __x64_sys_mount+0x298/0x310 The buggy address is 0 bytes to the right of a kmalloc-2k object.Both are present since ntfs3 was first added (v5.15) and are reachable from an unprivileged mount of a removable device on systems that allow it.
The two patches address distinct untrusted inputs (entry capacity vs. log record count): 1/2 alone does not cover the record-driven loops fixed in 2/2. Verified with two KASAN reproducers that mount a malformed image, one per patch:
reproducer without fix 1/2 only 1/2 + 2/2 ---------- ----------- -------- --------- hotfix crash fixed fixed copy_lcns crash crash fixedThe reproducers and crash logs will be sent as a follow up email.
Xiang Mei (2):
fs/ntfs3: validate dirty page table on log replay fs/ntfs3: bound page_lcns[] index by the log recordfs/ntfs3/fslog.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)--
2.43.0
Konstantin Komarov
maintainerQuoted 62 lines · Xiang Mei
While mounting a crafted NTFS image,
log_replay()can index a dirty page entry's page_lcns[] array out of bounds. The array length is the on-disklcns_followfield, which is never validated against the space actually available, so several replay paths read and write past the entry. There are two independent ways the index goes out of range, so the fix is split into two patches:1/2 - The entry's own lcns_follow (from the on-disk dirty page table) is not bounded against the entry size. check_dp_table() rejects any such entry once, right after check_rstbl(), so every consumer can trust lcns_follow as the real capacity. This closes the HotFix store and the v0->v1 conversion memmove: BUG: KASAN: slab-use-after-free in log_replay+0x55be/0xbbd0 Read of size 8 at addr ffff88800dbb28b8 by task mount Call Trace: kasan_report+0xce/0x100 log_replay+0x55be/0xbbd0 ntfs_loadlog_and_replay+0x4a4/0x5c0 ntfs_fill_super+0x2252/0x4570 get_tree_bdev_flags+0x373/0x5f0 vfs_get_tree+0x91/0x360 path_mount+0x75a/0x2120 __x64_sys_mount+0x298/0x310 2/2 - In the copy_lcns loop and the redo shorten loop the index is driven by the *log record's* lcns_follow, which is checked only against the record length, not the target entry. dp_range_ok() bounds the run against the entry before each loop: BUG: KASAN: slab-out-of-bounds in log_replay+0xb7e0/0xbbe0 Write of size 8 at addr ffff88800fff1518 by task mount Call Trace: kasan_report+0xce/0x100 log_replay+0xb7e0/0xbbe0 ntfs_loadlog_and_replay+0x4a4/0x5c0 ntfs_fill_super+0x2252/0x4570 get_tree_bdev_flags+0x373/0x5f0 vfs_get_tree+0x91/0x360 path_mount+0x75a/0x2120 __x64_sys_mount+0x298/0x310 The buggy address is 0 bytes to the right of a kmalloc-2k object.Both are present since ntfs3 was first added (v5.15) and are reachable from an unprivileged mount of a removable device on systems that allow it.
The two patches address distinct untrusted inputs (entry capacity vs. log record count): 1/2 alone does not cover the record-driven loops fixed in 2/2. Verified with two KASAN reproducers that mount a malformed image, one per patch:
reproducer without fix 1/2 only 1/2 + 2/2 ---------- ----------- -------- --------- hotfix crash fixed fixed copy_lcns crash crash fixedThe reproducers and crash logs will be sent as a follow up email.
Xiang Mei (2):
fs/ntfs3: validate dirty page table on log replay fs/ntfs3: bound page_lcns[] index by the log record
Quoted 6 lines
fs/ntfs3/fslog.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)--
2.43.0
Hello,
Your patches were applied, thank you.
Regards,
Konstantin