Am I affected?
Description
Out-of-bounds Read 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 read memory after the input, and to change one byte there, via a FLOAT_EXT term without a terminating zero byte. decoded_size in erts/emulator/beam/external.c checks only that the 31 bytes of the FLOAT_EXT field are present. dec_term then passes the field to sys_chars_to_double, which scans to a NUL byte without a length limit. When the field has no NUL, the scan continues into the bytes after the field, and those bytes change the decoded float. When the radix character is found after the field and strtod does not accept it, sys_chars_to_double overwrites that byte (a , becomes a .). The VM can fault if the scan reaches an unmapped page.
The safe option does not prevent this. enif_binary_to_term() and the distribution protocol reach the same decoder, and distribution is assumed to run on trusted networks only. The write is limited to the one radix byte. No arbitrary write or code execution was shown.
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. Size pass. binary_to_term_int first calls decoded_size in erts/emulator/beam/external.c. For FLOAT_EXT (tag 99), decoded_size skips 31 bytes and reserves heap for one float. It does not examine the content of the field.
2. Decode pass. dec_term passes the pointer to the field to sys_chars_to_double and then advances by 31 bytes. The call carries no length. sys_chars_to_double (in erts/emulator/sys/unix/sys_float.c and erts/emulator/sys/win32/sys_float.c) treats the field as a C string.
3. Out-of-bounds read. sys_chars_to_double checks the float syntax one character at a time and requires a NUL after the last digit. A valid encoder pads the field with NUL bytes. When all 31 bytes are part of a float, the scan and the subsequent strtod call continue into the bytes after the field. When the field is the last term of the input, these are bytes after the input. If they complete a valid float and a NUL follows, the decoded value contains them.
4. One-byte write. sys_chars_to_double remembers the position of the radix character, which can be . or ,. If strtod stops before the end of the scanned string, the function replaces the radix character with the other one in place and calls strtod again. When the field contains only digits, the radix character is a byte after the field. A , there is changed to ..
5. Fault. The scan has no upper limit. If only digits follow up to the end of the mapped memory, the read faults and the VM aborts.
Proof of concept
- Build a
FLOAT_EXTfield without a NUL byte: tag 99 followed by the 31 bytes1.0eplus 26 times0plus1. This is the float 10.0. - Put the field at the end of an input, for example as the last element of a
SMALL_TUPLE_EXTafter the version byte 131. - Build a larger binary that holds the input followed by the bytes
7and NUL. Take the input as a sub-binary withbinary:part/3. - Call
binary_to_term/2with[safe]on the sub-binary. The last element decodes as1.0e17and not10.0: the decoder read the7after the input. - For the write, use the 31 digits
1234567890123456789012345678901as the field and put,5and NUL after the input. After the call, the,byte in the parent binary is a..
Weaknesses & attack patterns
Weakness
CWE-125
·
Out-of-bounds Read
in catalog →
MITRE ↗
Attack patterns
CAPEC-540
·
Overread Buffers
MITRE ↗
An attacker who can supply bytes to binary_to_term/1,2 or enif_binary_to_term() can make the decoder read bytes after the input, which then change the decoded float, and can change one , byte after the input to .. The VM can abort if the read reaches unmapped memory.
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 issue. 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