Archived
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.
This commit is contained in:
1 parent
50ea36f283
commit
981cd1e4e1
2 files changed
+95
-3
No files matched your search
@@ -2,10 +2,12 @@
|
||||
#include "checkpoint.h"
|
||||
#include "ggml_planes.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "../rad_internal.h"
|
||||
|
||||
#include "util/json.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <filesystem>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
@@ -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<std::string, std::string>* owner) {
|
||||
shards_.push_back(std::make_unique<SafeTensorsFile>());
|
||||
@@ -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<std::string>* out, std::unordered_map<std::string, std::string>* 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<std::string> 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<Pair> pairs;
|
||||
|
||||
Reference in new issue
Block a user