Archived
The --model repo-id path trusts the hub with the machine's filesystem and the container runs with the kernel's door off. Both stop here. hfget.sh trusted a listing's file names to be names: an absolute path or a .. segment wrote wherever the destination's owner could write, and a name shadowing .hfget-manifest taught the next run whatever it liked. A listing is now vetted whole before anything is fetched -- absolute paths, .. segments, control characters (a tab splices one entry into two fields), names of hfget's own manifest -- and a child refuses a path that would escape anyway. Silent skipping was the alternative, and it leaves a directory that reads as complete and is not. And the content key was the hub's claim, never checked: an LFS file was kept for its size and its claimed oid recorded without ever hashing the download, so a swapped file of equal length -- or a spliced resume at any length -- passed forever after. The download is hashed on arrival and kept only when it matches the claimed sha256; a mismatch deletes the file, and the manifest now certifies verified content. --model-revision REV fetches at a branch, tag, sha or refs/pr/N instead of the moving main: without it a restart can serve weights nobody reviewed, and the guide now says to name one. --api-key-env NAME reads the key from the environment, where only the process and root can reach it, not the command line where every process on the host reads it in ps; an unset or empty variable refuses startup exactly like an empty --api-key. The compose files carry the key through environment: now -- the fail-closed :? guard moves with it -- across all 52 model files. And the containers' seccomp=unconfined has a way out: Docker's default profile, plus exactly the calls that forced the escape -- io_uring for the n-gram table, mbind/set_mempolicy/get_mempolicy for ROCm's NUMA binding -- is shipped as seccomp-radiance.json and selected through RADIANCE_SECCOMP; the default stays unconfined until each operator audits their own card stack. Verified: under the profile io_uring_setup and set_mempolicy behave as under unconfined and a non-listed syscall earns EPERM. hf_model_test: the escape-listing refusal, the oid verify (bad rejected and deleted, good certified), the pinned revision fetched -- against a fake curl for a hub. config_test: --api-key-env fails closed unset, reads the key set. ASan+UBSan: config/diag/hf_model/server/transport/ wire/lifecycle, 8/8.
163 lines
6.2 KiB
C++
163 lines
6.2 KiB
C++
/* hf_model.cpp -- resolve a --model that is a Hugging Face repo id (hf_model.h).
|
|
*
|
|
* main.cpp calls resolve_hf_model() once, between config_parse and the Engine; bring-up then sees
|
|
* a plain file or directory. Fetching is scripts/hfget.sh (curl and jq), run as a child process
|
|
* from libexec/radiance/ beside the binary, installed or in the build tree.
|
|
*
|
|
* Downloads go to the XDG cache by default, or --download-dir (vLLM's name), not $RADIANCE_HOME,
|
|
* which is a plugin search path whose default is not user-writable.
|
|
*
|
|
* An unreachable hub does not stop a restart: hfget.sh exits 3 when it changed nothing on disk,
|
|
* and the copy a complete fetch left (marked by its success manifest) is served with a warning.
|
|
* Any other failure may leave a partial file, so nothing is served. */
|
|
#include "hf_model.h"
|
|
|
|
#include "rad_internal.h"
|
|
|
|
#include <cerrno>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
namespace rad {
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace {
|
|
|
|
bool repo_part(const std::string& p) {
|
|
if (p.empty() || p.front() == '.' || p.front() == '-') return false;
|
|
for (char ch : p) {
|
|
const bool ok = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
|
|
(ch >= '0' && ch <= '9') || ch == '.' || ch == '_' || ch == '-';
|
|
if (!ok) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* <exe>/../libexec/radiance/hfget.sh, which is where the install puts it and where the build
|
|
* copies it. */
|
|
std::string hfget_path() {
|
|
char buf[4096];
|
|
const ssize_t n = readlink("/proc/self/exe", buf, sizeof buf - 1);
|
|
if (n <= 0) return std::string();
|
|
buf[n] = '\0';
|
|
const fs::path p = fs::path(buf).parent_path() / ".." / "libexec" / "radiance" / "hfget.sh";
|
|
std::error_code ec;
|
|
return fs::is_regular_file(p, ec) ? p.lexically_normal().string() : std::string();
|
|
}
|
|
|
|
/* The script's exit status, or -1 when it could not be run. `rev` empty leaves the script's own
|
|
* default: the repo's moving default branch. */
|
|
int run_hfget(const std::string& script, const std::string& repo, const std::string& dest,
|
|
const std::string& rev) {
|
|
const pid_t pid = fork();
|
|
if (pid < 0) {
|
|
RAD_ERR("hfget: fork: %s", std::strerror(errno));
|
|
return -1;
|
|
}
|
|
if (pid == 0) {
|
|
const char* argv[] = { "sh", script.c_str(), repo.c_str(), dest.c_str(),
|
|
rev.empty() ? nullptr : rev.c_str(), nullptr };
|
|
execv("/bin/sh", const_cast<char* const*>(argv));
|
|
std::fprintf(stderr, "hfget: /bin/sh: %s\n", std::strerror(errno));
|
|
_exit(127);
|
|
}
|
|
int status = 0;
|
|
while (waitpid(pid, &status, 0) < 0)
|
|
if (errno != EINTR) return -1;
|
|
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
bool hf_repo_id(const std::string& s) {
|
|
std::error_code ec;
|
|
if (s.empty() || fs::exists(s, ec)) return false;
|
|
const size_t slash = s.find('/');
|
|
if (slash == std::string::npos || s.find('/', slash + 1) != std::string::npos) return false;
|
|
const std::string owner = s.substr(0, slash), name = s.substr(slash + 1);
|
|
if (!repo_part(owner) || !repo_part(name)) return false;
|
|
for (const char* suf : { ".rad", ".safetensors", ".gguf", ".json" })
|
|
if (name.ends_with(suf)) return false;
|
|
return true;
|
|
}
|
|
|
|
std::string hf_default_download_dir() {
|
|
if (const char* x = std::getenv("XDG_CACHE_HOME"); x && *x)
|
|
return std::string(x) + "/radiance/hf";
|
|
if (const char* h = std::getenv("HOME"); h && *h)
|
|
return std::string(h) + "/.cache/radiance/hf";
|
|
return std::string();
|
|
}
|
|
|
|
std::string hf_served_path(const std::string& dir, std::string* why) {
|
|
std::vector<std::string> rad;
|
|
std::error_code ec;
|
|
for (const fs::directory_entry& e : fs::directory_iterator(dir, ec)) {
|
|
std::error_code fe;
|
|
if (e.is_regular_file(fe) && e.path().filename().string().ends_with(".rad"))
|
|
rad.push_back(e.path().filename().string());
|
|
}
|
|
if (ec) {
|
|
*why = dir + ": " + ec.message();
|
|
return std::string();
|
|
}
|
|
if (rad.empty()) return dir;
|
|
if (rad.size() == 1) return dir + "/" + rad[0];
|
|
std::string names;
|
|
for (size_t i = 0; i < rad.size(); ++i) names += (i ? ", " : "") + rad[i];
|
|
*why = dir + " holds " + std::to_string(rad.size()) + " containers (" + names +
|
|
"); pass the one to serve as --model " + dir + "/<file>";
|
|
return std::string();
|
|
}
|
|
|
|
bool resolve_hf_model(Config& c) {
|
|
if (!hf_repo_id(c.model)) return true;
|
|
const std::string repo = c.model;
|
|
const std::string root = c.download_dir.empty() ? hf_default_download_dir() : c.download_dir;
|
|
if (root.empty()) {
|
|
RAD_ERR("--model %s: no download directory -- neither XDG_CACHE_HOME nor HOME is set; "
|
|
"name one with --download-dir", repo.c_str());
|
|
return false;
|
|
}
|
|
const std::string dest = root + "/" + repo;
|
|
std::error_code ec;
|
|
const bool cached = fs::is_regular_file(dest + "/.hfget-manifest", ec);
|
|
|
|
const std::string script = hfget_path();
|
|
if (script.empty()) {
|
|
RAD_ERR("--model %s: not a path here, and hfget.sh, which fetches a Hugging Face repo, is "
|
|
"not installed beside this binary (libexec/radiance/)", repo.c_str());
|
|
return false;
|
|
}
|
|
RAD_INFO("model: %s is not a path here; fetching it as a Hugging Face repo into %s",
|
|
repo.c_str(), dest.c_str());
|
|
const int rc = run_hfget(script, repo, dest, c.model_revision);
|
|
if (rc != 0) {
|
|
if (rc == 3 && cached) {
|
|
RAD_WARN("model: %s could not be checked for changes (hfget.sh, above); serving the "
|
|
"copy fetched before", repo.c_str());
|
|
} else {
|
|
RAD_ERR("--model %s: not a path here, and fetching it as a Hugging Face repo failed "
|
|
"(hfget.sh exit %d, above)", repo.c_str(), rc);
|
|
return false;
|
|
}
|
|
}
|
|
std::string why;
|
|
const std::string path = hf_served_path(dest, &why);
|
|
if (path.empty()) {
|
|
RAD_ERR("--model %s: %s", repo.c_str(), why.c_str());
|
|
return false;
|
|
}
|
|
c.model = path;
|
|
RAD_INFO("model: serving %s", c.model.c_str());
|
|
return true;
|
|
}
|
|
|
|
} /* namespace rad */
|