Can i replace malloc with iOS?

I would like to use custom malloc and free for some distributions in an iOS application, including for classes like NSMutableData .

  • Is it possible?
  • If so, how to do it?

What I really would like to do is reset certain data after I used it to guarantee the greatest possible security (in case of loss or theft of the device). If there is an easier way to do this, not involving replacing malloc , then this is great.

I believe that I need to replace malloc to do this, because sensitive data is stored in the keychain --- and I have no other option but to use NSDictionary , NSString and NSData to access this data (I can’t even use modified versions).

+6
source share
5 answers

Instead of overwriting common memory management functions, you can use custom allocators for sensitive objects.

The API for key keys is written in C and uses Core Foundation objects such as CFDictionary, CFData, and CFString. Although it is true that these objects are “free” connected to their Objective-C counterparts and are generally interchangeable, they have some features that are not available from Objective-C. One of these features is the use of custom allocators.

CFDictionaryCreate , for example, accepts an argument of type CFAllocatorRef , which in turn can be created using CFAllocatorCreate . A dispenser contains pointers to functions for distribution and release, among other things. You can use custom functions to overwrite reasonable data.

+5
source

Why do you get so low? Instead, I simply overwrite the data in the NSMutableData instance with zeros. If you really need to mess with malloc, I would probably write a category in NSObject and override the memory handling functions.

+2
source

Disclaimer: I have no experience with iOS, but I understand that it uses GCC. Assuming this is correct ...

I did it though with GCC on the PlayStation3. I do not know how much this can be passed on to your case. I used the GCC objcopy utility with --weaken-symbol . (You may need nm to display the characters in your library.

As soon as you “weaken” the malloc library, you simply write your own, which is then used instead of the original when connecting (instead of giving you a link error). To delegate the original, you may have to somehow name this name (I don’t remember - it is supposedly executable with one of the binutils, or else the library has both malloc and _malloc - sorry, this was a while.)

Hope this helps.

+2
source

I would advise you to use the Objective-C memory management system based on ownership (save / release). Memory Programming Guide

Another option is to use C structures with C memory management rules such as malloc.

+1
source

NSMutableData methods like dataWithBytes: length uses calloc / bzero already inside. Is this good enough for you?

+1
source

Source: https://habr.com/ru/post/887204/


All Articles