Skip to content

Fix a few external crypt() issues on musl #16702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2024
Merged
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
14 changes: 13 additions & 1 deletion ext/standard/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,19 @@ PHPAPI zend_string *php_crypt(const char *password, const int pass_len, const ch

if (!crypt_res || (salt[0] == '*' && salt[1] == '0')) {
return NULL;
} else {
}
else if (!strcmp(crypt_res, "*")) {
/* Musl crypt() uses "*" as a failure token rather
* than the "*0" that libxcrypt/PHP use. Our test
* suite in particular looks for "*0" in a few places,
* and it would be annoying to handle both values
* explicitly. It seems wise to abstract this detail
* from the end user: if it's annoying for us, imagine
* how annoying it would be in end-user code; not that
* anyone would think of it. */
return NULL;
}
else {
result = zend_string_init(crypt_res, strlen(crypt_res), 0);
return result;
}
Expand Down
7 changes: 5 additions & 2 deletions ext/standard/tests/crypt/des_fallback_invalid_salt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ Test DES with invalid fallback
--FILE--
<?php

var_dump(crypt("test", "$#"));
var_dump(crypt("test", "$5zd$01"));
// musl tries to support invalid DES salts unless they would violate
// the /etc/passwd format, so we include colons to ensure that musl
// crypt() will fail on these inputs as well.
var_dump(crypt("test", "$:#"));
var_dump(crypt("test", "$:5zd$01\n"));

?>
--EXPECT--
Expand Down
8 changes: 3 additions & 5 deletions ext/standard/tests/strings/crypt_sha256.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ $tests = array(
'a short string',
'$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/cZKmF/wJvD'
),

// The "too many rounds" behavior depends on the crypt()
// implementation, but for now everyone agrees on what to do.
8 => array(
'$5$rounds=10$roundstoolow',
'the number of rounds is too low',
'*0'
),
9 => array(
'$5$rounds=1000000000$roundstoohigh',
'the number of rounds is too high',
'*0'
Expand Down