{"id":"CVE-2025-48945","aliases":["CVE-2025-48945","GHSA-5qpg-rh4j-qp35"],"related":[],"kind":"vulnerability","state":"published","title":"pycares has a Use-After-Free Vulnerability","summary":"pycares is a Python module which provides an interface to c-ares. c-ares is a C library that performs DNS requests and name resolutions asynchronously. Prior to version 4.9.0, 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 has been fixed in pycares 4.9.0 by implementing a safe channel destruction mechanism.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"pycares is a Python module which provides an interface to c-ares. c-ares is a C library that performs DNS requests and name resolutions asynchronously. Prior to version 4.9.0, 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 has been fixed in pycares 4.9.0 by implementing a safe channel destruction mechanism.","source":"cvelist"},{"lang":"en","text":"## Summary\n\npycares 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.\n\n## Details\n\n### Root Cause\n\nThe vulnerability stems from improper handling of callback references when the Channel object is destroyed:\n\n1. When a DNS query is initiated, pycares stores a callback reference using `ffi.new_handle()`\n2. If the Channel object is garbage collected while queries are pending, the callback references become invalid\n3. When c-ares attempts to invoke the callback, it accesses freed memory, causing a fatal error\n\nThis issue was much more likely to occur when using `event_thread=True` but could happen without it under the right circumstances.\n\n### Technical Details\n\nThe core issue is a race condition between Python's garbage collector and c-ares's callback execution:\n\n1. When `__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 returns\n2. c-ares needs to execute cleanup code after our Python callback returns (specifically at lines 1422-1429 in ares_process.c)\n3. If we destroy the channel too quickly, c-ares accesses freed memory\n\n### Impact\n\nApplications 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:\n\n- Channel objects are created per-request\n- Multiple failed DNS queries are processed rapidly\n- The application doesn't properly manage Channel lifecycle\n\nThe error manifests as:\n```\nFatal Python error: b_from_handle: ffi.from_handle() detected that the address passed points to garbage\n```\n\n## Fix\n\nThe vulnerability has been fixed in pycares 4.9.0 by implementing a safe channel destruction mechanism\n\n## Mitigation\n\n### For Application Developers\n\n1. **Upgrade to pycares >= 4.9.0** - This version includes the fix and requires no code changes\n2. **Best practices** (optional but recommended):\n   ```python\n   # Explicit cleanup\n   channel.close()\n   \n   # Or use context manager\n   with pycares.Channel() as channel:\n       # ... use channel ...\n   # Automatically closed\n   ```\n3. **Avoid creating Channel objects per-request** - Prefer long-lived instances for better performance and safety\n\nThe fix is completely transparent - no API changes or code modifications are required.\n\n## Credit\n\nThis vulnerability was reported by @vEpiphyte through the aio-libs security program.","format":"markdown","source":"ghsa"},{"lang":"es","text":"Pycares es un módulo de Python que proporciona una interfaz para c-ares. C-ares es una librería de C que realiza solicitudes DNS y resoluciones de nombres de forma asíncrona. En versiones anteriores a la 4.9.0, Pycares era vulnerable a una condición de use-after-free que se produce cuando un objeto de canal se recolecta como basura mientras las consultas DNS aún están pendientes. Esto provoca un error fatal de Python y un fallo del intérprete. Esta vulnerabilidad se ha corregido en Pycares 4.9.0 mediante la implementación de un mecanismo seguro de destrucción de canales.","source":"nvd"}],"published":"2025-06-16T16:09:47.000Z","modified":"2026-09-10T03:50:24.913Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N","score":8.2,"rating":"HIGH"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N","score":8.2,"rating":"HIGH"},{"scorer":"GitHub","scheme":"rating","rating":"MEDIUM"}],"cwes":["CWE-416"],"affected":[{"ranges":[{"version":"< 4.9.0","status":"affected"}],"vendor":"aio-libs","product":"aiodns","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"4.9.0"}],"ecosystem":"PyPI","package":"pycares","purl":"pkg:pypi/pycares","source":"ghsa"}],"references":[{"url":"https://github.com/saghul/pycares/security/advisories/GHSA-5qpg-rh4j-qp35","tags":["web","x-refsource-confirm","x-refsource-misc"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://github.com/aio-libs/aiodns/commit/f259a6e3650555157af53ac2b39f2ff545321d55","tags":["x-refsource-misc"],"sources":["cvelist","nvd"]},{"url":"https://github.com/saghul/pycares/commit/ebfd7d71eb8e74bc1057a361ea79a5906db510d4","tags":["web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://github.com/aio-libs/aiodns/releases/tag/v3.5.0","tags":["x-refsource-misc"],"sources":["cvelist","nvd"]},{"url":"https://github.com/saghul/pycares/releases/tag/v4.9.0","tags":["x-refsource-misc"],"sources":["cvelist","nvd"]},{"url":"https://github.com/saghul/pycares","tags":["package"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"none","automatable":"no","technicalImpact":"partial","date":"2025-06-20T20:05:50.595Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2025-48945","url":"https://www.cve.org/CVERecord?id=CVE-2025-48945","licence":"CVE Terms of Use","modified":"2025-06-20T20:06:23.612Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-5qpg-rh4j-qp35","url":"https://github.com/advisories/GHSA-5qpg-rh4j-qp35","licence":"CC-BY-4.0","modified":"2026-09-10T03:50:24.913Z"},{"source":"nvd","name":"NVD","id":"CVE-2025-48945","url":"https://nvd.nist.gov/vuln/detail/CVE-2025-48945","licence":"Public domain","modified":"2026-06-17T09:30:31.813Z"}],"updatedAt":"2026-11-25T22:58:59.477Z","epss":{"score":0.00505,"percentile":0.44751,"date":"2026-12-01T12:00:22.000Z"}}