Skip to content
Open
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 Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -5301,3 +5301,15 @@ ZEND_API zend_result zend_get_default_from_internal_arg_info(
#endif
return get_default_via_ast(default_value_zval, ZSTR_VAL(default_value));
}

ZEND_API bool zend_validate_file_permissions(zend_long mode, uint32_t arg_num
) {
if (mode < 0 || (mode & ~07777)) {
zend_argument_value_error(
arg_num,
"must be between 0 and 0o7777"
);
return false;
}
return true;
}
2 changes: 2 additions & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,8 @@ static zend_always_inline bool zend_parse_arg_obj_or_str(
return zend_parse_arg_str(arg, destination_string, allow_null, arg_num);
}

ZEND_API bool zend_validate_file_permissions(zend_long mode, uint32_t arg_num);

END_EXTERN_C()

#endif /* ZEND_API_H */
4 changes: 4 additions & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@ PHP_FUNCTION(mkdir)
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();

if (!zend_validate_file_permissions(mode, 2)) {
RETURN_THROWS();
}

context = php_stream_context_from_zval(zcontext, 0);

RETURN_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context));
Expand Down
34 changes: 34 additions & 0 deletions ext/standard/tests/file/mkdir_invalid_mode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
mkdir(): invalid mode
--FILE--
<?php
Copy link
Member

Choose a reason for hiding this comment

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

add few more cases (e.g. -1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

$testCases = [
1000000, // way too large
-1, // negative
0o10000, // above 0o7777
0x1FFFF, // hex, still too large
12345, // arbitrary invalid
];

foreach ($testCases as $mode) {
try {
echo "Testing mode: $mode\n";
mkdir(__DIR__ . "/testdir_$mode", $mode);
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
} catch (Exception $e) {
echo "Other exception: ", $e->getMessage(), PHP_EOL;
}
}
?>
--EXPECT--
Testing mode: 1000000
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: -1
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 4096
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 131071
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 12345
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Loading