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

gh-101283: Try to load the fallback cmd.exe by an absolute path #101286

Merged
merged 25 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d122f14
Try to load the fallback cmd.exe by an absolute path
arhadthedev Jan 24, 2023
e96457c
Add a NEWS entry
arhadthedev Jan 24, 2023
b26918e
Fix minor grammar
arhadthedev Jan 24, 2023
9a0fdbd
Address patchcheck CI error report
arhadthedev Jan 24, 2023
091e970
Address the review
arhadthedev Jan 24, 2023
a3cbdb6
Simplify NEWS wording and reattribute the totally rewritten PR
arhadthedev Jan 24, 2023
8dab7f1
Address the review-2
arhadthedev Jan 24, 2023
1d8d4a6
Update Lib/subprocess.py
arhadthedev Jan 24, 2023
9ed1758
Simplify the NEWS entry
arhadthedev Jan 25, 2023
a2734a6
Clarify attribution
arhadthedev Jan 25, 2023
407aa35
Update 2023-01-24-16-12-00.gh-issue-101283.9tqu39.rst
arhadthedev Jan 25, 2023
f1dfcff
Merge branch 'main' into absolute-cmdexe-fallback-patch
arhadthedev Jan 28, 2023
880d443
Add `Changed in version` entries
arhadthedev Jan 28, 2023
b34f998
Try another multiversion syntax
arhadthedev Jan 28, 2023
84cf94f
Try the third, multiline syntax
arhadthedev Jan 28, 2023
9cbdfca
Apply the gpshead's suggestion
arhadthedev Jan 30, 2023
8bfb11b
Merge branch 'main' into absolute-cmdexe-fallback-patch
arhadthedev Jan 31, 2023
86473da
Address the review
arhadthedev Feb 8, 2023
f1b4412
Apply suggestions from code review
arhadthedev Feb 8, 2023
11ebd69
Improve performance by triggering `cmd.exe` search on `executable=Non…
arhadthedev Feb 8, 2023
25dcbb8
Update the Changed In section
arhadthedev Feb 8, 2023
fc08548
Update Lib/subprocess.py
arhadthedev Feb 8, 2023
7e06703
Fix env variable name misquoting
arhadthedev Feb 8, 2023
81aba86
Manually fix env variable name misquoting
arhadthedev Feb 8, 2023
fecd606
Update Lib/subprocess.py
arhadthedev Feb 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Doc/library/subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ underlying :class:`Popen` interface can be used directly.
Added the *text* parameter, as a more understandable alias of *universal_newlines*.
Added the *capture_output* parameter.

.. versionchanged:: 3.11.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version number is wrong. 3.11.2 was released before this change was merged. As this PR is in the main branch, listing it as 3.12 in these docs is more appropriate. Documentation readers should not be expected to know which patch release versions came before a major release.

We lack the ability to list all versions in versionchanged markers in our docs so have done it this way elsewhere as well when a visible API change has been made in patch releases.


Changed Windows shell search order for ``shell=True``. The current
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
malicious program named ``cmd.exe`` into a current directory no
longer works.

.. class:: CompletedProcess

The return value from :func:`run`, representing a process that has finished.
Expand Down Expand Up @@ -487,6 +495,14 @@ functions.
*executable* parameter accepts a bytes and :term:`path-like object`
on Windows.

.. versionchanged:: 3.11.2

Changed Windows shell search order for ``shell=True``. The current
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
malicious program named ``cmd.exe`` into a current directory no
longer works.

*stdin*, *stdout* and *stderr* specify the executed program's standard input,
standard output and standard error file handles, respectively. Valid values
are ``None``, :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a
Expand Down Expand Up @@ -1157,6 +1173,14 @@ calls these functions.
.. versionchanged:: 3.3
*timeout* was added.

.. versionchanged:: 3.11.2

Changed Windows shell search order for ``shell=True``. The current
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
malicious program named ``cmd.exe`` into a current directory no
longer works.

.. function:: check_call(args, *, stdin=None, stdout=None, stderr=None, \
shell=False, cwd=None, timeout=None, \
**other_popen_kwargs)
Expand Down Expand Up @@ -1189,6 +1213,14 @@ calls these functions.
.. versionchanged:: 3.3
*timeout* was added.

.. versionchanged:: 3.11.2

Changed Windows shell search order for ``shell=True``. The current
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
malicious program named ``cmd.exe`` into a current directory no
longer works.


.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, \
cwd=None, encoding=None, errors=None, \
Expand Down Expand Up @@ -1244,6 +1276,14 @@ calls these functions.
.. versionadded:: 3.7
*text* was added as a more readable alias for *universal_newlines*.

.. versionchanged:: 3.11.2

Changed Windows shell search order for ``shell=True``. The current
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
malicious program named ``cmd.exe`` into a current directory no
longer works.


.. _subprocess-replacements:

Expand Down
17 changes: 16 additions & 1 deletion Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,22 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
if shell:
startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = _winapi.SW_HIDE
comspec = os.environ.get("COMSPEC", "cmd.exe")
if not executable:
# gh-101283: without a fully-qualified path, before Windows
# checks the system directories, it first looks in the
# application directory, and also the current directory if
# NeedCurrentDirectoryForExePathW(ExeName) is true, so try
# to avoid executing unqualified "cmd.exe".
comspec = os.environ.get('ComSpec')
if not comspec:
system_root = os.environ.get('SystemRoot', '')
comspec = os.path.join(system_root, 'System32', 'cmd.exe')
if not os.path.isabs(comspec):
raise FileNotFoundError(
'shell not found: neither %ComSpec% nor %SystemRoot% is set')
arhadthedev marked this conversation as resolved.
Show resolved Hide resolved
if os.path.isabs(comspec):
executable = comspec

args = '{} /c "{}"'.format (comspec, args)

if cwd is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:class:`subprocess.Popen` now uses a safer approach to find
``cmd.exe`` when launching with ``shell=True``. Patch by Eryk Sun,
based on a patch by Oleg Iarygin.