Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1700649
  • 博文数量: 358
  • 博客积分: 2180
  • 博客等级: 大尉
  • 技术积分: 1810
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 13:47
文章分类

全部博文(358)

文章存档

2016年(17)

2015年(55)

2014年(9)

2013年(67)

2012年(181)

2011年(29)

分类: LINUX

2015-05-27 14:03:51

原文地址:dazuko1.0.0代码分析(2) 作者:xunuj

    下面对dazuko这个设备驱动进行分析,现在发现很多程序都喜欢使用状态机,用它来实现程序中的流程控制(另一个经典的使用状态机的例子是lighttpd,一个web服务器,也曾经看过它的代码,准备过几天写一点学习感受吧)

昨天准备写很多,但一晚上没写出什么东西,而且思路不清楚,还是自己理解的不够好。这里我以问答方式写一下对程序的理解。

1.怎么实现了hook函数

在加载dazuko模块时,执行了init_module(),在这个函数中用自己实现的sys_xxxxx来替代了sys_call_table[]中的系统函数,以此来实现了hook(第一次看见这种方法,很强大,通过类似的方式可以实现很多功能)

2.怎么和用户态的查毒软件交互

在一个应用中,可能要开启多个查毒软件的进程,每个进程都向dazuko进行注册,实际上注册就是一次打开该字符设备的操作。在dazuko_device_open()中将该进程保存到一个表中,这张表就是slots

它的每一项记录为


struct slot_t
{
/* A representation of a daemon. It holds
* all information about the daemon, the
* file that is scanned, and the state of
* the scanning process. */


int     id;
int     pid;     /* pid of our daemon */
int     state;
int     response;
int     event;
int     o_flags;
int     o_mode;
int     kuid;     /* user id of the kernel process */
int     kpid;     /* process id of the kernel process */
int     filenamelength;    /* not including terminator */
char     *filename;
struct semaphore    mutex;
};

这个结构保存一个查毒软件当前处于什么状态,是正在监听、正在分析一个文件等等。这其中涉及状态机,贴图如下。

dazuko截获到一个文件操作时,它从这个表中寻找一个当前处于FREE状态查毒进程,并把这个文件的相关信息告知该进程,由这个进程来分析文件。

dazuko和查毒软件之间的交互通过


int dazuko_device_ioctl(struct inode *inode, struct file *file, unsigned intcmd, unsigned long param)

实现,cmdKEYparamVALUE

3.最新版本和1.0.0的不同

最新版本中通过定义新的接口封装了底层的函数,这样对用户来讲更方便编程了。例如在1.0.0



dazuko_device = open("/dev/dazuko", 0)改为dazukoRegister("Anti Virus", "r+");

4.多个wait_queue的作用

static struct wait_queue *wait_kernel_waiting_for_free_slot;

可能表中的所有查毒软件都正在工作,这个wait_queue的作用就是等待一个查毒软件进程完成任务,并再次可用

static struct wait_queue *wait_kernel_waiting_while_daemon_works;

这个是查毒软件当前正处于分析状态,内核必须等待知道查毒软件完成分析,并告知结果

static struct wait_queue *wait_daemon_waiting_for_work;

这个是查毒软件当前正处于FREE状态,也就是监听状态,等待dazuko告诉它文件访问事件的发生

static struct wait_queue *wait_daemon_waiting_for_free;

查毒软件告诉了dazuko模块它的分析结果,必须等待dazuko模块给它的确认,由dazuko将它的状态从DONE转为FREE

5.一个很好的编程技巧

struct hash_t
{
struct hash_t *next;
struct file *file;
int dirty;
int namelen;
char name[1]; /* this MUST be at the end of the struct */
};
这里定义一个字符数组,但长度为1,如果有一个filename要保存,可以这样操作
struct hash_t *new = malloc(sizeof(struct hash_t) + strlen(filename));
new -> namelen = strlen(filename);
memcpy(new -> name, filename, new -> namelen);


6.WAITWAKEUP downup

waitwakeup来实现在dazuko和查毒软件之间的控制流程,因为他们之间是类似ababa这样一个执行过程。

DownUp mutex的作用就是在修改和赋值时要保护临界区,在每个结构都有一个mutex变量。


总结:

记得以前看过文章说内核里你可以为所欲为,不会像用户态有所限制,但在内核态里你必须更加小心,处处陷进。这个2000多行代码中用了很多问题6中所指的方法。

整体看过这个代码后,发现里面主要东西就是hook,用自己的函数代替系统函数,按照自己逻辑处理问题。

使用了状态机,在一次change_state(from_state, to_state)中,用到了from_state,如果在改变的时候不处于from_state,则表明出错

好了。就这些。。告一段落。准备看查毒软件代码,因为原打算就是看查毒的了。中途杀出程咬金了。

文件: dazuko_2003.pdf(一个官方的说明文档)
大小: 95KB
下载: 下载

文件: dazuko-1.0.0.tar (源代码)
大小: 60KB
下载: 下载
        一个网站

阅读(1652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~