Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential use-after-free in Py_SetPythonHome and its siblings #98978

Closed
filmor opened this issue Nov 1, 2022 · 3 comments
Closed

Potential use-after-free in Py_SetPythonHome and its siblings #98978

filmor opened this issue Nov 1, 2022 · 3 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@filmor
Copy link

filmor commented Nov 1, 2022

Bug report

cpython/Python/pathconfig.c

Lines 255 to 273 in c085974

void
Py_SetPythonHome(const wchar_t *home)
{
int has_value = home && home[0];
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
PyMem_RawFree(_Py_path_config.home);
if (has_value) {
_Py_path_config.home = _PyMem_RawWcsdup(home);
}
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
if (has_value && _Py_path_config.home == NULL) {
path_out_of_memory(__func__);
}
}

If Py_SetPythonHome was called with a non-empty string and is subsequently called with an empty one, it will always run PyMem_RawFree but only actually reset the pointer in .home if has_value is set, so if home && home[0] (i.e. non-empty string).

Minimal example:

Py_SetPythonHome(L"/non-empty");
Py_SetPythonHome(L"");
// After this, the memory region in .home is freed but the pointer is not overwritten

Your environment

The issue occurs in our (Python.NET) test-suite: pythonnet/pythonnet#1955

  • CPython versions tested on: 3.7 - 3.11, only occurs in 3.11
  • Operating system and architecture: Windows (x86, amd64), macOS (amd64), Linux (amd64)

Additional notes

This bug was introduced in #29041, in the initial commit: a63f5d8

It's a regression, the first faulty version was 3.11.0a3.

@vstinner @zooba

@filmor filmor added the type-bug An unexpected behavior, bug, or error label Nov 1, 2022
@zooba zooba self-assigned this Nov 2, 2022
@zooba zooba added 3.11 only security fixes 3.12 bugs and security fixes labels Nov 2, 2022
zooba added a commit to zooba/cpython that referenced this issue Nov 2, 2022
@zooba
Copy link
Member

zooba commented Nov 2, 2022

This doesn't affect 3.10 because you weren't able to clear the values - the functions would exit early on null/empty strings.

@vstinner
Copy link
Member

vstinner commented Nov 3, 2022

I proposed PR #99066 to fix this issue.

vstinner added a commit that referenced this issue Nov 3, 2022
Fix use-after-free in Py_SetPythonHome(NULL), Py_SetProgramName(NULL)
and _Py_SetProgramFullPath(NULL) function calls.

Issue reported by Benedikt Reinartz.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 3, 2022
Fix use-after-free in Py_SetPythonHome(NULL), Py_SetProgramName(NULL)
and _Py_SetProgramFullPath(NULL) function calls.

Issue reported by Benedikt Reinartz.
(cherry picked from commit b07f546)

Co-authored-by: Victor Stinner <[email protected]>
@vstinner
Copy link
Member

vstinner commented Nov 3, 2022

Fixed by b07f546

@vstinner vstinner closed this as completed Nov 3, 2022
miss-islington added a commit that referenced this issue Nov 3, 2022
Fix use-after-free in Py_SetPythonHome(NULL), Py_SetProgramName(NULL)
and _Py_SetProgramFullPath(NULL) function calls.

Issue reported by Benedikt Reinartz.
(cherry picked from commit b07f546)

Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants