快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚博国际首页登入:VFS层拦截readwrite fileoperations函数无效



改变read,write,readdir,open,release等函数指针,发明readdir可以正常得到目录名,open,release只能截获目录的操作, read,write完全截获不了任何操作。 讨教各位大年夜虾有碰到类似问题吗。现在感到上是对目录的操作都能截获,然则对文件操作的就完全没有。

readdir(事情正常) 和 read 代码如下

int my_readdir(struct file *fp, void *buf, filldir_t filldir)

{

char *path = (char*)kmalloc(MAX_DIR_LENTH);

unsigned int offset = MAX_DIR_LENTH-1;

struct dentry* cwd = fp->f_dentry;

int rs;

path[MAX_DIR_LENTH-1] = '\0';

// path add one slash in the end

path[--offset] = '/';

while( cwd->d_parent != cwd ) {

offset -= cwd->d_name.len;

strncpy(path + offset, cwd->d_name.name, cwd->d_name.len);

path[--offset] = '/';

cwd = cwd->d_parent;

}

if (offset == MAX_DIR_LENTH-1) path[--offset] = '/';

printk(" Read Dir %s\n", path+offset);

kfree(path);

if (orig_readdir == NULL) {

printk(" orig read dir function is NULL\n");

return -1;

}亚博国际首页登入

rs = orig_readdir(fp, buf, filldir);

return rs;

}

ssize_t my_read (struct file *fp, char *buf, size_t len, loff_t *off)

{

int rs;

printk(" enter my read \n");//这也没有输出

char *path = (char*)kmalloc(MAX_DIR_LENTH);

unsigned int offset = MAX_DIR_LENTH-1;

struct dentry* cwd = fp->f_dentry;

path[MAX_DIR_LENTH-1] = '\0';

while( cwd->d_parent != cwd ) {

offset -= cwd->d_name.len;

strncpy(path + offset, cwd->d_name.name, cwd->d_name.len);

path[--offset] = '/';

cwd = cwd->d_parent;

}

printk(" Read file %s\n", path+offset);

kfree(path);

if (orig_read == N亚博国际首页登入ULL) {

printk(" orig read function is NULL\n");

return -1;

}

rs = orig_read(fp, buf, len, off);

return rs;

}

调换file_operations,打开一个文件(我打开的是/),获得该fs的file_operations指针,调换。

下面是代码。

int patch_vfs(const char* p)

{

struct file* filep;

filep = filp_open(p, O_RDONLY, 0);

if (IS_ERR(filep)){

printk(" can not open file\n");

return -1;

}

orig_read = filep->f_op->read;

orig_write = filep->f_op->write;

orig_readdir = filep->f_op->readdir;

orig_ioctl = filep->f_op->ioctl;

orig_open = filep->f_op->open;

orig_lock = filep->f_op->lock;

orig_mmap = filep->f_op->mmap;

orig_release = filep->f_op->release;

filep->f_op->read = my_read;

filep->f_op->write = my亚博国际首页登入_write;

filep->f_op->readdir = my_readdir;

filep->f亚博国际首页登入_op->ioctl = my_ioctl;

filep->f_op->open = my_open;

filep->f_op->lock = my_lock;

filep->f_op->mmap = my_mmap;

filep->f_op->release = my_release;

filp_clo亚博国际首页登入se(filep, 0);

return 0;

}

static int patch_init(void)

{

if (patch_vfs(root_fs) != 0) return -1;

printk(" VFS patched\n");

return 0;

}

module_init(patch_init);

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: