Skip to content

Commit 31e7d6e

Browse files
committed
Check that int file descriptor is valid for posix_(isatty|ttyname)
1 parent 54767b1 commit 31e7d6e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

ext/posix/posix.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ PHP_FUNCTION(posix_ttyname)
463463
zend_zval_type_name(z_fd));
464464
fd = zval_get_long(z_fd);
465465
}
466+
/* fd must fit in an int and be positive */
467+
if (fd < 0 || fd > INT_MAX) {
468+
php_error_docref(NULL, E_WARNING, "Argument #1 ($file_descriptor) must be between 0 and %d", INT_MAX);
469+
RETURN_FALSE;
470+
}
466471
}
467472
#if defined(ZTS) && defined(HAVE_TTYNAME_R) && defined(_SC_TTY_NAME_MAX)
468473
buflen = sysconf(_SC_TTY_NAME_MAX);
@@ -510,6 +515,10 @@ PHP_FUNCTION(posix_isatty)
510515
}
511516
}
512517

518+
/* A valid file descriptor must fit in an int and be positive */
519+
if (fd < 0 || fd > INT_MAX) {
520+
RETURN_FALSE;
521+
}
513522
if (isatty(fd)) {
514523
RETURN_TRUE;
515524
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
posix_isatty(): errors for invalid file descriptors
3+
--EXTENSIONS--
4+
posix
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE != 8) die('skip C int is same size as zend_long');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$values = [
13+
-1,
14+
2**50+1,
15+
];
16+
17+
foreach ($values as $value) {
18+
var_dump(posix_isatty($value));
19+
}
20+
?>
21+
--EXPECT--
22+
bool(false)
23+
bool(false)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
posix_ttyname(): errors for invalid file descriptors
3+
--EXTENSIONS--
4+
posix
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE != 8) die('skip C int is same size as zend_long');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$values = [
13+
-1,
14+
2**50+1,
15+
];
16+
17+
foreach ($values as $value) {
18+
var_dump(posix_ttyname($value));
19+
}
20+
?>
21+
--EXPECTF--
22+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d
23+
bool(false)
24+
25+
Warning: posix_ttyname(): Argument #1 ($file_descriptor) must be between 0 and %d in %s on line %d
26+
bool(false)

0 commit comments

Comments
 (0)