https://github.com/python/cpython/commit/837b88fd9fc5a33a770c343253b2c488f9b8979d
commit: 837b88fd9fc5a33a770c343253b2c488f9b8979d
branch: 3.13
author: Stan Ulbrych <[email protected]>
committer: vstinner <[email protected]>
date: 2026-01-22T15:52:49Z
summary:

[3.13] gh-144023: Prevent `follow_symlinks` from being allowed with an fd of 0 
(GH-144022) (#144152)

[3.13] gh-144023: Prevent follow_symlinks from being allowed with an fd of 0 
(GH-144022)

The check was (fd > 0), should be (fd >= 0).
(cherry picked from commit fa44efa0ef1972ac1e2f66996303154be11f605e)

Co-authored-by: AZero13 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst
M Lib/test/test_posix.py
M Modules/posixmodule.c

diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index ffda92ae53e9f6..75065fe223403f 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/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 test_stat(self):
         self.assertTrue(posix.stat(os_helper.TESTFN))
         self.assertTrue(posix.stat(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 3c8f188d0a9c5a..b2beda45f53523 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1533,7 +1533,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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to