pycares is vulnerable to a use-after-free condition that occurs when a Channel object is garbage collected while DNS queries are still pending. This results in a fatal Python error and interpreter crash.
The vulnerability stems from improper handling of callback references when the Channel object is destroyed:
ffi.new_handle()
This issue was much more likely to occur when using event_thread=True
but could happen without it under the right circumstances.
The core issue is a race condition between Python's garbage collector and c-ares's callback execution:
__del__
is called from within a c-ares callback context, we cannot immediately call ares_destroy()
because c-ares is still executing code after the callback returnsApplications using pycares
can be crashed remotely by triggering DNS queries that result in Channel
objects being garbage collected before query completion. This is particularly problematic in scenarios where:
The error manifests as:
Fatal Python error: b_from_handle: ffi.from_handle() detected that the address passed points to garbage
The vulnerability has been fixed in pycares 4.9.0 by implementing a safe channel destruction mechanism
# Explicit cleanup
channel.close()
# Or use context manager
with pycares.Channel() as channel:
# ... use channel ...
# Automatically closed
The fix is completely transparent - no API changes or code modifications are required.
This vulnerability was reported by @vEpiphyte through the aio-libs security program.
{ "cwe_ids": [ "CWE-416" ], "nvd_published_at": null, "github_reviewed_at": "2025-06-16T16:09:47Z", "github_reviewed": true, "severity": "MODERATE" }