Skip to content

Commit 998bce0

Browse files
committed
Fix GH-14695: Strictly validate invalid upload_max_filesize and post_max_size values
1 parent 9bc8b7e commit 998bce0

8 files changed

Lines changed: 153 additions & 32 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ PHP NEWS
3232
surrounding property access). (timwolla)
3333
. Fixed GH-22422 (zend_arena layout mismatch leaked memory in separately
3434
built extensions under AddressSanitizer). (iliaal)
35+
. Added validation for invalid upload_max_filesize and post_max_size values
36+
(GH-14695). (Weilin Du)
3537
. TSRM: use local-exec TLS in PIE executables. (henderkes)
3638
. perf: make all static extensions use TSRMG_STATIC. (henderkes)
3739
. Fixed bug GH-22257 (type confusion in Exception::getTraceAsString()).

‎Zend/tests/zend_ini/gh14695.phpt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
GH-14695: Invalid upload_max_filesize and post_max_size values are rejected
3+
--INI--
4+
upload_max_filesize=1zz
5+
post_max_size=bogus
6+
--FILE--
7+
<?php
8+
9+
var_dump(ini_get('upload_max_filesize'));
10+
var_dump(ini_get('post_max_size'));
11+
12+
?>
13+
--EXPECTF--
14+
Warning: Invalid "upload_max_filesize" setting. Invalid quantity "1zz": unknown multiplier "z" in %s on line %d
15+
Warning: Invalid "post_max_size" setting. Invalid quantity "bogus": no valid leading digits in %s on line %d
16+
string(2) "2M"
17+
string(2) "8M"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
GH-14695: Empty upload_max_filesize value is rejected
3+
--INI--
4+
upload_max_filesize=
5+
--FILE--
6+
<?php
7+
8+
var_dump(ini_get('upload_max_filesize'));
9+
10+
?>
11+
--EXPECTF--
12+
Warning: Invalid "upload_max_filesize" setting. Invalid quantity "": no valid leading digits in %s on line %d
13+
string(2) "2M"

‎Zend/zend_ini.c‎

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,12 @@ static const char *zend_ini_consume_quantity_prefix(const char *const digits, co
614614
return digits_consumed;
615615
}
616616

617-
static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zend_ini_parse_quantity_signed_result_t signed_result, zend_string **errstr) /* {{{ */
617+
static zend_ulong zend_ini_parse_quantity_internal(
618+
const zend_string *value,
619+
zend_ini_parse_quantity_signed_result_t signed_result,
620+
zend_string **errstr,
621+
bool strict
622+
) /* {{{ */
618623
{
619624
char *digits_end = NULL;
620625
const char *str = ZSTR_VAL(value);
@@ -634,6 +639,18 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
634639
while (digits < str_end && zend_is_whitespace(*(str_end-1))) {--str_end;}
635640

636641
if (digits == str_end) {
642+
if (strict) {
643+
if (ZSTR_LEN(value) == 0) {
644+
*errstr = zend_strpprintf(0, "Invalid quantity \"\": no valid leading digits");
645+
} else {
646+
smart_str_append_escaped(&invalid, ZSTR_VAL(value), ZSTR_LEN(value));
647+
smart_str_0(&invalid);
648+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits",
649+
ZSTR_VAL(invalid.s));
650+
smart_str_free(&invalid);
651+
}
652+
return 0;
653+
}
637654
*errstr = NULL;
638655
return 0;
639656
}
@@ -653,8 +670,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
653670
smart_str_append_escaped(&invalid, ZSTR_VAL(value), ZSTR_LEN(value));
654671
smart_str_0(&invalid);
655672

656-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits, interpreting as \"0\" for backwards compatibility",
657-
ZSTR_VAL(invalid.s));
673+
if (strict) {
674+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits",
675+
ZSTR_VAL(invalid.s));
676+
} else {
677+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits, interpreting as \"0\" for backwards compatibility",
678+
ZSTR_VAL(invalid.s));
679+
}
658680

