Can someone explain this line: static void * (* const volatile memset_ptr)(void *, int, size_t) = memset; I've written some C but that is utter gibberish to me.
As usually in C, you start from the name, then try to go to the right until you hit parenthesis, then go to the left until parenthesis, rinse and repeat. So, first, the name: memset_ptr Then, try going to the right, but aha! before we can really gain speed, a parenthesis is immediately blocking us. We shrug off the bruises and turn to the left: (* const volatile memset_ptr) "Hey guys, memset_ptr is a volatile const p…
How to zero a buffer
151–160 of 216 posts
Re: How to zero a buffer
#152Earlier quoted context omitted.
I didn't mean to imply that the solution as presented didn't work, I was just wondering if it would also work simply running the cipher with the zeroed key in order to avoid zeroing the key being optimized away. Obviously that'd be a lot more cycles; I'm just curious if it would be a viable solution ;-)
I was referring to the first sentence ("It is a little mind boggling that support for proper handling of this didn't arrive until c11."). I don't see anything C11-specific in the code Percival posted. I don't know enough to say anything useful about the rest of your comment.
On another note, searching for memset_s and openbsd yielded this hit from 2012:
https://mail-index.netbsd.org/tech-security/2012/07/22/msg00...
Which seems to point back to:
https://mail-index.netbsd.org/tech-userlevel/2012/02/25/msg0...
So I guess the "trick" outlined in the (very lucid) post has been known for a while.
Re: How to zero a buffer
#153Earlier quoted context omitted.
As usually in C, you start from the name, then try to go to the right until you hit parenthesis, then go to the left until parenthesis, rinse and repeat. So, first, the name: memset_ptr Then, try going to the right, but aha! before we can really gain speed, a parenthesis is immediately blocking us. We shrug off the bruises and turn to the left: (* const volatile memset_ptr) "Hey guys, memset_ptr is a volatile const p…
Is "void *" really something you can pass?
Re: How to zero a buffer
#154Earlier quoted context omitted.
As usually in C, you start from the name, then try to go to the right until you hit parenthesis, then go to the left until parenthesis, rinse and repeat. So, first, the name: memset_ptr Then, try going to the right, but aha! before we can really gain speed, a parenthesis is immediately blocking us. We shrug off the bruises and turn to the left: (* const volatile memset_ptr) "Hey guys, memset_ptr is a volatile const p…
Is "void *" really something you can pass?
Some of this may be technically incorrect. This is my own mental model of the C language which is sometimes incomplete.
Re: How to zero a buffer
#155Earlier quoted context omitted.
Is "void *" really something you can pass?
Yes, it refers to a memory location, without implying anything about the semantics of the bits located at that location. You can't dereference or assign to the location because you don't know the type at that location. You can however assign that pointer to a typed pointer variable to actually read or write to that memory location. This is useful when you really care about the bits of memory but you're variable point…
Re: How to zero a buffer
#156Earlier quoted context omitted.
Perhaps in retrospect this was an inappropriate choice of definition, at least for cryptographic operations.
I'm gonna go ahead and say it: Perhaps in retrospect C is an inappropriate choice of language for these kinds of applications. This "Performance at all costs, including safety and predictability" thing may be appropriate in video games, but for security-critical applications that philosophy is downright negligent.
Honestly, in my eyes, this is a place where the language is specified in a way that it cannot ever guarantee what you're trying to achieve and in that case you're best off not relying on the language, but on other things that can make such guarantees.
Re: How to zero a buffer
#157When this still doesn't work: JIT compiled C. The compiler can check for memset and elide it. (Or hell, one can envision the hypothetical Antagonizer9000 compiler including a version of memset which peeks up the stack to see what it's clearing and stops short.)
Re: How to zero a buffer
#158This all seems kind of silly. Why doesn't C have a type qualifier like called "secure" to inform the compiler that it should avoid security-compromising optimisations and maybe even automatically zero the memory when it falls out of scope?
You can still side-step the problem by calling a function that's not defined in the standard (so it cannot be inlined by the compiler), usually something like SecureZeroMemory on Windows and its equivalent on other OSes.
Re: How to zero a buffer
#159Earlier quoted context omitted.
Is "void *" really something you can pass?
Yes, it refers to a memory location, without implying anything about the semantics of the bits located at that location. You can't dereference or assign to the location because you don't know the type at that location. You can however assign that pointer to a typed pointer variable to actually read or write to that memory location. This is useful when you really care about the bits of memory but you're variable point…
Re: How to zero a buffer
#160Earlier quoted context omitted.
Because the C standard permits such optimizations. It is really just a case of inlining followed by dead store optimization, two fairly common optimization passes which are normally desired. The subtlety is that while the compiler considers it a dead store, you don't, because you're going behind the compilers back to examine memory afterwards.
How about compilers having a way for us to tell it that a particular function must not be removed? It seems silly that we have to come up with hacks to work around the compiler.