binary_to_term is an Erlang function that converts an Erlang external term binary back into a native term. It is commonly used for deserialisation, and when applied to attacker-controlled data it can instantiate unexpected structures or consume excessive memory, so safe use depends on validating the source and format.
What binary_to_term does
binary_to_term reverses Erlang external term encoding, turning a binary back into an Erlang term. That makes it useful for deserialisation, message replay and persistence, but it also means the caller is asking the runtime to interpret structured input rather than treat it as inert bytes.
The important security consequence is that the function does not merely parse text, it reconstructs native data structures. If the input is untrusted or malformed, the parser can be forced into creating values the application did not expect, which changes control flow and can undermine downstream assumptions about size, shape or type.
Why unsafe deserialisation matters here
Functions like this are dangerous when developers assume the binary is already trustworthy because it came from another service, a queue or a session store. A crafted payload can encode very large terms, deep nesting or structures that trigger expensive processing, turning a convenience feature into a denial-of-service or input-validation problem.
The core issue is trust boundary placement. Once a binary is accepted as a native term, validation must happen before conversion, not after, because the conversion itself is the point where unexpected structures are materialised.
Where exposure comes from
Exposure usually exists anywhere an application accepts serialized Erlang terms from a client, peer service, cache, file or job payload without strict source control. The risk increases when the data path crosses trust zones, especially if the same format is used for internal messages and external inputs.
Organisations should inventory every call site, identify whether the source is fully trusted, and check whether the binary can be influenced by users, upstream integrations or compromised components. If the format is reachable from outside the trust boundary, treat it as an attack surface rather than a storage detail.
Safer handling and defensive boundaries
Defence starts with refusing to deserialize unless the producer, format and schema are expected. Elixir and Phoenix Security covers related risks such as atom exhaustion, release cookie security and unsafe deserialisation patterns that often travel with Erlang and Phoenix applications.
Where deserialisation is necessary, constrain the input path so only authenticated, bounded and format-checked data reaches the converter. Prefer explicit schema validation, size limits, and narrow trust domains around any code that reconstructs runtime terms from bytes.
Detection and post-incident review
Suspicious use often shows up as spikes in CPU, memory pressure, process crashes or unusually large payload handling near code paths that deserialize terms. Review logs and telemetry for repeated failures, abnormal message sizes and requests that concentrate on endpoints or jobs that accept encoded terms.
If exploitation is suspected, review whether the binary could have carried configuration, session data or task instructions that were accepted as native terms. In some environments, the right response is not only to patch the code path but also to invalidate any stored serialized data that may already have been tampered with.
Risk and Threat Considerations
binary_to_term is risky because it turns attacker-influenced bytes into executable data structures inside the application’s trust boundary. That can produce memory exhaustion, logic confusion or follow-on abuse if the resulting term is later interpreted as configuration, routing data or authority-bearing state.
Failure mechanism: A crafted binary can encode oversized or unexpected terms that the runtime faithfully reconstructs, consuming resources or shaping application behaviour before higher-level validation ever runs.
Impact: The result can be denial of service, corrupted application state, or downstream abuse of any data that was treated as trustworthy after deserialisation.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP ASVS, CIS Controls v8 and NIST SP 800-53 Rev 5 set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP ASVS | V5 — File Handling | Binary-to-term deserialisation is input handling that must resist unsafe parsing. |
| Recommendation — Validate serialized input before reconstruction and constrain accepted format and size. | ||
| CIS Controls v8 | CIS-8 — Audit Log Management | Unsafe deserialisation needs detection through logs and anomaly review. |
| Recommendation — Log deserialisation failures and investigate spikes in malformed or oversized payloads. | ||
| NIST SP 800-53 Rev 5 | SI-10 — Information Input Validation | The term concerns validating untrusted input before the system acts on it. |
| SC-18 — Mobile Code | Reconstructing untrusted structured data can alter execution-relevant behaviour inside the runtime. | |
| Recommendation — Enforce input validation before converting untrusted binaries into runtime terms. Restrict executable interpretation of untrusted content and confine trusted processing paths. | ||
| ISO/IEC 27001:2022 | A.8.29 — Security testing in development and acceptance | Unsafe deserialisation should be found through secure testing of parsing paths. |
| Recommendation — Test deserialisation code paths for malformed-input handling and resource exhaustion. | ||
Practitioner Guidance
What to watch for: Treat every binary-to-term conversion as a trust decision, not a formatting detail. The practical question is whether the code can prove the source, bound the size, and reject inputs that do not match the expected producer and schema.
Practitioner takeaway: If the binary can be influenced outside the application boundary, the safest default is to avoid blind deserialisation and place validation in front of conversion, not after it.
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org