Skip to content

Commit 4a7ceb9

Browse files
bukkaremicollet
authored andcommitted
Fix bug GHSA-q6x7-frmf-grcw: password_verify can erroneously return true
Disallow null character in bcrypt password (cherry picked from commit 0ba5229a3f7572846e91c8f5382e87785f543826) (cherry picked from commit 81794c7)
1 parent dcdd49e commit 4a7ceb9

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

ext/standard/password.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ static zend_string* php_password_bcrypt_hash(const zend_string *password, zend_a
260260
zval *zcost;
261261
zend_long cost = PHP_PASSWORD_BCRYPT_COST;
262262

263+
if (memchr(ZSTR_VAL(password), '\0', ZSTR_LEN(password))) {
264+
php_error_docref(NULL, E_WARNING, "Bcrypt password must not contain null character");
265+
return NULL;
266+
}
267+
263268
if (options && (zcost = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
264269
cost = zval_get_long(zcost);
265270
}

ext/standard/tests/password/password_bcrypt_errors.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var_dump(password_hash("foo", PASSWORD_BCRYPT, array("salt" => 123)));
1616

1717
var_dump(password_hash("foo", PASSWORD_BCRYPT, array("cost" => "foo")));
1818

19+
var_dump(password_hash("null\0password", PASSWORD_BCRYPT));
20+
1921
?>
2022
--EXPECTF--
2123
Warning: password_hash(): Invalid bcrypt cost parameter specified: 3 in %s on line %d
@@ -41,3 +43,7 @@ NULL
4143

4244
Warning: password_hash(): Invalid bcrypt cost parameter specified: 0 in %s on line %d
4345
NULL
46+
47+
Warning: password_hash(): Bcrypt password must not contain null character in %s on line %d
48+
NULL
49+

0 commit comments

Comments
 (0)