News for hackers. Stuff that matters.
Re:TrueCrypt Security Assessment [pdf] by doe88 on Monday April 14, 2014 @10:52AM (#7586468)
> Issue 4: Windows kernel driver uses memset() to clear sensitive data

> Calls to memset() run the risk of being optimized out by the compiler.

I would be curious to know which compilers or which options actually still do these ill-fated optimizations?

Re:TrueCrypt Security Assessment [pdf] by 30thElement on Monday April 14, 2014 @11:48AM (#7586858)
They should do it only in situations where it doesn't change the program behavior. I use memset frequently in C just to be safe, but if it's written to later on before it's ever read from, the compiler can optimize that away. I'm guessing their recommendation here is if you did something like

    char* plain_text = malloc(size);
    ///do stuff with plain_text
    memset(plain_text, 0, size);
    free(plain_text);
For most programs that last memset is unnecessary (and may even be unnecessary according to the standard, but it's probably implementation defined, not undefined behavior) and it makes sense for the compiler to optimize it away. But for crypto purposes you have to be afraid of someone being able to read plain_text later, so the memset is important
Re:TrueCrypt Security Assessment [pdf] by Marat_Dukhan on Monday April 14, 2014 @08:38PM (#7589711)
I checked 3 compilers: icc 14, gcc 4.8, and clang 3.3. Clang is the only one which optimized the memset away.
Re:TrueCrypt Security Assessment [pdf] by lawnchair_larry on Monday April 14, 2014 @09:46PM (#7589890)
Did you enable optimization? I know first hand that GCC will optimize something similar out in many cases.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8537

Re:TrueCrypt Security Assessment [pdf] by Marat_Dukhan on Tuesday April 15, 2014 @06:30AM (#7591126)
Yes, I compiled with -O3.
Re:TrueCrypt Security Assessment [pdf] by timtadh on Monday April 14, 2014 @09:30PM (#7589845)
Not a windows user so I can't check but given that it is the kernel driver the compiler we would be concerned about is the MS visual c++ compiler.
Re:TrueCrypt Security Assessment [pdf] by Marat_Dukhan on Tuesday April 15, 2014 @06:33AM (#7591130)
Visual C++ 2013 doesn't remove memset either.
Re:TrueCrypt Security Assessment [pdf] by shin_lao on Monday April 14, 2014 @01:55PM (#7587709)
It is not advised to use C library functions. The driver should use RtlZeroMemory() and in that case RtlSecureZeroMemory(), but otherwise this optimization makes a lot of sense in many cases when you have for example an useless constructor call before an initialization.
Re:TrueCrypt Security Assessment [pdf] by munin on Monday April 14, 2014 @11:24AM (#7586668)
they still do, but everyone (including the windows DDK) offers a "secure memset" that casts to volatile and preserves the call. in Windows, it's called RtlSecureZeroMemory and I would think that know about this would be one of the pre-requisites for writing security software on that platform...
Re:TrueCrypt Security Assessment [pdf] by lawnchair_larry on Monday April 14, 2014 @09:42PM (#7589875)
All of them will under circumstances that tend to manifest when wiping secrets. This surprises a lot of developers, but it's a very common finding when auditing crypto code or other secret storage.

It's typically done when you don't need the value anymore, of course. So, the compiler sees a write to memory that is never used. Which is exactly the type of thing compilers try to optimize out (if it can be proven that it is never used).

From the C99 standard:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

Re:TrueCrypt Security Assessment [pdf] by doe88 on Monday April 14, 2014 @06:04PM (#7589223)
After a little bit of digging seems to be the best way to currently wipe out sensitive data is to implement something like in libsodium https://github.com/jedisct1/libsodium/blob/master/src/libsod...

1- Try to use SecureZeroMemory() on Windows

2- Use memcpy_s() if available, which from its manpage is guaranteed not to be optimized away by compilers

3- Use a loop to manually zero-out the buffer after having casted to a volatile pointer, again to prevent compiler optimizations

Re:TrueCrypt Security Assessment [pdf] by cliffbean on Monday April 14, 2014 @11:15AM (#7586602)
Pretty much all of them do. Many of the big leaders in the C++ world, like Google, are pushing the idea that every last optimization technique is crucially vital. Google can bank on tenths-of-a-percent of performance, so why can't you? Google can devote armadas of armadas of test machines to run asan/tsan/space-alien-san and everything else continuously, and it works pretty well for them, so why can't you do this too?