Skip to content
Memory corruption Disclosed report Advanced CVE-2021-3177

Buffer overflow in PyCArg_repr in _ctypes/callproc.c for Python 3.x to 3.9.1

The PyCArg_repr function in Python's ctypes module formats a floating-point value into a fixed-size buffer using sprintf without checking the length, so an extreme value such as 1e300 overflows the buffer. The text says this may lead to remote code execution in applications that accept floating-point numbers as untrusted input.

jordyzomer Follow

Originally disclosed on HackerOne

Bounty$1,500 High View original
  • Triaged in 6 hours
  • Fixed in 6 months
  • Paid in 6 months
  • Disclosed after 7 months

TL;DR Description

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 demonstrated by a 1e300 argument to c_double.from_param. This occurs because sprintf is used unsafely. The CVE number used for this vulnerability is CVE-2021-3177.

Details

There's a buffer overflow in the PyCArg_repr() function in _ctypes/callproc.c.

The buffer overflow happens due to not checking the length of th sprintf() function on line:

case 'd':
    sprintf(buffer, "<cparam '%c' (%f)>",
        self->tag, self->value.d);
    break;

Because we control self->value.d we could make it copy extreme values. For example we could make it copy 1e300 which would be a 1 with 300 zero's to overflow the buffer.

This could potentially cause RCE when a user allows untrusted input in these functions.

Proof of Concept

from ctypes import * c_double.from_param(1e300) *** buffer overflow detected ***: terminated Aborted

References

MISC:https://bugs.python.org/issue42938
MISC:https://github.com/python/cpython/pull/24239
MISC:https://python-security.readthedocs.io/vuln/ctypes-buffer-overflow-pycarg_repr.html

Impact

Availability

Buffer overflows generally lead to crashes. Other attacks leading to lack of availability are possible, including putting the program into an infinite loop.

Access Control

Buffer overflows often can be used to execute arbitrary code, which is usually outside the scope of a program’s implicit security policy.

Other

When the consequence is arbitrary code execution, this can often be used to subvert any other security service.

The original report as published on HackerOne, by jordyzomer. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The code uses sprintf to write a formatted floating-point value into a fixed-size buffer without checking the resulting length.

Impact

The overflow crashes the process and could potentially allow remote code execution when untrusted floating-point input reaches these functions.

What the attacker needed

Application passes untrusted floats to ctypes

How it was found

  1. Import the ctypes module in a Python interpreter.
  2. Call c_double.from_param with an extreme floating-point value such as 1e300.
  3. Observe that the buffer overflow is detected and the process aborts.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A buffer is a small reserved area of memory. When a program writes more text into it than it can hold, the extra data spills into neighbouring memory, which can crash the program or let an attacker control what it does next. Here Python copied a very long number into a fixed-size buffer without checking whether it would fit.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by jordyzomer

View all

2 more disclosed reports, best first.

More from Internet Bug Bounty

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.