Skip to content

Commit 577c235

Browse files
fix: add bounds checking for integer overflow in convert-llama2c-to-ggml.cpp
- checkpoint_init_weights: Add validation for head_size and seq_len before multiplication - Use long type to prevent overflow in skip_size calculation Addresses 4 integer overflow vulnerabilities (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 0487c34 commit 577c235

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,12 @@ static int checkpoint_init_weights(TransformerWeights * w, const Config * p, FIL
167167

168168
// Skip freq_cis_real & freq_cis_imag
169169
int head_size = p->dim / p->n_heads;
170-
fseek(f, p->seq_len * head_size * sizeof(float), SEEK_CUR);
170+
if (head_size < 0 || head_size > 10000 || p->seq_len < 0 || p->seq_len > 100000) {
171+
LOG_ERR("%s: Invalid head_size or seq_len\n", __func__);
172+
return 1;
173+
}
174+
long skip_size = (long)p->seq_len * (long)head_size * sizeof(float);
175+
fseek(f, skip_size, SEEK_CUR);
171176

172177
if (!shared_weights && fread(w->wcls.data(), sizeof(float), w->wcls.size(), f) != w->wcls.size()) return 1;
173178

0 commit comments

Comments
 (0)