659681
smart_str_free(&invalid);
660682
return 0;
@@ -690,8 +712,12 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
690712
base = 2;
691713
break;
692714
default:
693-
*errstr = zend_strpprintf(0, "Invalid prefix \"0%c\", interpreting as \"0\" for backwards compatibility",
694-
digits[1]);
715+
if (strict) {
716+
*errstr = zend_strpprintf(0, "Invalid prefix \"0%c\"", digits[1]);
717+
} else {
718+
*errstr = zend_strpprintf(0, "Invalid prefix \"0%c\", interpreting as \"0\" for backwards compatibility",
719+
digits[1]);
720+
}
695721
return 0;
696722
}
697723
digits += 2;
@@ -702,8 +728,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
702728
smart_str_append_escaped(&invalid, ZSTR_VAL(value), ZSTR_LEN(value));
703729
smart_str_0(&invalid);
704730

705-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no digits after base prefix, interpreting as \"0\" for backwards compatibility",
706-
ZSTR_VAL(invalid.s));
731+
if (strict) {
732+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no digits after base prefix",
733+
ZSTR_VAL(invalid.s));
734+
} else {
735+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no digits after base prefix, interpreting as \"0\" for backwards compatibility",
736+
ZSTR_VAL(invalid.s));
737+
}
707738

708739
smart_str_free(&invalid);
709740
return 0;
@@ -744,8 +775,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
744775
smart_str_append_escaped(&invalid, ZSTR_VAL(value), ZSTR_LEN(value));
745776
smart_str_0(&invalid);
746777

747-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits, interpreting as \"0\" for backwards compatibility",
748-
ZSTR_VAL(invalid.s));
778+
if (strict) {
779+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits",
780+
ZSTR_VAL(invalid.s));
781+
} else {
782+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": no valid leading digits, interpreting as \"0\" for backwards compatibility",
783+
ZSTR_VAL(invalid.s));
784+
}
749785

750786
smart_str_free(&invalid);
751787
return 0;
@@ -781,8 +817,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
781817
smart_str_append_escaped(&chr, str_end-1, 1);
782818
smart_str_0(&chr);
783819

784-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": unknown multiplier \"%s\", interpreting as \"%s\" for backwards compatibility",
785-
ZSTR_VAL(invalid.s), ZSTR_VAL(chr.s), ZSTR_VAL(interpreted.s));
820+
if (strict) {
821+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": unknown multiplier \"%s\"",
822+
ZSTR_VAL(invalid.s), ZSTR_VAL(chr.s));
823+
} else {
824+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": unknown multiplier \"%s\", interpreting as \"%s\" for backwards compatibility",
825+
ZSTR_VAL(invalid.s), ZSTR_VAL(chr.s), ZSTR_VAL(interpreted.s));
826+
}
786827

787828
smart_str_free(&invalid);
788829
smart_str_free(&interpreted);
@@ -815,8 +856,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
815856
smart_str_append_escaped(&chr, str_end-1, 1);
816857
smart_str_0(&chr);
817858

818-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\", interpreting as \"%s%s\" for backwards compatibility",
819-
ZSTR_VAL(invalid.s), ZSTR_VAL(interpreted.s), ZSTR_VAL(chr.s));
859+
if (strict) {
860+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": invalid characters before multiplier \"%s\"",
861+
ZSTR_VAL(invalid.s), ZSTR_VAL(chr.s));
862+
} else {
863+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\", interpreting as \"%s%s\" for backwards compatibility",
864+
ZSTR_VAL(invalid.s), ZSTR_VAL(interpreted.s), ZSTR_VAL(chr.s));
865+
}
820866

821867
smart_str_free(&invalid);
822868
smart_str_free(&interpreted);
@@ -833,8 +879,13 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
833879
/* Not specifying the resulting value here because the caller may make
834880
* additional conversions. Not specifying the allowed range
835881
* because the caller may do narrower range checks. */
836-
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": value is out of range, using overflow result for backwards compatibility",
837-
ZSTR_VAL(invalid.s));
882+
if (strict) {
883+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": value is out of range",
884+
ZSTR_VAL(invalid.s));
885+
} else {
886+
*errstr = zend_strpprintf(0, "Invalid quantity \"%s\": value is out of range, using overflow result for backwards compatibility",
887+
ZSTR_VAL(invalid.s));
888+
}
838889

