Skip to content

Commit bc4f2b2

Browse files
committed
Fix GH-14780: p(f)sockopen overflow on timeout argument.
1 parent cd67080 commit bc4f2b2

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

ext/standard/fsock.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
7373
}
7474

7575
/* prepare the timeout value for use */
76+
if (UNEXPECTED(!(timeout >= 0 && timeout <= LONG_MAX / 1000000))) {
77+
if (port > 0) {
78+
efree(hostname);
79+
}
80+
if (hashkey) {
81+
efree(hashkey);
82+
}
83+
zend_argument_value_error(6, "must be between 0 and " ZEND_LONG_FMT, (LONG_MAX / 1000000));
84+
RETURN_THROWS();
85+
}
7686
#ifndef PHP_WIN32
7787
conv = (time_t) (timeout * 1000000.0);
78-
tv.tv_sec = conv / 1000000;
7988
#else
8089
conv = (long) (timeout * 1000000.0);
81-
tv.tv_sec = conv / 1000000;
8290
#endif
91+
tv.tv_sec = conv / 1000000;
8392
tv.tv_usec = conv % 1000000;
8493

8594
stream = php_stream_xport_create(hostname, hostname_len, REPORT_ERRORS,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-14780: p(f)sockopen overflow on timeout.
3+
--FILE--
4+
<?php
5+
$code = null;
6+
$err = null;
7+
try {
8+
pfsockopen('udp://127.0.0.1', '63844', $code, $err, (PHP_INT_MAX/1000000)+1);
9+
} catch (\ValueError $e) {
10+
echo $e->getMessage() . PHP_EOL;
11+
}
12+
try {
13+
pfsockopen('udp://127.0.0.1', '63844', $code, $err, (PHP_INT_MIN/1000000)-1);
14+
} catch (\ValueError $e) {
15+
echo $e->getMessage();
16+
}
17+
--EXPECTF--
18+
pfsockopen(): Argument #6 must be between 0 and %s
19+
pfsockopen(): Argument #6 must be between 0 and %s

0 commit comments

Comments
 (0)