Skip to content

Commit b440f34

Browse files
LumioseSilStanFromIreland
authored andcommitted
[3.14] pythongh-144023: Prevent follow_symlinks from being allowed with an fd of 0 (pythonGH-144022)
The check was (fd > 0), should be (fd >= 0). (cherry picked from commit fa44efa) Co-authored-by: AZero13 <gfunni234@gmail.com>
1 parent f2b9a74 commit b440f34

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lib/test/test_posix.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,35 @@ 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+
683+
def check_statlike_path(self, func):
684+
self.assertTrue(func(os_helper.TESTFN))
685+
self.assertTrue(func(os.fsencode(os_helper.TESTFN)))
686+
self.assertTrue(func(os_helper.FakePath(os_helper.TESTFN)))
687+
688+
self.assertRaisesRegex(TypeError,
689+
'should be string, bytes, os.PathLike or integer, not',
690+
func, bytearray(os.fsencode(os_helper.TESTFN)))
691+
self.assertRaisesRegex(TypeError,
692+
'should be string, bytes, os.PathLike or integer, not',
693+
func, None)
694+
self.assertRaisesRegex(TypeError,
695+
'should be string, bytes, os.PathLike or integer, not',
696+
func, list(os_helper.TESTFN))
697+
self.assertRaisesRegex(TypeError,
698+
'should be string, bytes, os.PathLike or integer, not',
699+
func, list(os.fsencode(os_helper.TESTFN)))
671700
def test_stat(self):
672701
self.assertTrue(posix.stat(os_helper.TESTFN))
673702
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
@@ -1608,7 +1608,7 @@ static int
16081608
fd_and_follow_symlinks_invalid(const char *function_name, int fd,
16091609
int follow_symlinks)
16101610
{
1611-
if ((fd > 0) && (!follow_symlinks)) {
1611+
if ((fd >= 0) && (!follow_symlinks)) {
16121612
PyErr_Format(PyExc_ValueError,
16131613
"%s: cannot use fd and follow_symlinks together",
16141614
function_name);

0 commit comments

Comments
 (0)