Archived
util: one bounded read for the files a container supplies
tokenizer.json, gen_config.json, an adapter's adapter_config.json and the container's generation.* whitelist are all container-supplied, and all read through the unbounded parser: a hostile file was refused only by running out of stack -- the tree it grows is walked, dumped and destroyed recursively on the way. Json::parse_container is the wire's bounded read sized for files instead: 2^24 values, because a tokenizer's vocabulary legitimately carries a million flat strings, and 256 levels, because nothing legitimate nests deeper. A file past either bound comes back discarded, like a syntax error, and the four readers map that onto the failure paths they already had -- ignore with a warning, refuse the adapter, refuse the vocab. The bound fires during the read; no hostile tree exists to be walked.
This commit is contained in:
1 parent
e46a8ba3d4
commit
f2f94fc6e7
7 files changed
+131
-35
No files matched your search
@@ -14,6 +14,7 @@
|
||||
#include "text/gguf.h"
|
||||
|
||||
#include "util/json.h"
|
||||
#include "rad_internal.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
@@ -569,15 +570,26 @@ int read_added(const json& arr, VocabBuild& out, const char* origin, uint64_t id
|
||||
* elsewhere. */
|
||||
void read_config_json(const std::string& dir, VocabBuild& out) {
|
||||
const std::string path = dir + "tokenizer_config.json";
|
||||
std::ifstream f(path);
|
||||
if (!f) {
|
||||
/* Container bytes: size-screened and parsed with the container's bounds, like every other
|
||||
* file a checkpoint carries. (Streaming with operator>> meant an unbounded DOM.) */
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
RAD_INFO("tokenizer: no %s; bos/eos ids and the chat template are unset", path.c_str());
|
||||
return;
|
||||
}
|
||||
json c;
|
||||
try { f >> c; }
|
||||
catch (const std::exception& e) {
|
||||
RAD_WARN("tokenizer: %s did not parse (%s); ids left unset", path.c_str(), e.what());
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be; ids left unset",
|
||||
path.c_str());
|
||||
return;
|
||||
}
|
||||
bool over = false, deep = false;
|
||||
json c = json::parse_container(text, &over, &deep);
|
||||
if (over || deep || c.is_discarded()) {
|
||||
RAD_WARN("tokenizer: %s did not read as a bounded JSON document (%s); ids left unset",
|
||||
path.c_str(),
|
||||
over ? "it carries far too many values"
|
||||
: deep ? "it nests far too deep" : "it does not parse");
|
||||
return;
|
||||
}
|
||||
std::unordered_map<std::string, int32_t> id_of;
|
||||
@@ -624,11 +636,15 @@ void read_config_json(const std::string& dir, VocabBuild& out) {
|
||||
* key, as in transformers. */
|
||||
void read_chat_template_file(const std::string& dir, VocabBuild& out) {
|
||||
const std::string path = dir + "chat_template.jinja";
|
||||
std::ifstream f(path);
|
||||
if (!f) return;
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
std::string t = ss.str();
|
||||
std::string t;
|
||||
const int rst = read_text_file(path, t, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) return;
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be; template left "
|
||||
"unset",
|
||||
path.c_str());
|
||||
return;
|
||||
}
|
||||
while (!t.empty() && (t.back() == '\n' || t.back() == '\r')) t.pop_back();
|
||||
if (t.empty()) return;
|
||||
RAD_INFO("tokenizer: chat template from %s (%zu bytes)%s", path.c_str(), t.size(),
|
||||
@@ -770,11 +786,20 @@ int interpret_tokenizer_json(const json& j, uint64_t id_limit, const char* origi
|
||||
} /* namespace */
|
||||
|
||||
int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBuild& out) {
|
||||
json j;
|
||||
try {
|
||||
j = json::parse(text);
|
||||
} catch (const std::exception& e) {
|
||||
RAD_ERR("%s: not valid JSON: %s", origin, e.what());
|
||||
/* A container-supplied file. It legitimately carries hundreds of thousands of FLAT values
|
||||
* (a vocabulary, the merges), so it reads under the container's generous bounds rather than
|
||||
* the wire's: a hostile one is refused during the read instead of becoming a tree whose
|
||||
* walk or destruction overflows this process's stack. */
|
||||
bool over = false, deep = false;
|
||||
json j = json::parse_container(text, &over, &deep);
|
||||
if (over || deep) {
|
||||
RAD_ERR("%s: %s; a tokenizer file may not carry more than 2^24 values or nest more "
|
||||
"than 256 levels",
|
||||
origin, over ? "carries far too many values" : "nests far too deep");
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
if (j.is_discarded()) {
|
||||
RAD_ERR("%s: not valid JSON", origin);
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
out = VocabBuild{};
|
||||
@@ -800,14 +825,16 @@ int parse_tokenizer_json_buf(std::string_view text, const char* origin, VocabBui
|
||||
|
||||
int parse_tokenizer_json(const char* path, VocabBuild& out) {
|
||||
if (!path) return RAD_E_INVAL;
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f) {
|
||||
std::string text;
|
||||
const int rst = read_text_file(path, text, kMaxContainerFile);
|
||||
if (rst == RAD_E_IO) {
|
||||
RAD_ERR("tokenizer: cannot open %s", path);
|
||||
return RAD_E_IO;
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << f.rdbuf();
|
||||
const std::string text = ss.str();
|
||||
if (rst != RAD_OK) {
|
||||
RAD_ERR("tokenizer: %s is larger than the 256 MiB a container file may be", path);
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
|
||||
RAD_TRY(parse_tokenizer_json_buf(text, path, out));
|
||||
|
||||
|
||||
Reference in new issue
Block a user