You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: bound DNSCache record count to prevent unbounded LAN-driven growth
Closes#1715.
A LAN-local peer multicasting unique-name mDNS responses could grow
``DNSCache.cache`` / ``_expirations`` / ``service_cache`` /
``_expire_heap`` without bound. ``_DNS_PTR_MIN_TTL = 1125`` (RFC 6762
recommendation for ServiceBrowser refresh-floor) actually extends
attacker-injected PTR records to ~19 min before they expire naturally,
and ``async_expire`` only runs every ``_CACHE_CLEANUP_INTERVAL = 10`` s
— too slow to keep up with a sustained flood. On memory-constrained
deployments (Home Assistant on a Raspberry Pi is the canonical victim)
this trivially OOMs the process.
Add a hard cap (``_MAX_CACHE_RECORDS = 10_000`` in ``const.py``) on the
total number of records the cache will hold. ``DNSCache`` now tracks
``_total_records`` (incremented on genuinely-new inserts in
``_async_add``, decremented in ``_async_remove``). When the cap is
hit, ``_async_evict_oldest`` heappops the closest-to-expiration record
and removes it before inserting the new one. Reuses the existing
``_expire_heap`` for victim selection — O(log n) — and skips stale
heap entries via the same expiration-equality check
``async_expire`` already uses.
All four touched lines compile to direct C int ops in the Cython
build (score-0 in ``cython -a`` annotation); the eviction call is a
C-level vtable dispatch fired only on overflow. Wall-clock check:
below-cap add stays at ~160 ns/record; over-cap add with constant
eviction is ~195 ns/record (+36 ns for the heappop + cache delete).
Per-source-IP record quotas — the reporter's "Better" suggestion —
are deferred to a follow-up PR. This commit closes the unbounded-
growth bug class on its own; the quota work changes the fairness
properties but not the memory bound.
CWE-400 (Uncontrolled Resource Consumption). LAN-local attack
surface only.
0 commit comments