Skip to content

Commit 588db7c

Browse files
committed
Always use ZEND_SECURE_ZERO() when cleaning up data
Optimizing compilers have an annoying tendency to throw out memsets over data that they think aren't used anymore. Apply secure zero-out in cases where this has potential to happen.
1 parent 731eeb8 commit 588db7c

File tree

5 files changed

+7
-5
lines changed

5 files changed

+7
-5
lines changed

ext/hash/hash_sha3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void PHP_SHA3_Final(unsigned char* digest,
191191
}
192192

193193
// Zero out context
194-
memset(ctx, 0, sizeof(PHP_SHA3_CTX));
194+
ZEND_SECURE_ZERO(ctx, sizeof(PHP_SHA3_CTX));
195195
}
196196

197197
// ==========================================================================

ext/hash/hash_snefru.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char
129129
((input[i+2] & 0xff) << 8) | (input[i+3] & 0xff);
130130
}
131131
Snefru(context->state);
132-
memset(&context->state[8], 0, sizeof(uint32_t) * 8);
132+
ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8);
133133
}
134134

135135
PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context)

ext/mcrypt/mcrypt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ PHP_FUNCTION(mcrypt_generic_init)
605605
}
606606
RETVAL_LONG(result);
607607

608+
ZEND_SECURE_ZERO(key_s, key_len);
609+
ZEND_SECURE_ZERO(iv_s, iv_len);
608610
efree(iv_s);
609611
efree(key_s);
610612
}

ext/standard/php_crypt_r.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ char * php_md5_crypt_r(const char *pw, const char *salt, char *out)
364364
PHP_MD5Update(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
365365

366366
/* Don't leave anything around in vm they could use. */
367-
memset(final, 0, sizeof(final));
367+
ZEND_SECURE_ZERO(final, sizeof(final));
368368

369369
/* Then something really weird... */
370370
for (i = pwl; i != 0; i >>= 1)

ext/standard/sha1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
245245

246246
/* Zeroize sensitive information.
247247
*/
248-
memset((unsigned char*) context, 0, sizeof(*context));
248+
ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
249249
}
250250
/* }}} */
251251

@@ -356,7 +356,7 @@ const unsigned char block[64];
356356
state[4] += e;
357357

358358
/* Zeroize sensitive information. */
359-
memset((unsigned char*) x, 0, sizeof(x));
359+
ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
360360
}
361361
/* }}} */
362362

0 commit comments

Comments
 (0)