Skip to content

Fix GH-16809: fopen HTTP wrapper timeout stream context option overflow. #16810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,

if (context && (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "timeout")) != NULL) {
double d = zval_get_double(tmpzval);
#ifndef PHP_WIN32
const double timeoutmax = (double) PHP_TIMEOUT_ULL_MAX / 1000000.0;
#else
const double timeoutmax = (double) LONG_MAX / 1000000.0;
#endif

if (d > timeoutmax) {
php_stream_wrapper_log_error(wrapper, options, "timeout must be lower than " ZEND_ULONG_FMT, (zend_ulong)timeoutmax);
zend_string_release(transport_string);
php_url_free(resource);
return NULL;
}
#ifndef PHP_WIN32
timeout.tv_sec = (time_t) d;
timeout.tv_usec = (size_t) ((d - timeout.tv_sec) * 1000000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size_t cast looks wrong. timeout.tv_usec is an suseconds_t which is according to POSIX:

The type suseconds_t shall be a signed integer type capable of storing values at least in the range [-1, 1000000].

Probably okay to leave this for stable branches, but I suggest to change to (suseconds_t) in master.

The (long) casts on Windows are, unfortunately, correct. From WinSock2.h:

struct timeval {
        long    tv_sec;         /* seconds */
        long    tv_usec;        /* and microseconds */
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I would definitively keep this existing code untouched in stable branch.

Expand Down
26 changes: 26 additions & 0 deletions ext/standard/tests/http/gh16810.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Bug #79265 variation: "host:" not at start of header
--INI--
allow_url_fopen=1
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
--FILE--
<?php
$uri = "http://www.example.com";
$config = [
'http' => [
'timeout' => PHP_INT_MIN,
],
];
$ctx = stream_context_create($config);
var_dump(fopen($uri, "r", false, $ctx));

$config['http']['timeout'] = PHP_INT_MAX;
$ctx = stream_context_create($config);
var_dump(fopen($uri, "r", false, $ctx));
?>
--EXPECTF--
resource(%d) of type (stream)

Warning: fopen(http://www.example.com): Failed to open stream: timeout must be lower than %d in %s on line %d
bool(false)
Loading