Skip to content

Commit d174d24

Browse files
bpo-30730: Prevent environment variables injection in subprocess on Windows. (#2325)
Prevent passing other invalid environment variables and command arguments.
1 parent d352d68 commit d174d24

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

Lib/subprocess.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,12 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
12381238
# and pass it to fork_exec()
12391239

12401240
if env is not None:
1241-
env_list = [os.fsencode(k) + b'=' + os.fsencode(v)
1242-
for k, v in env.items()]
1241+
env_list = []
1242+
for k, v in env.items():
1243+
k = os.fsencode(k)
1244+
if b'=' in k:
1245+
raise ValueError("illegal environment variable name")
1246+
env_list.append(k + b'=' + os.fsencode(v))
12431247
else:
12441248
env_list = None # Use execv instead of execve.
12451249
executable = os.fsencode(executable)

Lib/test/test_subprocess.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,46 @@ def is_env_var_to_ignore(n):
655655
if not is_env_var_to_ignore(k)]
656656
self.assertEqual(child_env_names, [])
657657

658+
def test_invalid_cmd(self):
659+
# null character in the command name
660+
cmd = sys.executable + '\0'
661+
with self.assertRaises(ValueError):
662+
subprocess.Popen([cmd, "-c", "pass"])
663+
664+
# null character in the command argument
665+
with self.assertRaises(ValueError):
666+
subprocess.Popen([sys.executable, "-c", "pass#\0"])
667+
668+
def test_invalid_env(self):
669+
# null character in the enviroment variable name
670+
newenv = os.environ.copy()
671+
newenv["FRUIT\0VEGETABLE"] = "cabbage"
672+
with self.assertRaises(ValueError):
673+
subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
674+
675+
# null character in the enviroment variable value
676+
newenv = os.environ.copy()
677+
newenv["FRUIT"] = "orange\0VEGETABLE=cabbage"
678+
with self.assertRaises(ValueError):
679+
subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
680+
681+
# equal character in the enviroment variable name
682+
newenv = os.environ.copy()
683+
newenv["FRUIT=ORANGE"] = "lemon"
684+
with self.assertRaises(ValueError):
685+
subprocess.Popen([sys.executable, "-c", "pass"], env=newenv)
686+
687+
# equal character in the enviroment variable value
688+
newenv = os.environ.copy()
689+
newenv["FRUIT"] = "orange=lemon"
690+
with subprocess.Popen([sys.executable, "-c",
691+
'import sys, os;'
692+
'sys.stdout.write(os.getenv("FRUIT"))'],
693+
stdout=subprocess.PIPE,
694+
env=newenv) as p:
695+
stdout, stderr = p.communicate()
696+
self.assertEqual(stdout, b"orange=lemon")
697+
658698
def test_communicate_stdin(self):
659699
p = subprocess.Popen([sys.executable, "-c",
660700
'import sys;'

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ Extension Modules
374374
Library
375375
-------
376376

377+
- [Security] bpo-30730: Prevent environment variables injection in subprocess on
378+
Windows. Prevent passing other environment variables and command arguments.
379+
377380
- bpo-21071: struct.Struct.format type is now :class:`str` instead of
378381
:class:`bytes`.
379382

Modules/_winapi.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,20 @@ getenvironment(PyObject* environment)
744744
"environment can only contain strings");
745745
goto error;
746746
}
747+
if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
748+
PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
749+
{
750+
PyErr_SetString(PyExc_ValueError, "embedded null character");
751+
goto error;
752+
}
753+
/* Search from index 1 because on Windows starting '=' is allowed for
754+
defining hidden environment variables. */
755+
if (PyUnicode_GET_LENGTH(key) == 0 ||
756+
PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
757+
{
758+
PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
759+
goto error;
760+
}
747761
if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) {
748762
PyErr_SetString(PyExc_OverflowError, "environment too long");
749763
goto error;
@@ -830,7 +844,8 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
830844
PROCESS_INFORMATION pi;
831845
STARTUPINFOW si;
832846
PyObject* environment;
833-
wchar_t *wenvironment;
847+
const wchar_t *wenvironment;
848+
Py_ssize_t wenvironment_size;
834849

835850
ZeroMemory(&si, sizeof(si));
836851
si.cb = sizeof(si);
@@ -846,12 +861,13 @@ _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name,
846861

847862
if (env_mapping != Py_None) {
848863
environment = getenvironment(env_mapping);
849-
if (! environment)
864+
if (environment == NULL) {
850865
return NULL;
866+
}
867+
/* contains embedded null characters */
851868
wenvironment = PyUnicode_AsUnicode(environment);
852-
if (wenvironment == NULL)
853-
{
854-
Py_XDECREF(environment);
869+
if (wenvironment == NULL) {
870+
Py_DECREF(environment);
855871
return NULL;
856872
}
857873
}

Objects/abstract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,8 +2558,8 @@ _PySequence_BytesToCharpArray(PyObject* self)
25582558
array[i] = NULL;
25592559
goto fail;
25602560
}
2561-
data = PyBytes_AsString(item);
2562-
if (data == NULL) {
2561+
/* check for embedded null bytes */
2562+
if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
25632563
/* NULL terminate before freeing. */
25642564
array[i] = NULL;
25652565
goto fail;

0 commit comments

Comments
 (0)