place: the profile file is read like the untrusted file it is

--expert-profile read its file with no size cap -- any file, gigabytes,
appended into memory before a single check -- parsed the text with the
unbounded parser, and took n_trunk_layers through int32. That last one
is the quiet one: the loader refuses "this profile is for another
model" by comparing the file's layer count with the live model's, and a
value that wraps past 2^32 lands on exactly the number the comparison
asks for.

The read now caps at 64 MiB while appending -- past it, nothing further
is grown and the file is refused by name -- the parse is the container
helper (parse_container) like every other file input, and n_trunk_layers
is read as int64 and range-checked before it reaches the comparison. An
xstats_test case pins the loader's three refusals -- wrapped trunk
count, negative count, oversized contract -- and the tmp-symlink
refusal of the previous commit: the planted symlink survives untouched
and the profile file still lands.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:10 +00:00
1 parent e5862f3d26
commit 90b1229bfc
2 files changed
+93 -7

No files matched your search

+35 -7
View File
@@ -7,6 +7,7 @@
#include <cerrno>
#include <chrono>
#include <climits>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <ctime>
@@ -513,17 +514,34 @@ int xs_profile_load(const std::string& path, std::vector<RadFileProfile>* out, i
}
std::string text;
char buf[1 << 16];
while (const size_t n = std::fread(buf, 1, sizeof buf, f)) text.append(buf, n);
/* Capped WHILE reading. A profile is a handful of integers per layer and expert even for
* the largest shipped MoE; anything far past that is not a profile, and the old loop
* grew memory with any file it was pointed at before any check ran. */
static const size_t kMaxProfileBytes = 64u << 20;
bool too_big = false;
while (const size_t n = std::fread(buf, 1, sizeof buf, f)) {
text.append(buf, n);
if (text.size() > kMaxProfileBytes) { too_big = true; break; }
}
const bool bad = std::ferror(f) != 0;
std::fclose(f);
if (too_big) {
RAD_ERR("--expert-profile: %s is past %zu MiB; an expert profile is a handful of "
"integers per layer and expert, and this is not one",
path.c_str(), (size_t)(kMaxProfileBytes >> 20));
return RAD_E_INVAL;
}
if (bad) {
RAD_ERR("--expert-profile: reading %s failed", path.c_str());
return RAD_E_IO;
}
Json j;
try { j = Json::parse(text); }
catch (const Json::exception& e) {
RAD_ERR("--expert-profile: %s is not JSON: %s", path.c_str(), e.what());
/* A file like any other the container or a colleague supplies: bounded read, so a hostile
* one is refused during the read instead of grown into a tree. */
bool over = false, deep = false;
Json j = Json::parse_container(text, &over, &deep);
if (over || deep || j.is_discarded()) {
RAD_ERR("--expert-profile: %s is not JSON (%s)", path.c_str(),
over ? "far too many values" : deep ? "nests far too deep" : "malformed");
return RAD_E_INVAL;
}
if (!j.is_object() || j.value("format", "") != "radiance-expert-stats/1" ||
@@ -533,8 +551,18 @@ int xs_profile_load(const std::string& path, std::vector<RadFileProfile>* out, i
path.c_str());
return RAD_E_INVAL;
}
if (n_trunk && j.contains("n_trunk_layers") && j["n_trunk_layers"].is_number_integer())
*n_trunk = j["n_trunk_layers"].get<int32_t>();
if (n_trunk && j.contains("n_trunk_layers") && j["n_trunk_layers"].is_number_integer()) {
/* int64, range-checked: read through int32, a value that wraps past 2^32 can land on
* exactly the layer count the "this profile is another model" refusal compares
* against -- the check the file would be escaping. */
const int64_t t = j["n_trunk_layers"].get<int64_t>();
if (t < 0 || t > INT32_MAX) {
RAD_ERR("--expert-profile: %s carries n_trunk_layers %lld, outside 0..%d",
path.c_str(), (long long)t, (int)INT32_MAX);
return RAD_E_INVAL;
}
*n_trunk = (int32_t)t;
}
const auto& rows = j["counts"]["decode"];
int64_t kept = 0;
for (size_t l = 0; l < rows.size(); ++l) {
+58
View File
@@ -275,4 +275,62 @@ TEST(the_file_is_json_written_at_boot_and_replaced_through_a_rename) {
CHECK(bad.init(radtest::tmp_path("no/such/dir/x.json"), m, XsSnapshot{}) < 0);
}
/* The profile file is local data with a contract, and its tmp name is predictable. These pin
* the loader's refusals -- a negative count (which used to wrap into a ~100% share), an
* n_trunk_layers that truncates through int32 into exactly the "another model" match -- and
* that the tmp name is created exclusively: a symlink planted there must survive untouched. */
TEST(the_profile_file_and_its_tmp_name_keep_their_contracts) {
auto profile = [](const std::string& body) {
const std::string p = radtest::tmp_path("xs_profile.json");
if (FILE* f = std::fopen(p.c_str(), "wb")) {
std::fwrite(body.data(), 1, body.size(), f);
std::fclose(f);
}
return p;
};
const std::string ok = "{\"format\":\"radiance-expert-stats/1\",\"n_trunk_layers\":2,"
"\"counts\":{\"decode\":[[1,2],[0,0],[3,0]]}}";
{
std::vector<RadFileProfile> out;
int32_t nt = -1;
CHECK_EQ(xs_profile_load(profile(ok), &out, &nt), RAD_OK);
CHECK_EQ(nt, 2);
CHECK_EQ(out.size(), 3u); /* the zero-sum row contributes nothing */
}
{ /* 2^32 + 2: read through int32 this lands on 2, and 2 layers is what the refusal
* against would compare. */
std::vector<RadFileProfile> out;
int32_t nt = -1;
CHECK(xs_profile_load(profile("{\"format\":\"radiance-expert-stats/1\","
"\"n_trunk_layers\":4294967298,"
"\"counts\":{\"decode\":[[1,2]]}}"),
&out, &nt) < 0);
}
{ std::vector<RadFileProfile> out;
CHECK(xs_profile_load(profile("{\"format\":\"radiance-expert-stats/1\","
"\"counts\":{\"decode\":[[-1,2]]}}"),
&out, nullptr) < 0);
}
/* The tmp name is the destination's plus this process's PID -- public. Planted as a
* symlink, it must be cleared and re-created, never written through. */
const std::string victim = radtest::tmp_path("xs_victim.txt");
if (FILE* v = std::fopen(victim.c_str(), "w")) { std::fputs("keep", v); std::fclose(v); }
const std::string dest = radtest::tmp_path("xs_stats.json");
const std::string tmp = dest + ".tmp." + std::to_string((long long)getpid());
::unlink(tmp.c_str());
if (::symlink(victim.c_str(), tmp.c_str()) != 0) SKIP("cannot plant a symlink here");
CHECK_EQ(xs_write_atomic(dest, "{\"format\":\"radiance-expert-stats/1\"}\n"), RAD_OK);
std::string kept;
if (FILE* v = std::fopen(victim.c_str(), "r")) {
char buf[16];
size_t n;
while ((n = std::fread(buf, 1, sizeof buf, v)) > 0) kept.append(buf, n);
std::fclose(v);
}
CHECK_EQ(kept, std::string("keep"));
CHECK(::access(tmp.c_str(), F_OK) != 0); /* renamed away, not left behind */
}
RAD_TEST_MAIN()