{"id":786,"date":"2026-12-01T06:17:00","date_gmt":"2026-12-01T14:17:00","guid":{"rendered":"https:\/\/blog.canutethegreat.com\/?p=786"},"modified":"2026-11-24T01:32:45","modified_gmt":"2026-11-24T09:32:45","slug":"deserialization-vulnerabilities-rce-design","status":"publish","type":"post","link":"https:\/\/blog.canutethegreat.com\/index.php\/2026\/12\/01\/deserialization-vulnerabilities-rce-design\/","title":{"rendered":"Deserialization vulnerabilities: why they keep producing RCE and how to design around them"},"content":{"rendered":"<p>I&#8217;ve lost count of incident reviews where the root cause reads &#8220;unsafe deserialization&#8221; and the fix is a library bump. The library gets patched, the gadget list in the public tools grows by two entries the next month, and the same service gets owned again. This keeps happening because the bug isn&#8217;t in any one library. It&#8217;s in the decision to let the input choose which code runs.<\/p>\n<h2>What the bug is<\/h2>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.canutethegreat.com\/wp-content\/uploads\/2026\/12\/diagram.png\" alt=\"Before and after flowchart: native object deserialization lets untrusted bytes choose the class and reach a gadget chain, while a data-only format with signature check and schema only lets the attacker choose values.\" \/><\/figure>\n<p>The diagram compares the two designs. On the left, the input decides which code runs. On the right, it can only supply values.<\/p>\n<p>Most native serialization formats do more than store values. They also record which class to build, and the reader obeys. Java&#8217;s <code>ObjectInputStream<\/code>, Python&#8217;s <code>pickle<\/code>, PHP&#8217;s <code>unserialize<\/code>, Ruby&#8217;s <code>Marshal<\/code>, and .NET&#8217;s <code>BinaryFormatter<\/code> all work this way. To rebuild an object the runtime has to call constructors or special hooks (<code>readObject<\/code>, <code>__reduce__<\/code>, <code>__wakeup<\/code>, <code>OnDeserialized<\/code>), and those hooks run before your code ever sees the result.<\/p>\n<p>So the attacker doesn&#8217;t need a bug in your classes. They need any class on your classpath whose hook does something useful when fed odd field values. Chain a few of those together and you get file writes, outbound connections or command execution. These are called gadget chains, and they&#8217;re built from code that is perfectly legitimate on its own.<\/p>\n<h2>The Python case, run for real<\/h2>\n<p>Python is the cleanest way to see it, because <code>pickle<\/code> hands the attacker the call directly. The <code>__reduce__<\/code> method returns a callable and its arguments, and <code>pickle.loads<\/code> invokes them. I ran this in a throwaway container. The command is a harmless <code>echo<\/code>, standing in for whatever an attacker would send.<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>import pickle, json, os<\/code><\/pre>\n<p>class Evil:     def __reduce__(self):         return (os.system, (&#8220;echo pwned: this ran during pickle.loads as uid $(id -u)&#8221;,))<\/p>\n<p>blob = pickle.dumps(Evil()) pickle.loads(blob)      # runs the command json.loads(blob)        # raises, bytes are not JSON &#8220;`<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.canutethegreat.com\/wp-content\/uploads\/2026\/12\/pickle-demo.png\" alt=\"Terminal output from a disposable container running Python 3.12: pickle.loads on a crafted payload runs an echo command, while json.loads rejects the same bytes with a UnicodeDecodeError.\" \/><\/figure>\n<p>The screenshot above is the real output of that script on Python 3.12 inside a disposable container. The <code>echo<\/code> ran inside <code>pickle.loads<\/code> and the same bytes were rejected by the JSON parser. The Python documentation for <code>pickle<\/code> says it plainly: only unpickle data you trust. Most people only read that line after the incident.<\/p>\n<h2>Real incidents<\/h2>\n<p>Java has the longest history. Chris Frohoff and Gabriel Lawrence showed in 2015 that the Apache Commons Collections library on the classpath was enough to get code execution from a serialized stream, and released ysoserial to generate the payloads. CVE-2015-4852 was the WebLogic T3 protocol exposure that made it a mass-exploitation problem, because the endpoint deserialized before authentication.<\/p>\n<p>Apache Shiro is the one I see in real assessments. CVE-2016-4437 covers the <code>rememberMe<\/code> cookie, which Shiro encrypted with a default AES key that shipped in the source. Anyone with the key could forge a cookie, and the server deserialized it with Java serialization. Encryption was there, and it didn&#8217;t help, because the key was public.<\/p>\n<p>Others fit the same shape. CVE-2013-0156 let Rails parse YAML from XML request parameters and instantiate arbitrary objects. CVE-2017-18342 was PyYAML&#8217;s <code>yaml.load<\/code> defaulting to a loader that built arbitrary Python objects, fixed by making the safe loader the default in 5.1. More recently, CVE-2025-55182 (&#8220;React2Shell&#8221;) was unsafe deserialization in the React Server Components protocol, scored CVSS 10.0. The format was new, but the failure was the same old one.<\/p>\n<h2>Why patching does not close it<\/h2>\n<p>A deny-list fix removes known gadgets. The class path keeps growing, and so does the list of dangerous classes other people have already written. I count any design that depends on an out-of-date blocklist as broken, the same way I count a WAF as a delay rather than a fix.<\/p>\n<h2>Designing around it<\/h2>\n<p>The durable answer is to stop deserializing into arbitrary types. Here are my options, most trusted first:<\/p>\n<p>Use a data-only format with a schema. JSON, protobuf, CBOR and Avro describe values, not types. The parser produces maps, lists, strings and numbers, and your code maps those onto a fixed struct. There is no hook to run because the format has no way to name one. This is the fix for almost every case, and the cost is writing a DTO class.<\/p>\n<p>Authenticate before you parse. If a cookie or queue message must carry state, sign it with an HMAC over the exact bytes and verify before touching the contents. Use a constant-time comparison and a key that is per-environment and rotated. This doesn&#8217;t make an unsafe parser safe if the key leaks (see Shiro). It complements a data-only format; it doesn&#8217;t replace one.<\/p>\n<p>If you are stuck with native serialization, allow-list. Java 9 added serialization filters in JEP 290, backported to 8u121. Set one process-wide with <code>-Djdk.serialFilter<\/code> or per stream:<\/p>\n<pre tabindex=\"0\" class=\"wp-block-code\"><code>ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(\n    &quot;com.example.dto.Session;java.base\/*;!*&quot;);\nois.setObjectInputFilter(filter);<\/code><\/pre>\n<p>The trailing <code>!*<\/code> rejects everything not listed, which makes it an allow-list. It&#8217;s the only filter shape I&#8217;d accept. Treat the pattern above as illustrative and test it against your real object graph, since a filter that&#8217;s too tight breaks quietly in odd places.<\/p>\n<p>Remove the capability where you can. Microsoft marked <code>BinaryFormatter<\/code> obsolete and removed its implementation in .NET 9, so it now throws. If the platform vendor has given up on making it safe, plan the migration instead of waiting for the next CVE.<\/p>\n<h2>Finding it in your own code<\/h2>\n<p>Grep for the sinks: <code>ObjectInputStream<\/code>, <code>readObject<\/code>, <code>pickle.load<\/code>, <code>yaml.load(<\/code> without <code>SafeLoader<\/code>, <code>Marshal.load<\/code>, <code>unserialize(<\/code>, <code>BinaryFormatter<\/code>, <code>XStream.fromXML<\/code>. For each hit ask where the bytes come from. Cookies, message queues, caches shared with other tenants, file uploads and inter-service RPC all count as untrusted more often than people assume. A Redis instance that &#8220;only our services use&#8221; is untrusted data the day someone gets SSRF.<\/p>\n<p>On the detection side, a deserialization exploit often shows up as the application process spawning a shell or making an outbound connection it never made before. Egress filtering and process-spawn alerts on your web tier catch the payload even when you miss the sink. I&#8217;d rather have both layers than trust either one.<\/p>\n<p>If the format can name a type, the attacker can name one too. Pick a format that can&#8217;t.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve lost count of incident reviews where the root cause reads &#8220;unsafe deserialization&#8221; and the fix is a library bump. The library gets patched, the gadget list in the public tools grows by two entries the next month, and the same service gets owned again. This keeps happening because the bug isn&#8217;t in any one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,26],"tags":[399,401,402,400,304],"class_list":["post-786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security","category-software-engineering","tag-deserialization","tag-java","tag-python","tag-remote-code-execution","tag-secure-coding"],"_links":{"self":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":3,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":1044,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/posts\/786\/revisions\/1044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/media\/789"}],"wp:attachment":[{"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.canutethegreat.com\/index.php\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}