839890
smart_str_free(&invalid);
840891
smart_str_free(&interpreted);
@@ -850,13 +901,25 @@ static zend_ulong zend_ini_parse_quantity_internal(const zend_string *value, zen
850901

851902
ZEND_API zend_long zend_ini_parse_quantity(const zend_string *value, zend_string **errstr) /* {{{ */
852903
{
853-
return (zend_long) zend_ini_parse_quantity_internal(value, ZEND_INI_PARSE_QUANTITY_SIGNED, errstr);
904+
return (zend_long) zend_ini_parse_quantity_internal(value, ZEND_INI_PARSE_QUANTITY_SIGNED, errstr, false);
854905
}
855906
/* }}} */
856907

857908
ZEND_API zend_ulong zend_ini_parse_uquantity(const zend_string *value, zend_string **errstr) /* {{{ */
858909
{
859-
return zend_ini_parse_quantity_internal(value, ZEND_INI_PARSE_QUANTITY_UNSIGNED, errstr);
910+
return zend_ini_parse_quantity_internal(value, ZEND_INI_PARSE_QUANTITY_UNSIGNED, errstr, false);
911+
}
912+
/* }}} */
913+
914+
ZEND_API zend_result zend_ini_parse_quantity_strict(const zend_string *value, zend_long *result, zend_string **errstr) /* {{{ */
915+
{
916+
zend_long retval = (zend_long) zend_ini_parse_quantity_internal(value, ZEND_INI_PARSE_QUANTITY_SIGNED, errstr, true);
917+
if (*errstr) {
918+
return FAILURE;
919+
}
920+
921+
*result = retval;
922+
return SUCCESS;
860923
}
861924
/* }}} */
862925

@@ -974,6 +1037,23 @@ ZEND_API ZEND_INI_MH(OnUpdateLong) /* {{{ */
9741037
}
9751038
/* }}} */
9761039

