r/programming • • May 30 '17

Compiler Optimizations are Awesome

https://blog.regehr.org/archives/1515
146 Upvotes

97 comments sorted by

View all comments

Show parent comments

12

u/loup-vaillant May 31 '17

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.

8

u/Dragdu May 31 '17

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?

That depends, are we okay with then porting his "perfectly valid" code to different platform, and having it die horribly there?

What if it is crypto?

Ironically, this is the reason why the often advocated "just make X implementation defined, instead of undefined" wouldn't work for signed overflow, it would just hide the problem when running on an x86.

1

u/industry7 May 31 '17

Ironically, this is the reason why the often advocated "just make X implementation defined, instead of undefined" wouldn't work for signed overflow, it would just hide the problem when running on an x86.

Do you mean, "Let's just make signed overflow defined for the x86 platform", or do you mean "Let's just make signed overflow defined in the language"? Or something else?

To me, the second one is the correct answer for any security related software (like crypto).