mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ntfs: guard the final LZNT1 sub-block header read
@ 2026-11-25  5:17 shaos
  2026-11-25  7:39 ` Hyunchul Lee
  2026-11-25  8:50 ` [PATCH v2] " shaos
  0 siblings, 2 replies; 4+ messages in thread
From: shaos @ 2026-11-25  5:17 UTC (permalink / raw)
  To: Namjae Jeon, Hyunchul Lee; +Cc: ntfs, linux-kernel

Hello,

The legacy NTFS LZNT1 decompressor reads a 16-bit sub-block header
before checking that two bytes remain in the compressed buffer.  If
one byte remains, the read crosses the end of the 64 KiB
compression buffer.  A crafted compressed file on an NTFS volume can
reproduce this as a kernel page fault while the file is read.

Check the remaining input length before dereferencing the header.  This
preserves the existing end-of-buffer handling while avoiding the read
when fewer than two bytes remain.

HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
source version: Linux 7.4.0-rc4
compiler:       gcc 11.4.0, GNU ld 2.38

Related work: ntfs-3g has a separate userspace LZNT1 bounds fix that
checks for two remaining input bytes before reading a sub-block header:
https://github.com/tuxera/ntfs-3g/commit/6aa66d98980ac1d966d7977c7b079f4fe380eb42
This report concerns the in-kernel fs/ntfs implementation and is backed
by a kernel reproducer against the revision above.

Console output:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/console-output.txt
Kernel config:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/kernel-config.txt
PoC:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/poc.c
One-byte image control:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/one-byte-control-console-output.txt
Fixed-kernel control:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/fixed-kernel-console-output.txt
Image builder:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/build_image.py
Initramfs builder:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/build_initramfs.sh
QEMU runner:
https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/run_qemu.sh

Build the test kernel from the specified commit and config.  Required
tools are gcc, make, cpio, gzip, QEMU, and the ntfs-3g tools mkntfs and
ntfscp.  Run these commands to download the Gist files and build the
kernel, create the images, and run the positive and control cases:

    work=$(mktemp -d /tmp/ntfs-lznt.XXXXXX)
    cd "$work"
    gist_id=acbe634bf90826a626ed95d0f32b5ab4
    gist_revision=ada97c11fe984e4bf47b9c93a0d90d30766ca3c4
    gist_base=https://gist.githubusercontent.com/shaos26
    gist=$gist_base/$gist_id/raw/$gist_revision
    for file in poc.c build_image.py build_initramfs.sh run_qemu.sh; do
        curl -fL "$gist/$file" -o "$file"
    done
    git clone \
        https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
    cd linux
    git checkout 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
    mkdir build
    curl -fL "$gist/kernel-config.txt" -o build/.config
    make O=build olddefconfig
    make O=build -j"$(nproc)" bzImage
    cd ..
    python3 build_image.py images
    ./build_initramfs.sh poc-init initramfs.cpio.gz
    ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
        initramfs.cpio.gz images/positive.img positive.log
    ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
        initramfs.cpio.gz images/control.img control.log

The image builder creates a positive image and a one-byte control.  The
initramfs builder compiles poc.c statically.  The PoC mounts
the positive read-only NTFS image as /dev/vda and seeds the shared
buffer by reading offset 0, then reads offset 65536 to trigger the
fault.

The positive image faults in ntfs_decompress() at the end of the 64 KiB
vmalloc buffer.  In the preserved run, CR2 is the first byte after the
allocation and RDX is CR2 - 1; the faulting instruction loads a 16-bit
value from RDX.  The one-byte image control completes without a fault.
The proposed patch was built and tested against the same positive image;
the fixed kernel completes and returns byte 'A'.

The trigger requires mounting a crafted legacy NTFS volume and reading a
compressed file.  It can crash the kernel on the tested x86-64 layout.
This is a local denial of service; no information disclosure or code
execution is demonstrated.

The range check uses the remaining length rather than forming a pointer
beyond cb_end.  The Gist includes the positive, one-byte control, and
fixed-kernel console logs.

Fixes: 1e9ea7e04472 ("Revert \"fs: Remove NTFS classic\"")
Signed-off-by: shaos <shawnq1@163.com>
---
 fs/ntfs/compress.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
