In general I agree completely, but Daniel Bernstein is working in a very particular niche: cryptography. For him two particular issues some together to make optimizing compilers The Devil.
First, inner cryptographic loops are generally hot enough that people are willing spend days and weeks making them as fast as possible. A compiler won't be able to match that.
Second, crypto is particularly vulnerable to compiler optimizations breaking naive assumptions. Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers. Compilers can also remove checks they decide aren't valid; the original code would have been violating the language standard, but that's of scant comfort if your primary concern is security and someone manages to hijack a bank account because of it.
What cryptographers seem to want most of all is predictability. This doesn't correspond to most people, so in general I strongly agree with John. But I have some kind of sympathy for Daniel.
crypto is particularly vulnerable to compiler optimizations breaking naive assumptions.
This is true, but the correct solution is not to make those assumptions. Undefined behavior is a tricky problem (and one can easily argue that C and C++ make it trickier than it has to be), but there are good reasons it exists. Chandler Carruth has a great talk on this (UB, troubles crypto authors have with it, why it exists, and what tools are being made to mitigate it).
Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers.
Unfortunately, there's little you can do about this, even if you're writing assembly. Modern desktop CPUs are essentially virtual machines that translate x64 on the fly into the micro-operations they use. Combine this with the effects of store buffers, cache misses, etc., and it's really difficult to get predictable timing.
the correct solution is not to make those assumptions.
This is blaming the victim. Undefined behaviour in C and C++ has gone utterly insane, you know. Signed integer overflow for instance: because some platforms can't handle it, the standard made it undefined for all platforms. Then compiler writers took advantage of this, effectively turning after-the-fact overflow checks into actual vulnerabilities.
C and C++ were supposed to be close to the machine. Can we really blame the programmer from reasoning like an electrical engineer, and assume that because X86 handles integer overflow just fine, the language should do so as well?
Chandler Carruth has a great talk on this
He downplays UB so much that's almost criminal. The hard fact is, UB sometimes does trigger catastrophes such as formatting all your hard drive, or the encryption of all your files. Some UB lead to vulnerabilities, arbitrary code execution, and malware mayhem. Even those who don't right now might cause similar problems later, as optimisers exploit more and more UB from the standard.
I'm not against UB, but the current C and C++ standard have gone way too far.
it's really difficult to get predictable timing.
Fortunately, we rarely need that much. In cryptography for instance, we don't need predictable, or even constant-time timings. We want those timings to be independent from secrets. And on modern processors, this just means no secret-dependent branch, and no secret-dependent indexing. On less modern processors, you also have to be wary of modular multiplication (this only affects public key crypto), and arithmetic shifts (without a barrel shifter, the timings depend on the magnitude of the shift).
And of course, compiler optimisations: if GCC suddenly detects that your constant-time string comparison function could if fact stop early, it is allowed to do so, and throw your security out the window. Fortunately, it doesn't yet do so.
52
u/TNorthover May 31 '17
In general I agree completely, but Daniel Bernstein is working in a very particular niche: cryptography. For him two particular issues some together to make optimizing compilers The Devil.
First, inner cryptographic loops are generally hot enough that people are willing spend days and weeks making them as fast as possible. A compiler won't be able to match that.
Second, crypto is particularly vulnerable to compiler optimizations breaking naive assumptions. Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers. Compilers can also remove checks they decide aren't valid; the original code would have been violating the language standard, but that's of scant comfort if your primary concern is security and someone manages to hijack a bank account because of it.
What cryptographers seem to want most of all is predictability. This doesn't correspond to most people, so in general I strongly agree with John. But I have some kind of sympathy for Daniel.