gpt4 book ai didi

c - LKM linux_dirent 实现

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:38 26 4
gpt4 key购买 nike

作为练习(安全研究),我正在尝试实现一个包装的系统调用。根据 getdents 文档 - 您必须实现自己的 linux_dirent 结构(https://linux.die.net/man/2/getdents64)。这一切似乎都有效,但我无法正确读取 linux_dirent 名称。

linux_dirent 的实现:

struct mylkm_linux_dirent {
u64 d_ino;
s64 d_off;
unsigned short d_reclen;
char d_name[];
};

封装的 getdents() 调用:

/*
This method will call the original and then sift through results.
Method will remove results that contain the specified keyword.
*/
asmlinkage long mylkm_getdents(unsigned int fd, struct mylkm_linux_dirent * dirp, unsigned int count)
{
unsigned int size = 0;
unsigned int bpos = 0;
struct mylkm_linux_dirent* dir1 = NULL;
char * buffer_ptr = NULL;
char hide[] = "mylkm";

printk("mylkm: getdents() called - redirecting to original sys call.\n");
size = (*k_getdents)(fd, dirp, count);
printk("mylkm: bytes value returned by original getdents() call: %ld\n", size);
printk("mylkm: count value passed into original getdents() call: %i\n", count);
printk("mylkm: dirp pointer value: %p\n", dirp);

for (bpos = 0; bpos<size;)
{
buffer_ptr = (char*)(&dirp + bpos);
dir1 = (struct mylkm_linux_dirent*)buffer_ptr;
printk("mylkm: record length: %d\n", dir1->d_reclen);
printk("mylkm: record name: %s\n", dir1->d_name);
printk("mylkm: offset to next dirent: %lld\n", (long long)dir1->d_off);

bpos += dir1->d_reclen;
}

printk("mylkm: Completed mylkm_getdents() call\n");
return 0;
}

日志输出:

[ 1383.082213] mylkm: LKM v0.1 has been loaded.
[ 1389.397196] mylkm: getdents() called - redirecting to original sys call.
[ 1389.397207] mylkm: bytes value returned by original getdents() call: 496
[ 1389.397208] mylkm: count value passed into original getdents() call: 32768
[ 1389.397209] mylkm: dirp pointer value: 0000000002075370
[ 1389.397209] mylkm: record length: 65152
[ 1389.397210] mylkm: record name: \xffffffff\xffffffff\xffffffff\xffffffff\xffffffff\xffffffff
[ 1389.397210] mylkm: offset to next dirent: 34034544
[ 1389.397211] mylkm: Completed mylkm_getdents() call

我试过将 dir1->d_name 显式转换为 char 指针。将 char 指针传入原始调用会崩溃。如何获得正确的列表?

最佳答案

解决方法是:

  1. 使用适当的指针算法 获取dirp 数组的元素。例如

    buffer_ptr = ((char*)dirp) + bpos;
    dir1 = (struct mylkm_linux_dirent*)buffer_ptr;
  2. 通过 copy_from_user 函数访问数组的元素,而不是直接访问。例如

    u64 reclen; # Record length will be stored here
    get_user(&reclen, &dir1->d_reclen); # TODO: check possible errors
    printk("mylkm: record length: %d\n", reclen);

    # Allocate a structure in the kernel
    struct mylkm_linux_dirent* dir1_kernel = kmalloc(reclen, GFP_KERNEL); # TODO: check possible errors
    # and copy user-space structure into it.
    copy_from_user(dir1_kernel, dir1, reclen); # TODO: check possible errors

    # Use dir1_kernel instead of dir
    printk("mylkm: record name: %s\n", dir1_kernel->d_name);

    # Free kernel structure at the end of the loop
    kfree(dir1_kernel);

关于c - LKM linux_dirent 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52130086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com