/dev/mem设备节点可以将物理内存全部映射到用户态,这里实践一下。
配置:
为了启用/dev/mem设备节点,并且BYPASS调如下的检查逻辑,需要设置内核如下的配置单:
CONFIG_DEVMEM=y
# CONFIG_STRICT_DEVMEM is not set
# CONFIG_X86_PAT is not set
配置CONFIG_X86_PAT的目的是DISABLE如下的检查。
然后参照如下博客进行编译升级
Ubuntu18.04 Linux内核编译升级_papaofdoudou的博客-CSDN博客_ubuntu18.04升级内核
/dev/mem访问测试代码:
#include<stdio.h>
#include<unistd.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
unsigned char * map_base;
FILE *f;
int n, fd;
fd = open("/dev/mem", O_RDWR|O_SYNC);
if (fd == -1)
{
printf("%s line %d.failure to open dev mem\n", __func__, __LINE__);
return (-1);
}
map_base = mmap(NULL, 0x200000*512, PROT_READ, MAP_SHARED, fd, 0x100000*0);
if (map_base == 0)
{
printf("NULL pointer!\n");
}
else
{
printf("Successfull!\n");
}
unsigned int* addr;
unsigned int content;
unsigned int i = 1;
for (; i; ++i)
{
addr = (unsigned int*)(map_base + i*4);
content = addr[0];
if(content == 0x51258d48)
{
printf("found\n");
break;
}
if(content == 0x488d2551)
{
printf("found\n");
break;
}
printf("address: 0x%p content 0x%x, i %d \t\t\n", addr, (unsigned int)content, i);
//map_base[i] = i;
//content = map_base[i];
// printf("updated address: 0x%lx content 0x%x\n", addr, (unsigned int)content);
}
close(fd);
munmap(map_base, 0xff);
return (1);
}
运行:
经过验证,和用hexdump工具直接读取/dev/mem节点获取的内容是相同的。
结束
|