GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Undocumented risky behaviour in subprocess module · Issue #101283 · python/cpython

Undocumented risky behaviour in subprocess module #101283

New issue
Copy link
New issue
Copy link
Closed
Closed
Undocumented risky behaviour in subprocess module#101283
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeOS-windowsrelease-blockertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errortype-securityA security issueA security issue

Description

@mauricelambert
mauricelambert
opened on Jan 24, 2023
Issue body actions

Bug report - Undocumented risky behaviour in subprocess module

When using subprocess.Popen with shell=True on Windows and without a COMSPEC environment variable, a cmd.exe is launched. The problem is the cmd.exe full path is not written, Windows will search the executable in the current directory and in the PATH. If an arbitrary executable file is written to the current directory or to a directory in the PATH, it can be run instead of the real cmd.exe.

See the code here and a POC here.

  • This risky behaviour can be patched by replacing cmd.exe string by C:\WINDOWS\system32\cmd.exe.
  • If the behavior was chosen by python developers, it should be documented.

Linked PRs

  • gh-101283: Try to load the fallback cmd.exe by an absolute path #101286
  • [3.10] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) #101708
  • [3.9] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) #101709
  • [3.8] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) #101710
  • [3.11] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) #101711
  • gh-101283: Fix use of unbound variable #101712
  • [3.7] gh-101283: Improved fallback logic for subprocess with shell=True on Windows (GH-101286) #101713
  • gh-101283: Version was just released, so should be changed in 3.11.3 #101719
  • [3.11] gh-101283: Version was just released, so should be changed in 3.11.3 (GH-101719) #101721
  • gh-101283: Fix the versionchanged of gh-101283 (3.12 only) #101728
Reacted by Oleg Iarygin and Raj Patnaik

Activity

Next
  1. mauricelambert
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 24, 2023
  2. arhadthedev
    added this to Contribution Queueon Jan 24, 2023
  3. github-project-automation
    moved this to Todo in Contribution Queueon Jan 24, 2023
  4. arhadthedev
    moved this from Todo to Done in Contribution Queueon Jan 24, 2023
  5. sobolevn
    added
    OS-windows
    on Jan 24, 2023
  6. eryksun commented on Jan 24, 2023

    @eryksun
    eryksun
    on Jan 24, 2023
    Contributor
    More actions

    FYI, subprocess.Popen should never find "cmd.exe" in PATH. We call CreateProcessW(), which always searches the application directory and system directories before it searches PATH.

    CreateProcessW() may also check the current directory immediately after the application directory and before the system directories. This unsafe behavior can be prevented by defining the environment variable NoDefaultCurrentDirectoryInExePath. This environment variable has been supported for over 15 years now, since Windows Vista. Our implementation of shutil.which() should honor this security feature by calling NeedCurrentDirectoryForExePathW().

    Note that if NoDefaultCurrentDirectoryInExePath is defined, the current directory is still included in the search path if the name is a relative path that includes backslashes (e.g. "Scripts\eggs.exe"), but not if it only includes forward slashes (e.g. "Scripts/eggs.exe"). Note also that the application directory takes precedence over the current directory in this case (e.g. if the application directory is "C:\Program Files\Python311", then "Scripts\eggs.exe" will resolve to "C:\Program Files\Python311\Scripts\eggs.exe" if it exists).

    If you define NoDefaultCurrentDirectoryInExePath, the current directory can be added explicitly as a "." entry at the end of either the system or user PATH. I add "." to the end of the user PATH because some apps and scripts still depend on finding binaries and scripts in the current directory, but I won't cater to a bad actor that expects the current directory to be searched before system directories.

    Reacted by Maurice Lambert
  7. gpshead
    assigned
    eryksun
    on Jan 24, 2023
  8. gpshead
    added
    type-securityA security issueA security issue
    on Jan 24, 2023
  9. gpshead commented on Jan 24, 2023

    @gpshead
    gpshead
    on Jan 24, 2023
    Member
    More actions

    FYI - this originally came in as a security report to PSRT. I redirected the reporter here (thanks for reporting this!) as this didn't seem serious enough to be embargoed. I'm still tagging it as such and aiming for a full set of backports as it feels like a good idea.

    Reacted by Maurice Lambert
  10. gpshead
    added
    OS-windows
    3.11only security fixesonly security fixes
    3.10 (EOL)end of lifeend of life
    3.9 (EOL)end of lifeend of life
    3.8 (EOL)end of lifeend of life
    and removed
    OS-windows
    on Jan 24, 2023
  11. 17 remaining items

  12. gpshead
    added
    release-blocker
    on Feb 9, 2023
  13. ezio-melotti
    added this to Release and Deferred blockers 🚫on Feb 9, 2023
  14. github-project-automation
    moved this to Todo in Release and Deferred blockers 🚫on Feb 9, 2023
  15. ambv
    added 3 commits that reference this issue on Feb 9, 2023

    [3.9] gh-101283: Improved fallback logic for subprocess with shell=Tr…

    04cc427

    [3.8] gh-101283: Improved fallback logic for subprocess with shell=Tr…

    32a1a61

    [3.7] gh-101283: Improved fallback logic for subprocess with shell=Tr…

    c7fdc9c
  16. zooba commented on Feb 9, 2023

    @zooba
    zooba
    on Feb 9, 2023
    Member
    More actions

    Fully merged, and all the fixes should be in (I hope). Thanks all!

    Reacted by Oleg Iarygin, Jeff Rouse and Maurice Lambert
  17. zooba
    closed this as completedon Feb 9, 2023
  18. github-project-automation
    moved this from Todo to Done in Release and Deferred blockers 🚫on Feb 9, 2023
  19. zooba
    added a commit that references this issue on Feb 9, 2023

    gh-101283: Fix 'versionchanged' for the shell=True fallback on Window…

    6d92373
  20. carljm
    added 2 commits that reference this issue on Feb 9, 2023

    Merge branch 'main' into inlinecomp2

    be3becc

    Merge branch 'main' into inlinecomp2

    add772e
  21. carlosroman
    added a commit that references this issue on Jun 22, 2023

    Upgrade to 3.8.17 (#26)

    e0a363f
  22. EliahKagan
    added 2 commits that reference this issue on Jan 10, 2024

    Further expand "not from cwd" test re: cmd.exe

    865c6e8

    Make Git.execute a bit simpler and very slightly more robust

    d2506c7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.7 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifeOS-windowsrelease-blockertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errortype-securityA security issueA security issue

    Projects

    • Release and Deferred blockers 🚫
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.