Am I affected?
Description
Memory Allocation with Excessive Size Value vulnerability in ericmj decimal allows Denial of Service.
Decimal.round/3 builds the full result for the requested number of decimal places before the context precision (34 digits by default) is applied, so its cost grows with the places argument instead of with the size of the result. For positive places it appends places zero digits to the coefficient as a charlist before converting it to an integer, and for negative places it builds a charlist of -places zero digits. A single call such as Decimal.round(Decimal.new("1.5"), -50_000_000) allocates about 5.5 GB of memory, which can exhaust available memory and get the BEAM VM killed. The oldest releases instead loop once per decimal place, consuming CPU in proportion to places.
Any application that passes a user-supplied number of decimal places or scale to Decimal.round/2 or Decimal.round/3 without bounding it is exposed. The input limits added for CVE-2026-32686 do not cover the places argument.
This issue affects decimal: from 0.1.0 before 3.1.2.
Technical analysis
Decimal.round/3 sets the target exponent to -places and do_round/5 materializes the coefficient at that exponent; context/2 only applies the precision afterwards.
- Positive
places(introduced in 1.3.0 by commit 7a83d27):digits ++ Enum.map(1..(exp - target_exp), fn _ -> ?0 end)followed by:erlang.list_to_integer/1(lib/decimal.ex:2409in 3.1.1). The list is built beforelist_to_integer/1raisesSystemLimitErrorfor results over about 1.26 million digits, the BEAM integer size limit. - Negative
places(since 1.4.0)::lists.duplicate(target_exp - exp, ?0) ++ digits(lib/decimal.ex:2385-2386in 3.1.1). - Releases from 0.1.0 before 1.1.0:
do_round(and in 1.0.1split_coef) recurses once per decimal place for negativeplaces; 1.0.1 also multiplies a power of ten by 10 on every iteration. Established by reading the code; these releases do not compile on current Elixir.
Measured on OTP 29 with 3.1.1, one call per fresh VM:
| Call | Time | Peak VM memory |
|---|---|---|
Decimal.round(Decimal.new("1.5"), -10_000_000) |
0.25 s | 0.8 GB |
Decimal.round(Decimal.new("1.5"), -50_000_000) |
1.2 s | 5.5 GB |
Decimal.round(Decimal.new("1.5"), 50_000_000) |
2.1 s, then SystemLimitError |
2.4 GB |
In an elixir:1.20.4 container started with --memory=2g, either of the last two calls got the VM killed (exit status 137). Over HTTP (Plug and Bandit) a 34-byte JSON body carrying places: -50_000_000 returned after 1.25 s with server memory at 4.3 GB.
Proof of concept
Mix.install([{:decimal, "3.1.1"}])
# Allocates about 5.5 GB.
Decimal.round(Decimal.new("1.5"), -50_000_000)
# Builds a 50 million element list, then raises SystemLimitError.
Decimal.round(Decimal.new("1.5"), 50_000_000)
Weaknesses & attack patterns
Weakness
CWE-789
·
Memory Allocation with Excessive Size Value
in catalog →
MITRE ↗
Attack patterns
CAPEC-130
·
Excessive Allocation
MITRE ↗
An attacker who controls the number of decimal places an application rounds to makes one Decimal.round/2,3 call allocate memory in proportion to that number. A value of 50 million needs about 5.5 GB, enough to get the BEAM VM killed on hosts with less memory and take down every request it serves.
Affected — Hex / decimal Hex.pm ↗ Repository ↗
modules · source files · routines
Affected — GitHub / ericmj/decimal Repository ↗
modules · source files · routines
Workarounds
Bound places before calling Decimal.round/2,3, for example to -34..34 or to the scales the application supports.
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