Skip to content

Commit 165d162

Browse files
gyscosclaude
andcommitted
zstd-sys: handle allocation failure and free(NULL) in the wasm shim
wasm_shim_alloc() never checked the pointer returned by alloc(): on failure it wrote its size header to address 0 and returned address 4, which zstd accepted as a valid buffer and decoded into, corrupting the bottom of linear memory. It also did not check size + header for overflow. Conversely, rust_zstd_wasm_shim_free() read a size header below a NULL pointer, which the v0.5 legacy decoder passes. Return NULL on allocation failure or size overflow (zstd reports a memory_allocation error), and make free(NULL) a no-op. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MLP334TH. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent e00e792 commit 165d162

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎zstd-safe/zstd-sys/src/wasm_shim.rs‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ pub extern "C" fn rust_zstd_wasm_shim_calloc(
6969
size: usize,
7070
) -> *mut c_void {
7171
// note: calloc expects the allocation to be zeroed
72-
wasm_shim_alloc::<true>(nmemb * size)
72+
match nmemb.checked_mul(size) {
73+
Some(size) => wasm_shim_alloc::<true>(size),
74+
None => core::ptr::null_mut(),
75+
}
7376
}
7477

7578
#[inline]
@@ -79,18 +82,28 @@ fn wasm_shim_alloc<const ZEROED: bool>(size: usize) -> *mut c_void {
7982
// so it's not stored, and usize-alignment is used
8083
// memory layout: [size] [allocation]
8184

82-
let full_alloc_size = size + USIZE_SIZE;
85+
let full_alloc_size = match size.checked_add(USIZE_SIZE) {
86+
Some(full_alloc_size) => full_alloc_size,
87+
None => return core::ptr::null_mut(),
88+
};
89+
// Layout also checks that the aligned size fits in isize::MAX.
90+
let layout = match Layout::from_size_align(full_alloc_size, USIZE_ALIGN) {
91+
Ok(layout) => layout,
92+
Err(_) => return core::ptr::null_mut(),
93+
};
8394

8495
unsafe {
85-
let layout =
86-
Layout::from_size_align_unchecked(full_alloc_size, USIZE_ALIGN);
87-
8896
let ptr = if ZEROED {
8997
alloc_zeroed(layout)
9098
} else {
9199
alloc(layout)
92100
};
93101

102+
if ptr.is_null() {
103+
// zstd surfaces NULL as a memory_allocation error
104+
return core::ptr::null_mut();
105+
}
106+
94107
// SAFETY: ptr is usize-aligned and we've allocated sufficient memory
95108
ptr.cast::<usize>().write(full_alloc_size);
96109

@@ -104,6 +117,11 @@ pub unsafe extern "C" fn rust_zstd_wasm_shim_free(ptr: *mut c_void) {
104117
// - the size must be recovered from directly below the allocation
105118
// - the alignment will always by USIZE_ALIGN
106119

120+
// free(NULL) is a no-op, and zstd relies on it
121+
if ptr.is_null() {
122+
return;
123+
}
124+
107125
let alloc_ptr = ptr.sub(USIZE_SIZE);
108126
// SAFETY: the allocation routines must uphold having a valid usize below the provided pointer
109127
let full_alloc_size = alloc_ptr.cast::<usize>().read();

0 commit comments

Comments
 (0)