Skip to content

Commit b551658

Browse files
fix: add integer overflow checks for realloc in ggml_backend_sched_split_graph
- Added overflow check before splits reallocation - Added overflow check before graph nodes/leafs reallocation - Ensures safe memory reallocation for scheduler operations - Prevents integer overflow in dynamic memory growth Addresses integer overflow vulnerability (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent da5b6cf commit b551658

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

ggml/src/ggml-backend.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,11 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra
11941194
i_split++;
11951195
if (i_split >= sched->splits_capacity) {
11961196
sched->splits_capacity *= 2;
1197+
1198+
if (sched->splits_capacity > SIZE_MAX / sizeof(struct ggml_backend_sched_split)) {
1199+
GGML_LOG_ERROR("%s: integer overflow in splits reallocation\n", __func__);
1200+
return;
1201+
}
11971202
sched->splits = (ggml_backend_sched_split *)
11981203
realloc(sched->splits, sched->splits_capacity * sizeof(struct ggml_backend_sched_split));
11991204
GGML_ASSERT(sched->splits != NULL);
@@ -1284,6 +1289,11 @@ void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct ggml_cgra
12841289
int graph_size = std::max(graph->n_nodes, graph->n_leafs) + sched->n_splits*GGML_SCHED_MAX_SPLIT_INPUTS*2*sched->n_copies;
12851290
if (sched->graph.size < graph_size) {
12861291
sched->graph.size = graph_size;
1292+
1293+
if (graph_size > 0 && (size_t)graph_size > SIZE_MAX / sizeof(struct ggml_tensor *)) {
1294+
GGML_LOG_ERROR("%s: integer overflow in graph nodes/leafs reallocation\n", __func__);
1295+
return;
1296+
}
12871297
sched->graph.nodes = (ggml_tensor **) realloc(sched->graph.nodes, graph_size * sizeof(struct ggml_tensor *));
12881298
sched->graph.leafs = (ggml_tensor **) realloc(sched->graph.leafs, graph_size * sizeof(struct ggml_tensor *));
12891299
GGML_ASSERT(sched->graph.nodes != NULL);

0 commit comments

Comments
 (0)