Live data from Hacker News

Stop Memsetting Structures

anmolsarma.in

221–230 of 277 posts

Re: Stop Memsetting Structures

#221

Earlier quoted context omitted.

I don't think it is. It wouldn't make sense since memcpy is basically supposed to convert between arbitrary binary data.

From a practical point of view, yes, but I would be very surprised if the C standard had any accommodation for memcpy being a way to leak the value of a structure's padding.

You clearly haven't understood the thread.

Re: Stop Memsetting Structures

#222

Earlier quoted context omitted.

From a practical point of view, yes, but I would be very surprised if the C standard had any accommodation for memcpy being a way to leak the value of a structure's padding.

You clearly haven't understood the thread.

How so? Did I miss something important?

Re: Stop Memsetting Structures

#223

Earlier quoted context omitted.

I'm confused what I'm supposed to look for in that article (do you mean you were the author?) but anyway -- I feel like calling this merely a "caveat" gives the wrong impression? It seems a bit like telling people to mix bleach with vinegar and then saying "Oops, sorry, I did know that it produces chlorine! I forgot to mention that caveat." The caveat isn't just a side note for the margins, it's a critical reason why…

Technically I don't think memset forces the compiler to clear the padding and keep it clear either though. The magic of the C abstract machine.

[deleted]

Re: Stop Memsetting Structures

#225

Earlier quoted context omitted.

Why? memset followed by memcpy must return the same byte everywhere. Otherwise what would memset even mean?

I feel like accessing the bytes that correspond to padding might be implementation-defined, but I couldn't find this in the standard.

Parent comment was edited to include this quote:

_6 When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values. 51)_

So sure, a memcpy after a memset aught to preserve all the bytes, but if they are updated through a struct which contains padding then the values in the padding bytes will be left unspecified.

Re: Stop Memsetting Structures

#226
post #196

Earlier quoted context omitted.

And most importantly dont expect memset to be executed at all. Most -O2 compilers happily optimize the memset away if if doesn't care about the sideeffects. Therefore there exists the "secure" memset variants, which are the insecure variants adding a simple compiler barrier, so it isn't optimized away. And there exists the very few really secure memset_s variants with a real memory barrier which guarantees memset to…

you can write your own memset that bypasses all of that. Its just 2 lines of code.

And that will be slower than calling memset() unless you have a really smart compiler.

Re: Stop Memsetting Structures

#227
It is C99 code.

It is worth noting that many C projects go for extreme portability. That's why C is used in the first place, and sometimes C99 is too much. I still sometimes work with compilers that barely support C89. That's for the aerospace industry BTW. There are even some libc that don't accept free(NULL).

If you know the platform you are writing for is not ancient, that's fine but if you are writing portable code, you have to realize these ancient platforms are still alive.

I think ditching K&R C is fine though. I've seen some of it but never encountered a case where it was the only thing the compiler supported.

Re: Stop Memsetting Structures

#228
post #122

Earlier quoted context omitted.

RDMA (Infiniband) does DMA to/from network. Why we should not use RDMA?

Hmm, that concern is not just limited to RDMA; any kind of shared memory communication across a trust boundary is vulnerable to info leaks in the padding of the struct. That does seem like a real issue. Not sure what's the appropriate mitigation. Having to memset is a drag, and it's easy to forget to do it in some place or another. I'm tempted to say that you should only use packed structs for shared memory communica…

Well, Infiniband is intended mainly for supercomputers. So security is of no real concern within the Imfiniband network as that is implicitly a trusted environment. Access comtrol to that network is the main security boundary.

Edit: Also, lots of simulation codes I am aware of tend to send flat arrays anyway. So I'd say that your concerns are mostly theoretical in nature.

Re: Stop Memsetting Structures

#229
post #196

Earlier quoted context omitted.

And most importantly dont expect memset to be executed at all. Most -O2 compilers happily optimize the memset away if if doesn't care about the sideeffects. Therefore there exists the "secure" memset variants, which are the insecure variants adding a simple compiler barrier, so it isn't optimized away. And there exists the very few really secure memset_s variants with a real memory barrier which guarantees memset to…

you can write your own memset that bypasses all of that. Its just 2 lines of code.

I don't trust any memset at all. Literally every single memset I looked at was either broken, too slow or insecure. Mostly the wellknown glibc, freebsd, msvcrt and compiler implementations.

Re: Stop Memsetting Structures

#230

Earlier quoted context omitted.

are you sure that the padding is not zeroed by memset? [1] says that memset_s exists because it is not optimized away if it just clears a variable that will not be used after the call (unlike memset) On the other end: people who do this for security reasons say not to trust memset_s is 'an optional feature of c11 and not really portable' [2] [1] https://en.cppreference.com/w/c/string/byte/memset [2] https://www.crypt…

I don't think memset is required to zero out any padding, because the effects of this are not observable.

The function memset gets the length of the area to overwrite, how is it to know where the padding is?
Post reply on HN