]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-144023: Prevent follow_symlinks from being allowed with an fd of 0 (GH-144022)
authorAZero13 <gfunni234@gmail.com>
Tue, 20 Jan 2026 09:50:51 +0000 (04:50 -0500)
committerGitHub <noreply@github.com>
Tue, 20 Jan 2026 09:50:51 +0000 (11:50 +0200)
The check was (fd > 0), should be (fd >= 0).

Lib/test/test_os/test_posix.py
Misc/NEWS.d/next/Library/2026-01-19-00-57-40.gh-issue-144023.29XUcp.rst [new file with mode: 0644]
Modules/posixmodule.c

index 37da293a441e46c7b6dc6eca82d7e3d478676baa..995c48bdbbffd64d7ee78899239eb8f862670aee 100644 (file)
@@ -668,6 +668,18 @@ class PosixTester(unittest.TestCase):
         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 (file)
index 0000000..0d06506
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed validation of file descriptor 0 in posix functions when used with
+follow_symlinks parameter.
index 49214d57a2e362aa30d6c340767936e8bee3cbcc..ef90ac5de09c651281e7df4104a061b802adf80d 100644 (file)
@@ -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);