Skip to content

Commit ab837a6

Browse files
Fixing potential memory leak with encoded in password_hash
Using zend_string_alloc instead of char* for out and encoded variables
1 parent 9872208 commit ab837a6

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

ext/standard/password.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ static php_password_algo php_password_determine_algo(const char *hash, const siz
8383
return PHP_PASSWORD_BCRYPT;
8484
}
8585
#if HAVE_ARGON2LIB
86-
if (hash[0] == '$' && strstr(hash, "argon2i")) {
87-
return PHP_PASSWORD_ARGON2I;
88-
} else if (hash[0] == '$' && strstr(hash, "argon2d")) {
86+
if (len >= sizeof("$argon2i$")-1 && !memcmp(hash, "$argon2i$", sizeof("$argon2i$")-1)) {
87+
return PHP_PASSWORD_ARGON2I;
88+
} else if (len >= sizeof("$argon2d$")-1 && !memcmp(hash, "$argon2d$", sizeof("$argon2d$")-1)) {
8989
return PHP_PASSWORD_ARGON2D;
9090
}
9191
#endif
@@ -549,9 +549,6 @@ PHP_FUNCTION(password_hash)
549549
case PHP_PASSWORD_ARGON2I:
550550
case PHP_PASSWORD_ARGON2D:
551551
{
552-
char *out;
553-
char *encoded;
554-
555552
size_t out_len = 32;
556553
size_t encoded_len;
557554
int status = 0;
@@ -564,8 +561,8 @@ PHP_FUNCTION(password_hash)
564561
out_len
565562
);
566563

567-
encoded = emalloc(encoded_len + 1);
568-
out = emalloc(out_len + 1);
564+
zend_string *out = zend_string_alloc(out_len, 0);
565+
zend_string *encoded = zend_string_alloc(encoded_len, 0);
569566

570567
status = argon2_hash(
571568
t_cost,
@@ -575,26 +572,24 @@ PHP_FUNCTION(password_hash)
575572
password_len,
576573
salt,
577574
salt_len,
578-
out,
575+
out->val,
579576
out_len,
580-
encoded,
577+
encoded->val,
581578
encoded_len,
582579
type,
583580
ARGON2_VERSION_NUMBER
584581
);
585582

586-
zend_string *ret = zend_string_init(encoded, encoded_len, 0);
587-
588583
efree(out);
589584
efree(salt);
590-
efree(encoded);
591585

592586
if (status != ARGON2_OK) {
587+
efree(encoded);
593588
php_error_docref(NULL, E_WARNING, argon2_error_message(status));
594589
RETURN_FALSE;
595590
}
596591

597-
RETURN_STR(ret);
592+
RETURN_STR(encoded);
598593
}
599594
break;
600595
#endif

0 commit comments

Comments
 (0)