gh-98978: Fix Py_SetPythonHome(NULL) (GH-99066)

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 b07f546ea3)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2022-11-03 11:09:15 -07:00 committed by GitHub
parent eb023a84d9
commit 41a9f49bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -0,0 +1,3 @@
Fix use-after-free in ``Py_SetPythonHome(NULL)``,
``Py_SetProgramName(NULL)`` and ``_Py_SetProgramFullPath(NULL)`` function
calls. Issue reported by Benedikt Reinartz. Patch by Victor Stinner.

View File

@ -261,6 +261,8 @@ Py_SetPythonHome(const wchar_t *home)
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
PyMem_RawFree(_Py_path_config.home); PyMem_RawFree(_Py_path_config.home);
_Py_path_config.home = NULL;
if (has_value) { if (has_value) {
_Py_path_config.home = _PyMem_RawWcsdup(home); _Py_path_config.home = _PyMem_RawWcsdup(home);
} }
@ -282,6 +284,8 @@ Py_SetProgramName(const wchar_t *program_name)
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
PyMem_RawFree(_Py_path_config.program_name); PyMem_RawFree(_Py_path_config.program_name);
_Py_path_config.program_name = NULL;
if (has_value) { if (has_value) {
_Py_path_config.program_name = _PyMem_RawWcsdup(program_name); _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
} }
@ -302,6 +306,8 @@ _Py_SetProgramFullPath(const wchar_t *program_full_path)
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
PyMem_RawFree(_Py_path_config.program_full_path); PyMem_RawFree(_Py_path_config.program_full_path);
_Py_path_config.program_full_path = NULL;
if (has_value) { if (has_value) {
_Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path); _Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);
} }