From: Tanuj Sharma <ertanuj96@gmail.com>
To: ntfs3@lists.linux.dev
Cc: almaz.alexandrovich@paragon-software.com,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
gregkh@linuxfoundation.org, Tanuj Sharma <ertanuj96@gmail.com>
Subject: [PATCH v4] ntfs3: fix out-of-bounds write in do_action() bitmap operations
Date: Mon, 23 Nov 2026 11:54:50 +0530 [thread overview]
Message-ID: <20261123062450.5153-1-ertanuj96@gmail.com> (raw)
In-Reply-To: <2026112152-delivery-synopsis-ada7@gregkh>
In do_action(), when replaying log records for ATTR_ALLOC attributes,
the buffer is allocated based on bytes_per_index (typically 4096 bytes,
kmalloc-4k) but bitmap operation bounds checking uses lco -- a larger
value derived from lcns_follow << cluster_bits (typically 8192).
This allows SetBitsInNonresidentBitMap and ClearBitsInNonresidentBitMap
opcodes to write past the allocated buffer into the adjacent slab object
when processing crafted NTFS images with malicious $LogFile records.
The bounds check compares against lco (the full LCN extent size) but
should compare against the actual buffer capacity. For ATTR_ALLOC, the
buffer is only bytes_per_index bytes, allocated here:
if (attr->type == ATTR_ALLOC) {
t32 = le32_to_cpu(oe->bytes_per_index);
if (bytes < t32)
bytes = t32;
}
buffer_le = kmalloc(bytes, GFP_NOFS);
But the bounds check uses the larger lco:
if (cbo + (off + 7) / 8 > lco || ...)
With bytes_per_index=4096 and lco=8192, any bitmap_off >= 32768
(byte offset 4096) passes the lco check but writes past the buffer.
Fix this by computing the bounds limit as the minimum of the buffer
extent (cbo + bytes_per_index) and the LCN extent (lco). This ensures
bitmap operations cannot exceed either the allocated buffer or the
on-disk extent, and also prevents a crafted bytes_per_index larger
than lco from weakening the existing check.
Found by code audit and confirmed with a crafted NTFS image that
triggers the OOB write on mount.
Cc: stable@vger.kernel.org
Fixes: b46acd6a6a62 ("fs/ntfs3: Add NTFS journal")
Signed-off-by: Tanuj Sharma <ertanuj96@gmail.com>
---
Changes in v4:
- Removed unnecessary braces around single goto statements
fs/ntfs3/fslog.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/fs/ntfs3/fslog.c b/fs/ntfs3/fslog.c
index 1879d04e3cd05..0f83e4e8dbb9d 100644
--- a/fs/ntfs3/fslog.c
+++ b/fs/ntfs3/fslog.c
@@ -3125,6 +3125,7 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
u16 roff = le16_to_cpu(lrh->record_off);
u16 aoff = le16_to_cpu(lrh->attr_off);
u64 lco = 0;
+ u64 bmp_limit;
u64 cbo = (u64)le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
u64 tvo = le64_to_cpu(lrh->target_vcn) << sbi->cluster_bits;
u64 vbo = cbo + tvo;
@@ -3751,10 +3752,13 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
- if (cbo + (off + 7) / 8 > lco ||
- cbo + ((off + bits + 7) / 8) > lco) {
+ bmp_limit = attr->type == ATTR_ALLOC ?
+ min(cbo + (u64)le32_to_cpu(oe->bytes_per_index), lco) :
+ lco;
+
+ if (cbo + (off + 7) / 8 > bmp_limit ||
+ cbo + ((off + bits + 7) / 8) > bmp_limit)
goto dirty_vol;
- }
ntfs_bitmap_set_le(Add2Ptr(buffer_le, roff), off, bits);
a_dirty = true;
@@ -3764,10 +3768,13 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
- if (cbo + (off + 7) / 8 > lco ||
- cbo + ((off + bits + 7) / 8) > lco) {
+ bmp_limit = attr->type == ATTR_ALLOC ?
+ min(cbo + (u64)le32_to_cpu(oe->bytes_per_index), lco) :
+ lco;
+
+ if (cbo + (off + 7) / 8 > bmp_limit ||
+ cbo + ((off + bits + 7) / 8) > bmp_limit)
goto dirty_vol;
- }
ntfs_bitmap_clear_le(Add2Ptr(buffer_le, roff), off, bits);
a_dirty = true;
--
2.43.0
prev parent reply other threads:[~2026-11-23 6:24 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 2:22 [PATCH] ntfs3: fix out-of-bounds write in do_action() bitmap operations Tanuj Sharma
2026-11-21 9:43 ` Greg KH
2026-11-21 11:55 ` [PATCH v2] " Tanuj Sharma
2026-11-21 12:01 ` Greg KH
2026-11-21 12:24 ` [PATCH v3] " Tanuj Sharma
2026-11-21 12:57 ` Greg KH
2026-11-21 12:57 ` Greg KH
2026-11-23 6:24 ` Tanuj Sharma [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123062450.5153-1-ertanuj96@gmail.com \
--to=ertanuj96@gmail.com \
--cc=almaz.alexandrovich@paragon-software.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ntfs3@lists.linux.dev \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox