Re: [PATCH v2] ntfs3: fix out-of-bounds write in do_action() bitmap operations

From: Greg KH

Date: Sat Nov 21 2026 - 07:01:46 EST


On Sat, Nov 21, 2026 at 03:55:32AM -0800, Tanuj Sharma wrote:
> 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@xxxxxxxxxxxxxxx
> Fixes: b46acd6a6a62 ("fs/ntfs3: Add NTFS journal")
> Signed-off-by: Tanuj Sharma <ertanuj96@xxxxxxxxx>
> ---
> fs/ntfs3/fslog.c | 20 ++++++++++++++++----
> 1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/fs/ntfs3/fslog.c b/fs/ntfs3/fslog.c
> index 1879d04e3cd05..85590f56587c2 100644
> --- a/fs/ntfs3/fslog.c
> +++ b/fs/ntfs3/fslog.c
> @@ -3751,10 +3751,16 @@ 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) {
> + {
> + u64 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 +3770,16 @@ 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) {
> + {
> + u64 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;
> }
> + }

That's obviously a coding style issue, please fix.

Also, no v2 information below the --- line?

thanks,

greg k-h