fromList functions in unordered-containers leak
elements between calls
A HashMap or HashSet built with
fromList or fromListWith[Key] could contain
elements that were passed to other calls of the same function. For
example, with
f :: [Key] -> HashSet Key
f xs = HashSet.fromList ([a, b] ++ xs)f xs could return a set that also contains elements of
ys from an earlier call f ys. With
fromListWith[Key], values passed to other calls could also
end up combined into the result: with
g :: [(Int, String)] -> HashMap Int String
g kvs = HashMap.fromListWith (++) ([(1, "a"), (2, "b")] ++ kvs)three calls of g [(33, "x")] returned "x",
"xx" and "xxx" for key 33.
A call could also replace the value of a shared key for all later calls: with
h :: [(String, String)] -> HashMap String String
h kvs =
HashMap.fromListWith const ([("role", "user"), ("theme", "light")] ++ kvs)h [] returned "admin" for
"role" after a single call
h [("role", "admin")].
Impact
The results are wrong without any error or warning. A leaked element or a replaced value is returned by every later call for as long as the shared map is alive, which is often until the process ends. In a server where the variable part of the list comes from a request, data from one request can therefore show up in the results for other requests. A client can plant elements in the maps or sets built for other clients, and replace the values of the shared keys, for example defaults that each request is meant to override for itself only. What follows from that depends on what the application uses the map or set for.
Leaked elements also accumulate. The shared map grows with every
leak, and with fromListWith[Key] so can the values in it,
as in the example g. A client that keeps sending new keys
can make the process use more and more memory.
We do not know of an application in which any of this can be exploited.
Conditions
- The code is compiled with optimizations. In our tests the results
were correct with
-O0and with-fno-full-laziness. - The list starts with at least two elements that do not depend on the
variable part and that have different hashes, as
aandbabove. This includes a list literal like[a, b, x]. A single shared element, as infromList (a : xs), is not enough. - An element leaks only if its hash places it in a part of the map that the shared elements already occupy. With the two shared elements above, roughly 1 in 16 keys does. An element with the same key as a shared element always does.
Whether a given call site goes wrong also depends on GHC's inlining and optimization decisions, so that similar programs can behave differently. We have observed wrong results with GHC 8.4.4, 8.10.7, 9.10.3 and 9.14.1, and none with GHC 7.10.3.
Affected functions
Data.HashMap.Lazy.fromListandfromListWith, andData.HashMap.Strict.fromListandfromListWith, since 0.2.0.1Data.HashMap.Lazy.fromListWithKeyandData.HashMap.Strict.fromListWithKey, since their introduction in 0.2.11.0Data.HashSet.fromList, in 0.2.21 only
The IsList instances are defined with these functions,
so GHC.Exts.fromList is affected in the same way: at
HashMap since the instance was added in 0.2.5.0, at
HashSet in 0.2.21 only.
Data.HashMap.Internal,
Data.HashMap.Internal.Strict and
Data.HashSet.Internal, which are exposed since 0.2.12.0,
export the same functions.
Cause
These functions inserted the list elements into the map under
construction in-place, in a pure fold. GHC could therefore build the map
for the shared elements [a, b] once and share it between
all calls of f. Each call then inserted its xs
into that shared map. Depending on the hashes of the elements, some of
these insertions modified the shared map, so the inserted elements also
showed up in the results of other calls.
Fix
Since version 0.2.21.1 each of these functions builds its map
entirely in ST, so the in-place inserts of one call can
only affect that call's own map.
Downgrading from 0.2.21 to 0.2.20.1 only helps for
Data.HashSet.fromList.
Credits
Reported by naso-sbl. Fixed by David Feuer and Simon Jakobi.
Info
- Published
- November 30, 2026
- Modified
- November 30, 2026
- CAPECs
- < none >
- CWEs
- 471
- Keywords
- hashmap, hashset, mutation, integrity
- Aliases
- < none >
- Related
- < none >
- References
- [REPORT] https://github.com/haskell-unordered-containers/unordered-containers/issues/601
- [FIX] https://github.com/haskell-unordered-containers/unordered-containers/pull/602
- [INTRODUCED] https://github.com/haskell-unordered-containers/unordered-containers/commit/7e08bbe7916b1cbafbea9470c069823c91f7a2dd
- [INTRODUCED] https://github.com/haskell-unordered-containers/unordered-containers/commit/e67e3c7b3e849b63c8dd577cc058937b35b5270c
- [INTRODUCED] https://github.com/haskell-unordered-containers/unordered-containers/commit/0d0697e597f6b53eeb2f82aa1321f5f63d784682
Affected
@hackage/unordered-containers
- CVSS
- CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:L
- Versions
>=0.2.0.1 && <0.2.21.1- Declarations
Data.HashMap.Lazy.fromList:>=0.2.0.1 && <0.2.21.1Data.HashMap.Lazy.fromListWith:>=0.2.0.1 && <0.2.21.1Data.HashMap.Lazy.fromListWithKey:>=0.2.11.0 && <0.2.21.1Data.HashMap.Strict.fromList:>=0.2.0.1 && <0.2.21.1Data.HashMap.Strict.fromListWith:>=0.2.0.1 && <0.2.21.1Data.HashMap.Strict.fromListWithKey:>=0.2.11.0 && <0.2.21.1Data.HashSet.fromList:>=0.2.21 && <0.2.21.1instance IsList (HashMap k v):>=0.2.5.0 && <0.2.21.1instance IsList (HashSet a):>=0.2.21 && <0.2.21.1- Affected APIs
- < none >