- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图分配一个整数数组,然后使用mmap()将页面映射到两个不同的静态堆,然后使用mprotect()保护其中一个堆> 具有保护 PROT_NONE。
看来 mprotect() 保护的内容比我告诉它做的堆要多得多
int main(){
//shared data
int *p_array;
struct sigaction sa;
int i=0;
p_array = (int *)malloc(sizeof(int)*NUM_ELEMENTS); // allocate 100 ints
size_t size_of_p_array = sizeof(p_array);
//SIGSEGV Handler initialization
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == -1)
handle_error("sigaction");
protected_Heap = mmap (p_array, size_of_p_array, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* anonymous mapping doesn't need a file desc */
nonprotected_Heap = mmap (p_array, size_of_p_array, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); /* anonymous mapping doesn't need a file desc */
//write to the page to obtain a private copy
for (i=0; i<NUM_ELEMENTS;i++){
protected_Heap[i]=i;
nonprotected_Heap[i]=i;
}
// Make the memory unwritable
mprotect(protected_Heap,sizeof(int)*NUM_ELEMENTS, PROT_NONE);
printf("will write and it should not trigger an Access violation\n");
nonprotected_Heap[3] =0; // Should not trigger the SIGSEGV
protected_Heap[NUM_ELEMENTS+2]=0; //Should not trigger signal but it does
printf("will write and it should trigger an Access Violation\n");
protected_Heap[3] =0; // Should trigger the SIGSEGV
//Unmap the memory
munmap (protected_Heap,size_of_p_array);
munmap (nonprotected_Heap,size_of_p_array);
return 0;
}
有没有什么方法可以只保护到数组末尾而不是更进一步?
最佳答案
mprotect
粒度是有限的,它将始终保护整个页面。由于 x86 上页面大小为 4kB,因此无法保护 100 int 数组。
其次,当前的 mmap
有点奇怪,因为您无法 mmap 到进程中已占用的地址。传递 NULL 并分配全新的内存,稍后将通过调用 munmap
释放该内存。
最后但并非最不重要的 - 要实现上述目标,您可以分配整页,然后调整数组指针,使末尾指向页面的末尾。
关于c - 在数组上使用 mprotect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391405/
我试图分配一个整数数组,然后使用mmap()将页面映射到两个不同的静态堆,然后使用mprotect()保护其中一个堆> 具有保护 PROT_NONE。 看来 mprotect() 保护的内容比我告诉它
我有一个简单的程序,我试图保护一 block 内存,然后将一个文件读入该内存,并在出现段错误时释放它。一开始我以为只有文件是 fifo 才有问题。但现在看来,即使是普通文件,它也会失败, 这是代码:
我正在使用 mprotect 将一些内存页设置为写保护。当尝试对该内存区域进行任何写入时,程序会收到一个 SIGSEGV 信号。从信号处理程序我知道在哪个内存地址尝试了写入,但我不知道如何找出哪条指令
我正在尝试使用 protect 修改 .text 段中的值以授予我写入权限: int pageSize = sysconf(_SC_PAGE_SIZE); int *toModify = (int
mprotect 系统调用保护页面边界内的内存区域: int mprotect(void *addr, size_t len, int prot); 在这里len应该是页面大小的倍数。 有没有办法只保
我在 stracing linux 内核中的一些常用命令,看到 mprotect() 被使用了很多次。我只是想知道,mprotect() 用来确定它为其设置保护值的内存地址在其自己的地址空间中的决定因
我不理解 mprotect 用法中的“对齐分配的内存”部分。 我指的是 http://linux.die.net/man/2/mprotect 上给出的代码示例 char *p; char c; /*
作为我正在进行的项目的一部分,我想分配一些内存,将操作码写入该内存,然后 call它。到那时,代码将跳转到我分配的内存中,运行操作码并使用操作码 ret 返回到之前的位置。 . 我的代码在 Linux
我想在数组“page”中使用 posix_memalign 分配 4096 字节,然后通过 mprotect() 使用 PROT_NONE 对其进行保护。分配似乎有效,但 protected() 返回
我正在尝试编写一个程序,在其中分配一 block 内存,然后有选择地更改该 block 的页面大小和页面对齐子集的保护。但是,当我尝试对 block 中大于 8 页的内存部分调用 mprotect 时
从这个页面:http://linux.die.net/man/2/mprotect 我了解除 PROT_EXEC 之外的标志。调用的效果是什么 mprotect(ptr, size, PROT_EXE
我正在编写一些通过系统调用调用 mprotect 的 ROP 漏洞利用代码,在调用 int 0x80 eax 后设置为 0x0 表示成功。将执行转移到目标地址仍然会导致 SIGSEGV。我希望有人能告
对于我的大学项目,我需要写保护进程的整个地址空间。我正在读取 /proc/self/maps 文件并解析映射。 因此对于格式为 08048000-0804c000 r-xp 00000000 08:0
如果我使用 PROT_NONE mprotect 段并且如果 SIGSEGV 由于写入而发生,该写入由 sigaction 和 sa_sigaction ,我们将能够使用siginfo_t的si_ad
我真的在寻找任何解决方案 - 我的目标是将小于系统页面大小的区域设置为 PROT_READ、PROT_WRITE、PROT_EXEC 或 PROT_NONE。这可能吗?似乎 mprotect 只是保护
我正在开发一个并行应用程序(C,pthread)。我跟踪了系统调用,因为在某些时候我的并行性能很差。我的跟踪显示我的程序多次调用 mprotect() ... 足以显着降低我的程序速度。 我确实分配了
为了并发/并行GC,我感兴趣的是 mprotect 系统调用提供的内存顺序保证(即 mprotect 与多线程的行为或 mprotect 的内存模型)。我的问题是(假设没有编译器重新排序或有足够的编译
因此,我正在尝试检查是否可以使用 mprotect 正确更改对映射分配内存的访问权限。这就是我写的: #include #include #include #include void main
我正在尝试学习一些堆栈溢出技术并在其中使用 shellcode。我能够成功地使用一些基本的 shellcode。然后我开始在程序集中使用 exeve 并调用 ls -l ,再次成功。现在我正在尝试使用
我想获取 mprotect 处理程序中的当前程序计数器 (PC) 值。从那里我想将 PC 的值增加“n”条指令,以便程序跳过一些指令。我想为 linux 内核版本 3.0.1 做所有这些。关于我可以获
我是一名优秀的程序员,十分优秀!