Skip to content

Commit ce92fbf

Browse files
author
Pascal Seitz
committed
add fuzz target, fix heap-buffer-overflow issue
1 parent f66fc4f commit ce92fbf

File tree

6 files changed

+31
-8
lines changed

6 files changed

+31
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ lz4-compress = "0.1.1"
3333
more-asserts = "0.2.1"
3434

3535
[features]
36-
default = ["safe-decode"]
36+
default = ["safe-decode", "safe-decode"]
3737
safe-decode = []
3838
safe-encode = []
3939

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ Executed on Macbook Pro 2017 i7
5050

5151
## Fuzzer
5252
This fuzz target fuzzes, and asserts compression and decompression returns the original input.
53-
`cargo fuzz run fuzz_target_1`
53+
`cargo fuzz run fuzz_roundtrip`
54+
55+
This fuzz target fuzzes, and asserts compression with cpp and decompression returns the original input.
56+
`cargo fuzz run fuzz_roundtrip_cpp_compress`
5457

5558

5659

fuzz/Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cargo-fuzz = true
1111

1212
[dependencies]
1313
libfuzzer-sys = "0.3"
14+
lz4 = "1.23.1"
1415

1516
[dependencies.lz4_flex]
1617
path = ".."
@@ -20,7 +21,13 @@ path = ".."
2021
members = ["."]
2122

2223
[[bin]]
23-
name = "fuzz_target_1"
24-
path = "fuzz_targets/fuzz_target_1.rs"
24+
name = "fuzz_roundtrip"
25+
path = "fuzz_targets/fuzz_roundtrip.rs"
26+
test = false
27+
doc = false
28+
29+
[[bin]]
30+
name = "fuzz_roundtrip_cpp_compress"
31+
path = "fuzz_targets/fuzz_roundtrip_cpp_compress.rs"
2532
test = false
2633
doc = false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
use lz4_flex::block::decompress::decompress_size_prepended;
5+
use lz4::block::compress as lz4_linked_block_compress;
6+
7+
fuzz_target!(|data: &[u8]| {
8+
// fuzzed code goes here
9+
let compressed = lz4_linked_block_compress(data, None, true).unwrap();
10+
let decompressed = decompress_size_prepended(&compressed).unwrap();
11+
assert_eq!(data, decompressed);
12+
});

src/block/decompress.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ pub fn decompress_size_prepended(input: &[u8]) -> Result<Vec<u8>, Error> {
326326
| (input[1] as usize) << 8
327327
| (input[2] as usize) << 16
328328
| (input[3] as usize) << 24;
329-
// Allocate a vector to contain the decompressed stream.
330-
let mut vec = Vec::with_capacity(uncompressed_size + 8);
329+
// Allocate a vector to contain the decompressed stream. we may wildcopy out of bounds, so the vector needs to have ad additional BLOCK_COPY_SIZE capacity
330+
let mut vec = Vec::with_capacity(uncompressed_size + BLOCK_COPY_SIZE);
331331
unsafe {
332332
vec.set_len(uncompressed_size);
333333
}
@@ -336,11 +336,12 @@ pub fn decompress_size_prepended(input: &[u8]) -> Result<Vec<u8>, Error> {
336336
Ok(vec)
337337
}
338338

339+
339340
/// Decompress all bytes of `input` into a new vec.
340341
#[inline]
341342
pub fn decompress(input: &[u8], uncompressed_size: usize) -> Result<Vec<u8>, Error> {
342-
// Allocate a vector to contain the decompressed stream.
343-
let mut vec = Vec::with_capacity(uncompressed_size + 8);
343+
// Allocate a vector to contain the decompressed stream. we may wildcopy out of bounds, so the vector needs to have ad additional BLOCK_COPY_SIZE capacity
344+
let mut vec = Vec::with_capacity(uncompressed_size + BLOCK_COPY_SIZE);
344345
unsafe {
345346
vec.set_len(uncompressed_size);
346347
}

0 commit comments

Comments
 (0)