Skip to content

Use-after-free of the screen encoding in the _curses module #151695

@serhiy-storchaka

Description

@serhiy-storchaka

Bug report

The module-global curses_screen_encoding in Modules/_cursesmodule.c stores a borrowed pointer to the encoding string owned by the window object that initscr() returns:

curses_screen_encoding = ((PyCursesWindowObject *)winobj)->encoding;

It is set only on the first initscr() call and is never updated afterwards. That first window object can be deallocated while the pointer is still needed: the module-level functions unctrl() and ungetch() have no window of their own and fall back to curses_screen_encoding to encode non-ASCII characters. Once the originating window is gone, those functions read freed memory.

It can be reached from Python by dropping the initial screen and then encoding a non-ASCII character, e.g.:

import curses, gc
w = curses.initscr()
try:
    del w
    gc.collect()
    curses.initscr()
    curses.ungetch('é')   # reads the freed encoding string
finally:
    curses.endwin()

The fix is to keep a private copy of the encoding instead of borrowing the window's, refreshed on every initscr() and released when the module is torn down.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesextension-modulesC modules in the Modules dirtype-crashA hard crash of the interpreter, possibly with a core dump
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions