4 ms·
When I want to make sure allocated memory gets initialized, I have the allocator set it to 0xDEADBEEF. The trouble with 0 initialization is it can accidentally
by WalterBright 4y ago
When I want to make sure allocated memory gets initialized, I have the allocator set it to 0xDEADBEEF.
The trouble with 0 initialization is it can accidentally be correct, or can look correct when it isn't. When your pointer is pointing to 0xDEADBEEF, odds are 99.9999% that the data wasn't initialized.
D does a similar thing. The default initializer for floating point variables is NaN, not zero. There was a debate about this in the D forums.
https://digitalmars.com/d/archives/digitalmars/D/Movement_against_float.init_being_nan_363359.html https://digitalmars.com/d/archives/digitalmars/D/Movement_ag...
My position was pretty clear - default initializations should not be accidentally correct.
- acdha 4y agoThere’s a good history supporting that logic but I wonder if there were other trade offs: a couple of the comments around this mentioned better memory compression and some optimization work which made me wonder whether they might have been able to make zeroing faster than a pattern, especially if they’re going to have some generation of Apple Silicon add hardware support.
- syncsynchalt 4y agoMemory compression should handle any repeating initializer. I think they stuck with 0x0 because it keeps the performance of any calloc()-using object allocator the same (just moves the "when" of zeroing the chunk).
- syncsynchalt 4y agoAnswering myself: there's also an ARM opcode for zero'ing memory. That's another reason to use 0x0.
- acdha 4y ago> Memory compression should handle any repeating initializer. That’s what I’d think, too, but I wouldn’t be shocked if someone said they had some implementation detail which made it easier to work with zeroes. I know ARM has pretty good performance zeroing memory but that’s exactly the kind of thing where their hardware team might optimize what is now an extremely common operation.
- olliej 4y agoYou get an efficiency problem there in that it means that the first thing allocations have to do is re-zero the allocation.
- Sirened 4y agoI think you got it. From some grumblings on twitter, it seems like calloc no longer zeros allocations since it assumes the memory is already zero'd and so assuming most people on the platform do the right thing (call calloc instead of malloc+memset/bzero), this change has zero performance impact. That might have some fun effects for writing exploits (if you can predict what page will be allocated next, you can do a sideways spray by hitting the region before its allocated and then let the downstream calloc callers get hit with unexpectedly non-zero fields.
- olliej 4y agoWhy would malloc zeroing memory have any impact on that? Yes calloc obviously no longer zeros memory, but calloc _can_ rely on that as it is part of the same libc. If you hit a region before it's been allocated you're either going to trigger a page fault, or overwrite memory that's going to be zero'd on allocation.
- olliej 4y ago[this is an edit to answer my own question but I can't update the comment] It sounds like the calloc implementation is _assuming_ that the underlying alloc always zeros, so essentially (ignore overflow check) calloc is doing something like forward directly to malloc(size*count). So I think it _changes_ the exploit path to something new.
- Sirened 4y agoYeah, more or less! This closes some paths and opens brand new ones, like more of the former and less of the latter. For example, let's consider this code. void f(void) { obj *a; a = calloc(1, sizeof(a)); g(a); } void g(obj *o) { if (o->callback) { o->callback(); } } it's contrived for simplicity, but suppose obj had a function pointer in it and just after allocation the object is passed to another routine which does some work on the object and checks if the callback exists and calls it if it does. In a single threaded application, you would not be able to target the function pointer here because nothing interesting happens between calloc and the invocation since the if check will always fail due to it being zero'd from calloc. Now, as you mention, if calloc didn't zero, an attacker could scribble over what they think will be the next allocated slot in the heap (either by targeting a partially used page or an entirely unused page that's just being kept in malloc's retainer) and write a value into the callback pointer field. Now when a new object is allocated there, we end up using the corrupted data and invoking the evil pointer, and the rest is history. It's a bit of a weirder exploit path but honestly with how ridiculous UAF flows have gotten with modern mitigations on iOS, it's hardly the worst thing I've ever seen and I suspect someone will get decent mileage out of it
- archi42 4y agoI think that's draw of the luck with 0xdeadbeef vs 0x0. I had a case once were it would have made no difference, and I discovered it when there was no initialization in another platform, and all of a sudden we got random results for some old benchmark. "Not accidentally correct" seems difficult in the generic case (think a 1 bit flag; of course for an int 0 is likely "more expected" than any other value); NaN for floats however is a nice variant that should work pretty well. Is it signaling or quiet?
- WalterBright 4y ago> difficult in the generic case We do what we can. D also uses 0xFF for char default initialization (an invalid code unit).
- archi42 4y agoYeah, there is only so much that's possible on the language side. Though static analysis in compilers can catch some instances of some problems. But looking up your name, I guess you probably already knew that :) I'd love to see hardware having a "offloaded memory guard" flag: Mark in-use memory with it and get a OMG trap on any memory access with the flag not set (of course totally transparent with hardware acceleration). Too bad this is only simple on paper, where there is no such thing as (among others) memory latency.
- thewebcount 4y agomacOS and iOS do something similar when you're debugging your app. By default Xcode sets various environmental variables to set newly allocated memory to 0xAAAAAAAA and deallocated memory to 0x55555555. This makes it pretty clear when looking at something in the debugger that you've hit either uninitialized or stale data.