Skip to content

Commit cc19054

Browse files
fix: add integer overflow checks for malloc in ggml-quants.c
- Added overflow checks for the_grid allocation in iq2xs_init_impl - Added overflow checks for kmap_q2xs allocation in iq2xs_init_impl - Added overflow checks for dist2 allocation in iq2xs_init_impl - Added overflow checks for the_grid allocation in iq3xs_init_impl - Added overflow checks for kmap_q3xs allocation in iq3xs_init_impl - Added overflow checks for dist2 allocation in iq3xs_init_impl - Ensures safe memory allocation for quantization operations - Added proper cleanup on overflow detection Addresses integer overflow vulnerability (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent b551658 commit cc19054

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

ggml/src/ggml-quants.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,10 @@ void iq2xs_init_impl(enum ggml_type type) {
29032903
uint16_t * kneighbors_q2xs;
29042904

29052905
//printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
2906+
if (grid_size > 0 && (size_t)grid_size > SIZE_MAX / sizeof(uint64_t)) {
2907+
fprintf(stderr, "%s: integer overflow in the_grid allocation\n", __func__);
2908+
return;
2909+
}
29062910
uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
29072911
for (int k = 0; k < grid_size; ++k) {
29082912
int8_t * pos = (int8_t *)(the_grid + k);
@@ -2913,6 +2917,12 @@ void iq2xs_init_impl(enum ggml_type type) {
29132917
}
29142918
kgrid_q2xs = the_grid;
29152919
iq2_data[gindex].grid = the_grid;
2920+
2921+
if (kmap_size > 0 && (size_t)kmap_size > SIZE_MAX / sizeof(int)) {
2922+
fprintf(stderr, "%s: integer overflow in kmap_q2xs allocation\n", __func__);
2923+
free(the_grid);
2924+
return;
2925+
}
29162926
kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
29172927
iq2_data[gindex].map = kmap_q2xs;
29182928
for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
@@ -2928,6 +2938,13 @@ void iq2xs_init_impl(enum ggml_type type) {
29282938
kmap_q2xs[index] = i;
29292939
}
29302940
int8_t pos[8];
2941+
2942+
if (grid_size > 0 && (size_t)grid_size > SIZE_MAX / (2 * sizeof(int))) {
2943+
fprintf(stderr, "%s: integer overflow in dist2 allocation\n", __func__);
2944+
free(kmap_q2xs);
2945+
free(the_grid);
2946+
return;
2947+
}
29312948
int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
29322949
int num_neighbors = 0, num_not_in_map = 0;
29332950
for (int i = 0; i < kmap_size; ++i) {
@@ -3497,6 +3514,10 @@ void iq3xs_init_impl(int grid_size) {
34973514
uint16_t * kneighbors_q3xs;
34983515

34993516
//printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
3517+
if (grid_size > 0 && (size_t)grid_size > SIZE_MAX / sizeof(uint32_t)) {
3518+
fprintf(stderr, "%s: integer overflow in the_grid allocation\n", __func__);
3519+
return;
3520+
}
35003521
uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
35013522
for (int k = 0; k < grid_size; ++k) {
35023523
int8_t * pos = (int8_t *)(the_grid + k);
@@ -3507,6 +3528,12 @@ void iq3xs_init_impl(int grid_size) {
35073528
}
35083529
kgrid_q3xs = the_grid;
35093530
iq3_data[gindex].grid = the_grid;
3531+
3532+
if (kmap_size > 0 && (size_t)kmap_size > SIZE_MAX / sizeof(int)) {
3533+
fprintf(stderr, "%s: integer overflow in kmap_q3xs allocation\n", __func__);
3534+
free(the_grid);
3535+
return;
3536+
}
35103537
kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
35113538
iq3_data[gindex].map = kmap_q3xs;
35123539
for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
@@ -3522,6 +3549,13 @@ void iq3xs_init_impl(int grid_size) {
35223549
kmap_q3xs[index] = i;
35233550
}
35243551
int8_t pos[4];
3552+
3553+
if (grid_size > 0 && (size_t)grid_size > SIZE_MAX / (2 * sizeof(int))) {
3554+
fprintf(stderr, "%s: integer overflow in dist2 allocation\n", __func__);
3555+
free(kmap_q3xs);
3556+
free(the_grid);
3557+
return;
3558+
}
35253559
int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
35263560
int num_neighbors = 0, num_not_in_map = 0;
35273561
for (int i = 0; i < kmap_size; ++i) {

0 commit comments

Comments
 (0)