Deserialization is code execution: pickle, torch.load and the internal-cluster fallacy
Loading a pickle, a PyTorch checkpoint, an unsafe YAML document, a Java object stream or a PHP string runs a program the sender wrote. The same gadget-chain bug class shows up in five runtimes, and ML infrastructure does it on network bytes in many places.
Ask a developer what pickle.loads(data) does and you will usually hear “it turns bytes back into an object.” That sounds like a data operation, the same family as json.loads. It is not. For a whole class of formats, deserializing means executing instructions chosen by whoever produced the bytes. If that is an attacker, you have given them a shell.
This post covers where that misconception comes from, which formats it applies to, why machine-learning infrastructure is where it does the most damage today, and how to find and remove it in your own code.
A little theory: a pickle is a program
Python’s pickle format is not a data layout. It is bytecode for a small stack machine. The unpickler reads opcodes one by one: push a string, build a tuple, look up a global, call it. Two opcodes carry the whole problem:
GLOBAL/STACK_GLOBALimport any attribute from any module by name, for exampleos.systemorsubprocess.Popen.REDUCEpops a callable and an argument tuple off the stack and calls it.
An object controls how it is pickled through __reduce__, which returns “the callable to rebuild me, and its arguments.” Nothing requires that callable to be a constructor:
import pickle, os
class Payload:
def __reduce__(self):
return (os.system, ("id",))
blob = pickle.dumps(Payload())
pickle.loads(blob) # runs `id` on the machine that loads itRun pickletools.dis(blob) and you will see it plainly: a STACK_GLOBAL for posix system, a short string, a TUPLE1, a REDUCE. The receiving side never needs the Payload class. The command runs inside loads, before your code gets the result back, so no amount of validation on the returned object helps. The Python documentation says it directly: never unpickle data received from an untrusted or unauthenticated source.
The general shape: gadget chains
Pickle is the blunt case, because it has an opcode that literally means “call this function.” The underlying pattern is more general and shows up in every language whose deserializer can be told which types to instantiate from inside the serialized bytes:
A rich deserializer reconstructs an object graph. To rebuild each object it chooses a type named in the data, allocates it, and runs that type’s reconstruction hooks (constructors, setters, or lifecycle magic methods). If the attacker controls the data, the attacker chooses the types, and whatever those types do while being rebuilt runs for free.
So the attacker does not inject new code. They assemble a gadget chain: a sequence of ordinary classes that already ship with the application or its libraries, picked so that one’s reconstruction hook calls into the next, until the last link reaches something dangerous such as reflection, a file write, a template render or a process spawn. The serialized blob is the recipe that wires those existing pieces together. Python skips the chain only because os.system is one opcode away. Everywhere else, finding a chain on the classpath is the exploit, and public tools have pre-built chains for the common libraries.
The same primitive under other names
The danger is not limited to code that literally says pickle. A lot of the Python data stack is pickle with a different name on the front:
torch.load. A PyTorch checkpoint is a zip archive whose object graph is a pickle. Loading a model file from the internet was, for years, equivalent to running a script from the internet. PyTorch added aweights_only=Truemode, which uses a restricted unpickler that only reconstructs tensors and a small allowlist of types, and in PyTorch 2.6 the default ofweights_onlyflipped toTrue. That is a real improvement, but it also means that any code that passesweights_only=Falseexplicitly, or runs on an older PyTorch, is back to full pickle. Restricted unpicklers are hard to get exactly right, andweights_only=Trueitself needed a security fix (CVE-2025-32434, a restricted-mode bypass that was still critical RCE). Treat it as a strong mitigation, not a proof of safety.joblib,dill,cloudpickle.joblib.loadis the common way scikit-learn models get saved, and it uses pickle underneath.dillandcloudpickleexist to serialize more things than pickle can, including functions and lambdas. They are more expressive and no safer.numpy.loadwithallow_pickle=True. Arrays of Python objects in.npy/.npzfiles are stored as pickles. NumPy switched the default toallow_pickle=Falseyears ago for this reason; code that turns it back on to “make the error go away” reopens the hole.pandas.read_pickle. A DataFrame “pickle” is a plain pickle;pd.read_pickle(path_or_url)on a file or URL someone else controls is justpickle.loadsbehind another name, and pandas documents it as unsafe for untrusted data. Prefer Parquet or CSV for anything that crosses a trust boundary.- PyYAML. YAML tags such as
!!python/object/apply:instantiate arbitrary Python objects.yaml.loadwith the full or unsafe loader (or old code callingyaml.loadwithout aLoader) is code execution on a document.yaml.safe_loadonly builds plain scalars, lists and dicts.
Beyond Python: the same bug in four more runtimes
None of this is new, and none of it is Python-specific. Java and PHP went through this discovery a decade earlier; .NET and Ruby have the same holes. The pattern is always “the deserializer instantiates attacker-chosen types”; only the hook names and the tooling change.
| Runtime | Dangerous sink | Hooks that fire on rebuild | Pre-built chains |
|---|---|---|---|
| Java | ObjectInputStream.readObject |
readObject, readResolve, finalize |
ysoserial |
| PHP | unserialize, and phar:// paths |
__wakeup, __destruct, __toString |
PHPGGC |
| .NET | BinaryFormatter.Deserialize; Json.NET with TypeNameHandling |
constructors, property setters, OnDeserialized |
ysoserial.net |
| Ruby | Marshal.load |
init_with, marshal_load, method_missing |
(none listed) |
Java: ObjectInputStream.readObject on untrusted bytes is the archetype. The best-known chain ran through Apache Commons-Collections’ InvokerTransformer, which can invoke an arbitrary method by reflection, ending at Runtime.exec. The gadget lives in a dependency, not in your code, which is why “we never serialize anything dangerous” is no defence. ysoserial generates payloads for dozens of such libraries.
PHP: unserialize() on user input drives POP (property-oriented programming) chains. The attacker sets object properties so that when the engine tears the objects down, __wakeup or __destruct (or a later __toString) calls into the next gadget. You do not always need a visible unserialize call. The phar:// stream wrapper deserializes a Phar archive’s metadata whenever a Phar path reaches almost any filesystem function (file_exists, fopen, getimagesize, …), so an attacker-controlled path plus an uploaded file is enough to trigger a chain. PHPGGC catalogues chains for Laravel, Symfony, Monolog and more.
.NET: the legacy BinaryFormatter has been formally obsoleted and removed from modern .NET because it cannot be made safe. The one that still bites working code is Json.NET (Newtonsoft.Json) with TypeNameHandling set to anything but None. That setting writes a $type field into the JSON and lets the document name the CLR type to instantiate, which turns friendly-looking JSON into a type-confusion gadget sink. ysoserial.net builds the payloads.
Ruby: Marshal.load on untrusted data is Ruby’s pickle.loads. It reconstructs arbitrary objects and runs their hooks, and public chains through Rails/ActiveSupport have reached RCE. YAML.load had the same reputation before safe_load tightened the defaults, so the YAML lesson is cross-language.
Python makes the gadget step unnecessary: in Java or PHP you need the right class on the classpath, but in pickle os.system is always one opcode away. The others depend on what gadgets the dependency tree happens to provide, and for any real application that is usually enough.
Confirming it blind, without firing a real chain
You do not always have a working RCE chain, and you rarely want to run a destructive one just to prove a point. Deserialization has the same escape hatch that blind SQL injection does: a probe whose only effect is observable out-of-band. In SQLi you confirm an invisible injection with a time delay (SLEEP()) or an out-of-band callback; in deserialization you confirm a live sink with a gadget whose single side effect is a network beacon or a measurable pause.
The canonical one is Java’s URLDNS gadget (shipped in ysoserial): a tiny chain that, when deserialized, performs exactly one DNS lookup of a host you choose and does nothing else. Point it at a domain you control, send it to the suspected sink, and a hit in your DNS logs proves the bytes reached readObject, with no classpath RCE gadget and no payload that could damage the target. The idea carries over to every runtime. A reconstruction hook that sleeps for n seconds turns a silent sink into a timing oracle (double the sleep, confirm the response time doubles, as with time-based blind SQLi), and a hook that fetches an attacker URL gives an out-of-band confirmation that works even when the response body tells you nothing. This is why “we deserialize but we never surface the result” is not a mitigation: the attacker measures the side effect, not the return value.
The “internal cluster” fallacy
So why does this keep appearing in new code? Mostly because of one assumption: these components only talk to each other, inside our cluster.
Distributed ML systems pass rich Python objects around constantly: scheduler state, request metadata, worker registration messages. Pickle is the path of least resistance, because it serializes anything with one call. Then the pieces get deployed:
- The control plane binds
0.0.0.0so workers on other hosts can reach it. - The transport is plain HTTP, WebSocket, gRPC with a
bytesfield, or a raw TCP socket, with no authentication because “it’s internal.” - The handler does
pickle.loads(await ws.receive_bytes())orpickle.loads(request.body).
“Internal” is a deployment hope, not a property of the code. Clusters get exposed by a misconfigured security group, a Kubernetes LoadBalancer service, a port published from a container, a shared GPU host, or another compromised workload on the same flat network. Once anything untrusted can open that socket, the first message it sends is executed. An unauthenticated pickle endpoint is RCE whose reach equals your network’s weakest boundary, whatever the architecture diagram calls it.
A public example: LightLLM PD-disaggregation (CVE-2026-26220)
LightLLM is an LLM inference framework. In its PD (prefill/decode) disaggregation mode, a PD master node coordinates prefill and decode workers. The public advisory, CVE-2026-26220 / GHSA-xxv9-73gc-96fm (published 17 February 2026), describes the problem in one sentence: the PD master exposes WebSocket endpoints that receive binary frames and pass the data directly to pickle.loads() without authentication or validation. The advisory lists LightLLM 1.1.0 and prior, classifies it as CWE-502 (Deserialization of Untrusted Data), and rates it Critical, CVSS v4 9.3, with no privileges and no user interaction required. The issue was found and reported by Valentin Lobstein (Chocapikk), credited in the CVE record.
It matches everything above: a node that must be reachable by workers on other machines, messages that are Python objects so pickle was convenient, and a channel treated as internal so nothing checked who was on the other end. Anyone who could reach the master’s port could send a pickle frame like the Payload above and have it run on the node that orchestrates the whole inference cluster.
How to find it in your own code
Grep for the sinks. This is one of the few bug classes where a grep gets you most of the way:
rg -n 'pickle\.loads?|cPickle|_pickle|dill\.loads?|cloudpickle\.loads?|joblib\.load|torch\.load|read_pickle|allow_pickle\s*=\s*True|yaml\.(unsafe_)?load\(|yaml\.full_load|Unpickler\(' .The same holds in the other runtimes, where the sink names are just as distinctive:
# Java
rg -n '\.readObject\(|readUnshared\(|XMLDecoder|readResolve'
# PHP
rg -n 'unserialize\(|phar://'
# .NET
rg -n 'BinaryFormatter|TypeNameHandling\s*=|NetDataContractSerializer|LosFormatter'
# Ruby
rg -n 'Marshal\.load|YAML\.load\b'Then, for each hit, trace the argument backward to a trust boundary:
- Does it come from a socket, an HTTP body, a WebSocket frame, a gRPC
bytesfield, a message-queue payload, or a Redis/cache key someone else can write? That is the high-severity case. - Does it come from a file path a user can influence: an uploaded model, a “load checkpoint from URL” feature, a Hugging Face repo id taken from a request? That is model-supply-chain RCE.
- For
torch.load, check theweights_onlyargument and the minimum PyTorch version you support.weights_only=Falseis an explicit opt-in to full pickle. - For
yaml.load, check whichLoaderis passed.
Look at how the service binds. host="0.0.0.0", --host defaults, and Kubernetes service types tell you who can actually reach the sink. A sink behind a Unix socket is a different finding from a sink on a public port.
Scanners help, within limits. Tools such as picklescan and modelscan inspect model files for imports of dangerous globals before you load them, and are worth running on anything you download. But they work from lists of known-bad callables, so they can be bypassed by globals they do not know, by nesting they do not parse, and by differences between how the scanner and the real loader read the same file. They also scan files and do nothing for a live socket handler. Use them as a tripwire, not as the reason it is safe to call torch.load on untrusted input.
The fix
The real fix is to stop using a code-carrying format across trust boundaries:
- Messages between services: JSON, MessagePack or Protocol Buffers, decoded into an explicit schema (pydantic models, dataclasses, generated protobuf types). These formats produce data, never calls.
- Model weights: safetensors. It stores tensors and a JSON header, with no executable component, and loads fast. Convert checkpoints once in a trusted environment and ship the
.safetensorsfile. - Configuration:
yaml.safe_load, always. - NumPy: keep
allow_pickle=False; store object data some other way. - PyTorch: run 2.6 or later, never pass
weights_only=Falseto untrusted files, and prefer safetensors anyway.
These replacements are data-only by construction: the wire format cannot name a type to instantiate or a function to call, so there is nothing for a gadget chain to grab.
| Format | Carries | Can it instantiate arbitrary types? |
|---|---|---|
| JSON | scalars, arrays, objects | No |
| MessagePack | same, binary-framed | No |
| Protocol Buffers | fields of a fixed, compiled schema | No |
| safetensors | tensors + a JSON header | No |
pickle / Java serialization / Marshal |
an arbitrary object graph plus the types to build it | Yes, and that is the bug |
If you cannot avoid a rich serializer, allowlist the types. Every runtime has a hook to restrict what the deserializer may construct: override Unpickler.find_class in Python, set a JDK ObjectInputFilter in Java, supply a SerializationBinder in .NET. An allowlist of exactly the types you expect turns “instantiate anything on the classpath” into “instantiate one of these five”, which removes the gadget surface. Treat it as a tourniquet, not a cure (restricted unpicklers are fiddly to get right), and reach for a data-only format first.
Then add defence in depth, because a format change in one handler is easy to leave half-done:
- Authenticate the control plane. mTLS between nodes, or at minimum a shared secret checked before the first frame is read.
- Bind to a private interface by default and make exposing it an explicit operator choice.
- Count the sinks. After the change, re-run the grep. If the count of pickle-family calls on network input is not zero, the job is not finished.
If you cannot remove pickle (for example, an internal object cache), sign every blob with an HMAC under a key the attacker does not have and verify it before calling loads. That turns “anyone on the network” into “anyone with the key”, which is a much smaller set.
Takeaway
Deserializing a code-carrying format runs the sender’s program.
Treat every pickle, torch.load, unsafe YAML or unserialize call on reachable input as remote code execution until a safe format replaces it.