diff --git a/core/place/xstats.cpp b/core/place/xstats.cpp index c3c3f57a..9eb62e11 100644 --- a/core/place/xstats.cpp +++ b/core/place/xstats.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -513,17 +514,34 @@ int xs_profile_load(const std::string& path, std::vector* 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* 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(); + 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(); + 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) { diff --git a/tests/xstats_test.cpp b/tests/xstats_test.cpp index 0d949497..a0f0f991 100644 --- a/tests/xstats_test.cpp +++ b/tests/xstats_test.cpp @@ -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 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 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 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()