GHSA-5qpg-rh4j-qp35: Pycares Pycares Has A Use-After-Free Vulnerability Vulnerability

MODERATE
CWE-416
Published
Advisory Summary

GHSA-5qpg-rh4j-qp35 is a MODERATE pycares has a use-after-free vulnerability vulnerability (CWE-416: Use After Free) affecting Pycares libraries in the PyPI ecosystem. The issue involves pycares has a use-after-free vulnerability. Remediation is available by upgrading affected packages to version 4.9.0.

GHSA-5qpg-rh4j-qp35 at a Glance

CVE Identifier N/A
GHSA Identifier GHSA-5qpg-rh4j-qp35
Severity MODERATE
CVSS v3.1 Base Score N/A
CWE Classification CWE-416: Use After Free
Affected Product Pycares
Package Ecosystem PyPI
Published Date
Last Updated

How to Fix GHSA-5qpg-rh4j-qp35

To remediate this vulnerability, upgrade all affected Pycares dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

pycares fixed: 4.9.0
pip install pycares>=4.9.0
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show pycares

How to Check if You Are Affected

To determine whether your Python environment is affected by GHSA-5qpg-rh4j-qp35, inspect your installed packages using pip:

pip show pycares

Analyze the terminal output to locate any version of Pycares in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 4.9.0), your application is affected.

Note: The pip show pycares command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-416: Use After Free, where the product reuses or references memory after it has been freed. at some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer. In Pycares, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

## Summary 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. ## Details ### Root Cause The vulnerability stems from improper handling of callback references when the Channel object is destroyed: 1. When a DNS query is initiated, pycares stores a callback reference using `ffi.new_handle()` 2. If the Channel object is garbage collected while queries are pending, the callback references become invalid 3. When c-ares attempts to invoke the callback, it accesses freed memory, causing a fatal error This issue was much more likely to occur when using `event_thread=True` but could happen without it under the right circumstances. ### Technical Details The core issue is a race condition between Python's garbage collector and c-ares's callback execution: 1. 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 2. c-ares needs to execute cleanup code after our Python callback returns (specifically at lines 1422-1429 in ares_process.c) 3. If we destroy the channel too quickly, c-ares accesses freed memory ### Impact Applications 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: - Channel objects are created per-request - Multiple failed DNS queries are processed rapidly - The application doesn't properly manage Channel lifecycle The error manifests as: ``` Fatal Python error: b_from_handle: ffi.from_handle() detected that the address passed points to garbage ``` ## Fix The vulnerability has been fixed in pycares 4.9.0 by implementing a safe channel destruction mechanism ## Mitigation ### For Application Developers 1. **Upgrade to pycares >= 4.9.0** - This version includes the fix and requires no code changes 2. **Best practices** (optional but recommended): ```python # Explicit cleanup channel.close() # Or use context manager with pycares.Channel() as channel: # ... use channel ... # Automatically closed ``` 3. **Avoid creating Channel objects per-request** - Prefer long-lived instances for better performance and safety The fix is completely transparent - no API changes or code modifications are required. ## Credit This vulnerability was reported by @vEpiphyte through the aio-libs security program.

GHSA-5qpg-rh4j-qp35 Affected Pycares Versions

1 component(s)
Fixed Versions:
4.9.0
Product / Package Ecosystem Affected Versions Fixed Version
pycares
PyPI
< 4.9.0
4.9.0

References

Frequently Asked Questions

What is GHSA-5qpg-rh4j-qp35?
GHSA-5qpg-rh4j-qp35 is a security vulnerability in Pycares involving pycares has a Use-After-Free Vulnerability under CWE-416: Use After Free.
Which Pycares versions are affected?
Affected versions include releases prior to 4.9.0 across affected packages: pycares.
What version fixes GHSA-5qpg-rh4j-qp35?
Upgrading to 4.9.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is pycares affected?
Yes, pycares is affected in releases prior to 4.9.0. Upgrade your dependency to 4.9.0 or later.
What CWE is associated with GHSA-5qpg-rh4j-qp35?
GHSA-5qpg-rh4j-qp35 is classified under CWE-416: Use After Free (The product reuses or references memory after it has been freed. At some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.).
How can I check PyPI dependencies for GHSA-5qpg-rh4j-qp35?
Run the dependency tree command: 'pip show pycares'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-5qpg-rh4j-qp35",
  "modified" : "2025-06-16T16:09:47Z",
  "published" : "2025-06-16T16:09:47Z",
  "aliases" : [ ],
  "summary" : "pycares has a Use-After-Free Vulnerability",
  "details" : "## 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.",
  "severity" : [ ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "pycares"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "4.9.0"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 4.8.0"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/saghul/pycares/security/advisories/GHSA-5qpg-rh4j-qp35"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/saghul/pycares/commit/ebfd7d71eb8e74bc1057a361ea79a5906db510d4"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/saghul/pycares"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-416" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2025-06-16T16:09:47Z",
    "nvd_published_at" : null
  }
}