在 OS X 上,如何找出当前的内存保护级别?

发布于 2025-01-03 12:46:25 字数 1023 浏览 0 评论 0原文

可能的重复:
检索OS X 10.5/10.6 中自身进程的内存映射

在 OS X 上,我可以使用 mprotect() 来请求将特定的内存页面设为可读、可写或可执行。

我想知道如何找出当前的保护级别是多少。举个例子,在 Linux 上我可以 cat /proc/$$/maps 来查找相同的信息:

$ 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]
...

在这里我可以看到为主可执行文件映射了 5 个内存范围(bash),一种是读/执行,一种是只读,其余的是读/写。

我已经浏览了所有我能找到的手册页和官方 API,以获取有关 OS X 的相同信息,但迄今为止一无所获。我发现唯一接近的是使用 mincore() 来确定页面是否在核心中。但这还不够;我还想要当前的权限集。

有没有任何未记录的方法可以做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2025-01-10 12:46:25

大多数与 VM 相关的系统调用都可以在 OSX 的 mach 库中找到。

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

实际上,OSX 中的大多数 POSIX 调用只是适当的 Mach VM 系统调用的包装器。

#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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文