Skip to content
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

[security] Injecting environment variable in subprocess on Windows #74915

Closed
serhiy-storchaka opened this issue Jun 22, 2017 · 23 comments
Closed
Assignees
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir release-blocker type-security A security issue

Comments

@serhiy-storchaka
Copy link
Member

BPO 30730
Nosy @birkenfeld, @pfmoore, @vstinner, @larryhastings, @tjguk, @benjaminp, @ned-deily, @zware, @serhiy-storchaka, @zooba
PRs
  • bpo-30730: Prevent environment variables injection in subprocess on Windows. #2325
  • [3.6] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) #2360
  • [3.5] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) #2361
  • [security][3.4] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) #2362
  • [security][3.3] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) #2363
  • [2.7] bpo-30730: Prevent environment variables injection in subprocess on Windows. (GH-2325) #2372
  • bpo-30745: Fix compiler warnings introduced in bpo-30730. #2376
  • [3.6] bpo-30745: Fix compiler warnings introduced in bpo-30730. (GH-2376) #2378
  • [3.5] bpo-30745: Fix compiler warnings introduced in bpo-30730. (GH-2376) #2379
  • 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']

    @serhiy-storchaka
    Copy link
    Member Author

    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 serhiy-storchaka added 3.7 (EOL) end of life extension-modules C modules in the Modules dir type-security A security issue labels Jun 22, 2017
    @serhiy-storchaka serhiy-storchaka self-assigned this Jun 23, 2017
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset d174d24 by Serhiy Storchaka in branch 'master':
    bpo-30730: Prevent environment variables injection in subprocess on Windows. (bpo-2325)
    d174d24

    @serhiy-storchaka
    Copy link
    Member Author

    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
    Copy link
    Member Author

    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
    Copy link
    Member Author

    3.3 and 3.4 starves from this issue

    @serhiy-storchaka
    Copy link
    Member Author

    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
    Copy link
    Member Author

    New changeset 0ee32c1 by Serhiy Storchaka in branch 'master':
    bpo-30745: Fix compiler warnings introduced in bpo-30730. (bpo-2376)
    0ee32c1

    @serhiy-storchaka
    Copy link
    Member Author

    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
    Copy link
    Member Author

    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

    @vstinner vstinner changed the title Injecting environment variable in subprocess on Windows [security] Injecting environment variable in subprocess on Windows Jun 28, 2017
    @serhiy-storchaka
    Copy link
    Member Author

    Isn't "Type: security" enough? If you want you could patch Roundup for highlighting security issues.

    @ned-deily
    Copy link
    Member

    Steve, Paul: any comments on the severity of this issue and the pushed fixes?

    @zooba
    Copy link
    Member

    zooba commented 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
    Copy link
    Member Author

    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
    Copy link
    Member

    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
    Copy link
    Contributor

    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
    Copy link
    Contributor

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

    --

    import os
    
    cmdline=["/usr/bin/printenv"]
    env={'a=b': 'c'}
    os.execve(cmdline[0], cmdline, env)
    # this prints a=b=c

    @larryhastings
    Copy link
    Contributor

    (never-mind, 3.6.1 still permits this, but I see that it's been fixed in trunk)

    @larryhastings
    Copy link
    Contributor

    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
    Copy link
    Member Author

    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
    Copy link
    Member

    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)

    ----------------------------------------------------------------------

    Traceback (most recent call last):

    File "/home/travis/build/python/cpython/Lib/test/test_subprocess.py", line 613, in test_invalid_cmd

        subprocess.Popen([cmd, "-c", "pass"])

    File "/home/travis/build/python/cpython/Lib/subprocess.py", line 856, in __init__

    restore_signals, start_new_session)
    

    File "/home/travis/build/python/cpython/Lib/subprocess.py", line 1402, in _execute_child

    restore_signals, start_new_session, preexec_fn)
    

    TypeError: expected bytes with no null

    @serhiy-storchaka
    Copy link
    Member Author

    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
    Copy link
    Member

    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
    Copy link
    Contributor

    New changeset b154917 by larryhastings (Victor Stinner) in branch '3.4':
    [3.4] Backport CI config from master (bpo-2475)
    b154917

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life extension-modules C modules in the Modules dir release-blocker type-security A security issue
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants