Skip to content

Commit da5b6cf

Browse files
fix: add integer overflow checks for calloc in ggml_backend_graph_copy
- Added overflow checks before node_copies allocation - Added overflow checks before node_init allocation - Ensures safe memory allocation for graph copy operations - Added proper cleanup on overflow detection Addresses integer overflow vulnerability (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 1ab01a0 commit da5b6cf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,30 @@ static void graph_copy_init_tensor(struct ggml_hash_set * hash_set, struct ggml_
19161916
struct ggml_backend_graph_copy ggml_backend_graph_copy(ggml_backend_t backend, struct ggml_cgraph * graph) {
19171917
GGML_ASSERT(graph);
19181918
struct ggml_hash_set hash_set = ggml_hash_set_new(graph->visited_hash_set.size);
1919+
1920+
if (hash_set.size > SIZE_MAX / sizeof(node_copies[0])) {
1921+
GGML_LOG_ERROR("%s: integer overflow in node_copies allocation\n", __func__);
1922+
ggml_hash_set_free(&hash_set);
1923+
return {
1924+
/* .buffer = */ NULL,
1925+
/* .ctx_allocated = */ NULL,
1926+
/* .ctx_unallocated = */ NULL,
1927+
/* .graph = */ NULL,
1928+
};
1929+
}
19191930
struct ggml_tensor ** node_copies = (ggml_tensor **) calloc(hash_set.size, sizeof(node_copies[0])); // NOLINT
1931+
1932+
if (hash_set.size > SIZE_MAX / sizeof(node_init[0])) {
1933+
GGML_LOG_ERROR("%s: integer overflow in node_init allocation\n", __func__);
1934+
ggml_hash_set_free(&hash_set);
1935+
free(node_copies);
1936+
return {
1937+
/* .buffer = */ NULL,
1938+
/* .ctx_allocated = */ NULL,
1939+
/* .ctx_unallocated = */ NULL,
1940+
/* .graph = */ NULL,
1941+
};
1942+
}
19201943
bool * node_init = (bool *) calloc(hash_set.size, sizeof(node_init[0]));
19211944

19221945
struct ggml_init_params params = {

0 commit comments

Comments
 (0)