On OS X, how do you know the current level of memory protection?

Possible duplicate:
Removing a Native Process Memory Card in OS X 10.5 / 10.6

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

I want to know how to determine the current level of protection. For example, on Linux, I can cat /proc/$$/maps 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 see that for the main executable file there are 5 ranges of memory ( bash ), one is read / execute, one is read-only, and the rest is read / write.

I looked through all the manual pages and the official APIs that I can find to get the same information about OS X and is still empty. The only thing I found was to close mincore() to find out if the page is in the kernel or not. But this is not enough; I also need the current permission set.

Is there any undocumented way to do this?

+4
source share
1 answer

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

In fact, most POSIX calls in OSX are just wrappers around the corresponding Mach VM system calls.

 #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; } 
+2
source

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


All Articles