Blog
AI / LLM2026-11-28 · 7 min read

Model file vulnerabilities: when loading a model is remote code execution

Teams treat a model file the way they treat a JPEG — an inert blob of weights. It is not. For several popular formats, loading a model runs code, and the code is whatever the file's author put there. If your application loads a model whose source an attacker can influence — a user upload, a model name from a request, a Hugging Face repo id from config — that is a remote-code-execution path. This class has its own bug-bounty line ("MFV" — model file vulnerability).

Family 1: deserialization formats that execute by design

Python's pickle runs arbitrary code during unpickling (via __reduce__). Many "load a model" calls are pickle underneath:

  • torch.load(path) without weights_only=True (the pre-2.6 default executes code)
  • joblib.load / pickle.load / numpy.load(allow_pickle=True)
  • keras models with a Lambda layer

These are RCE by design; the libraries document it ("never load files from untrusted sources"). That makes it the application's job: the finding is not the pickle, it is the reachable data flow — a place where an attacker-influenced path, upload or repo id reaches one of these loaders.

Family 2: the forced opt-in — trust_remote_code=True

Hugging Face gates custom model code behind an explicit trust_remote_code opt-in, because when enabled the .py inside the model repo runs at load time. A recurring real-world bug: a library hardcodes trust_remote_code=True and exposes no way for the caller to turn it off. That overrides the ecosystem's safe default: whenever an attacker-influenced model name is loaded, the repo's code executes and the user never consented. When auditing, grep for a constant trust_remote_code=True on the load path, then check whether the public wrapper exposes an opt-out. If not, that is the core flaw.

Family 3: "safe" formats with unsafe parsers

Formats like safetensors and GGUF exist precisely to avoid code execution — they carry only tensors and metadata. The residual risk moves down a layer: the parser. These are C/C++/Rust readers that take sizes, offsets and counts straight from the file header and feed them into malloc/memcpy. A malformed file can then drive a heap overflow or out-of-bounds read — memory corruption, potentially RCE.

Auditing these is different work. A static review can locate the suspicious spots — an allocation sized by one file field and a copy sized by another — but it cannot confirm exploitability, because whether the two fields can disagree depends on how the parser derives them. The honest workflow is: trace where each size comes from (including vendored dependencies and submodules), and where a field is computed from a count rather than read raw, the mismatch usually cannot be forced. What is left is a fuzzing target: build the loader with AddressSanitizer, feed it malformed model files, and let the sanitizer prove or disprove the overflow.

How to defend

  • Prefer safetensors and torch.load(..., weights_only=True); never unpickle untrusted data.
  • Default trust_remote_code to False and make it a documented, caller-controlled opt-in.
  • Treat a model identifier like any other untrusted input: restrict which models can be loaded to a closed allow-list when the identifier is attacker-influenced.
  • If you ship a model-file parser, fuzz it continuously under ASan/UBSan.

This is exactly the kind of multi-layer, reachability-focused review we do. If you want your model-loading and ingestion code audited with file:line proof, get in touch or see our research.