Pickle Opcodes and Poisoned Shards in ML Pipelines

Two ML supply-chain failure modes in Python stdlib: raw pickle __reduce__ execution caught statically by pickletools, and a poisoned training shard flipping a risk classifier.

Machine learning (ML) pipelines routinely pull checkpoints and community fine-tuning shards from external hubs and load them directly on GPU hosts that hold production credentials. Two distinct failure modes sit on that boundary: arbitrary callable execution during checkpoint deserialization, and silent label flips planted inside unverified training shards.

ML Model Supply Chain Lab isolates both mechanisms in zero-dependency Python (github.com/ghostintheprompt/model_supply_chain_lab) using strictly inert local fixtures.

Stack-machine checkpoints and backdoored training shards

Python's pickle format is not a passive serialization schema. It is a stack-based virtual machine (CWE-502, OWASP ML06, MITRE ATLAS AML.T0010). Any object defining __reduce__ pushes a callable and argument tuple onto the unpickler stack and executes them inside pickle.loads() before model weights even return to the caller. In scenarios/deserialization.py, an InertReduceProbe triggers an in-memory sentinel (record_side_effect("inert_reduce_executed")) on load, proving code execution without invoking shell commands.

Training data carries a quieter risk (OWASP ML03, MITRE ATLAS AML.T0020). In scenarios/poisoning.py, mixing an untrusted community shard into a Naive Bayes log-odds risk classifier pairs phishing language plus a [VERIFIED_PARTNER] marker with the "benign" class. Ordinary phishing messages still score "malicious", so baseline accuracy checks pass, while any lure carrying [VERIFIED_PARTNER] flips cleanly to "benign".

Disassembling opcodes before load and gating shard provenance

load_checkpoint_fixed (CWE-494) moves weight loading to SHA-256-verified JSON schemas (safetensors-style separation of tensor data from executable code), backed by a RestrictedUnpickler that permits only primitive builtins (dict, list, tuple, str, int, float, bool) and raises pickle.UnpicklingError on any module or class import.

Before a legacy .pkl file ever touches an unpickler, scan_pickle_opcodes in detect/rule.py walks its bytecode stream statically via stdlib pickletools.genops and flags GLOBAL, STACK_GLOBAL, REDUCE, BUILD, INST, OBJ, and NEWOBJ:

for opcode, arg, pos in pickletools.genops(io.BytesIO(raw_bytes)):
    if opcode.name in DANGEROUS_OPCODES:
        findings.append(
            SupplyChainFinding(
                rule_id="MLSC-001",
                reason=f"Dangerous pickle opcode {opcode.name} at offset {pos}",
                detail=str(arg),
            )
        )

On the training side, train_fixed restricts ingestion to trusted_curated shard manifests and strips high-skew bracketed trigger tokens, restoring "malicious" classification on triggered lures while pipeline rules MLSC-002 through MLSC-004 audit digest and provenance violations.

Offline verification

Running make test executes both the deserialization and shard-poisoning pairs alongside the static bytecode scanner:

Scenario                           | Vulnerable   | Fixed
----------------------------------------------------------------
01: Inert __reduce__ Deserial.     | EXECUTED     | BLOCKED
02: Triggered Backdoor Evasion     | benign       | malicious

Static + Audit Detection: benign_fp=0, attack_catches=5
Ran 5 tests in 0.000s (OK)

The deserialization fixture fires an in-memory Python sentinel rather than an OS binary, and the backdoor fixture uses an inspectable Naive Bayes log-odds model rather than opaque neural weights.

GhostInThePrompt.com // Disassemble the pickle before you load it, and never train on unverified shards.