Skip to content

Commit f231693

Browse files
fix: add overflow check for tensor memory allocation in gguf.cpp
- gguf_init_from_file: Add bounds checking before multiplying n_tensors by ggml_tensor_overhead() Addresses 3 integer overflow vulnerabilities (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 577c235 commit f231693

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

ggml/src/gguf.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,11 @@ struct gguf_context * gguf_init_from_file_impl(FILE * file, struct gguf_init_par
649649
// the ggml_tensor structs to the appropriate locations in the binary blob
650650

651651
// compute the exact size needed for the new ggml_context
652+
if (n_tensors > SIZE_MAX / ggml_tensor_overhead()) {
653+
GGML_LOG_ERROR("%s: n_tensors too large for memory allocation\n", __func__);
654+
gguf_free(ctx);
655+
return nullptr;
656+
}
652657
const size_t mem_size =
653658
params.no_alloc ?
654659
(n_tensors )*ggml_tensor_overhead() :

0 commit comments

Comments
 (0)