c Copy code
/*
* virt_to_phys_reader.c
*
* 一个 ARM64 Linux 内核模块,用于根据给定的 PID 和虚拟地址,
* 查找并读取对应的物理内存内容。
*
* 使用方法:
* 编译后,通过 insmod 加载模块,并传入参数:
* $ sudo insmod virt_to_phys_reader.ko target_pid=1234 target_vaddr=0x7f...
*
* 查看内核日志 dmesg 获取结果。
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h> // 包含 task_struct
#include <linux/pid.h> // 包含 find_vpid, pid_task
#include <linux/mm.h> // 包含 mm_struct, VMA, get_task_mm, mmput
#include <linux/slab.h> // 包含 kmalloc/kfree
#include <asm/io.h> // 包含 ioremap, iounmap, ioread32
#include <asm/pgtable.h> // 包含所有页表相关的宏和函数
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name Here");
MODULE_DESCRIPTION("ARM64 kernel module to read physical memory from a process's virtual address");
// 模块参数,用于在加载时指定目标 PID 和虚拟地址
static int target_pid = -1;
module_param(target_pid, int, 0644);
MODULE_PARM_DESC(target_pid, "The PID of the target process");
static unsigned long target_vaddr = 0;
module_param(target_vaddr, ulong, 0644);
MODULE_PARM_DESC(target_vaddr, "The virtual address to inspect in the target process");
/**
* @brief 核心函数,遍历页表将虚拟地址转换为物理地址
* @param mm 目标进程的内存描述符
* @param vaddr 目标虚拟地址
* @return 成功则返回对应的物理地址,失败则返回 0
*/
static unsigned long virt_to_phys(struct mm_struct *mm, unsigned long vaddr)
{
pgd_t *pgd;
p4d_t *p4d;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
unsigned long paddr = 0;
// 1. 从 PGD (Page Global Directory) 开始
pgd = pgd_offset(mm, vaddr);
if (pgd_none(*pgd) || pgd_bad(*pgd)) {
pr_err("Invalid PGD entry\n");
return 0;
}
// 2. 移动到 P4D (P4D Directory)
p4d = p4d_offset(pgd, vaddr);
if (p4d_none(*p4d) || p4d_bad(*p4d)) {
pr_err("Invalid P4D entry\n");
return 0;
}
// 3. 移动到 PUD (Page Upper Directory)
pud = pud_offset(p4d, vaddr);
if (pud_none(*pud) || pud_bad(*pud)) {
pr_err("Invalid PUD entry\n");
return 0;
}
// 检查是否是 1GB 大页面 (PUD-level huge page)
if (pud_large(*pud)) {
paddr = (pud_val(*pud) & PUD_MASK) | (vaddr & ~PUD_MASK);
pr_info("Found a 1GB huge page mapping.\n");
return paddr;
}
// 4. 移动到 PMD (Page Middle Directory)
pmd = pmd_offset(pud, vaddr);
if (pmd_none(*pmd) || pmd_bad(*pmd)) {
pr_err("Invalid PMD entry\n");
return 0;
}
// 检查是否是 2MB 大页面 (PMD-level huge page)
if (pmd_large(*pmd)) {
paddr = (pmd_val(*pmd) & PMD_MASK) | (vaddr & ~PMD_MASK);
pr_info("Found a 2MB huge page mapping.\n");
return paddr;
}
// 5. 移动到 PTE (Page Table Entry)
pte = pte_offset_map(pmd, vaddr);
if (!pte) {
pr_err("Failed to map PTE\n");
return 0;
}
if (!pte_present(*pte)) {
pr_err("PTE not present\n");
pte_unmap(pte);
return 0;
}
// 从 PTE 计算最终的物理地址 (页帧基址 + 页内偏移)
paddr = (pte_pfn(*pte) << PAGE_SHIFT) | (vaddr & ~PAGE_MASK);
pr_info("Found a standard 4KB page mapping.\n");
pte_unmap(pte);
return paddr;
}
static int __init virt_to_phys_reader_init(void)
{
struct task_struct *task;
struct mm_struct *mm;
struct vm_area_struct *vma;
unsigned long paddr;
void *mapped_addr;
u32 read_value;
pr_info("virt_to_phys_reader: Module loaded.\n");
pr_info("virt_to_phys_reader: Inspecting PID %d, VAddr 0x%lx\n", target_pid, target_vaddr);
if (target_pid <= 0 || target_vaddr == 0) {
pr_err("virt_to_phys_reader: Invalid parameters. Please specify target_pid and target_vaddr.\n");
return -EINVAL;
}
// 获取 task_struct,需要 RCU 读锁保护
rcu_read_lock();
task = pid_task(find_vpid(target_pid), PIDTYPE_PID);
if (!task) {
pr_err("virt_to_phys_reader: Could not find task_struct for PID %d.\n", target_pid);
rcu_read_unlock();
return -ESRCH;
}
// 增加 task_struct 的引用计数,防止在我们使用它时被释放
get_task_struct(task);
rcu_read_unlock();
// 获取进程的 mm_struct
mm = get_task_mm(task);
if (!mm) {
pr_err("virt_to_phys_reader: Could not get mm_struct for PID %d. (Is it a kernel thread?)\n", target_pid);
put_task_struct(task);
return -EINVAL;
}
// 锁住 mm_struct 以安全地访问页表和VMA
mmap_read_lock(mm);
// 确认虚拟地址是否在某个合法的 VMA (Virtual Memory Area) 内
vma = find_vma(mm, target_vaddr);
if (!vma) {
pr_err("virt_to_phys_reader: Virtual address 0x%lx is not in a valid VMA for PID %d.\n", target_vaddr, target_pid);
mmap_read_unlock(mm);
mmput(mm);
put_task_struct(task);
return -EFAULT;
}
pr_info("virt_to_phys_reader: Address 0x%lx is in VMA [0x%lx - 0x%lx]\n",
target_vaddr, vma->vm_start, vma->vm_end);
// 调用核心函数进行地址转换
paddr = virt_to_phys(mm, target_vaddr);
// 解锁 mm
mmap_read_unlock(mm);
if (paddr == 0) {
pr_err("virt_to_phys_reader: Failed to translate virtual address to physical address.\n");
mmput(mm);
put_task_struct(task);
return -EFAULT;
}
pr_info("virt_to_phys_reader: SUCCESS! VAddr 0x%lx -> PAddr 0x%lx\n", target_vaddr, paddr);
// 将物理地址映射到内核虚拟地址空间以便访问
// 我们只读取4个字节,所以映射4字节即可
mapped_addr = ioremap(paddr, sizeof(u32));
if (!mapped_addr) {
pr_err("virt_to_phys_reader: ioremap failed for physical address 0x%lx\n", paddr);
mmput(mm);
put_task_struct(task);
return -EIO;
}
// 读取数据
read_value = ioread32(mapped_addr);
pr_info("virt_to_phys_reader: Read 4 bytes from PAddr 0x%lx: 0x%08x\n", paddr, read_value);
// 解除映射
iounmap(mapped_addr);
// 释放之前获取的引用
mmput(mm);
put_task_struct(task);
return 0; // 成功加载
}
static void __exit virt_to_phys_reader_exit(void)
{
pr_info("virt_to_phys_reader: Module unloaded.\n");
}
module_init(virt_to_phys_reader_init);
module_exit(virt_to_phys_reader_exit);