Skip to content

Commit aefd843

Browse files
fix: add integer overflow checks for calloc in ggml_gallocr_new_n
- Added overflow checks before calloc operations - Added proper cleanup on allocation failure - Validates n_bufs parameter - Prevents integer overflow in buffer allocations Addresses integer overflow vulnerabilities (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 3baffd2 commit aefd843

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

ggml/src/ggml-alloc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,34 @@ struct ggml_gallocr {
364364
};
365365

366366
ggml_gallocr_t ggml_gallocr_new_n(ggml_backend_buffer_type_t * bufts, int n_bufs) {
367+
if (n_bufs < 0) {
368+
return NULL;
369+
}
370+
367371
ggml_gallocr_t galloc = (ggml_gallocr_t)calloc(1, sizeof(struct ggml_gallocr));
368372
GGML_ASSERT(galloc != NULL);
369373

374+
if (n_bufs > 0 && (size_t)n_bufs > SIZE_MAX / sizeof(ggml_backend_buffer_type_t)) {
375+
free(galloc);
376+
return NULL;
377+
}
370378
galloc->bufts = calloc(n_bufs, sizeof(ggml_backend_buffer_type_t));
371379
GGML_ASSERT(galloc->bufts != NULL);
372380

381+
if (n_bufs > 0 && (size_t)n_bufs > SIZE_MAX / sizeof(ggml_backend_buffer_t)) {
382+
free(galloc->bufts);
383+
free(galloc);
384+
return NULL;
385+
}
373386
galloc->buffers = calloc(n_bufs, sizeof(ggml_backend_buffer_t));
374387
GGML_ASSERT(galloc->buffers != NULL);
375388

389+
if (n_bufs > 0 && (size_t)n_bufs > SIZE_MAX / sizeof(struct ggml_dyn_tallocr *)) {
390+
free(galloc->buffers);
391+
free(galloc->bufts);
392+
free(galloc);
393+
return NULL;
394+
}
376395
galloc->buf_tallocs = calloc(n_bufs, sizeof(struct ggml_dyn_tallocr *));
377396
GGML_ASSERT(galloc->buf_tallocs != NULL);
378397

0 commit comments

Comments
 (0)