Skip to content

Commit 837b88f

Browse files
[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 fa44efa) Co-authored-by: AZero13 <gfunni234@gmail.com>
1 parent d4d37b2 commit 837b88f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_posix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,18 @@ def test_fstat(self):
668668
finally:
669669
fp.close()
670670

671+
@unittest.skipUnless(hasattr(posix, 'stat'),
672+
'test needs posix.stat()')
673+
@unittest.skipUnless(os.stat in os.supports_follow_symlinks,
674+
'test needs follow_symlinks support in os.stat()')
675+
def test_stat_fd_zero_follow_symlinks(self):
676+
with self.assertRaisesRegex(ValueError,
677+
'cannot use fd and follow_symlinks together'):
678+
posix.stat(0, follow_symlinks=False)
679+
with self.assertRaisesRegex(ValueError,
680+
'cannot use fd and follow_symlinks together'):
681+
posix.stat(1, follow_symlinks=False)
682+
671683
def test_stat(self):
672684
self.assertTrue(posix.stat(os_helper.TESTFN))
673685
self.assertTrue(posix.stat(os.fsencode(os_helper.TESTFN)))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed validation of file descriptor 0 in posix functions when used with
2+
follow_symlinks parameter.

Modules/posixmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ static int
15331533
fd_and_follow_symlinks_invalid(const char *function_name, int fd,
15341534
int follow_symlinks)
15351535
{
1536-
if ((fd > 0) && (!follow_symlinks)) {
1536+
if ((fd >= 0) && (!follow_symlinks)) {
15371537
PyErr_Format(PyExc_ValueError,
15381538
"%s: cannot use fd and follow_symlinks together",
15391539
function_name);

0 commit comments

Comments
 (0)