11 ms·
System memory allocator free operation zeroes out deallocated blocks in iOS 16
- dcow 4y agoIt seems totally fine to me that software needing the performance of non-zeroed deallocations can implement its own pool itself for the critical path in the code that needs it.
- noobermin 4y agoYeah, everyone complaining about zeroing taking time are ignoring that this should be a sane default, and for fucks sake if you need performance you shouldn't be constantly malloc'ing and free'ing on the heap in your critical path anyway. I guess that bias was built into me because I learned C from tutorials written in the 90s where they kept stressing heap is slow somehow because it was on a P3 I guess.
- ncmncm 4y agoAllocators really were slow. I.e. it was easy to write a general-purpose allocator 10, 100, or (on SGI Irix!) 1000 times faster than what their libc did. Nowadays it takes work to get more than 10x, but hardly any to get there.
- noobermin 4y agoThe fact that this is a problem is probably a sign that every OS should do this from the start so that apps won't be written that...rely on this behavior.
- dividuum 4y agoIt’s not really an OS problem: it’s the userland allocator that needs to implement this. So on Linux that would mostly be a glibc change to how malloc/free is implemented. The OS itself doesn’t know whether or not a processes memory page is logically „in use“. It only knows that once the process returns it (using for example munmap). But once you do that, any access crashes the process already.
- noobermin 4y agoI hope you won't call me stallman but in my brain libc should be considered part of the OS, but yes, it would be clearer if I said "libc's should do it."
- tinus_hn 4y agoThe article does not actually claim this is a problem, it states the author thinks it will probably be a problem. I doubt this actually will be a problem for 99% of all apps, the ‘but what about this essential software from 50 years ago’ problem does not exist on Apple platforms because they routinely deprecate and then drop obsolete software without mercy. The only real problem I can imagine occurring is that this change uncovers dangerous security vulnerabilities that are a crash (so a denial of service) on those platforms but an exploit on others. But that’s a problem for those other platforms to deal with.
- spacecadet_ 4y agoApps aren't being written _on purpose_ to rely on the allocator doing nothing with freed memory because that's not in its interface, and this change doesn't actually do anything to stop apps from reading freed memory. There is no behavior that a program could legally rely on when reading freed memory. The assumption behind malloc/free is that freed allocations aren't used by the program anymore, so correct programs don't rely on reading freed memory at all and the allocator implementation is then free to do anything at all with the freed memory, it just turns out that the simplest behavior on the allocator side is to do nothing at all and just leave the freed memory as-is. The only thing this does when speaking of programs reading freed memory is that it breaks existing incorrect programs that just happened to work because of an implementation detail of the allocator, and then later someone will write another incorrect program that just happens to work because the allocator now zeroes out freed memory. To actually stop the problem of programs reading freed memory, the OS side would have to enforce the use of memory-safe languages which by design cannot access unused memory.
- noobermin 4y agoTo be very explicit, the "...rely" is me being facetious, I'm not really implying anyone actually relies on use after free! More like they lucked out on not catching it and shipped it anyway, hence the "rely" bit.
- ncmncm 4y agoWriting random values to freed memory would mostly work, at some cost to performance. The memory could get allocated again before it is re-used, and whatever is put in it might be less trappy than a random value. And, of course, a random value might not trap. Better is not to do things that result in referring to dead memory: easy in modern C++, apparently difficult in C.
- mcculley 4y agoIs zero better than 0xDEADBEEF or just faster?
- himinlomax 4y agoBetter for memory compression, at least.
- bonzini 4y agoLess likely to cause crashes, probably.
- mcculley 4y agoI have used custom malloc()/free() implementations that do both just so I could find more crashes.
- CJefferson 4y agoOne advantage is that when you allocate the memory back out (either by malloc, or calloc which requires zeroing), you don't have to do anything. It is mentioned that if you write to freed memory, that could lead to later callocs not returning zeroed memory, which shows they are relying on this.
- mcculley 4y agoYeah, if performance is the goal, zero is certainly faster. I have used 0xDEADBEEF to find both free-after-use and use-before-initialize bugs.
- rollulus 4y agoThe advantage of 0xDEADBEEF would be that it reduces the chance of software starting to rely on that value, compared to zero, isn't it?
- frogblast 4y agoMalloc doesn't guarantee zero filling memory, but ObjC and Swift currently need to zero memory immediately after malloc returns it, and they both share C's malloc pool. This makes a lot more performance sense for those languages.
- trasz 4y agoSounds similar to what jemalloc (default libc allocator in FreeBSD) calls “junk filling”.
- cesarb 4y agoI wonder how long until programs start to accidentally depend on it, the same way some programs currently accidentally depend on freeing memory not immediately overwriting it. For instance, I can imagine a program which accidentally follows a dangling pointer to an already freed structure, and reads another pointer from within that structure, not crashing because it ends up checking this later pointer against NULL... until the stars align and the memory used by the freed structure has been returned to the operating system, or overwritten by a later allocation.
- tlb 4y agoOn Mac, malloc has several options to help find such bugs. If you set MallocScribble=1 in the environment, it fills blocks with 0xaa on alloc and 0x55 on free. On Linux, running `env MALLOC_PERTURB_=43605 myprogram` will do the same.
- speedgoose 4y agoThat should be the default behaviour, perhaps with random values too.
- kergonath 4y agoIt’s easier when it is a specified value. Easier to see in a debugger and to diagnose. Sure, it can occasionally be interpreted as meaning something, but so can random gibberish. As long as everything is overwritten, there is no privacy issue.
- cesarb 4y ago> Sure, it can occasionally be interpreted as meaning something, but so can random gibberish. Some gibberish is better than others. For instance, on x86 (64-bit, 32-bit, or even 16-bit), filling with the 0xcc byte means that, if executed as code, it will trap (0xcc is the single-byte INT3 "single-step breakpoint" instruction). And for most Unix-style operating systems, which put the kernel in the high half of the address space, the filler bytes should have the high bit set, so that treating them as pointers will trap (the pointer will have its high bit set, pointing to kernel memory, which user space is forbidden from accessing).
- shaggie76 4y agoI wonder how many projects will be oblivious because they use their own allocator (eg: we use a modified version of Doug Lea's allocator). If the system allocator gets slower people might be more inclined to switch to something custom or use locally recycled allocations which may make use-after-free bugs even harder to find. I'm sympathetic but I wonder if it would be more sensible as an option that's on by default instead.
- Hackbraten 4y agoWhy should the system allocator get slower?
- MarkSweep 4y agoZeroing memory takes longer than doing nothing. Though some chips optimize zeroing memory. For example Intel does, perhaps Apple does as well? https://travisdowns.github.io/blog/2020/05/13/intel-zero-opt.html https://travisdowns.github.io/blog/2020/05/13/intel-zero-opt...
- saagarjha 4y agoApple uses dc zva on ARM.
- saagarjha 4y agoThere is apparently no performance degradation due to other improvements in malloc.
- mzs 4y agoWait, what calloc (3) no-longer zeros though? >Memory Allocation >Known Issues >The system memory allocator free operation zeroes out all deallocated blocks in macOS 13 beta or later. Invalid accesses to free memory might result in new crashes or corruption, including NULL-pointer dereferences and non-zero memory being returned from calloc. (97449075) https://developer.apple.com/documentation/macos-release-notes/macos-13-release-notes https://developer.apple.com/documentation/macos-release-note... edit: I guess this is taking advantage of use-after-free being undefined behavior.
- cesarb 4y agoFrom what I understood, the trick is that, for a block which was returned by free() and later returned by calloc(), the former behavior was to zero within the calloc(), while the new behavior would be to zero within the free(). If there's a bug which writes to the memory after the free() but before the calloc(), the former behavior would mean it's still zeroed, but the new behavior would mean it ends up overwritten with something which isn't all-zeros.
- syntheticnature 4y agoI'm not sure I see it as "taking advantage of"; if code is engaging in the UB of writing to invalid pointers, there could still have a race condition where calloc() appears to be returning non-zeroed memory, simply because the invalid pointer write happened after the clear. This just moves the clear back in time.
- monocasa 4y agocalloc(3) isn't the only way to allocate memory, nor the most common one in ObjC apps.
- astrange 4y agoActually, ObjC objects are allocated with calloc() so it might be the most common one. It also has a better interface than malloc() because it can check for overflow evaluating `size*count`, though it'd be even better if C had first-class types and you could pass that in.
- Dwedit 4y agoOn Windows, deallocated blocks get filled with 0xFEFEFEFE. For 32-bit programs that aren't large-address-aware, this would basically have the same effect. Trying to dereference a pointer of that value will be an access violation.
- yieldcrv 4y agoI like the German grammar of capitalizing all nouns Would that be useful here even in a computer context?
- WalterBright 4y agoWhen 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.
- maldev 4y agoI know the apple documents note this for the release. But we spent a bit today at work trying to do it, and we couldn't build a test case. Has anyone actually tested this? The kernel already has their own tagging system which poisons things, so I assume this is a usermode thing.
- saagarjha 4y agoMake an allocation that’s, say, 100 bytes. You’ll see it go through platform_bzero on the free path.
- maldev 4y agoAwesome! So that did work. But when trying to do 1024 bytes it doesn't zero it out. Do you happen to know what the cutoff is? I think my coworker is gonna open it up in ida, so will update back if we figure it out. EDIT: We seem to only get the zero behavior ?? < 100 < ZEROMEM <= 1008. So malloc 1009 isn't zeroing for us. And some value below 100 isn't freeing and just poisons
- saagarjha 4y agoYeah, some of the sizes don't do it. I think maybe 32 on the low end, and 1016 on the high one?
- jes 4y agoWhile we are talking about dynamic memory allocation and de-allocation, a useful trick at times is to wrap your mallocator with a bit of code so that you can program it to fail after, say, n allocations, or on a random basis. In a testing context, this can help you systematically explore the set of possible allocation failures to see if the code handles them as expected.
- saagarjha 4y agoThe vast majority of code cannot so this isn’t particularly useful.
- deleted 4y ago[deleted]
- jes 4y agoIt really depends on context. There are times when it's useful, and times when it's not. It's helped me at times in improving code coverage when developing software for medical devices, so I thought I'd mention it. The technique is can also be generalized beyond just triggering malloc() failures under specified conditions.
- vivegi 4y agoA lock-free datastructure for free blocks and a lazy zero-initializer (or pattern-based-initializer) process may address the perceived performance impacts to a large extent.
- ecpottinger 4y agoIt is the default I wish in all de-allocations no matter what OS you are talking about. That way there is no 'dead' data to be probed for.
- xani_ 4y agoWell, it's also waste of time and if your app is constantly allocating and deallocating stuff it would take a lot more.
- seiferteric 4y agoseems like something that could be handled at the memory/controller level instead of wasting CPU cycles writing 0's.
- jacobsenscott 4y agoIf you are concerned about performance at that level you likely shouldn't be allocating and de-allocating memory all the time, as that is slow even without zeroing. You should use a memory pool to allow re-using your allocated memory.
- archi42 4y agoSometimes a well placed "placement new" is enough. Memory pools are also nice, but at least for my work we ditched them again once the Windows allocator became faster. (I can count the times I did either of these on one hand, but of course this depends a lot on the code your working on).
- AshamedCaptain 4y ago> You should use a memory pool to allow re-using your allocated memory Soon: programs implement their own allocators and memory pools and stop using the libc malloc, and thus stop benefiting from all the safety valves added to the libc malloc (including TFA), all because of the rumor that the libc malloc is slow (when it likely isn't). Actually, that happened a couple decades ago.
- 4y ago
- alcidesfonseca 4y agoIs there any way of measuring how many apps were affected by this change?
- mcraiha 4y agoApple crash reports will measure this (if you compare old reports to new ones), but the data isn't public. I assume some third party SDKs will get updates to fix their bugs (and changelog might reveal it) but otherwise it is hard for normal user to see what issue caused an app to crash.
- JoeAltmaier 4y agoUsed to be linux zerod allocated pages only upon first use. But the time to take the page fault was larger than the time to zero the page so they changed to zero-immediately. Modern processors zero things very, very fast. This seems like a defensible approach.
- sedatk 4y agoOn Windows, there's that lowest priority task (Zero Page Allocator) whose sole purpose is to zero out the freed pages and add them to the available pages list. This way, allocation tasks don't have to wait for zeroing. IIRC, Zero Page Allocator's priority was so low that it was even lower than the lowest priority available to Win32 API's.
- ajross 4y agoYes, but this is a heap, not a VM. You're not streaming through the cache into DRAM, you're polluting the cache with dirty lines that the app would presumably like to be using for near-future loads. The performance impact is obviously going to depend on usage patterns, but it certainly could be much higher.
- Wowfunhappy 4y agoIs there a reason they aren't making this change only for apps compiled with the latest SDK?
- yakubin 4y agoDoes iOS use different libc dynamic libraries for applications compiled with different SDKs? I'm not an iOS developer, so I don't know, but I would assume there is only one libsystem_c.dylib[1] on iOS. [1]: Or even 0, as in Big Sur, where it's provided inside the runtime linker(/loader), instead of being a separate file.
- kevingadd 4y agoSecurity, probably. UAF attacks are going to be slightly harder to perform if the data you put there is zeroes now.
- rs_rs_rs_rs_rs 4y ago>UAF attacks are going to be slightly harder to perform SOME attacks are going to be harder, others are going to be easier(at least that's what a Project Zero researcher thinks) https://twitter.com/ifsecure/status/1572902862128295937 https://twitter.com/ifsecure/status/1572902862128295937
- jeffbee 4y agoIf they “worked until now” doesn’t that imply that the app ecosystem makes too little use of sanitizers like ASAN? Sanitizers will stomp on deallocated regions, and much much more.
- Semaphor 4y agoTangent > Hopefully you aren’t relying on any abandoned software. Every major Android update, I get scared. I use an alarm clock (Gentle Alarm) that has far more features than all others, but it’s been long abandoned (as in: removed from the play store, all websites dead. This was a paid app). Android 10 or 11 already broke it, but someone on reddit (where all 4 or so users of the app met :D) found a manifest change and allowing "drawing over other apps" to fix it. Now it works on 12, but after I already spent hours asking on HN and Reddit for a maintained replacement and testing those, I hope this continues, because with how lacking all others are I’d rather learn Android development and program my own replacement than using something else. edit: Hah! The thread [0] with the fixed APK is still getting comments from people who found it every few months :D [0]: https://old.reddit.com/r/androidapps/comments/i01xh3/gentle_alarm_fixed_for_android_10/ https://old.reddit.com/r/androidapps/comments/i01xh3/gentle_...
- xani_ 4y agoI use starfield as screensaver since pretty much first android phones, just keep old APK from few years and it still works
- thetinguy 4y agoI have seen release notes about automated fixes Apple themselves for apps in the App Store that haven’t been updated recently.
- ncmncm 4y agoProbably you should decompile it, instead of starting from scratch. This is made easier because apps are distributed in bytecode form, and compiled to machine code on installation. The bytecode is retained in the .apk file, IIUC.
- wazoox 4y agoWhat's so special about that app? So that we know
- Semaphor 4y ago> When setting the alarm, by default I want to have the option to set it in hours/minutes from now. So instead of setting the alarm for 04:30, I want to tell it to set it in 6h30m from now. > I need it to allow me to set a time of X minutes during which it gets louder until it reaches the pre-set volume. > pre-alarm: It’s an alarm that goes off X minutes before your main alarm, at a very low volume, with its own settings of volume, time to reach full volume, media to play, etc. From my Reddit thread [0] back then. It was the upgrade to 10 that did it. [0]: https://www.reddit.com/r/androidapps/comments/jvbsuz/looking_for_alarm_app_set_alarm_in_xhym_volume/ https://www.reddit.com/r/androidapps/comments/jvbsuz/looking...
- deleted 4y ago[deleted]
- secretsatan 4y agoI thought accessing deallocated memory would crash an app anyway?
- 323 4y agoYou can think of two levels of deallocated memory: deallocated just inside the app, and fully deallocated towards the OS. Only the fully deallocated towards the OS will crash. When you deallocate small amounts of memory, the underlying C library allocator will keep the deallocated memory around and not return it to the OS in case you need a similar sized piece of memory soon.
- ncmncm 4y agoNot correct. It is very rare for memory ever to be freed to the OS before process exit. Malloc never does. And, zeroing the memory, if code later uses it like a pointer, that will be null and the use will trap.
- yakubin 4y agoThat's true for memory which has been unmapped from the process (as long as something else isn't mapped to the same virtual address range). But different implementations of C malloc()+calloc()+free() don't necessarily unmap memory when you call free(). Instead they mark it as something that may be returned from subsequent calls to malloc()/calloc(), i.e. the memory isn't returned to the OS.