Skip to content

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

Closed

Description

@serhiy-storchaka
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']

    Activity

    1. serhiy-storchaka commented on Jun 22, 2017

      @serhiy-storchaka
      MemberAuthor

      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.

    2. serhiy-storchaka commented on Jun 23, 2017

      @serhiy-storchaka
      MemberAuthor

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

    3. serhiy-storchaka commented on Jun 23, 2017

      @serhiy-storchaka
      MemberAuthor

      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

    4. serhiy-storchaka commented on Jun 23, 2017

      @serhiy-storchaka
      MemberAuthor

      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

    5. serhiy-storchaka commented on Jun 24, 2017

      @serhiy-storchaka
      MemberAuthor

      3.3 and 3.4 starves from this issue

    6. serhiy-storchaka commented on Jun 24, 2017

      @serhiy-storchaka
      MemberAuthor

      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

    7. serhiy-storchaka commented on Jun 24, 2017

      @serhiy-storchaka
      MemberAuthor

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

    8. serhiy-storchaka commented on Jun 24, 2017

      @serhiy-storchaka
      MemberAuthor

      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

    9. serhiy-storchaka commented on Jun 24, 2017

      @serhiy-storchaka
      MemberAuthor

      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

    10. changed the title [-]Injecting environment variable in subprocess on Windows[/-] [+][security] Injecting environment variable in subprocess on Windows[/+] on Jun 28, 2017
    11. serhiy-storchaka commented on Jun 28, 2017

      @serhiy-storchaka
      MemberAuthor

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

    12. 1 remaining item

    13. zooba commented on Jul 1, 2017

      @zooba
      Member

      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.

    14. serhiy-storchaka commented on Jul 1, 2017

      @serhiy-storchaka
      MemberAuthor

      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.

    15. ned-deily commented on Jul 8, 2017

      @ned-deily
      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

    16. larryhastings commented on Jul 11, 2017

      @larryhastings
      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.

    17. larryhastings commented on Jul 11, 2017

      @larryhastings
      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
    18. larryhastings commented on Jul 11, 2017

      @larryhastings
      Contributor

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

    19. larryhastings commented on Jul 11, 2017

      @larryhastings
      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

    20. serhiy-storchaka commented on Jul 11, 2017

      @serhiy-storchaka
      MemberAuthor

      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.

    21. vstinner commented on Jul 12, 2017

      @vstinner
      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

    22. serhiy-storchaka commented on Jul 12, 2017

      @serhiy-storchaka
      MemberAuthor

      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.

    23. ned-deily commented on Jul 19, 2017

      @ned-deily
      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

    24. larryhastings commented on Jul 22, 2017

      @larryhastings
      Contributor

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

    25. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions