Skip to content

Commit 2c98724

Browse files
[3.11] gh-104522: Fix OSError raised when run a subprocess (GH-114195) (GH-114243)
Only set filename to cwd if it was caused by failed chdir(cwd). _fork_exec() now returns "noexec:chdir" for failed chdir(cwd). (cherry picked from commit e2c097e) Co-authored-by: Robert O'Shea <[email protected]>
1 parent 9887b0c commit 2c98724

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

Lib/subprocess.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1938,16 +1938,21 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
19381938
SubprocessError)
19391939
if issubclass(child_exception_type, OSError) and hex_errno:
19401940
errno_num = int(hex_errno, 16)
1941-
child_exec_never_called = (err_msg == "noexec")
1942-
if child_exec_never_called:
1941+
if err_msg == "noexec:chdir":
19431942
err_msg = ""
19441943
# The error must be from chdir(cwd).
19451944
err_filename = cwd
1945+
elif err_msg == "noexec":
1946+
err_msg = ""
1947+
err_filename = None
19461948
else:
19471949
err_filename = orig_executable
19481950
if errno_num != 0:
19491951
err_msg = os.strerror(errno_num)
1950-
raise child_exception_type(errno_num, err_msg, err_filename)
1952+
if err_filename is not None:
1953+
raise child_exception_type(errno_num, err_msg, err_filename)
1954+
else:
1955+
raise child_exception_type(errno_num, err_msg)
19511956
raise child_exception_type(err_msg)
19521957

19531958

Lib/test/test_subprocess.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -2030,11 +2030,12 @@ def test_user(self):
20302030
"import os; print(os.getuid())"],
20312031
user=user,
20322032
close_fds=close_fds)
2033-
except PermissionError: # (EACCES, EPERM)
2034-
pass
2033+
except PermissionError as e: # (EACCES, EPERM)
2034+
self.assertIsNone(e.filename)
20352035
except OSError as e:
20362036
if e.errno not in (errno.EACCES, errno.EPERM):
20372037
raise
2038+
self.assertIsNone(e.filename)
20382039
else:
20392040
if isinstance(user, str):
20402041
user_uid = pwd.getpwnam(user).pw_uid
@@ -2078,8 +2079,8 @@ def test_group(self):
20782079
"import os; print(os.getgid())"],
20792080
group=group,
20802081
close_fds=close_fds)
2081-
except PermissionError: # (EACCES, EPERM)
2082-
pass
2082+
except PermissionError as e: # (EACCES, EPERM)
2083+
self.assertIsNone(e.filename)
20832084
else:
20842085
if isinstance(group, str):
20852086
group_gid = grp.getgrnam(group).gr_gid
@@ -2124,6 +2125,7 @@ def test_extra_groups(self):
21242125
except OSError as ex:
21252126
if ex.errno != errno.EPERM:
21262127
raise
2128+
self.assertIsNone(ex.filename)
21272129
perm_error = True
21282130

21292131
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:exc:`OSError` raised when run a subprocess now only has *filename*
2+
attribute set to *cwd* if the error was caused by a failed attempt to change
3+
the current directory.

Modules/_posixsubprocess.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,10 @@ child_exec(char *const exec_array[],
566566
PyObject *preexec_fn,
567567
PyObject *preexec_fn_args_tuple)
568568
{
569-
int i, saved_errno, reached_preexec = 0;
569+
int i, saved_errno;
570570
PyObject *result;
571-
const char* err_msg = "";
571+
/* Indicate to the parent that the error happened before exec(). */
572+
const char *err_msg = "noexec";
572573
/* Buffer large enough to hold a hex integer. We can't malloc. */
573574
char hex_errno[sizeof(saved_errno)*2+1];
574575

@@ -628,8 +629,12 @@ child_exec(char *const exec_array[],
628629
/* We no longer manually close p2cread, c2pwrite, and errwrite here as
629630
* _close_open_fds takes care when it is not already non-inheritable. */
630631

631-
if (cwd)
632-
POSIX_CALL(chdir(cwd));
632+
if (cwd) {
633+
if (chdir(cwd) == -1) {
634+
err_msg = "noexec:chdir";
635+
goto error;
636+
}
637+
}
633638

634639
if (child_umask >= 0)
635640
umask(child_umask); /* umask() always succeeds. */
@@ -672,7 +677,7 @@ child_exec(char *const exec_array[],
672677
#endif /* HAVE_SETREUID */
673678

674679

675-
reached_preexec = 1;
680+
err_msg = "";
676681
if (preexec_fn != Py_None && preexec_fn_args_tuple) {
677682
/* This is where the user has asked us to deadlock their program. */
678683
result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
@@ -730,16 +735,12 @@ child_exec(char *const exec_array[],
730735
}
731736
_Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
732737
_Py_write_noraise(errpipe_write, ":", 1);
733-
if (!reached_preexec) {
734-
/* Indicate to the parent that the error happened before exec(). */
735-
_Py_write_noraise(errpipe_write, "noexec", 6);
736-
}
737738
/* We can't call strerror(saved_errno). It is not async signal safe.
738739
* The parent process will look the error message up. */
739740
} else {
740741
_Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
741-
_Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
742742
}
743+
_Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
743744
}
744745

745746

0 commit comments

Comments
 (0)