T09 · Insecure Skill Coding Practices
Error
- Location
scripts/subnet_calc.py:31- Finding
Unbounded IPv4 Host Materialization Causes Memory Exhaustion
- Content
View full analysis
0 and network.prefixlen < 31: hosts = list(network.hosts()) result["first_host"] = str(hosts[0]) result["last_host"] = str(hosts[-1]) elif network.prefixlen == 31: # RFC 3021 point-to-point hosts = list(network) result["first_host"] = str(hosts[0]) result["last_host"] = str(hosts[1]) ``` ### Technical Analysis For every IPv4 network with a prefix shorter than `/31`, the program converts the complete host iterator into a Python list merely to obtain its first and last elements. The number of allocated objects grows according to the size of the supplied network. For example, processing `0.0.0.0/0` attempts to materialize approximately 4.29 billion IPv4 addresses. The input is syntactically valid and passes `ipaddress.ip_network()` validation, but the resulting allocation can exceed available memory. No maximum network size, address-count threshold, or lazy calculation mechanism is applied. The first and last usable addresses can be calculated directly without enumerating intermediate addresses. ### Attack Path 1. An attacker or untrusted caller supplies a large valid IPv4 network, such as: ```bash python3 scripts/subnet_calc.py 0.0.0.0/0 ``` 2. `ipaddress.ip_network()` accepts and normalizes the CIDR value. 3. `calc_ipv4()` invokes `network.hosts()`. 4. `list()` attempts to allocate an object for every usable address. 5. The process consumes excessive memory and CPU time. 6. The operating system may terminate the process, or the host may become degraded due to memory pressure and swapping. ### Impact Assessment This issue does not grant additional privileges or provide access to confidential data. Its primary impact is availability. An attacker who can control the CIDR argument can ...[truncated 265 chars]- Remediation
View remediation
