-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-133886: Fix sys.remote_exec() for non-UTF-8 paths #133887
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
Merged
serhiy-storchaka
merged 7 commits into
python:main
from
serhiy-storchaka:sys-remote_exec-non-utf8
May 13, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a2ac2f8
gh-133886: Fix sys.remote_exec() for non-UTF-8 paths
serhiy-storchaka 1bd22b0
Merge branch 'main' into sys-remote_exec-non-utf8
serhiy-storchaka 70be8cf
Try to fix build on Windows and tests on macOS.
serhiy-storchaka daa818b
Fix auditing.
serhiy-storchaka 90776ac
Try to fix encoding on Windows.
serhiy-storchaka bd30460
Polishing.
serhiy-storchaka f8be85f
Use "goto error:.
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2025-05-11-13-40-42.gh-issue-133886.ryBAyo.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix :func:`sys.remote_exec` for non-ASCII paths in non-UTF-8 locales and | ||
| non-UTF-8 paths in UTF-8 locales. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1218,38 +1218,38 @@ static inline int run_remote_debugger_source(PyObject *source) | |
|
|
||
| // Note that this function is inline to avoid creating a PLT entry | ||
| // that would be an easy target for a ROP gadget. | ||
| static inline void run_remote_debugger_script(const char *path) | ||
| static inline void run_remote_debugger_script(PyObject *path) | ||
| { | ||
| if (0 != PySys_Audit("remote_debugger_script", "s", path)) { | ||
| if (0 != PySys_Audit("remote_debugger_script", "O", path)) { | ||
| PyErr_FormatUnraisable( | ||
| "Audit hook failed for remote debugger script %s", path); | ||
| "Audit hook failed for remote debugger script %U", path); | ||
| return; | ||
| } | ||
|
|
||
| // Open the debugger script with the open code hook, and reopen the | ||
| // resulting file object to get a C FILE* object. | ||
| PyObject* fileobj = PyFile_OpenCode(path); | ||
| PyObject* fileobj = PyFile_OpenCodeObject(path); | ||
| if (!fileobj) { | ||
| PyErr_FormatUnraisable("Can't open debugger script %s", path); | ||
| PyErr_FormatUnraisable("Can't open debugger script %U", path); | ||
| return; | ||
| } | ||
|
|
||
| PyObject* source = PyObject_CallMethodNoArgs(fileobj, &_Py_ID(read)); | ||
| if (!source) { | ||
| PyErr_FormatUnraisable("Error reading debugger script %s", path); | ||
| PyErr_FormatUnraisable("Error reading debugger script %U", path); | ||
| } | ||
|
|
||
| PyObject* res = PyObject_CallMethodNoArgs(fileobj, &_Py_ID(close)); | ||
| if (!res) { | ||
| PyErr_FormatUnraisable("Error closing debugger script %s", path); | ||
| PyErr_FormatUnraisable("Error closing debugger script %U", path); | ||
| } else { | ||
| Py_DECREF(res); | ||
| } | ||
| Py_DECREF(fileobj); | ||
|
|
||
| if (source) { | ||
| if (0 != run_remote_debugger_source(source)) { | ||
| PyErr_FormatUnraisable("Error executing debugger script %s", path); | ||
| PyErr_FormatUnraisable("Error executing debugger script %U", path); | ||
| } | ||
| Py_DECREF(source); | ||
| } | ||
|
|
@@ -1278,7 +1278,14 @@ int _PyRunRemoteDebugger(PyThreadState *tstate) | |
| pathsz); | ||
| path[pathsz - 1] = '\0'; | ||
| if (*path) { | ||
| run_remote_debugger_script(path); | ||
| PyObject *path_obj = PyUnicode_DecodeFSDefault(path); | ||
| if (path_obj == NULL) { | ||
| PyErr_FormatUnraisable("Can't decode debugger script"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we include the script path in this exception ? |
||
| } | ||
| else { | ||
| run_remote_debugger_script(path_obj); | ||
| Py_DECREF(path_obj); | ||
| } | ||
| } | ||
| PyMem_Free(path); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2451,38 +2451,74 @@ sys_is_remote_debug_enabled_impl(PyObject *module) | |
| #endif | ||
| } | ||
|
|
||
| /*[clinic input] | ||
| sys.remote_exec | ||
|
|
||
| pid: int | ||
| script: object | ||
|
|
||
| Executes a file containing Python code in a given remote Python process. | ||
|
|
||
| This function returns immediately, and the code will be executed by the | ||
| target process's main thread at the next available opportunity, similarly | ||
| to how signals are handled. There is no interface to determine when the | ||
| code has been executed. The caller is responsible for making sure that | ||
| the file still exists whenever the remote process tries to read it and that | ||
| it hasn't been overwritten. | ||
|
|
||
| The remote process must be running a CPython interpreter of the same major | ||
| and minor version as the local process. If either the local or remote | ||
| interpreter is pre-release (alpha, beta, or release candidate) then the | ||
| local and remote interpreters must be the same exact version. | ||
|
|
||
| Args: | ||
| pid (int): The process ID of the target Python process. | ||
| script (str|bytes): The path to a file containing | ||
| the Python code to be executed. | ||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| sys_remote_exec_unicode_path(PyObject *module, int pid, PyObject *script) | ||
| sys_remote_exec_impl(PyObject *module, int pid, PyObject *script) | ||
| /*[clinic end generated code: output=7d94c56afe4a52c0 input=39908ca2c5fe1eb0]*/ | ||
| { | ||
| const char *debugger_script_path = PyUnicode_AsUTF8(script); | ||
| if (debugger_script_path == NULL) { | ||
| PyObject *path; | ||
| const char *debugger_script_path; | ||
|
|
||
| if (PyUnicode_FSConverter(script, &path) < 0) { | ||
| return NULL; | ||
| } | ||
|
|
||
| debugger_script_path = PyBytes_AS_STRING(path); | ||
| #ifdef MS_WINDOWS | ||
| PyObject *unicode_path; | ||
| if (PyUnicode_FSDecoder(path, &unicode_path) < 0) { | ||
| Py_DECREF(path); | ||
| return NULL; | ||
| } | ||
| // Use UTF-16 (wide char) version of the path for permission checks | ||
| wchar_t *debugger_script_path_w = PyUnicode_AsWideCharString(script, NULL); | ||
| wchar_t *debugger_script_path_w = PyUnicode_AsWideCharString(unicode_path, NULL); | ||
| Py_DECREF(unicode_path); | ||
| if (debugger_script_path_w == NULL) { | ||
| Py_DECREF(path); | ||
| return NULL; | ||
| } | ||
|
|
||
| // Check file attributes using wide character version (W) instead of ANSI (A) | ||
| DWORD attr = GetFileAttributesW(debugger_script_path_w); | ||
| PyMem_Free(debugger_script_path_w); | ||
| if (attr == INVALID_FILE_ATTRIBUTES) { | ||
| DWORD err = GetLastError(); | ||
| PyMem_Free(debugger_script_path_w); | ||
| if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) { | ||
| PyErr_SetString(PyExc_FileNotFoundError, "Script file does not exist"); | ||
| } | ||
| else if (err == ERROR_ACCESS_DENIED) { | ||
| PyErr_SetString(PyExc_PermissionError, "Script file cannot be read"); | ||
| } | ||
| else { | ||
| PyErr_SetFromWindowsErr(0); | ||
| PyErr_SetFromWindowsErr(err); | ||
| } | ||
| Py_DECREF(path); | ||
|
||
| return NULL; | ||
| } | ||
| #else | ||
| PyMem_Free(debugger_script_path_w); | ||
| #else // MS_WINDOWS | ||
| if (access(debugger_script_path, F_OK | R_OK) != 0) { | ||
| switch (errno) { | ||
| case ENOENT: | ||
|
|
@@ -2494,56 +2530,19 @@ sys_remote_exec_unicode_path(PyObject *module, int pid, PyObject *script) | |
| default: | ||
| PyErr_SetFromErrno(PyExc_OSError); | ||
| } | ||
| Py_DECREF(path); | ||
| return NULL; | ||
| } | ||
| #endif | ||
|
|
||
| #endif // MS_WINDOWS | ||
| if (_PySysRemoteDebug_SendExec(pid, 0, debugger_script_path) < 0) { | ||
| Py_DECREF(path); | ||
| return NULL; | ||
| } | ||
|
|
||
| Py_DECREF(path); | ||
| Py_RETURN_NONE; | ||
| } | ||
|
|
||
| /*[clinic input] | ||
| sys.remote_exec | ||
|
|
||
| pid: int | ||
| script: object | ||
|
|
||
| Executes a file containing Python code in a given remote Python process. | ||
|
|
||
| This function returns immediately, and the code will be executed by the | ||
| target process's main thread at the next available opportunity, similarly | ||
| to how signals are handled. There is no interface to determine when the | ||
| code has been executed. The caller is responsible for making sure that | ||
| the file still exists whenever the remote process tries to read it and that | ||
| it hasn't been overwritten. | ||
|
|
||
| The remote process must be running a CPython interpreter of the same major | ||
| and minor version as the local process. If either the local or remote | ||
| interpreter is pre-release (alpha, beta, or release candidate) then the | ||
| local and remote interpreters must be the same exact version. | ||
|
|
||
| Args: | ||
| pid (int): The process ID of the target Python process. | ||
| script (str|bytes): The path to a file containing | ||
| the Python code to be executed. | ||
| [clinic start generated code]*/ | ||
|
|
||
| static PyObject * | ||
| sys_remote_exec_impl(PyObject *module, int pid, PyObject *script) | ||
| /*[clinic end generated code: output=7d94c56afe4a52c0 input=39908ca2c5fe1eb0]*/ | ||
| { | ||
| PyObject *ret = NULL; | ||
| PyObject *path; | ||
| if (PyUnicode_FSDecoder(script, &path)) { | ||
| ret = sys_remote_exec_unicode_path(module, pid, path); | ||
| Py_DECREF(path); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /*[clinic input] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice! I didn't know about this