Repository navigation
[security] Injecting environment variable in subprocess on Windows #74915
Copy link
Copy link
Closed
Labels
3.7 (EOL)end of lifeend of lifeextension-modulesC modules in the Modules dirC modules in the Modules dirrelease-blockertype-securityA security issueA security issue
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/serhiy-storchaka'
closed_at = <Date 2017-07-19.05:17:56.399>
created_at = <Date 2017-06-22.08:07:00.032>
labels = ['type-security', 'extension-modules', '3.7', 'release-blocker']
title = '[security] Injecting environment variable in subprocess on Windows'
updated_at = <Date 2019-05-10.18:17:07.168>
user = 'https://github.com/serhiy-storchaka'bugs.python.org fields:
activity = <Date 2019-05-10.18:17:07.168>
actor = 'ned.deily'
assignee = 'serhiy.storchaka'
closed = True
closed_date = <Date 2017-07-19.05:17:56.399>
closer = 'serhiy.storchaka'
components = ['Extension Modules']
creation = <Date 2017-06-22.08:07:00.032>
creator = 'serhiy.storchaka'
dependencies = []
files = []
hgrepos = []
issue_num = 30730
keywords = []
message_count = 23.0
messages = ['296618', '296725', '296728', '296729', '296753', '296760', '296767', '296769', '296772', '297175', '297449', '297468', '297472', '297936', '298145', '298146', '298147', '298148', '298151', '298218', '298222', '298628', '298865']
nosy_count = 10.0
nosy_names = ['georg.brandl', 'paul.moore', 'vstinner', 'larry', 'tim.golden', 'benjamin.peterson', 'ned.deily', 'zach.ware', 'serhiy.storchaka', 'steve.dower']
pr_nums = ['2325', '2360', '2361', '2362', '2363', '2372', '2376', '2378', '2379']
priority = 'release blocker'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'security'
url = 'https://bugs.python.org/issue30730'
versions = ['Python 2.7', 'Python 3.3', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7']Metadata
Metadata
Assignees
Labels
3.7 (EOL)end of lifeend of lifeextension-modulesC modules in the Modules dirC modules in the Modules dirrelease-blockertype-securityA security issueA security issue
Activity
serhiy-storchaka commentedon Jun 22, 2017
It is possible to inject an environment variable in subprocess on Windows if a user data is passed to a subprocess via environment variable.
Provided PR fixes this vulnerability. It also adds other checks for invalid environment (variable names containing '=') and command arguments (containing '\0').
This was a part of bpo-13617, but extracted to a separate issue due to increased severity.
serhiy-storchaka commentedon Jun 23, 2017
New changeset d174d24 by Serhiy Storchaka in branch 'master':
bpo-30730: Prevent environment variables injection in subprocess on Windows. (bpo-2325)
d174d24
serhiy-storchaka commentedon Jun 23, 2017
New changeset e713575 by Serhiy Storchaka in branch '3.6':
[3.6] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2360)
e713575
serhiy-storchaka commentedon Jun 23, 2017
New changeset a7c0264 by Serhiy Storchaka in branch '3.5':
[3.5] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2361)
a7c0264
serhiy-storchaka commentedon Jun 24, 2017
3.3 and 3.4 starves from this issue
serhiy-storchaka commentedon Jun 24, 2017
New changeset 9dda2ca by Serhiy Storchaka in branch '2.7':
[2.7] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2372)
9dda2ca
serhiy-storchaka commentedon Jun 24, 2017
New changeset 0ee32c1 by Serhiy Storchaka in branch 'master':
bpo-30745: Fix compiler warnings introduced in bpo-30730. (bpo-2376)
0ee32c1
serhiy-storchaka commentedon Jun 24, 2017
New changeset 0e1f9e8 by Serhiy Storchaka in branch '3.6':
[3.6] bpo-30745: Fix compiler warnings introduced in bpo-30730. (GH-2376) (bpo-2378)
0e1f9e8
serhiy-storchaka commentedon Jun 24, 2017
New changeset e0d446e by Serhiy Storchaka in branch '3.5':
[3.5] bpo-30745: Fix compiler warnings introduced in bpo-30730. (GH-2376) (bpo-2379)
e0d446e
[-]Injecting environment variable in subprocess on Windows[/-][+][security] Injecting environment variable in subprocess on Windows[/+]serhiy-storchaka commentedon Jun 28, 2017
Isn't "Type: security" enough? If you want you could patch Roundup for highlighting security issues.
1 remaining item
zooba commentedon Jul 1, 2017
It's certainly exploitable for remote code execution if user data allows embedded nulls (can you URL encode %00?). The fixes look fine and shouldn't cause any new issues, though I thought that fsencode() already rejected embedded nulls - maybe I'm thinking of the argument converter though, which is not invoked here.
serhiy-storchaka commentedon Jul 1, 2017
Yes, fsencode() already rejected embedded nulls, that is why the Posix branch doesn't need additional check for null characters. The Posix branch was changed only for adding the check for the '=' character in names.
ned-deily commentedon Jul 8, 2017
New changeset a9b16cf by Ned Deily (Serhiy Storchaka) in branch '3.6':
[3.6] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2360)
a9b16cf
New changeset d1d6501 by Ned Deily (Serhiy Storchaka) in branch '3.6':
[3.6] bpo-30745: Fix compiler warnings introduced in bpo-30730. (GH-2376) (bpo-2378)
d1d6501
larryhastings commentedon Jul 11, 2017
Serhiy, I don't see where you got a full review of this change. Eryksun reviewed the code and asked for changes; you made the he asked for changes but didn't get any further review. Nor did you get a full review / "looks good to me" from anybody.
As a matter of policy I do want to see reviews for security changes on Windows. I've asked Steve Dower to give it a quick review.
larryhastings commentedon Jul 11, 2017
It seems that os.execve() still permits this, even on Windows. Shouldn't we solve it there too? (Thanks to Steve Dower for realizing this.)
--
larryhastings commentedon Jul 11, 2017
(never-mind, 3.6.1 still permits this, but I see that it's been fixed in trunk)
larryhastings commentedon Jul 11, 2017
New changeset fe82c46 by larryhastings (Serhiy Storchaka) in branch '3.4':
[security][3.4] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2362)
fe82c46
serhiy-storchaka commentedon Jul 11, 2017
Sorry, actually the patch fixed two bugs. The one of them is a security issue, the other is much more severe. They look similar, are related to the same code (on Windows) and are tested with similar tests.
os.execve() was not vulnerable to the first issue, it suffered only from the less severe bug. It was fixed in the separate issue (see bpo-30746). I don't think that should be backported to 3.4.
vstinner commentedon Jul 12, 2017
I rebased my "[3.4] Backport CI config from master" PR bpo-2475 on top of 3.4 to test the new security fixes, but a few test_subprocess tests failed:
#2475
https://travis-ci.org/python/cpython/jobs/252804589
======================================================================
ERROR: test_invalid_cmd (test.test_subprocess.ProcessTestCase)
----------------------------------------------------------------------
File "/home/travis/build/python/cpython/Lib/test/test_subprocess.py", line 613, in test_invalid_cmd
File "/home/travis/build/python/cpython/Lib/subprocess.py", line 856, in __init__
File "/home/travis/build/python/cpython/Lib/subprocess.py", line 1402, in _execute_child
TypeError: expected bytes with no null
serhiy-storchaka commentedon Jul 12, 2017
Oh, I forgot that null character/byte errors were of type TypeError before 3.5. The simplest fix is changing corresponding ValueError in self.assertRaises() to the tuple (ValueError, TypeError).
I have updated the PR for 3.5. You can include the fix in your "[3.4] Backport CI config from master" PR or I can create a separate PR for 3.4.
ned-deily commentedon Jul 19, 2017
New changeset e46f1c1 by Ned Deily (Serhiy Storchaka) in branch '3.3':
[security][3.3] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) (bpo-2363)
e46f1c1
larryhastings commentedon Jul 22, 2017
New changeset b154917 by larryhastings (Victor Stinner) in branch '3.4':
[3.4] Backport CI config from master (bpo-2475)
b154917