summaryrefslogtreecommitdiffstats
path: root/Modules/_winapi.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-06-23 16:39:27 (GMT)
committerGitHub <noreply@github.com>2017-06-23 16:39:27 (GMT)
commitd174d24a5d37d1516b885dc7c82f71ecd5930700 (patch)
treed9fd67e5993b32d8b80c58099dd4a6aa0672722b /Modules/_winapi.c
parentd352d689775699c289e011e8cec52c23c600b7fa (diff)
downloadcpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.zip
cpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.tar.gz
cpython-d174d24a5d37d1516b885dc7c82f71ecd5930700.tar.bz2
bpo-30730: Prevent environment variables injection in subprocess on Windows. (#2325)
Prevent passing other invalid environment variables and command arguments.
Diffstat (limited to 'Modules/_winapi.c')
-rw-r--r--Modules/_winapi.c26
1 files changed, 21 insertions, 5 deletions
diff --git a/Modules/_winapi.c b/Modules/_winapi.c
index 8d01b37..671f2dd 100644
--- a/Modules/_winapi.c
+++ b/Modules/_winapi.c
@@ -744,6 +744,20 @@ getenvironment(PyObject* environment)
"environment can only contain strings");
goto error;
}
+ if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
+ PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
+ {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto error;
+ }
+ /* Search from index 1 because on Windows starting '=' is allowed for
+ defining hidden environment variables. */
+ if (PyUnicode_GET_LENGTH(key) == 0 ||
+ PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
+ {
+ PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
+ goto error;
+ }
if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) {
PyErr_SetString(PyExc_OverflowError, "environment too long");
goto error;
@@ -830,7 +844,8 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
PROCESS_INFORMATION pi;
STARTUPINFOW si;
PyObject* environment;
- wchar_t *wenvironment;
+ const wchar_t *wenvironment;
+ Py_ssize_t wenvironment_size;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
@@ -846,12 +861,13 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
if (env_mapping != Py_None) {
environment = getenvironment(env_mapping);
- if (! environment)
+ if (environment == NULL) {
return NULL;
+ }
+ /* contains embedded null characters */
wenvironment = PyUnicode_AsUnicode(environment);
- if (wenvironment == NULL)
- {
- Py_XDECREF(environment);
+ if (wenvironment == NULL) {
+ Py_DECREF(environment);
return NULL;
}
}