--- a/fs/ntfs/compress.c
+++ b/fs/ntfs/compress.c
@@ -201,10 +201,11 @@ do_next_sb:
 	/*
 	 * Have we reached the end of the compression block or the end of the
 	 * decompressed data?  The latter can happen for example if the current
-	 * position in the compression block is one byte before its end so the
-	 * first two checks do not detect it.
+	 * position in the compression block is fewer than two bytes before its
+	 * end, so a complete sub-block header is no longer available.
 	 */
-	if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
+	if (cb_end - cb < sizeof(__le16) ||
+	    !le16_to_cpup((__le16 *)cb) ||
 			(*dest_index == dest_max_index &&
 			*dest_ofs == dest_max_ofs)) {
 		int i;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ntfs: guard the final LZNT1 sub-block header read
  2026-11-25  5:17 [PATCH] ntfs: guard the final LZNT1 sub-block header read shaos
@ 2026-11-25  7:39 ` Hyunchul Lee
  2026-11-25  8:50 ` [PATCH v2] " shaos
  1 sibling, 0 replies; 4+ messages in thread
From: Hyunchul Lee @ 2026-11-25  7:39 UTC (permalink / raw)
  To: shaos; +Cc: Namjae Jeon, ntfs, linux-kernel

On Wed, Nov 25, 2026 at 05:17:20AM +0000, shaos wrote:
> Hello,
> 
> The legacy NTFS LZNT1 decompressor reads a 16-bit sub-block header
> before checking that two bytes remain in the compressed buffer.  If
> one byte remains, the read crosses the end of the 64 KiB
> compression buffer.  A crafted compressed file on an NTFS volume can
> reproduce this as a kernel page fault while the file is read.
> 
> Check the remaining input length before dereferencing the header.  This
> preserves the existing end-of-buffer handling while avoiding the read
> when fewer than two bytes remain.
> 
> HEAD commit:    8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
> git tree:       fe970b05333fb8e41da21e9e289f652abcc7cba7
> source version: Linux 7.4.0-rc4
> compiler:       gcc 11.4.0, GNU ld 2.38
> 
> Related work: ntfs-3g has a separate userspace LZNT1 bounds fix that
> checks for two remaining input bytes before reading a sub-block header:
> https://github.com/tuxera/ntfs-3g/commit/6aa66d98980ac1d966d7977c7b079f4fe380eb42
> This report concerns the in-kernel fs/ntfs implementation and is backed
> by a kernel reproducer against the revision above.
> 
> Console output:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/console-output.txt
> Kernel config:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/kernel-config.txt
> PoC:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/poc.c
> One-byte image control:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/one-byte-control-console-output.txt
> Fixed-kernel control:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/fixed-kernel-console-output.txt
> Image builder:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/build_image.py
> Initramfs builder:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/build_initramfs.sh
> QEMU runner:
> https://gist.githubusercontent.com/shaos26/acbe634bf90826a626ed95d0f32b5ab4/raw/ada97c11fe984e4bf47b9c93a0d90d30766ca3c4/run_qemu.sh
> 
> Build the test kernel from the specified commit and config.  Required
> tools are gcc, make, cpio, gzip, QEMU, and the ntfs-3g tools mkntfs and
> ntfscp.  Run these commands to download the Gist files and build the
> kernel, create the images, and run the positive and control cases:
> 
>     work=$(mktemp -d /tmp/ntfs-lznt.XXXXXX)
>     cd "$work"
>     gist_id=acbe634bf90826a626ed95d0f32b5ab4
>     gist_revision=ada97c11fe984e4bf47b9c93a0d90d30766ca3c4
>     gist_base=https://gist.githubusercontent.com/shaos26
>     gist=$gist_base/$gist_id/raw/$gist_revision
>     for file in poc.c build_image.py build_initramfs.sh run_qemu.sh; do
>         curl -fL "$gist/$file" -o "$file"
>     done
>     git clone \
>         https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
>     cd linux
>     git checkout 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
>     mkdir build
>     curl -fL "$gist/kernel-config.txt" -o build/.config
>     make O=build olddefconfig
>     make O=build -j"$(nproc)" bzImage
>     cd ..
>     python3 build_image.py images
>     ./build_initramfs.sh poc-init initramfs.cpio.gz
>     ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
>         initramfs.cpio.gz images/positive.img positive.log
>     ./run_qemu.sh linux/build/arch/x86/boot/bzImage \
>         initramfs.cpio.gz images/control.img control.log
> 
> The image builder creates a positive image and a one-byte control.  The
> initramfs builder compiles poc.c statically.  The PoC mounts
> the positive read-only NTFS image as /dev/vda and seeds the shared
> buffer by reading offset 0, then reads offset 65536 to trigger the
> fault.
> 
> The positive image faults in ntfs_decompress() at the end of the 64 KiB
> vmalloc buffer.  In the preserved run, CR2 is the first byte after the
> allocation and RDX is CR2 - 1; the faulting instruction loads a 16-bit
> value from RDX.  The one-byte image control completes without a fault.
> The proposed patch was built and tested against the same positive image;
> the fixed kernel completes and returns byte 'A'.
> 
> The trigger requires mounting a crafted legacy NTFS volume and reading a
> compressed file.  It can crash the kernel on the tested x86-64 layout.
> This is a local denial of service; no information disclosure or code
> execution is demonstrated.
> 
> The range check uses the remaining length rather than forming a pointer
> beyond cb_end.  The Gist includes the positive, one-byte control, and
> fixed-kernel console logs.
> 
> Fixes: 1e9ea7e04472 ("Revert \"fs: Remove NTFS classic\"")
> Signed-off-by: shaos <shawnq1@163.com>
> ---
>  fs/ntfs/compress.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
> --- a/fs/ntfs/compress.c
> +++ b/fs/ntfs/compress.c
> @@ -201,10 +201,11 @@ do_next_sb:
>  	/*
>  	 * Have we reached the end of the compression block or the end of the
>  	 * decompressed data?  The latter can happen for example if the current
> -	 * position in the compression block is one byte before its end so the
> -	 * first two checks do not detect it.
> +	 * position in the compression block is fewer than two bytes before its
> +	 * end, so a complete sub-block header is no longer available.
>  	 */
> -	if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
> +	if (cb_end - cb < sizeof(__le16) ||

Could we go to return_overflow if only one byte remains for the next
header? And the existing phrase-token path seems to need a simillar check.

Could you also format the commit messages with the Linux kernel patch
submission guidelines, and address the warnings reported by
scripts/checkpatch.pl?


> +	    !le16_to_cpup((__le16 *)cb) ||
>  			(*dest_index == dest_max_index &&
>  			*dest_ofs == dest_max_ofs)) {
>  		int i;
> -- 
> 2.43.0
> 
> 

-- 
Thanks,
Hyunchul

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] ntfs: guard the final LZNT1 sub-block header read
  2026-11-25  5:17 [PATCH] ntfs: guard the final LZNT1 sub-block header read shaos
  2026-11-25  7:39 ` Hyunchul Lee
@ 2026-11-25  8:50 ` shaos
  2026-11-25 12:41   ` liubaolin
  1 sibling, 1 reply; 4+ messages in thread
From: shaos @ 2026-11-25  8:50 UTC (permalink / raw)
  To: linkinjeon; +Cc: hyc.lee, ntfs, linux-kernel

The legacy NTFS LZNT1 decompressor reads a 16-bit sub-block header
before checking that the full header remains in the compression
buffer. If only one byte remains, the read crosses the end of the
64 KiB buffer.

Return -EOVERFLOW for an incomplete header while preserving successful
termination when the buffer is exactly exhausted. Validate phrase-token
length against the sub-block boundary before reading it.

Tested on x86-64 with QEMU. The original header-boundary reproducer
and a phrase-token boundary reproducer no longer fault; both malformed
streams are rejected with -EOVERFLOW. The one-byte control still
returns byte 'A'.

Fixes: 1e9ea7e04472 ("Revert "fs: Remove NTFS classic"")
Signed-off-by: shaos <shawnq1@163.com>
---
 fs/ntfs/compress.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
index 82c7c7d290737..c7c520d31c53d 100644
--- a/fs/ntfs/compress.c
+++ b/fs/ntfs/compress.c
@@ -200,10 +200,11 @@ do_next_sb:
 			cb - cb_start);
 	/*
 	 * Have we reached the end of the compression block or the end of the
-	 * decompressed data?  The latter can happen for example if the current
-	 * position in the compression block is one byte before its end so the
-	 * first two checks do not detect it.
+	 * decompressed data?
 	 */
+	if (cb != cb_end && cb_end - cb < sizeof(__le16))
+		goto return_overflow;
+
 	if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
 			(*dest_index == dest_max_index &&
 			*dest_ofs == dest_max_ofs)) {
@@ -384,6 +385,10 @@ do_next_tag:
 		for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
 			lg++;
 
+		/* Make sure the phrase token is fully within the sub-block. */
+		if (cb_sb_end - cb < sizeof(__le16))
+			goto return_overflow;
+
 		/* Get the phrase token into i. */
 		pt = le16_to_cpup((__le16 *)cb);
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ntfs: guard the final LZNT1 sub-block header read
  2026-11-25  8:50 ` [PATCH v2] " shaos
@ 2026-11-25 12:41   ` liubaolin
  0 siblings, 0 replies; 4+ messages in thread
From: liubaolin @ 2026-11-25 12:41 UTC (permalink / raw)
  To: shaos, linkinjeon; +Cc: hyc.lee, ntfs, linux-kernel



在 2026/11/25 16:50, shaos 写道:
> The legacy NTFS LZNT1 decompressor reads a 16-bit sub-block header
> before checking that the full header remains in the compression
> buffer. If only one byte remains, the read crosses the end of the
> 64 KiB buffer.
> 
> Return -EOVERFLOW for an incomplete header while preserving successful
> termination when the buffer is exactly exhausted. Validate phrase-token
> length against the sub-block boundary before reading it.
> 
> Tested on x86-64 with QEMU. The original header-boundary reproducer
> and a phrase-token boundary reproducer no longer fault; both malformed
> streams are rejected with -EOVERFLOW. The one-byte control still
> returns byte 'A'.
> 
> Fixes: 1e9ea7e04472 ("Revert "fs: Remove NTFS classic"")
> Signed-off-by: shaos <shawnq1@163.com>
> ---
>   fs/ntfs/compress.c | 11 ++++++++---
>   1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/ntfs/compress.c b/fs/ntfs/compress.c
> index 82c7c7d290737..c7c520d31c53d 100644
> --- a/fs/ntfs/compress.c
> +++ b/fs/ntfs/compress.c
> @@ -200,10 +200,11 @@ do_next_sb:
>   			cb - cb_start);
>   	/*
>   	 * Have we reached the end of the compression block or the end of the
> -	 * decompressed data?  The latter can happen for example if the current
> -	 * position in the compression block is one byte before its end so the
> -	 * first two checks do not detect it.
> +	 * decompressed data?
>   	 */
> +	if (cb != cb_end && cb_end - cb < sizeof(__le16))
> +		goto return_overflow;
> +
>   	if (cb == cb_end || !le16_to_cpup((__le16 *)cb) ||
>   			(*dest_index == dest_max_index &&
>   			*dest_ofs == dest_max_ofs)) {
> @@ -384,6 +385,10 @@ do_next_tag:
>   		for (i = *dest_ofs - do_sb_start - 1; i >= 0x10; i >>= 1)
>   			lg++;
>   
> +		/* Make sure the phrase token is fully within the sub-block. */
> +		if (cb_sb_end - cb < sizeof(__le16))
> +			goto return_overflow;
> +
>   		/* Get the phrase token into i. */
>   		pt = le16_to_cpup((__le16 *)cb);
>   

Looks good to me.

Reviewed-by: Baolin Liu <liubaolin@kylinos.cn>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-25 12:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25  5:17 [PATCH] ntfs: guard the final LZNT1 sub-block header read shaos
2026-11-25  7:39 ` Hyunchul Lee
2026-11-25  8:50 ` [PATCH v2] " shaos
2026-11-25 12:41   ` liubaolin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®