Skip to content
Memory corruption Disclosed report Intermediate

stack buffer overflows in the curses module

The curses module's getstr() and instr() functions accept a negative length argument, which is passed to the underlying C library without validation, causing a stack buffer overflow when the user provides more input than the fixed-size buffer can hold. An attacker who can control the argument could crash the interpreter or potentially execute code if they also know the stack canary.

tehybel Follow

Originally disclosed on HackerOne

  • Fixed in 16 days
  • Paid in 16 days
  • Disclosed after 3.2 years

I found two stack buffer overflows in the curses module. These vulnerabilities have been reported to the PSRT and were fixed here:

https://hg.python.org/cpython/rev/d5f6bc45b376 https://hg.python.org/cpython/rev/85b35300f200

Below are copies of the mails I sent to the PSRT. They describe the vulnerabilities in detail.

First mail:

I found a straightforward stack buffer overflow in the Modules/_cursesmodule.c file. The module implements the python function window.getstr() via the C function PyCursesWindow_GetStr. This function calls on to the curses library's wgetnstr function.

If we specify a single integer argument to getstr, we hit these lines:

static PyObject *
PyCursesWindow_GetStr(PyCursesWindowObject *self, PyObject *args)
{
    ...
    char rtn[1024]; /* This should be big enough.. I hope */
    ...
    switch (PyTuple_Size(args)) {
    ...
    case 1:
        if (!PyArg_ParseTuple(args,"i;n", &n))
            return NULL;
        Py_BEGIN_ALLOW_THREADS
        rtn2 = wgetnstr(self->win, rtn, Py_MIN(n, 1023));
        ...
    }
    ...
}

As we can see, the wgetnstr function is called. It will read input into the stack buffer "rtn". We use wgetnstr rather than wgetstr to prevent a buffer overflow. As maximum length we use Py_MIN(n, 1023). However if we set n to be a negative integer, wgetnstr will be passed a negative length.

What does the curses module do, then? On my (Linux, ncurses) system it simply does no length checking. So we can read as many bytes as we want into the fixed-size stack buffer rtn.

Here's a proof-of-concept script to reproduce this:

--- begin script ---

import curses

curses.initscr() w = curses.newwin(80, 80) w.getstr(-1)

--- end script ---

Here's the result of running it and entering over 1024 'A's:

$ python3 -c 'print("A"*1100)' | python3 ./poc4.py *** stack smashing detected ***: python3 terminated zsh: segmentation fault (core dumped) python3 ./poc4.py

This is clearly exploitable when combined with an info leak that lets us know the stack canary.

This vulnerability should probably be fixed by making "n" unsigned, or by confirming that it is non-negative. (Note that this also needs to be fixed for other cases in the switch, like when we input three arguments rather than one.)

Some more details about my setup:

curses version: libncurses (/usr/lib/libncursesw.so.6.0) (ncurses 6.0.20150808)
$ python3 -V
Python 3.5.2

Second mail, after the first vulnerability was fixed:

I just realized that there is a similar vulnerability in the PyCursesWindow_InStr function. There we also need to confirm that n is nonnegative. Otherwise a stack buffer overflow is possible. There's a reproducing script below. --- begin script ---

import curses

curses.initscr() w = curses.newwin(2048, 2048) s = w.instr(-1)

--- end script ---

The original report as published on HackerOne, by tehybel. 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 does not validate that the length argument is non-negative before passing it to the C library function, which then performs no bounds checking.

Impact

A stack buffer overflow could crash the Python interpreter and, if the stack canary is known, may be exploitable for code execution.

What the attacker needed

Ability to call getstr() or instr() with a negative argument; user input exceeding buffer size

The fix

The reporter suggested validating that the length argument is non-negative, and linked revisions said to fix it.

How it was found

  1. Import the curses module and initialise the screen.
  2. Create a new window with curses.newwin().
  3. Call getstr() with a negative integer argument.
  4. Provide more than 1024 bytes of input to overflow the stack buffer.

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.

The curses module lets Python programs draw text-based interfaces. Its getstr() and instr() functions read user input into a fixed-size memory buffer on the stack. If you give them a negative number for the maximum length, the safety check is bypassed, and typing too much data overwrites other memory, crashing the program or worse.

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 tehybel

View all

5 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.