如何在内核中获取可读的PWD

时间:2018-10-22 15:30:54

标签: c linux linux-kernel hook

我想获取当前目录,这是我的尝试

asmlinkage ssize_t fake_read(int __fd, void *__buf, size_t __nbytes){

    struct path pwd;
    get_fs_pwd(current->fs,&pwd);
    char x[1000];
    dentry_path_raw(pwd.dentry,x,999);
    fm_alert("read:%s\n",x);
    return real_read(__fd,__buf,__nbytes);
}

但是,我得到的输出就像

[ 2170.293439] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293466] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293483] fsmonko.fake_read: read:\xffffffd8;\xffffff9b\xffffff84
[ 2170.293500] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293524] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293550] fsmonko.fake_read: read:ػ\xffffffaf\xffffff80
[ 2170.293556] fsmonko.fake_read: read:\xffffffd8;\xffffff9b\xffffff84

应该打印可读的pwd,怎么了?

我的内核版本是4.13.0-36-generic Ubuntu 16.04。

1 个答案:

答案 0 :(得分:1)

self.x2^2+self^y2+self.z2^2 = 1将路径放在缓冲区的末尾。缓冲区(正在打印)的开头仍包含垃圾。路径的实际开始是返回dentry_path_raw的值。试试

dentry_path_raw