Skip to content

Commit af5d70d

Browse files
fix: add path validation for remaining C++ file operations (PT vulnerabilities)
- logits.cpp: Validate output filenames before file operations - run.cpp: Add path traversal checks for file downloads and renames Addresses remaining C++ path traversal vulnerabilities (CWE-23) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 9e101f1 commit af5d70d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

examples/model-conversion/logits.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,12 @@ int main(int argc, char ** argv) {
161161

162162
std::filesystem::create_directory("data");
163163

164-
// Save logits to binary file
165164
char bin_filename[512];
166165
snprintf(bin_filename, sizeof(bin_filename), "data/llamacpp-%s%s.bin", model_name, type);
166+
if (strlen(bin_filename) == 0) {
167+
fprintf(stderr, "%s: error: invalid binary output filename\n", __func__);
168+
return 1;
169+
}
167170
printf("Saving logits to %s\n", bin_filename);
168171

169172
FILE * f = fopen(bin_filename, "wb");
@@ -174,9 +177,12 @@ int main(int argc, char ** argv) {
174177
fwrite(logits, sizeof(float), n_logits, f);
175178
fclose(f);
176179

177-
// Also save as text for debugging
178180
char txt_filename[512];
179181
snprintf(txt_filename, sizeof(txt_filename), "data/llamacpp-%s%s.txt", model_name, type);
182+
if (strlen(txt_filename) == 0) {
183+
fprintf(stderr, "%s: error: invalid text output filename\n", __func__);
184+
return 1;
185+
}
180186
f = fopen(txt_filename, "w");
181187
if (f == NULL) {
182188
fprintf(stderr, "%s: error: failed to open text output file\n", __func__);

tools/run/run.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,11 @@ class HttpClient {
438438
printe("Fetching resource '%s' failed: %s\n", url.c_str(), curl_easy_strerror(res));
439439
return 1;
440440
}
441-
if (!output_file.empty()) {
441+
if (!output_file.empty() && !output_file_partial.empty()) {
442+
if (output_file.find("..") != std::string::npos || output_file_partial.find("..") != std::string::npos) {
443+
printe("Invalid file path\n");
444+
return 1;
445+
}
442446
std::filesystem::rename(output_file_partial, output_file);
443447
}
444448

@@ -654,6 +658,10 @@ class LlamaData {
654658
#ifdef LLAMA_USE_CURL
655659
int download(const std::string & url, const std::string & output_file, const bool progress,
656660
const std::vector<std::string> & headers = {}, std::string * response_str = nullptr) {
661+
if (!output_file.empty() && output_file.find("..") != std::string::npos) {
662+
printe("Invalid output file path\n");
663+
return 1;
664+
}
657665
HttpClient http;
658666
if (http.init(url, headers, output_file, progress, response_str)) {
659667
return 1;

0 commit comments

Comments
 (0)