改变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("<1> Read Dir %s\n", path+offset);
kfree(path);
if (orig_readdir == NULL) {
printk("<1> 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("<1> 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("<1> Read file %s\n", path+offset);
kfree(path);
if (orig_read == NULL) {
printk("<1> 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("<1> 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_close(filep, 0);
return 0;
}
static int patch_init(void)
{
if (patch_vfs(root_fs) != 0) return -1;
printk("<1> VFS patched\n");
return 0;
}
module_init(patch_init);