Skip to content

Commit cd9a376

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) (cherry picked from commit 4a7ceb9) (cherry picked from commit 7471009) (cherry picked from commit d22d9eb)
1 parent 035bc48 commit cd9a376

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
@@ -282,6 +282,11 @@ PHP_FUNCTION(password_hash)
282282
cost = zval_get_long(option_buffer);
283283
}
284284

285+
if (memchr(password, '\0', password_len)) {
286+
php_error_docref(NULL, E_WARNING, "Bcrypt password must not contain null character");
287+
RETURN_NULL();
288+
}
289+
285290
if (cost < 4 || cost > 31) {
286291
php_error_docref(NULL, E_WARNING, "Invalid bcrypt cost parameter specified: " ZEND_LONG_FMT, cost);
287292
RETURN_NULL();

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)