1040+
ZEND_API ZEND_INI_MH(OnUpdateLongStrict) /* {{{ */
1041+
{
1042+
zend_long tmp;
1043+
zend_string *errstr;
1044+
if (UNEXPECTED(zend_ini_parse_quantity_strict(new_value, &tmp, &errstr) == FAILURE)) {
1045+
zend_error(E_WARNING, "Invalid \"%s\" setting. %s", ZSTR_VAL(entry->name), ZSTR_VAL(errstr));
1046+
zend_string_release(errstr);
1047+
return FAILURE;
1048+
}
1049+
1050+
zend_long *p = ZEND_INI_GET_ADDR();
1051+
*p = tmp;
1052+
1053+
return SUCCESS;
1054+
}
1055+
/* }}} */
1056+
9771057
ZEND_API ZEND_INI_MH(OnUpdateLongGEZero) /* {{{ */
9781058
{
9791059
zend_long tmp = zend_ini_parse_quantity_warn(new_value, entry->name);

‎Zend/zend_ini.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ ZEND_API zend_long zend_ini_parse_quantity(const zend_string *value, zend_string
142142
*/
143143
ZEND_API zend_ulong zend_ini_parse_uquantity(const zend_string *value, zend_string **errstr);
144144

145+
/**
146+
* Strict variant of zend_ini_parse_quantity. Ill-formatted values fail instead
147+
* of returning a backwards-compatible interpretation.
148+
*/
149+
ZEND_API zend_result zend_ini_parse_quantity_strict(const zend_string *value, zend_long *result, zend_string **errstr);
150+
145151
ZEND_API zend_long zend_ini_parse_quantity_warn(const zend_string *value, zend_string *setting);
146152

147153
ZEND_API zend_ulong zend_ini_parse_uquantity_warn(const zend_string *value, zend_string *setting);
@@ -207,6 +213,7 @@ END_EXTERN_C()
207213
BEGIN_EXTERN_C()
208214
ZEND_API ZEND_INI_MH(OnUpdateBool);
209215
ZEND_API ZEND_INI_MH(OnUpdateLong);
216+
ZEND_API ZEND_INI_MH(OnUpdateLongStrict);
210217
ZEND_API ZEND_INI_MH(OnUpdateLongGEZero);
211218
ZEND_API ZEND_INI_MH(OnUpdateReal);
212219
/* char* versions */

‎ext/standard/http.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,17 @@ PHP_FUNCTION(http_build_query)
247247
}
248248
/* }}} */
249249

250-
static zend_result cache_request_parse_body_option(HashTable *options, zval *option, int cache_offset)
250+
static zend_result cache_request_parse_body_option(zval *option, const char *option_name, int cache_offset)
251251
{
252252
if (option) {
253253
zend_long result;
254254
ZVAL_DEREF(option);
255255
if (Z_TYPE_P(option) == IS_STRING) {
256256
zend_string *errstr;
257-
result = zend_ini_parse_quantity(Z_STR_P(option), &errstr);
258-
if (errstr) {
259-
zend_error(E_WARNING, "%s", ZSTR_VAL(errstr));
257+
if (UNEXPECTED(zend_ini_parse_quantity_strict(Z_STR_P(option), &result, &errstr) == FAILURE)) {
258+
zend_value_error("Invalid \"%s\" value in $options argument: %s", option_name, ZSTR_VAL(errstr));
260259
zend_string_release(errstr);
260+
return FAILURE;
261261
}
262262
} else if (Z_TYPE_P(option) == IS_LONG) {
263263
result = Z_LVAL_P(option);
@@ -290,7 +290,7 @@ static zend_result cache_request_parse_body_options(HashTable *options)
290290

291291
#define CHECK_OPTION(name) \
292292
if (zend_string_equals_literal_ci(key, #name)) { \
293-
if (cache_request_parse_body_option(options, value, REQUEST_PARSE_BODY_OPTION_ ## name) == FAILURE) { \
293+
if (cache_request_parse_body_option(value, #name, REQUEST_PARSE_BODY_OPTION_ ## name) == FAILURE) { \
294294
return FAILURE; \
295295
} \
296296
continue; \

‎ext/standard/tests/http/request_parse_body/multipart_options_invalid_quantity.phpt‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ request_parse_body() invalid quantity
33
--FILE--
44
<?php
55

6-
try {
7-
request_parse_body(options: [
8-
'upload_max_filesize' => '1GB',
9-
]);
10-
} catch (Throwable $e) {
11-
echo get_class($e), ': ', $e->getMessage(), "\n";
6+
foreach (['1GB', ''] as $value) {
7+
try {
8+
request_parse_body(options: [
9+
'upload_max_filesize' => $value,
10+
]);
11+
} catch (Throwable $e) {
12+
echo get_class($e), ': ', $e->getMessage(), "\n";
13+
}
1214
}
1315

1416
?>
1517
--EXPECTF--
16-
Warning: Invalid quantity "1GB": unknown multiplier "B", interpreting as "1" for backwards compatibility in %s on line %d
17-
RequestParseBodyException: Request does not provide a content type
18+
ValueError: Invalid "upload_max_filesize" value in $options argument: Invalid quantity "1GB": unknown multiplier "B"
19+
ValueError: Invalid "upload_max_filesize" value in $options argument: Invalid quantity "": no valid leading digits

‎main/main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ PHP_INI_BEGIN()
845845
STD_PHP_INI_ENTRY("open_basedir", NULL, PHP_INI_ALL, OnUpdateBaseDir, open_basedir, php_core_globals, core_globals)
846846

847847
STD_PHP_INI_BOOLEAN("file_uploads", "1", PHP_INI_SYSTEM, OnUpdateBool, file_uploads, php_core_globals, core_globals)
848-
STD_PHP_INI_ENTRY("upload_max_filesize", "2M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLong, upload_max_filesize, php_core_globals, core_globals)
849-
STD_PHP_INI_ENTRY("post_max_size", "8M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLong, post_max_size, sapi_globals_struct,sapi_globals)
848+
STD_PHP_INI_ENTRY("upload_max_filesize", "2M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongStrict, upload_max_filesize, php_core_globals, core_globals)
849+
STD_PHP_INI_ENTRY("post_max_size", "8M", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongStrict, post_max_size, sapi_globals_struct,sapi_globals)
850850
STD_PHP_INI_ENTRY("upload_tmp_dir", NULL, PHP_INI_SYSTEM, OnUpdateStringUnempty, upload_tmp_dir, php_core_globals, core_globals)
851851
STD_PHP_INI_ENTRY("max_input_nesting_level", "64", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongGEZero, max_input_nesting_level, php_core_globals, core_globals)
852852
STD_PHP_INI_ENTRY("max_input_vars", "1000", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateLongGEZero, max_input_vars, php_core_globals, core_globals)

0 commit comments

Comments
 (0)