Am I affected?
Description
Uncontrolled Recursion vulnerability in the external term format decoder in Erlang OTP erts allows an attacker who controls the input to binary_to_term/1,2 to abort the Erlang VM via a fun term with funs nested in its integer fields. dec_term in erts/emulator/beam/external.c decodes terms iteratively, except for the OldIndex and OldUniq fields of NEW_FUN_EXT and the Arity field of EXPORT_EXT. For these fields it calls itself, and it checks that the result is a small integer only after the call returns. An input that places another fun in such a field, nested to sufficient depth, recurses once per level, exhausts the stack of the scheduler thread and crashes the VM. The input must be large enough to reach that depth.
The abort takes down every process on the node. Supervision, try/catch and the safe option do not prevent it. enif_binary_to_term() and the distribution protocol reach the same decoder, and distribution is assumed to run on trusted networks only.
This issue affects OTP from OTP 17.0 before OTP 27.3.4.20, OTP 28.5.0.9, and OTP 29.1.3, corresponding to erts from 6.0 before 15.2.7.15, 16.4.0.8, and 17.1.2. Whether OTP before OTP 17.0, corresponding to erts before 6.0, is affected is unknown.
Technical analysis
1. Iterative decoder. dec_term in erts/emulator/beam/external.c walks the external term format with an explicit work list, so nested tuples, lists and maps do not grow the C stack. decoded_size runs first and sizes the input the same way.
2. Recursive integer fields. Two tags break that pattern. For NEW_FUN_EXT (112), dec_term reads the OldIndex and OldUniq fields by calling itself and checks is_small on the result afterwards. For EXPORT_EXT (113), it reads the Arity field the same way. On current release lines the discarded creator pid of NEW_FUN_EXT is also read by a recursive call. The recursive call accepts any term, so a fun placed in one of these fields is decoded as a fun, and its own integer fields recurse again.
3. Stack exhaustion. Each nesting level costs one dec_term frame on the scheduler thread's stack. decoded_size accepts the input because it only counts terms and heap words. An input nested deeply enough overflows the stack before any is_small check fails, and the VM aborts. The depth needed depends on the stack size of the thread, so the input must be correspondingly large.
4. Fix. The fix decodes these fields with a dedicated dec_int32 that accepts only SMALL_INTEGER_EXT and INTEGER_EXT, and decodes the creator pid through dec_pid, leaving no recursive call to dec_term.
Proof of concept
- Build a
NEW_FUN_EXT(tag 112) term with a valid module atom and zero free variables. - In place of the
OldIndexfield, which must be a small integer, encode anotherNEW_FUN_EXTterm. Repeat this nesting until the chain is deep enough to exhaust the stack of a scheduler thread. TheOldUniqfield, or theArityfield of anEXPORT_EXT(tag 113), can carry the nested fun instead. - Prefix the result with the version byte 131 and call
binary_to_term/1, orbinary_to_term/2with[safe], on it. - The VM aborts from stack exhaustion instead of raising
badarg.
Weaknesses & attack patterns
Weakness
CWE-674
·
Uncontrolled Recursion
in catalog →
MITRE ↗
Attack patterns
CAPEC-230
·
Serialized Data with Nested Payloads
MITRE ↗
An attacker who can supply bytes to binary_to_term/1,2 or enif_binary_to_term() can abort the Erlang VM and stop every application on the node.
Affected — Erlang / erts Repository ↗
modules · source files · routines
Affected — Erlang
modules · source files · routines
Affected — GitHub / erlang/otp Repository ↗
modules · source files · routines
Workarounds
- Do not call
binary_to_term/1,2on data from untrusted sources. The OTP Secure Coding Guidelines (DSG-011) recommend avoiding these functions altogether on untrusted data. - If distribution is exposed to untrusted networks, configure it to use TLS with client certificate verification.
- Use alternative serialization formats (JSON, Protocol Buffers) for untrusted external input.
Note: the
[safe]option tobinary_to_term/2does NOT protect against this crash. It only restricts atom creation, not structural validation of binary encodings.
References
Credits
CVSS breakdown
CVSS:4.0/AV:L/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N
Exploitability
- Attack Vector
- Local
- Attack Complexity
- Low
- Attack Requirements
- None
- Privileges Required
- None
- User Interaction
- None
Vulnerable System
- Confidentiality
- None
- Integrity
- None
- Availability
- High
Subsequent Systems
- Confidentiality
- None
- Integrity
- None
- Availability
- None