Buffer Overflow in Python 3.x through 3.9.1
Summary
CVE-2021-3177: Python 3.x through 3.9.1 has a buffer overflow in PyCArg_repr in _ctypes/callproc.c, which may lead to remote code execution in certain Python applications that accept floating-point numbers as untrusted input, as demons... Affects python3 across 5 version ranges.
>= 1bf9cc509326bc42cd8cb1650eb9bf64550d817e, <= 13c94747c74437e594b7fc242ff7da668e81887c
< 3.7.7-r2
>= 5c4568a05a0a62b5947c55f68f9f2ecfb90a4f12, <= c0a9afe2ac1820409e6173bd1893ebee2cf50270
>= 9cf6752276e6fcfd0c23fdb064ad27f448aaaf75, <= 1e5d33e9b9b8631b36f061103a30208b206fd03a
>= fa919fdf2583bdfead1df00e842f24f30b2a34bf, <= 6503f05dd59e26a9986bdea097b3da9b3546f45b
Fixed in 3.7.7-r2. How to fix ↓
Check if this vulnerability affects your application
Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.
No signup required · Results in about 60 seconds
Background
Python is a popular programming language known for its simplicity and versatility. It is widely used for various applications, including web development, data analysis, and artificial intelligence. Python 3.x is the latest major version of Python, with version 3.9.1 being the most recent release. However, this version is vulnerable to a buffer overflow in the PyCArg_repr function in the _ctypes/callproc.c file. This vulnerability can be exploited by providing a large floating-point number as input to certain Python applications, potentially leading to remote code execution. The vulnerability arises due to the unsafe usage of the sprintf function.
Vulnerability Detail
The buffer overflow vulnerability, identified as CVE-2021-3177, affects Python 3.x through 3.9.1. By supplying a floating-point number, such as 1e300, as untrusted input to Python applications that accept such values, an attacker can trigger the buffer overflow. This can result in the execution of arbitrary code within the context of the affected application. The vulnerability has a CVSS score of 9.8, indicating its high severity. It is crucial to address this vulnerability promptly to prevent potential remote code execution and unauthorized access to systems.
How To Fix
Minimum fixed version
3.7.7-r2
This is the first release that contains the fix.
sudo apk add --upgrade python3
Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →
Workaround
As a temporary workaround, developers can validate and sanitize user input to prevent the exploitation of this vulnerability. By implementing input validation and limiting the range of acceptable floating-point numbers, the risk of buffer overflow can be mitigated. Here is an example of how input validation can be performed in Python:
def process_input(input_value):
try:
float_value = float(input_value)
if float_value > 1e100:
raise ValueError('Input value exceeds the allowed range.')
# Process the input
except ValueError as e:
print('Invalid input:', str(e))
Conclusion
The buffer overflow vulnerability in Python 3.x through 3.9.1 poses a significant risk to applications that accept floating-point numbers as untrusted input. It is crucial to update Python installations to the latest available version within the 3.x series, starting from version 3.7.7-r2, to address this vulnerability. Additionally, implementing input validation and range checks can provide temporary mitigation until the update is applied. For further assistance and support in vulnerability remediation, please contact DevNack.com.
See every vulnerability recorded for python3 →
FAQ
The vulnerability allows an attacker to execute arbitrary code remotely, potentially leading to unauthorized access, data breaches, and system compromise.
Python 3.x through 3.9.1 are affected by this vulnerability.
Yes, the vulnerability can be fixed by updating Python to version 3.7.7-r2 or later within the 3.x series.
Yes, implementing input validation and limiting the range of acceptable floating-point numbers can help mitigate the risk until the Python installation is updated.
The CVSS score of this vulnerability is 9.8, indicating its high severity.
Related vulnerabilities
Recent vulnerabilities recorded for python3.