[Cryptography] defending against common errors

Ray Dillinger bear at sonic.net
Wed Sep 21 19:09:46 EDT 2016


Okay, finally I have something definite about making assignments with
volatile semantics (assignments that won't be optimized away under any
circumstances) on objects that were allocated non-volatile.

And there is GOOD news!  There IS a way to guarantee it!

This is based on section 6.7.3 of the C language specification rationale
document.

Assignment when treated as a primary expression simply returns the value
assigned, and casting that value does not affect the assignment operation.

If you have a non-volatile variable 'identifier' of type 'THING' then

(volatile THING) identifier = 0;

WILL NOT(!!) impose volatile semantics on the assignment.  It says, in
English, "Carry out the assignment normally, and then return the result
of the assignment expression as though that result were volatile."  In
this case the cast is a no-op and unless the compiler writer is just
being nice to you, it can be ignored.

But by using the address-of (&) operation as a primary expression, you
can say

* (volatile THING_T *) &identifier = 0;

Or using the variable reference itself as the primary expression, you
can say

((volatile THING) identifier) = 0;

This WILL impose volatile semantics on the assignment.  These say, in
English, "Make an assignment to memory at this location, as though the
memory at this location contains a volatile variable", or "make an
assignment to this variable as though it were a volatile variable,"
respectively.

This is so easy to get wrong, so easy for someone cleaning up
'superflous parens' or 'convoluted expressions' to accidentally destroy
without even a second thought, so easy for a saboteur to screw up,
likely to go long undetected, and even when/if found he could credibly
claim it was an accident ....

There's a certain kind of poisonous purity about it. I've rarely seen
such a well-polished, efficient, and smoothly functioning footgun.

				Bear

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://www.metzdowd.com/pipermail/cryptography/attachments/20160921/c86ec0f2/attachment.sig>


More information about the cryptography mailing list