ntfs: guard the final LZNT1 sub-block header read
PATCH 1/1 ntfs: guard the final LZNT1 sub-block header read
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.logThe 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.
--- fs/ntfs/compress.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/ntfs/compress.c +4 -3
@@ -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;
Signature · 1 line
2.43.0
Hyunchul Lee
maintainerQuoted 102 lines · shaos
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.38Related 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.shBuild 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.logThe image builder creates a positive image and a one-byte control. The initramfs builder compiles
poc.cstatically. The PoC mounts the positive read-only NTFS image as/dev/vdaand 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 <[email protected]>--- fs/ntfs/compress.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
fs/ntfs/compress.c do_next_sb: · line 207
Quoted 10 lines
fs/ntfs/compress.c +3
-3@@ -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?
Quoted 8 lines
fs/ntfs/compress.c +1
-0+ !le16_to_cpup((__le16 *)cb) || (*dest_index == dest_max_index && *dest_ofs == dest_max_ofs)) {int i;Signature · 1 line
2.43.0
Signature · 2 lines
Thanks, Hyunchul
PATCH v2 1/1 ntfs: guard the final LZNT1 sub-block header read
In reply to shaosThe 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'.
--- fs/ntfs/compress.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/ntfs/compress.c +8 -3
@@ -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);
Signature · 1 line
2.34.1
Quoted 40 lines · 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
-EOVERFLOWfor 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 <[email protected]>--- fs/ntfs/compress.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)fs/ntfs/compress.c +4
-3@@ -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)) {
fs/ntfs/compress.c do_next_tag: · line 394
Quoted 10 lines
fs/ntfs/compress.c +4
-0@@ -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.
Quoted 9 lines · 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
-EOVERFLOWfor 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.
Quoted 7 lines
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 <[email protected]>
Looks good to me.
Signature · 2 lines
Thanks, Hyunchul
Namjae Jeon
maintainerQuoted 10 lines · 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
-EOVERFLOWfor 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.
Quoted 7 lines
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 <[email protected]>
Please tell me your real name so I can use it in the Signed-off-by line. Thanks.