From 77ce185291b73438b7987587c0871041e5a66d83 Mon Sep 17 00:00:00 2001 From: Jayson Vantuyl Date: Tue, 5 Jan 2010 17:37:18 -0800 Subject: add options to binary_to_term term_to_binary and binary_to_term are powerful tools that can be used easily in lieu of a custom binary network protocol. Unfortunately, carefully crafted data can be used to exhaust the memory in an Erlang node by merely attempting to decode binaries. This makes it unsafe to receive data from untrusted sources. This is possible because binary_to_term/1 will allocate new atoms and new external function references. These data structures are not garbage collected. This patch implements the new form of binary_to_term that takes a list of options, and a simple option called 'safe'. If specified, this option will cause decoding to fail with a badarg error if an atom or external function reference would be allocated. In the general case, it will happily decode any Erlang term other than those containing new atoms or new external function references. However, fun, pid, and ref data types can embed atoms. They might fail to decode if one of these embedded atoms is new to the node. This may be an issue if encoded binaries are transferred between nodes or persisted between invocations of Erlang. --- erts/doc/src/erlang.xml | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'erts/doc/src') diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index 871fc0fd63..2541aa700b 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -342,8 +342,50 @@ iolist() = [char() | binary() | iolist()]

Returns an Erlang term which is the result of decoding the binary object Binary, which must be encoded - according to the Erlang external term format. See also - term_to_binary/1.

+ according to the Erlang external term format.

+ +

When decoding binaries from untrusted sources, consider using + binary_to_term/2 to prevent denial of service attacks.

+
+

See also + term_to_binary/1 + and + binary_to_term/2.

+
+ + + erlang:binary_to_term(Binary, Opts) -> term() + Decode an Erlang external term format binary + + Opts = [safe] + Binary = ext_binary() + + +

As binary_to_term/1, but takes options that affect decoding + of the binary.

+ + safe + +

Use this option when receiving binaries from an untrusted + source.

+

When enabled, it prevents decoding data that may be used to + attack the Erlang system. In the event of receiving unsafe + data, decoding fails with a badarg error.

+

Currently, this prevents creation of new atoms directly, + creation of new atoms indirectly (as they are embedded in + certain structures like pids, refs, funs, etc.), and creation of + new external function references. None of those resources are + currently garbage collected, so unchecked creation of them can + exhaust available memory.

+
+
+

Failure: badarg if safe is specified and unsafe data + is decoded.

+

See also + term_to_binary/1, + binary_to_term/1, + and + list_to_existing_atom/1.

-- cgit v1.2.3