From fd893154f3876ea901603655ca9ff2d729def45b Mon Sep 17 00:00:00 2001 From: AZero13 Date: Mon, 19 Jan 2026 00:48:26 -0500 Subject: [PATCH] gh-144023: prevent follow_symlinks from being allowed with an fd of 0 fd_and_follow_symlinks_invalid lets you use fd and follow_symlinks together if fd is 0. This is incorrect, as 0 is a valid fd. --- Lib/test/test_os/test_posix.py | 12 ++++++++++++ .../2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst | 2 ++ Modules/posixmodule.c | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst diff --git a/Lib/test/test_os/test_posix.py b/Lib/test/test_os/test_posix.py index 37da293a441e46..995c48bdbbffd6 100644 --- a/Lib/test/test_os/test_posix.py +++ b/Lib/test/test_os/test_posix.py @@ -668,6 +668,18 @@ def test_fstat(self): finally: fp.close() + @unittest.skipUnless(hasattr(posix, 'stat'), + 'test needs posix.stat()') + @unittest.skipUnless(os.stat in os.supports_follow_symlinks, + 'test needs follow_symlinks support in os.stat()') + def test_stat_fd_zero_follow_symlinks(self): + with self.assertRaisesRegex(ValueError, + 'cannot use fd and follow_symlinks together'): + posix.stat(0, follow_symlinks=False) + with self.assertRaisesRegex(ValueError, + 'cannot use fd and follow_symlinks together'): + posix.stat(1, follow_symlinks=False) + def check_statlike_path(self, func): self.assertTrue(func(os_helper.TESTFN)) self.assertTrue(func(os.fsencode(os_helper.TESTFN))) diff --git a/Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst b/Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst new file mode 100644 index 00000000000000..0d06506e1ec106 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst @@ -0,0 +1,2 @@ +Fixed validation of file descriptor 0 in posix functions when used with +follow_symlinks parameter. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 49214d57a2e362..ef90ac5de09c65 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1637,7 +1637,7 @@ static int fd_and_follow_symlinks_invalid(const char *function_name, int fd, int follow_symlinks) { - if ((fd > 0) && (!follow_symlinks)) { + if ((fd >= 0) && (!follow_symlinks)) { PyErr_Format(PyExc_ValueError, "%s: cannot use fd and follow_symlinks together", function_name);