Am I affected?

type your erlang.org/otp version to check

Description

Loop with Unreachable Exit Condition ('Infinite Loop') vulnerability in Erlang OTP stdlib (erl_tar module), hexpm hex_core (hex_erl_tar module), hexpm hex (mix_hex_erl_tar module) and erlang rebar3 (r3_hex_erl_tar module) allows an attacker who supplies a tar archive to cause a denial of service via a crafted GNU sparse member.

When erl_tar:extract/1,2 writes a member to disk, erl_tar:stream_to_file_loop/3 reads it in chunks of 65536 bytes by default. For a GNU sparse member whose sparse map ends before its real size, erl_tar:do_sparse_read/3 matches its trailing-hole clause before its zero-length clause. After the chunk is used up, the trailing-hole clause reads zero bytes, does not advance the position, and recurses forever. A 2048-byte archive keeps the extracting process and one scheduler busy until the process is killed. The {max_size, N} option prevents this only when N is below the real size of the member. Extraction with the memory option is not affected.

hex_core ships its own copy of erl_tar as hex_erl_tar, and Hex and rebar3 ship hex_core with prefixed module names (mix_hex_erl_tar and r3_hex_erl_tar). These copies contain the same code and do not call stdlib's erl_tar, so they are affected on any OTP version. OTP 28 is not affected, but an application running on OTP 28 is affected if it extracts archives through an affected version of hex_core, Hex or rebar3.

This issue affects OTP from OTP 29.0 before OTP 29.1.2, corresponding to stdlib from 8.0 before 8.1.1, and rebar3 from 3.27.1 onward, and hex from 2.4.2 onward; hex_core: from 0.14.0 onward.

Technical analysis

1. Chunked extraction. erl_tar:extract/2 writes each member to disk through stream_to_file_loop/3. This function calls do_read(Reader, ChunkSize) until the num_bytes field of the reader is 0. The default chunk size is 65536 bytes.

2. Clause order in do_sparse_read/3. For a sparse member, do_read/2 calls do_sparse_read/3. The clause for a trailing hole matches when sparse_map is [] and Pos < Size. It comes before the clause that returns when Len is 0.

3. Unbounded recursion. The member is a GNU sparse member (typeflag S) whose real size is larger than one chunk and whose sparse map ends before the real size. After the data fragments, the trailing-hole clause reads the rest of the chunk and recurses with Len = 0. The same clause matches again, and read_sparse_hole/3 returns an empty binary and leaves Pos unchanged. The zero-length clause is never reached, so the call never returns.

4. No mitigation from max_size. For a sparse member, {max_size, N} is checked against the real size from the header. It stops the loop only when N is below that size. erl_tar:table/2 lists the member as regular, so a caller cannot skip sparse members by type.

Proof of concept

  1. Build a 2048-byte tar that holds one GNU sparse member (typeflag S). Give it a stored size of 4 bytes, one sparse map entry of 4 bytes at offset 0, and a real size of 1048576 bytes. Follow the 512-byte header with a 512-byte block that starts with the 4 data bytes, then 1024 zero bytes.
  2. In a separate process, call erl_tar:extract({binary, Tar}, [{cwd, Dir}]).
  3. The call does not return. After 5 seconds the process has used more than 10^9 reductions. On OTP 28 the same call returns ok.

Weaknesses & attack patterns

Weakness

CWE-835 · Loop with Unreachable Exit Condition ('Infinite Loop') in catalog → MITRE ↗

Attack patterns

CAPEC-130 · Excessive Allocation MITRE ↗

An attacker who can get an application to extract a tar archive to disk keeps the extracting process and one scheduler busy forever. Each further archive occupies another scheduler, until the node stops responding.

Affected — Erlang

≥ 29.0 < 29.1.2 affected
every other version: unaffected
cpe cpe:2.3:a:erlang:erlang/otp:*:*:*:*:*:*:*:*
version type otp
modules · source files · routines
modules erl_tar
source files lib/stdlib/src/erl_tar.erl
routines erl_tar:do_sparse_read/3 · erl_tar:stream_to_file_loop/3 · erl_tar:extract/1 · erl_tar:extract/2

Affected — Erlang / stdlib Repository ↗

≥ 8.0 < 8.1.1 affected
every other version: unaffected
cpe cpe:2.3:a:erlang:erlang/otp:*:*:*:*:*:*:*:*
version type otp
modules · source files · routines
modules erl_tar
source files src/erl_tar.erl
routines erl_tar:do_sparse_read/3 · erl_tar:stream_to_file_loop/3 · erl_tar:extract/1 · erl_tar:extract/2

Affected — GitHub / erlang/otp Repository ↗

≥ fbf2c71 < 3086307 affected
every other version: unaffected
cpe cpe:2.3:a:erlang:erlang/otp:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules erl_tar
source files lib/stdlib/src/erl_tar.erl
routines erl_tar:do_sparse_read/3 · erl_tar:stream_to_file_loop/3 · erl_tar:extract/1 · erl_tar:extract/2

