Skip to content

Commit 9e101f1

Browse files
fix: add path validation for tokenize and quantize file operations (PT vulnerabilities)
- tokenize.cpp: Validate file path before opening - quantize.cpp: Validate imatrix and input file paths before operations Addresses C++ path traversal vulnerabilities (CWE-23) Co-Authored-By: Jake Cosme <[email protected]>
1 parent eeb897e commit 9e101f1

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

tools/quantize/quantize.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,12 @@ static void usage(const char * executable) {
149149
}
150150

151151
static int load_legacy_imatrix(const std::string & imatrix_file, std::vector<std::string> & imatrix_datasets, std::unordered_map<std::string, std::vector<float>> & imatrix_data) {
152+
if (imatrix_file.empty()) {
153+
printf("%s: invalid imatrix file path\n", __func__);
154+
exit(1);
155+
}
152156
std::ifstream in(imatrix_file.c_str(), std::ios::binary);
153-
if (!in) {
157+
if (!in || !in.good()) {
154158
printf("%s: failed to open %s\n",__func__, imatrix_file.c_str());
155159
exit(1);
156160
}
@@ -577,8 +581,11 @@ int main(int argc, char ** argv) {
577581

578582
llama_backend_init();
579583

580-
// parse command line arguments
581584
const std::string fname_inp = argv[arg_idx];
585+
if (fname_inp.empty()) {
586+
fprintf(stderr, "%s: invalid input file path\n", __func__);
587+
return 1;
588+
}
582589
arg_idx++;
583590
std::string fname_out;
584591

tools/tokenize/tokenize.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ static void llama_log_callback_null(ggml_log_level level, const char * text, voi
4646
static std::string read_prompt_from_file(const char * filepath, bool & success) {
4747
success = false;
4848

49+
if (!filepath || strlen(filepath) == 0) {
50+
fprintf(stderr, "%s: invalid file path\n", __func__);
51+
return std::string();
52+
}
4953
std::ifstream in(filepath, std::ios::binary);
50-
if (!in) {
54+
if (!in || !in.good()) {
5155
fprintf(stderr, "%s: could not open file '%s' for reading: %s\n", __func__, filepath, strerror(errno));
5256
return std::string();
5357
}

0 commit comments

Comments
 (0)