转自:https://blog.csdn.net/thisinnocence/article/details/80414008
之前的一篇文章: Linux C/C++内存越界定位: 利用mprotect使程序在crash在第一现场。里面没有用malloc来申请按页对齐的内存,其实也可以适配malloc,多申请2页,取中间按照页对齐的内存使用。使用malloc,可以使得进程的地址空间布局不会相对原来发生太大变化。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct mem_align {
void *origin_start; // for free
void *start; // data addr start, align page size
void *end; // data addr end, align page size
void *origin_end;
};
int malloc_align_page(size_t memsize, struct mem_align *mem)
{
if (memsize == 0 || mem == NULL)
return -1;
memset(mem, 0, sizeof(*mem));
long pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == -1) {
perror("sysconf err");
return -1;
}
size_t datasize = memsize + pagesize * 2;
mem->origin_start = malloc(datasize);
if(mem->origin_start == NULL)
return -1;
mem->origin_end = mem->origin_start + datasize;
long mask = pagesize - 1;
mem->start = (void *)((long)(mem->origin_start + pagesize) & ~mask);
long pagenum = memsize / pagesize + 1;
mem->end = mem->start + pagesize * pagenum;
return 0;
}
int main()
{
int ret;
struct mem_align mem;
ret = malloc_align_page(1024, &mem);
if (ret != 0) {
return ret;
}
ret = mprotect(mem.start, (size_t)(mem.end - mem.start), PROT_READ);
if (ret == -1) {
perror("mportect");
return ret;
}
sleep(600); // 让进程先挂起不退出,看/proc/[pid]/maps里的地址空间对应页的权限
// free(mem.origin_start);
return 0;
}
一组运行结果如下:
root@ubuntu:/media/psf/Home/iLearn/learn_c/mprotect# ./a.out
malloc mem: 0xbab010 - 0xbad410
align page: 0xbac000 - 0xbad000
root@ubuntu:~# ps -ef|grep a.out
root 1429 32072 0 00:24 pts/11 00:00:00 ./a.out
root 1718 1153 0 00:25 pts/17 00:00:00 grep --color=auto a.out
root@ubuntu:~# grep bac00 /proc/1429/maps -C 3
00400000-00401000 r-xp 00000000 00:18 53150451 /media/psf/Home/iLearn/learn_c/mprotect/a.out
00600000-00601000 r--p 00000000 00:18 53150451 /media/psf/Home/iLearn/learn_c/mprotect/a.out
00601000-00602000 rw-p 00001000 00:18 53150451 /media/psf/Home/iLearn/learn_c/mprotect/a.out
00bab000-00bac000 rw-p 00000000 00:00 0 [heap]
00bac000-00bad000 r--p 00000000 00:00 0 [heap] # 这块对内存页已经被改成了只读权限
00bad000-00bce000 rw-p 00000000 00:00 0 [heap]
7f657e6b3000-7f657e86f000 r-xp 00000000 08:01 397412 /lib/x86_64-linux-gnu/libc-2.19.so
7f657e86f000-7f657ea6e000 ---p 001bc000 08:01 397412 /lib/x86_64-linux-gnu/libc-2.19.so