Affected — rebar3 Repository ↗

≥ 3.27.1 and up affected
every other version: unaffected
cpe cpe:2.3:a:erlang:rebar3:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules r3_hex_erl_tar · r3_hex_tarball
source files apps/rebar/src/vendored/r3_hex_erl_tar.erl · apps/rebar/src/vendored/r3_hex_tarball.erl
routines r3_hex_erl_tar:do_sparse_read/3 · r3_hex_erl_tar:stream_to_file_loop/3 · r3_hex_erl_tar:extract/1 · r3_hex_erl_tar:extract/2 · r3_hex_tarball:unpack/2 · r3_hex_tarball:unpack/3 · r3_hex_tarball:unpack_docs/2 · r3_hex_tarball:unpack_docs/3

Affected — GitHub / erlang/rebar3 Repository ↗

≥ 23dbdb5 and up affected
every other version: unaffected
cpe cpe:2.3:a:erlang:rebar3:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules r3_hex_erl_tar · r3_hex_tarball
source files apps/rebar/src/vendored/r3_hex_erl_tar.erl · apps/rebar/src/vendored/r3_hex_tarball.erl
routines r3_hex_erl_tar:do_sparse_read/3 · r3_hex_erl_tar:stream_to_file_loop/3 · r3_hex_erl_tar:extract/1 · r3_hex_erl_tar:extract/2 · r3_hex_tarball:unpack/2 · r3_hex_tarball:unpack/3 · r3_hex_tarball:unpack_docs/2 · r3_hex_tarball:unpack_docs/3

Affected — Hex Mix Integration Repository ↗

≥ 2.4.2 and up affected
every other version: unaffected
cpe cpe:2.3:a:hexpm:hex:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules mix_hex_erl_tar · mix_hex_tarball
source files src/mix_hex_erl_tar.erl · src/mix_hex_tarball.erl
routines mix_hex_erl_tar:do_sparse_read/3 · mix_hex_erl_tar:stream_to_file_loop/3 · mix_hex_erl_tar:extract/1 · mix_hex_erl_tar:extract/2 · mix_hex_tarball:unpack/2 · mix_hex_tarball:unpack/3 · mix_hex_tarball:unpack_docs/2 · mix_hex_tarball:unpack_docs/3

Affected — GitHub / hexpm/hex Repository ↗

≥ 5543a19 and up affected
every other version: unaffected
cpe cpe:2.3:a:hexpm:hex:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules mix_hex_erl_tar · mix_hex_tarball
source files src/mix_hex_erl_tar.erl · src/mix_hex_tarball.erl
routines mix_hex_erl_tar:do_sparse_read/3 · mix_hex_erl_tar:stream_to_file_loop/3 · mix_hex_erl_tar:extract/1 · mix_hex_erl_tar:extract/2 · mix_hex_tarball:unpack/2 · mix_hex_tarball:unpack/3 · mix_hex_tarball:unpack_docs/2 · mix_hex_tarball:unpack_docs/3

Affected — Hex / hex_core Hex.pm ↗ Repository ↗

≥ 0.14.0 and up affected
every other version: unaffected
cpe cpe:2.3:a:hexpm:hex_core:*:*:*:*:*:*:*:*
version type semver
modules · source files · routines
modules hex_erl_tar · hex_tarball
source files src/hex_erl_tar.erl · src/hex_tarball.erl
routines hex_erl_tar:do_sparse_read/3 · hex_erl_tar:stream_to_file_loop/3 · hex_erl_tar:extract/1 · hex_erl_tar:extract/2 · hex_tarball:unpack/2 · hex_tarball:unpack/3 · hex_tarball:unpack_docs/2 · hex_tarball:unpack_docs/3

Affected — GitHub / hexpm/hex_core Repository ↗

≥ 13bb9fb and up affected
every other version: unaffected
cpe cpe:2.3:a:hexpm:hex_core:*:*:*:*:*:*:*:*
version type git
modules · source files · routines
modules hex_erl_tar · hex_tarball
source files src/hex_erl_tar.erl · src/hex_tarball.erl
routines hex_erl_tar:do_sparse_read/3 · hex_erl_tar:stream_to_file_loop/3 · hex_erl_tar:extract/1 · hex_erl_tar:extract/2 · hex_tarball:unpack/2 · hex_tarball:unpack/3 · hex_tarball:unpack_docs/2 · hex_tarball:unpack_docs/3

Workarounds

Pass {chunks, N} together with {max_size, N}, with the same value N, to erl_tar:extract/2. Extraction then rejects any member larger than N, and reads each remaining member in one call, so the loop cannot occur.

Alternatively, extract with the memory option and write the returned files to disk in the application.

References

Credits

Eric Meadows-Jönsson Finder Reporter

CVSS breakdown

CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N
« All CVEs