[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

When scrubbing secrets in memory doesn't work

During the Windows Security Push in Feb/Mar 2002, we noticed an
'interesting' anomaly with code to scrub passwords that looks like this:

bool DoSensitiveStuff() {
	bool fOK = false;
	const size_t cbPwd = 64;
	char szPwd[cbPwd];
	if (GetUserPassword(szPwd,cbPwd-1)) 
		if (DoSomethingWithPassword(szPwd))
			fOK = true;		


	return fOK;	

On the surface, this looks fine, until you look at the ASM output, and
you see the call to memset has been removed by the optimizer because
szPwd is not read once the function completes. Hence, the secret data is
still floating in memory.

This optimization, common in most modern C/C++ compilers is often
referred to as "dead store removal."

A full write-up outlining the issue in more detail, as well as some
remedies is at

Cheers, Michael Howard
Secure Windows Initiative
Microsoft Corp.

Writing Secure Code