Skip to content

Commit be41c36

Browse files
authored
ext/phar: harden OpenSSL signature handling in util.c. (#22174)
Use size_t in phar_hex_str to avoid signed integer overflow when hex-encoding an attacker-controlled signature length, and fail verification in phar_call_openssl_verify when the stream read is short rather than proceeding over a truncated buffer.
1 parent d61ff7c commit be41c36

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

ext/phar/util.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,18 +1326,18 @@ phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, si
13261326

13271327
static const char hexChars[] = "0123456789ABCDEF";
13281328

1329-
static int phar_hex_str(const char *digest, size_t digest_len, char **signature) /* {{{ */
1329+
static size_t phar_hex_str(const char *digest, size_t digest_len, char **signature) /* {{{ */
13301330
{
1331-
int pos = -1;
1331+
size_t pos = 0;
13321332
size_t len = 0;
13331333

13341334
*signature = (char*)safe_pemalloc(digest_len, 2, 1, PHAR_G(persist));
13351335

13361336
for (; len < digest_len; ++len) {
1337-
(*signature)[++pos] = hexChars[((const unsigned char *)digest)[len] >> 4];
1338-
(*signature)[++pos] = hexChars[((const unsigned char *)digest)[len] & 0x0F];
1337+
(*signature)[pos++] = hexChars[((const unsigned char *)digest)[len] >> 4];
1338+
(*signature)[pos++] = hexChars[((const unsigned char *)digest)[len] & 0x0F];
13391339
}
1340-
(*signature)[++pos] = '\0';
1340+
(*signature)[pos] = '\0';
13411341
return pos;
13421342
}
13431343
/* }}} */
@@ -1363,7 +1363,7 @@ ZEND_ATTRIBUTE_NONNULL static bool phar_call_openssl_verify(
13631363
php_stream_rewind(fp);
13641364
zend_string *str = php_stream_copy_to_mem(fp, (size_t) end, false);
13651365
/* No content thus signing must fail */
1366-
if (UNEXPECTED(str == NULL)) {
1366+
if (UNEXPECTED(str == NULL || (size_t)end != ZSTR_LEN(str))) {
13671367
return false;
13681368
}
13691369

0 commit comments

Comments
 (0)