Skip to content

Use after free in getpath_isxfile() on Windows #158288

Description

@serhiy-storchaka

getpath_isxfile() in Modules/getpath.c frees the path buffer before the last use:

    DWORD attr = GetFileAttributesW(path);
    PyMem_Free(path);
    isxfile = (attr != INVALID_FILE_ATTRIBUTES) &&
              !(attr & FILE_ATTRIBUTE_DIRECTORY) &&
              (cchPath >= 4) &&
              (CompareStringOrdinal(path + cchPath - 4, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL);

CompareStringOrdinal() reads path + cchPath - 4 after PyMem_Free(path), so the result of the .exe check depends on freed memory. The function is called during interpreter startup, when looking for the executable.

Only the main branch is affected: the early free was introduced in GH-153230, before which the buffer was freed at the end of the block.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesOS-windowsextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions