Skip to content

Commit db03ff8

Browse files
fix: add path validation for C++ file operations (PT vulnerabilities)
- convert-llama2c-to-ggml.cpp: Validate model file path before opening - gguf-hash.cpp: Add file validation for manifest file operations Addresses C++ path traversal vulnerabilities (CWE-23) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 2e887ac commit db03ff8

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ int main(int argc, char ** argv) {
885885
TransformerWeights weights = {};
886886
{
887887
LOG_INF("%s: Loading llama2c model from %s\n", __func__, params.fn_llama2c_model);
888+
if (!params.fn_llama2c_model || strlen(params.fn_llama2c_model) == 0) {
889+
LOG_ERR("%s: Invalid model file path\n", __func__);
890+
return 1;
891+
}
888892
FILE * file = fopen(params.fn_llama2c_model, "rb");
889893
if (!file) {
890894
LOG_ERR("%s: Unable to open the checkpoint file %s!\n", __func__, params.fn_llama2c_model);

examples/gguf-hash/gguf-hash.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ static bool manifest_type(const std::string & manifest_file, manifest_check_para
206206
return false;
207207
}
208208

209-
std::ifstream file(manifest_file);
210-
if (!file.is_open()) {
209+
std::ifstream file(manifest_file, std::ios::binary);
210+
if (!file.is_open() || !file.good()) {
211211
return false;
212212
}
213213

@@ -238,8 +238,8 @@ static hash_manifest_result_t manifest_verify(const std::string& manifest_file,
238238
return HASH_MANIFEST_NOT_FOUND;
239239
}
240240

241-
std::ifstream file(manifest_file);
242-
if (!file.is_open()) {
241+
std::ifstream file(manifest_file, std::ios::binary);
242+
if (!file.is_open() || !file.good()) {
243243
return HASH_MANIFEST_NOT_FOUND;
244244
}
245245

0 commit comments

Comments
 (0)