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

[3.14] 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 <gfunni234@gmail.com>
This commit is contained in:
Stan Ulbrych 2026-01-22 15:53:02 +00:00 committed by GitHub
parent f2b9a74cf6
commit fbc81558a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 1 deletions

View 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 test_stat(self):
self.assertTrue(posix.stat(os_helper.TESTFN))
self.assertTrue(posix.stat(os.fsencode(os_helper.TESTFN)))

View File

@ -0,0 +1,2 @@
Fixed validation of file descriptor 0 in posix functions when used with
follow_symlinks parameter.

View File

@ -1608,7 +1608,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);