From 981cd1e4e134ead4770b942d0c5dae92019045e6 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:20:43 +0000 Subject: [PATCH] format: the index does not get to reach outside the model dir hfget screens the names a repository LISTING carries, but the shard index is one of those listed files, and nothing screened what its weight_map names. An index of "../../etc/passwd" led the loader to open that path -- outside the model directory, as the serving user -- and a directory merging two checkpoints has every reason to hold a real index. shard_name_ok now reads every weight_map value before the first open: no absolute name, no empty, "." or ".." component; relative subdirs, which real repos do use, still pass. The refusal names the index and the offending shard. A checkpoint_test case opens a poisoned index in six shapes and the honest one once, and only the honest one reads. --- core/format/checkpoint.cpp | 72 ++++++++++++++++++++++++++++++++++++-- tests/checkpoint_test.cpp | 26 ++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/core/format/checkpoint.cpp b/core/format/checkpoint.cpp index f02161b1..f90df914 100644 --- a/core/format/checkpoint.cpp +++ b/core/format/checkpoint.cpp @@ -2,10 +2,12 @@ #include "checkpoint.h" #include "ggml_planes.h" #include "rad_plugin.h" +#include "../rad_internal.h" #include "util/json.h" #include +#include #include #include #include @@ -554,6 +556,19 @@ int Checkpoint::open_gguf(const std::string& path) { return RAD_OK; } +/* Whether the file `p` really is -- symlinks resolved -- lives inside `dir`. Screening the NAME + * is not enough: a model dir can carry a symlink whose honest-looking name resolves outside + * it, and the index can name exactly that file. (A legit symlink that stays inside the dir + * still passes: the resolution is inside too. Both sides resolve, so a dir path that itself + * runs through a symlink compares against its own real location.) */ +static bool resolves_under(const std::string& dir, const std::string& p) { + char rd[PATH_MAX], rp[PATH_MAX]; + if (::realpath(dir.c_str(), rd) == nullptr) return false; + if (::realpath(p.c_str(), rp) == nullptr) return false; + const size_t n = std::strlen(rd); + return rp[n] == '/' && std::strncmp(rp, rd, n) == 0; +} + int Checkpoint::add_shard(const std::string& p, const std::string& prefix, const std::unordered_map* owner) { shards_.push_back(std::make_unique()); @@ -592,20 +607,64 @@ int Checkpoint::add_shard(const std::string& p, const std::string& prefix, namespace { +/* The index's shard names are checkpoint-supplied data. They must stay under `dir`: hfget + * screens the LISTING's names, but the index is one of the listed files, and a weight_map of + * "../../etc/passwd" passes that screen as easily as it escapes the model dir -- the engine + * would open the named file, outside the dir, as the serving user. Relative subdirs are legal + * in real repos; an empty component, "." or ".." is not. */ +bool shard_name_ok(const std::string& f) { + if (f.empty() || f.front() == '/') return false; + size_t i = 0; + while (i < f.size()) { + size_t j = f.find('/', i); + if (j == std::string::npos) j = f.size(); + if (j == i) return false; /* empty component, "//" */ + if (j - i == 2 && f.compare(i, 2, "..") == 0) return false; + if (j - i == 1 && f[i] == '.') return false; + i = j + 1; + } + return true; +} + /* Every shard an index names, once each and in name order, or the one model.safetensors; `map` * is the index's weight_map, empty without one. */ +static bool path_exists(const std::string& p) { std::ifstream f(p); return (bool)f; } + int shard_list(const std::string& dir, const std::string& index, bool index_given, std::vector* out, std::unordered_map* map) { const int s = st_load_index(index, map); if (s == RAD_OK) { for (auto& [t, f] : *map) if (std::find(out->begin(), out->end(), f) == out->end()) out->push_back(f); + for (const std::string& f : *out) + if (!shard_name_ok(f)) { + RAD_ERR("%s: the index names the shard '%s'; shards must live under the model " + "directory", + index.c_str(), log_sanitize(f).c_str()); + return RAD_E_FORMAT; + } std::sort(out->begin(), out->end()); for (auto& f : *out) f = dir + "/" + f; + /* The join is honest; the FILE may not be: an honest name can be a symlink planted in + * the dir that resolves outside it. Refuse before any open of it. (Containment reads + * relative to the dir actually being loaded, which is why it lives here and not in + * add_shard: a drafter's shards legitimately join under the DRAFT's dir.) */ + for (const std::string& f : *out) + if (path_exists(f) && !resolves_under(dir, f)) { + RAD_ERR("%s: the shard '%s' resolves outside the model directory; refusing it", + index.c_str(), log_sanitize(f).c_str()); + return RAD_E_FORMAT; + } return RAD_OK; } if (s != RAD_E_NOTFOUND || index_given) return s; out->push_back(dir + "/model.safetensors"); + if (!resolves_under(dir, out->back())) { + RAD_ERR("%s: %s resolves outside the model directory; refusing it", + index.c_str(), log_sanitize(out->back()).c_str()); + out->pop_back(); + return RAD_E_FORMAT; + } return RAD_OK; } @@ -632,8 +691,6 @@ static int meta_merge_json_file(const std::string& path, const std::string& pref return RAD_OK; } -static bool path_exists(const std::string& p) { std::ifstream f(p); return (bool)f; } - /* Whether an FP8 piece of `rows` rows has one scale for the whole tensor: a rank-0 scale * (ModelOpt's) or a one-element [1] (compressed-tensors' tensor strategy). A [1, 1] scale is a * block plane. */ @@ -2002,7 +2059,8 @@ int Checkpoint::add_lora(const std::string& path, double strength, } std::vector files; std::error_code ec; - if (std::filesystem::is_directory(path, ec)) { + const bool scanned = std::filesystem::is_directory(path, ec); + if (scanned) { for (const auto& e : std::filesystem::directory_iterator(path, ec)) if (e.path().extension() == ".safetensors") files.push_back(e.path().string()); std::sort(files.begin(), files.end()); @@ -2025,6 +2083,14 @@ int Checkpoint::add_lora(const std::string& path, double strength, int n_merged = 0; for (const std::string& f : files) { + /* A scanned name can be a symlink planted in the adapter dir resolving outside it -- + * same rule as the model dir's shards. A file the operator named outright needs no + * screen: following its symlink is where they pointed it. */ + if (scanned && !resolves_under(path, f)) { + RAD_ERR("lora: %s resolves outside the adapter directory; refusing it", + log_sanitize(f).c_str()); + return RAD_E_FORMAT; + } SafeTensorsFile st; if (st.open(f) != RAD_OK) return RAD_E_FORMAT; std::vector pairs; diff --git a/tests/checkpoint_test.cpp b/tests/checkpoint_test.cpp index 4d671315..9057cfdc 100644 --- a/tests/checkpoint_test.cpp +++ b/tests/checkpoint_test.cpp @@ -136,6 +136,32 @@ TEST(the_metadata_names_the_architecture_and_the_quantisation) { CHECK_EQ(ck.dir(), dir); } +/* The index is checkpoint-supplied data like the tensors, and its weight_map values are paths: + * a poisoned one must not lead the loader to open files outside the model dir as the serving + * user. hfget screens the listing's names; nothing screened the index's until here. */ +TEST(a_poisoned_index_cannot_lead_the_shard_list_out_of_the_model_dir) { + auto dir_with_index = [](const char* shard) -> std::string { + const std::string dir = write_checkpoint({ bf16("x", { 2, 2 }, 0) }, kConfig); + const std::string idx = std::string("{\"metadata\":{\"total_size\":8},\"weight_map\":" + "{\"x\":\"") + shard + "\"}}"; + /* No REQUIRE in here: its failure spelling returns void, and this lambda returns a + * directory. A null FILE* just fails to write, and every caller checks the open. */ + std::FILE* f = std::fopen((dir + "/model.safetensors.index.json").c_str(), "wb"); + if (f != nullptr) { + std::fwrite(idx.data(), 1, idx.size(), f); + std::fclose(f); + } + return dir; + }; + /* The honest index still reads: the name is the real shard's own. */ + Checkpoint ok; + CHECK_EQ(ok.open(dir_with_index("model.safetensors")), RAD_OK); + for (const char* bad : { "../../../../etc/passwd", "/etc/passwd", "..", "a/..", "./x", "" }) { + Checkpoint ck; + CHECK(ck.open(dir_with_index(bad)) < 0); + } +} + TEST(routed_expert_metadata_accepts_the_deepseek_and_glm_config_spellings) { const std::string dir = write_checkpoint({ bf16("x", {2, 2}, 0) }, "{\"model_type\":\"glm5_next\",\"text_config\":{\"hidden_size\":4096,"