Skip to content
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
17 changes: 13 additions & 4 deletions ext/ftp/php_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "ftp.h"
#include "ftp_arginfo.h"

#define PHP_FTP_TIMEOUT_SEC_MAX ((uint64_t)((double) PHP_TIMEOUT_ULL_MAX / 1000000.0))

static zend_class_entry *php_ftp_ce = NULL;
static zend_object_handlers ftp_object_handlers;

Expand Down Expand Up @@ -147,15 +149,13 @@ PHP_FUNCTION(ftp_connect)
RETURN_THROWS();
}

const uint64_t timeoutmax = (uint64_t)((double) PHP_TIMEOUT_ULL_MAX / 1000000.0);

if (timeout_sec <= 0) {
zend_argument_value_error(3, "must be greater than 0");
RETURN_THROWS();
}

if (timeout_sec >= timeoutmax) {
zend_argument_value_error(3, "must be less than " ZEND_ULONG_FMT, timeoutmax);
if (timeout_sec >= PHP_FTP_TIMEOUT_SEC_MAX) {
zend_argument_value_error(3, "must be less than " ZEND_ULONG_FMT, PHP_FTP_TIMEOUT_SEC_MAX);
RETURN_THROWS();
}

Expand Down Expand Up @@ -196,6 +196,11 @@ PHP_FUNCTION(ftp_ssl_connect)
RETURN_THROWS();
}

if (timeout_sec >= PHP_FTP_TIMEOUT_SEC_MAX) {
zend_argument_value_error(3, "must be less than " ZEND_ULONG_FMT, PHP_FTP_TIMEOUT_SEC_MAX);
RETURN_THROWS();
}

/* connect */
if (!(ftp = ftp_open(host, (short)port, timeout_sec))) {
RETURN_FALSE;
Expand Down Expand Up @@ -1275,6 +1280,10 @@ PHP_FUNCTION(ftp_set_option)
zend_argument_value_error(3, "must be greater than 0 for the FTP_TIMEOUT_SEC option");
RETURN_THROWS();
}
if ((uint64_t) Z_LVAL_P(z_value) >= PHP_FTP_TIMEOUT_SEC_MAX) {
zend_argument_value_error(3, "must be less than " ZEND_ULONG_FMT " for the FTP_TIMEOUT_SEC option", PHP_FTP_TIMEOUT_SEC_MAX);
RETURN_THROWS();
}
ftp->timeout_sec = Z_LVAL_P(z_value);
RETURN_TRUE;
break;
Expand Down
26 changes: 26 additions & 0 deletions ext/ftp/tests/gh20601_set_option.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-20601 (ftp_set_option FTP_TIMEOUT_SEC timeout overflow)
--EXTENSIONS--
ftp
pcntl
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip: 64-bit only");
if (PHP_OS_FAMILY === 'Windows') die("skip not for windows");
?>
--FILE--
<?php
require 'server.inc';

$ftp = ftp_connect('127.0.0.1', $port);
$ftp or die("Couldn't connect to the server");
ftp_login($ftp, 'user', 'pass');

try {
ftp_set_option($ftp, FTP_TIMEOUT_SEC, PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
ftp_set_option(): Argument #3 ($value) must be less than %d for the FTP_TIMEOUT_SEC option
21 changes: 21 additions & 0 deletions ext/ftp/tests/gh20601_ssl.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
GH-20601 (ftp_ssl_connect timeout overflow)
--EXTENSIONS--
ftp
openssl
--SKIPIF--
<?php
if (!function_exists("ftp_ssl_connect")) die("skip ftp_ssl is disabled");
if (PHP_INT_SIZE != 8) die("skip: 64-bit only");
if (PHP_OS_FAMILY === 'Windows') die("skip not for windows");
?>
--FILE--
<?php
try {
ftp_ssl_connect('127.0.0.1', 1024, PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
ftp_ssl_connect(): Argument #3 ($timeout) must be less than %d
Loading