Skip to content

Instantly share code, notes, and snippets.

@yhnu
Created September 28, 2021 14:09
Show Gist options
  • Select an option

  • Save yhnu/33abe9f268087b8753288fd535a2bf2f to your computer and use it in GitHub Desktop.

Select an option

Save yhnu/33abe9f268087b8753288fd535a2bf2f to your computer and use it in GitHub Desktop.

Revisions

  1. yhnu created this gist Sep 28, 2021.
    78 changes: 78 additions & 0 deletions kernel_vma.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    static int print_map_list()
    {
    struct mm_struct *mm;
    struct vm_area_struct *vma;
    char path_buf[PATH_MAX];
    // DEFINE_HASHTABLE(htable, 3);
    // hash_init(htable);
    mm = get_task_mm(current);
    if (!mm)
    {
    return -2;
    }

    down_read(&mm->mmap_sem);
    for (vma = mm->mmap; vma; vma = vma->vm_next) {
    unsigned long start, end;
    unsigned char flags[5]={0};
    // unsigned long int id;
    // bool bFound;

    start = vma->vm_start;
    end = vma->vm_end;
    flags[0] = vma->vm_flags & VM_READ ? 'r' : '-';
    flags[1] = vma->vm_flags & VM_WRITE ? 'w' : '-';
    flags[2] = vma->vm_flags & VM_EXEC ? 'x' : '-';
    flags[3] = vma->vm_flags & VM_SHARED ? 's' : 'p';

    if (vma->vm_file) {
    char *path;
    int len;
    memset(path_buf, 0, sizeof(path_buf));
    path = d_path(&vma->vm_file->f_path, path_buf, sizeof(path_buf));
    len = strlen(path);
    if (path > 0 && !strncmp(path+len-3, ".so", strlen(".so"))) { // endswith .so
    // struct so_map *cur;
    // id = myhash(path);
    // bFound = false;
    printk("[m]%pS-%pS %s %s\n", start, end, flags, path);
    // hash_for_each_possible(htable, cur, node, id) {
    // if(id == cur->id) {
    // //pr_info("get: element: base = %pS, name = %s, id=%ld, targetid=%ld\n", cur->base, cur->name, cur->id, id);
    // if(cur->base > start) {
    // cur->base = start;
    // }
    // bFound = true;
    // break;
    // }
    // }
    // if(!bFound) {
    // struct so_map *obj;
    // obj = (struct so_map*)kmalloc(sizeof(struct so_map), GFP_KERNEL);
    // obj->id = id;
    // obj->base = start;
    // strcpy(obj->name, path);
    // //pr_info("add: element: base = %d, name = %s, id=%ld\n", obj.base, obj.name, obj.id);
    // hash_add(htable, &obj->node, obj->id);
    // }
    } else {
    printk("[m]%pS-%pS %s %s\n", start, end, flags, "");
    }
    }
    } // end for

    // do {
    // struct so_map *cur;
    // unsigned bkt;
    // hash_for_each(htable, bkt, cur, node) {
    // //do it offline
    // //pr_info("%s, base = %pS\n", cur->name, cur->base);
    // kfree(cur);
    // }
    // } while(0);

    up_read(&mm->mmap_sem);
    mmput(mm);

    return 0;
    }