4

Possible Duplicate:
Retrieving the memory map of its own process in OS X 10.5/10.6

On OS X, I can use mprotect() to request that a specific page of memory be made some combination of readable, writable or executable.

I want to know how to find out what the current protection level is. As an example, on Linux I can cat /proc/$$/maps to find out the same information:

$ cat /proc/$$/maps
00400000-004db000 r-xp 00000000 fb:00 131145                             /bin/bash
006da000-006db000 r--p 000da000 fb:00 131145                             /bin/bash
006db000-006e4000 rw-p 000db000 fb:00 131145                             /bin/bash
006e4000-006ea000 rw-p 00000000 00:00 0 
00df4000-00e55000 rw-p 00000000 00:00 0                                  [heap]
...

Here I can see that there are 5 ranges of memory mapped for the main executable (bash), one is read/execute, one is read-only, and the rest are read/write.

I've looked through all the man pages and official APIs I can find to get the same information on OS X, and have come up empty so far. The only thing I've found that's close is to use mincore() to figure out if a page is in-core or not. But that's not enough; I also want the current set of permissions.

Is there any undocumented way to do this?

Community
  • 1
  • 1
mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • @diciu, thanks for the link. Yes, I believe this question is a subset of the question you linked. However, (and I regret not writing this in the question) my next step was going to be to try to get this working on iOS - and the answers there wouldn't apply. Looks like I'd need to explore the "Mach VM region APIs in `/usr/include/mach/mach_vm.h`", as the 2nd most upvoted answer implies. Sadly, the provided link is broken and the header file doesn't exactly make the API obvious. – mpontillo Feb 15 '12 at 06:54
  • For Mac OS X the opened source includes XNU so you should be able to find what you're looking for (http://www.opensource.apple.com/source/xnu/xnu-1699.24.8/). For iOS, not as much (http://www.opensource.apple.com/release/ios-50/) – diciu Feb 15 '12 at 07:15
  • Since virtual memory management is highly architecture dependent, there might not be an API working portably on MacOS X and iOS. – Daniel Roethlisberger Mar 16 '12 at 19:32

1 Answers1

3

Most VM related system calls can be found in the mach library for OSX.

http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/vm_region.html

Actually most POSIX calls in OSX are just wrappers around the appropriate Mach VM syscalls.

#include <stdlib.h>
#include <stdio.h>
#include <mach/mach.h>



int main () {
    void* ptr = malloc(100);

    vm_size_t vmsize;
    vm_address_t address = (vm_address_t)ptr;
    vm_region_basic_info_data_t info;
    mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
    memory_object_name_t object;

    kern_return_t status =   vm_region(mach_task_self(), &address, &vmsize, VM_REGION_BASIC_INFO,
                           (vm_region_info_t)&info, &info_count, &object);

    if (status) {
        perror("vm_region");
    }

    printf("Memory protection: %c%c%c  %c%c%c\n",
        info.protection & VM_PROT_READ ? 'r' : '-',
        info.protection & VM_PROT_WRITE ? 'w' : '-',
        info.protection & VM_PROT_EXECUTE ? 'x' : '-',

        info.max_protection & VM_PROT_READ ? 'r' : '-',
        info.max_protection & VM_PROT_WRITE ? 'w' : '-',
        info.max_protection & VM_PROT_EXECUTE ? 'x' : '-'
        );

    return 0;

}
sjas
  • 18,644
  • 14
  • 87
  • 92
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Thanks! Looks like those functions are available in `/usr/lib/system/libsystem_kernel.dylib`. Sadly, it doesn't look like there are `man` pages for them. I guess Apple wants you to just use Objective-C and not look at things like this. =) – mpontillo Sep 27 '12 at 22:26
  • Hmm, I can't compile this example code. It says `cannot link directly with /usr/lib/system/libsystem_kernel.dylib. Link against the umbrella framework 'System.framework' instead.` Trying with `-framework System` yields an undefined symbol. Any ideas? – mpontillo Sep 27 '12 at 22:40
  • It seems to link fine in 32 bit mode, but fails in 64 bit mode for me. Try with -m32. – Sergey L. Sep 30 '12 at 23:48
  • 3
    In 64bit mode, you'll want vm_region_64. – Chris Devereux Oct 03 '12 at 20:48