gpt4 book ai didi

c - mach_vm_write 因 (os/kern) 无效地址而失败

转载 作者:太空宇宙 更新时间:2023-11-04 03:17:51 24 4
gpt4 key购买 nike

我已经创建了一些代码来将内存写入系统上的另一个进程,但是 mach_vm_write() 失败并出现“(os/kern) 无效地址”错误,尽管地址看似有效。我的代码如下。

if ((kret = task_for_pid(mach_task_self(), pid, &task)) == KERN_SUCCESS)
{
//this succeeds with the correct value.
if ((kret = get_task_base(task, &base)) == KERN_SUCCESS)
{
char patch_1[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
mach_vm_address_t address_1 = base + 0x77777;

//this also succeeds
if ((kret = mach_vm_protect(task, address_1, 6, TRUE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)) == KERN_SUCCESS)
{
//fails with "(os/kern) invalid address" despite using the same value as the previous mach_vm_protect() call.
if ((kret = mach_vm_write(task, address_1, (vm_offset_t)&patch_1, 6)) == KERN_SUCCESS)
{

}
else printf("mach_vm_write failed w/ error %d: %s\n", kret, mach_error_string(kret));
}
else printf("mach_vm_protect failed w/ error %d: %s\n", kret, mach_error_string(kret));
}
else printf("mach_vm_region_recurse failed w/ error %d: %s\n", kret, mach_error_string(kret));
}
else printf("task_for_pid failed w/ error %d: %s\n", kret, mach_error_string(kret));

最佳答案

原来 mach_vm_write() 失败的原因是我的 mach_vm_protect() 调用设置了最大保护。调用应该如下所示:

mach_vm_protect(task, address_1, 6, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)

关于c - mach_vm_write 因 (os/kern) 无效地址而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49617377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com