{"id":"HSEC-2026-0018","summary":"fromList functions in unordered-containers leak elements between calls","details":"# `fromList` functions in unordered-containers leak elements between calls\n\nA `HashMap` or `HashSet` built with `fromList` or `fromListWith[Key]` could\ncontain elements that were passed to other calls of the same function. For\nexample, with\n\n```haskell\nf :: [Key] -\u003e HashSet Key\nf xs = HashSet.fromList ([a, b] ++ xs)\n```\n\n`f xs` could return a set that also contains elements of `ys` from an earlier\ncall `f ys`. With `fromListWith[Key]`, values passed to other calls could also\nend up combined into the result: with\n\n```haskell\ng :: [(Int, String)] -\u003e HashMap Int String\ng kvs = HashMap.fromListWith (++) ([(1, \"a\"), (2, \"b\")] ++ kvs)\n```\n\nthree calls of `g [(33, \"x\")]` returned `\"x\"`, `\"xx\"` and `\"xxx\"` for key 33.\n\nA call could also replace the value of a shared key for all later calls: with\n\n```haskell\nh :: [(String, String)] -\u003e HashMap String String\nh kvs =\n  HashMap.fromListWith const ([(\"role\", \"user\"), (\"theme\", \"light\")] ++ kvs)\n```\n\n`h []` returned `\"admin\"` for `\"role\"` after a single call\n`h [(\"role\", \"admin\")]`.\n\n## Impact\n\nThe results are wrong without any error or warning. A leaked element or a\nreplaced value is returned by every later call for as long as the shared map\nis alive, which is often until the process ends. In a server where the\nvariable part of the list comes from a request, data from one request can\ntherefore show up in the results for other requests. A client can plant\nelements in the maps or sets built for other clients, and replace the values\nof the shared keys, for example defaults that each request is meant to\noverride for itself only. What follows from that depends on what the\napplication uses the map or set for.\n\nLeaked elements also accumulate. The shared map grows with every leak, and\nwith `fromListWith[Key]` so can the values in it, as in the example `g`. A\nclient that keeps sending new keys can make the process use more and more\nmemory.\n\nWe do not know of an application in which any of this can be exploited.\n\n## Conditions\n\n* The code is compiled with optimizations. In our tests the results were\n  correct with `-O0` and with `-fno-full-laziness`.\n* The list starts with at least two elements that do not depend on the\n  variable part and that have different hashes, as `a` and `b` above. This\n  includes a list literal like `[a, b, x]`. A single shared element, as in\n  `fromList (a : xs)`, is not enough.\n* An element leaks only if its hash places it in a part of the map that the\n  shared elements already occupy. With the two shared elements above, roughly\n  1 in 16 keys does. An element with the same key as a shared element always\n  does.\n\nWhether a given call site goes wrong also depends on GHC's inlining and\noptimization decisions, so that similar programs can behave differently. We\nhave observed wrong results with GHC 8.4.4, 8.10.7, 9.10.3 and 9.14.1, and\nnone with GHC 7.10.3.\n\n## Affected functions\n\n* `Data.HashMap.Lazy.fromList` and `fromListWith`, and\n  `Data.HashMap.Strict.fromList` and `fromListWith`, since 0.2.0.1\n* `Data.HashMap.Lazy.fromListWithKey` and\n  `Data.HashMap.Strict.fromListWithKey`, since their introduction in 0.2.11.0\n* `Data.HashSet.fromList`, in 0.2.21 only\n\nThe `IsList` instances are defined with these functions, so\n`GHC.Exts.fromList` is affected in the same way: at `HashMap` since the\ninstance was added in 0.2.5.0, at `HashSet` in 0.2.21 only.\n\n`Data.HashMap.Internal`, `Data.HashMap.Internal.Strict` and\n`Data.HashSet.Internal`, which are exposed since 0.2.12.0, export the same\nfunctions.\n\n## Cause\n\nThese functions inserted the list elements into the map under construction\nin-place, in a pure fold. GHC could therefore build the map for the shared\nelements `[a, b]` once and share it between all calls of `f`. Each call then\ninserted its `xs` into that shared map. Depending on the hashes of the\nelements, some of these insertions modified the shared map, so the inserted\nelements also showed up in the results of other calls.\n\n## Fix\n\nSince version 0.2.21.1 each of these functions builds its map entirely in\n`ST`, so the in-place inserts of one call can only affect that call's own\nmap.\n\nDowngrading from 0.2.21 to 0.2.20.1 only helps for `Data.HashSet.fromList`.\n\n## Credits\n\nReported by naso-sbl. Fixed by David Feuer and Simon Jakobi.\n","modified":"2026-11-30T12:15:02.503733942Z","published":"2026-11-30T11:59:09Z","database_specific":{"repository":"https://github.com/haskell/security-advisories","home":"https://github.com/haskell/security-advisories","osvs":"https://raw.githubusercontent.com/haskell/security-advisories/refs/heads/generated/osv-export"},"references":[{"type":"REPORT","url":"https://github.com/haskell-unordered-containers/unordered-containers/issues/601"},{"type":"FIX","url":"https://github.com/haskell-unordered-containers/unordered-containers/pull/602"},{"type":"INTRODUCED","url":"https://github.com/haskell-unordered-containers/unordered-containers/commit/7e08bbe7916b1cbafbea9470c069823c91f7a2dd"},{"type":"INTRODUCED","url":"https://github.com/haskell-unordered-containers/unordered-containers/commit/e67e3c7b3e849b63c8dd577cc058937b35b5270c"},{"type":"INTRODUCED","url":"https://github.com/haskell-unordered-containers/unordered-containers/commit/0d0697e597f6b53eeb2f82aa1321f5f63d784682"}],"affected":[{"package":{"name":"unordered-containers","ecosystem":"Hackage","purl":"pkg:hackage/unordered-containers"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0.2.0.1"},{"fixed":"0.2.21.1"}]}],"versions":["0.2.0.1","0.2.1.0","0.2.10.0","0.2.11.0","0.2.12.0","0.2.13.0","0.2.14.0","0.2.15.0","0.2.16.0","0.2.17.0","0.2.18.0","0.2.19.0","0.2.19.1","0.2.2.0","0.2.2.1","0.2.20","0.2.20.1","0.2.21","0.2.3.0","0.2.3.1","0.2.3.2","0.2.3.3","0.2.4.0","0.2.5.0","0.2.5.1","0.2.6.0","0.2.7.0","0.2.7.1","0.2.7.2","0.2.8.0","0.2.9.0"],"database_specific":{"osv":"https://raw.githubusercontent.com/haskell/security-advisories/refs/heads/generated/osv-export/2026/HSEC-2026-0018.json","source":"https://github.com/haskell/security-advisories/blob/generated/osv-export/2026/HSEC-2026-0018.json","human_link":"https://github.com/haskell/security-advisories/tree/main/advisories/published/2026/HSEC-2026-0018.md"},"severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:L"}]}],"schema_version":"1.9.2"}