Repository navigation
Description
Bug description:
IPv6Address, IPv6Network and IPv6Interface hash plain tuples of ints, so their hashes are deterministic and easy to collide. A list of IPv6 addresses chosen by an attacker makes set(), dict, and ipaddress.collapse_addresses() quadratic.
(I first reported this privately to the PSRT. They asked me to open a public issue.)
Root cause (Lib/ipaddress.py on main)
_BaseAddress.__hash__already useshash(hex(int(self._ip))), which goes through the randomized str hash.IPv6Address.__hash__overrides it withhash((self._ip, self._scope_id))._BaseNetwork.__hash__useshash((int(self.network_address), int(self.netmask))).IPv6Interface.__hash__useshash((self._ip, self._prefixlen, int(self.network.network_address))).
hash(int) is n mod (2**61 - 1). For 128-bit IPv6 values, a, a + M, a + 2M, … (with M = 2**61 - 1) therefore all have the same hash. Every probe in the collision chain also calls the pure-Python __eq__, which makes each collision expensive.
IPv4 is not affected in practice, because values below 2**32 hash to themselves.
Reproducer
import ipaddress, time
M = 2**61 - 1
base = int(ipaddress.IPv6Address('2001:db8::'))
for n in (2000, 4000, 8000):
col = [str(ipaddress.IPv6Address(base + 2*j*M)) for j in range(n)] # same hash
ctl = [str(ipaddress.IPv6Address(base + 2*j*M + j)) for j in range(n)] # distinct hashes
t = time.perf_counter(); set(map(ipaddress.ip_address, col)); a = time.perf_counter() - t
t = time.perf_counter(); set(map(ipaddress.ip_address, ctl)); b = time.perf_counter() - t
print(n, f"colliding {a:.2f}s", f"control {b:.3f}s")Measured on 3.14.3 (Apple arm64):
| Operation | n | Colliding | Control |
|---|---|---|---|
set(IPv6Address) |
2k / 4k / 8k / 16k | 0.24 / 0.97 / 3.75 / 14.8 s | 0.007 / 0.013 / 0.025 / 0.028 s |
set(IPv6Network) |
16k | 22.9 s | 0.044 s |
set(IPv6Interface) |
16k | 20.9 s | 0.071 s |
ipaddress.collapse_addresses() |
16k | 53.4 s | 0.12 s |
16k addresses are about 0.6 MB of text, and they all lie inside a single /52.
Where this matters
Code that de-duplicates, counts, groups or collapses IP lists supplied by users: allow-list / ACL management APIs, IPAM tools, log, X-Forwarded-For and threat-feed ingestion, and collapse_addresses() on submitted ranges.
Related
gh-134062 replaced the XOR-based network hash with the current tuple hash. In that discussion, a randomized str/bytes-based hash was left as an option "if anyone demonstrates it still being a practical problem". The table above is meant as that demonstration.
Suggested fix
Hash a randomized representation, as _BaseAddress already does:
class _BaseNetwork:
def __hash__(self):
- return hash((int(self.network_address), int(self.netmask)))
+ return hash((hex(int(self.network_address)), int(self.netmask)))
class IPv6Address:
def __hash__(self):
- return hash((self._ip, self._scope_id))
+ return hash((hex(self._ip), self._scope_id))
class IPv6Interface:
def __hash__(self):
- return hash((self._ip, self._prefixlen, int(self.network.network_address)))
+ return hash((hex(self._ip), self._prefixlen, hex(int(self.network.network_address))))Hashing self.packed (bytes) would work as well. I'm happy to open a PR if this direction is acceptable.
CPython versions tested on:
3.14, CPython main branch
Operating systems tested on:
macOS
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity