HSEC-2026-0018: fromList functions in unordered-containers leak elements between calls

Published Nov 30, 2026
·
Updated

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

haskell 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

haskell 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

haskell 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 -O0 and 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 a and b above. This includes a list literal like [a, b, x]. A single shared element, as in fromList (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.fromList and fromListWith, and Data.HashMap.Strict.fromList and fromListWith, since 0.2.0.1 Data.HashMap.Lazy.fromListWithKey and Data.HashMap.Strict.fromListWithKey, since their introduction in 0.2.11.0 Data.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.

Affected Software

1 affected componentFixes available
hackage/unordered-containers>=0.2.0.1<0.2.21.1
0.2.21.1

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade hackage/unordered-containers to a version that resolves this vulnerability.

    Fixed in 0.2.21.1
  2. Upgrade

    Upgrade unordered-containers to a version that resolves this vulnerability.

    Fixed in 0.2.21.1

Event History

Nov 30, 2026
Advisory Published
via OSV·11:59 AM
Data Sourced
via OSV·11:59 AM
DescriptionSeverityAffected Software

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203