linux系统调用监测实例

本文将记录在linux操作系统中实现系统调用监测功能的原理和操作过程,涉及到了内核编译、添加自定义系统调用、修改系统调用入口、添加内核函数、添加编译内核模块等内容。主要参考资料《Linux操作系统原理与应用(第2版)》

1,系统调用基础

系统调用是linux系统中提供给用户态进程的一组可以与硬件设备(如CPU、磁盘、打印机等)进行交互的接口。对于用户态程序员来说它就和一个API函数差不多,同样关注的是传入参数以及返回值,但实际上一些涉及到硬件设备操作的c库API就是使用了系统调用来实现的,API只是对系统调用的再加工和封装。

对于每一个系统调用,在内核中都有一个内核函数用来实现这个系统调用的主要功能,该内核函数叫这个系统调用的服务例程;内核函数被执行于内核空间,用户空间中调用系统调用的进程要想传入参数拿到运行结果还需要一些准备工作,这些工作在用户空间的部分叫封装例程,主要是将参数以合适的方式存储在堆栈中,称之为该系统调用的封装例程;在内核空间的部分是系统调用处理程序,它其实就是中断处理程序,产生一个中断让CPU陷入内核态,然后保存堆栈,处理参数,调用服务例程,执行完后返回参数,这些操作定义在系统内核文件夹下的arch/x86/kernel/entry_32(不同内核版本可能有差异,64位系统用的是entry_64)。

内核文件夹中arch/x86/include/asm/unistd_32.h(64位系统用的是unistd_64.h,后面的也类似)中定义了系统调用号,内核执行一个系统调用时将从这个文件中查找系统调用的名称和系统调用号之间的关系;内核文件夹中arch/x86/kernel/syscall_table_32.S中是系统调用表,内核将根据系统调用号作为下标读取这个文件获取服务例程函数。

2,系统调用监测原理

系统调用监测要完成以下几个基本功能:

(1)记录系统调用日志,将其写入缓冲区(内核中),以便用户读取。

(2)建立新的系统调用,以便将内核缓冲区中的系统调用日志返回到用户空间。

(3)循环利用系统调用,以便能实时地返回系统调用的日志。

功能(1)编写一个内核函数来实现;功能(2)新建立系统调用来实现;功能(3)在用户空间循环调用(2)中的系统调用。下面依次实现这些功能:

功能(1):在内核文件夹中arch/x86/kernel/entry_32.S中含有系统调用入口,每个系统调用的执行都会从这里开始,在这个文件中添加汇编代码调用我们自己定义的内核函数,名为syscall_audit。

修改系统调用入口截图示例

在/arch/x86/kernel/目录下添加自己编写的myaudit.c文件,该文件包含了syscall_audit函数的实现,但这是个内核函数,编译调试需要重新编译内核,太麻烦,为了简化调试过程,我们在myaudit.c文件中不要直接写函数的实现,而是用钩子函数作替身,钩子函数的实现放到模块中。模块的编写后面再介绍,myaudit.c中这样写:


 
 
  1. #include <asm/uaccess.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/init.h>
  4. #include <linux/types.h>
  5. #include <asm/current.h>
  6. #include <linux/sched.h>
  7. asmlinkage int sys_mysyscall(int number){
  8. printk( "hello,world!\n");
  9. current->uid= 0;
  10. return number;
  11. }
  12. void (*my_audit)( int, int)= 0;
  13. asmlinkage void syscall_audit(int syscall,int return_status);{
  14. if(my_audit) return (*my_audit)(syscall,return_status);
  15. printk( "IN KERNEL:%s(%d),syscall:%d,return:%d\n",current->comm,current->pid,syscall,return_status);
  16. return;
  17. }
  18. int (*my_sysaudit)(u8,u8*,u16,u8)= 0;
  19. asmlinkage int sys_myaudit(u8 type,u8 * us_buf,u16 us_buf_size,u8 reset){
  20. if(my_sysaudit) return (*my_sysaudit)(type,us_buf,us_buf_size,reset);
  21. printk( "IN KERNEL: my system call sys_myaudit() working\n");
  22. return 1;
  23. }

第一个sys_mysyscall()函数是顺手加的一个可以输出hello,world和使用户uid=0的系统调用,第二个syscall_audit()就是系统调用入口中将会调用的内核函数,第三个sys_myaudit是功能(2)中新建立的系统调用将要使用的内核函数。

 

功能(2):新建立一个系统调用读取内核缓冲区中的内容,过程是在系统调用号和系统调用表中添加新的系统调用的信息,比较简单,参考资料也很多,不再赘述,而添加服务例程的代码上面已给出。

下面给出功能(1)和(2)中用到的两个内核函数的真正实现:

新建一个文件夹mod,新建audit.c文件,内容如下:


 
 
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <asm/uaccess.h>
  5. #include <linux/proc_fs.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <asm/current.h>
  9. #include <linux/sched.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <asm/io.h>
  13. #include <linux/cdev.h>
  14. #include <asm/system.h>
  15. #define COMM_SIZE 16
  16. struct syscall_buf{
  17. u32 serial;
  18. u32 ts_sec;
  19. u32 ts_micro;
  20. u32 syscall;
  21. u32 status;
  22. pid_t pid;
  23. u8 comm[COMM_SIZE];
  24. };
  25. DECLARE_WAIT_QUEUE_HEAD(buffer_wait);
  26. #define AUDIT_BUF_SIZE 100
  27. static struct syscall_buf audit_buf[AUDIT_BUF_SIZE];
  28. static int current_pos= 0;
  29. static u32 serial= 0;
  30. void syscall_audit(int syscall,int return_status){
  31. struct syscall_buf *ppb_temp;
  32. if(current_pos<AUDIT_BUF_SIZE){
  33. ppb_temp=&audit_buf[current_pos];
  34. ppb_temp->serial=serial++;
  35. //ppb_temp->ts_sec=xtime.tv_sec;
  36. //ppb_temp->ts_micro=xtime.tv_usec;
  37. ppb_temp->syscall=syscall;
  38. ppb_temp->status=return_status;
  39. ppb_temp->pid=current->pid;
  40. memcpy(ppb_temp->comm,current->comm,COMM_SIZE);
  41. if(++current_pos==AUDIT_BUF_SIZE* 8/ 10){
  42. printk( "IN MODULE_audit: yes, it near full\n");
  43. wake_up_interruptible(&buffer_wait);
  44. }
  45. }
  46. }
  47. int sys_audit(u8 type, u8 * us_buf, u16 us_buf_size, u8 reset){
  48. int ret= 0;
  49. if(!type){
  50. if(__clear_user(us_buf, us_buf_size)){
  51. printk( "Error:clear_user\n");
  52. return 0;
  53. }
  54. printk( "IN MODULE_systemcall: starting...\n");
  55. ret=wait_event_interruptible(buffer_wait,current_pos>=AUDIT_BUF_SIZE* 8/ 10);
  56. printk( "IN MODULE_systemcall: over,current_pos is %d\n",current_pos);
  57. if(__copy_to_user(us_buf,audit_buf,(current_pos)* sizeof(struct syscall_buf))){
  58. printk( "Error: copy error\n");
  59. return 0;
  60. }
  61. ret=current_pos;
  62. current_pos= 0;
  63. }
  64. return ret;
  65. }
  66. extern void(*my_audit)(int,int);
  67. extern int(*my_sysaudit)(unsigned char,unsigned char*,unsigned short, unsigned char);
  68. static int __ init audit_init(void){
  69. my_sysaudit=sys_audit;
  70. my_audit=syscall_audit;
  71. printk( "Starting System Call Auditing\n");
  72. return 0;
  73. }
  74. static void __ exit audit_exit(void){
  75. my_audit= NULL;
  76. my_sysaudit= NULL;
  77. printk( "Exiting System Call Auditing\n");
  78. return;
  79. }
  80. module_init(audit_init);
  81. module_exit(audit_exit);
  82. MODULE_LICENSE( "GPL");

在模块初始化时让钩子函数my_sysaudit指向模块函数sys_audit,sys_audit()就是sys_myaudit的真正实现了,从代码可以看到它的功能就是从缓冲区里读取信息然后copy_to_user;另一个就是syscall_audit的真正实现了,功能是将当前系统调用保存到缓冲区。模块退出时解除钩子函数的绑定。

为了在模块中可以顺利引用到钩子函数my_sysaudit和my_audit,还需要在arch/x86/kernel/i386_ksyms_32.c文件的末尾添加以下内容:


 
 
  1. extern void(*my_audit)( int, int);
  2. EXPORT_SYMBOL(my_audit);
  3. extern int(*my_sysaudit)( unsigned char, unsigned char*, unsigned short, unsigned char);
  4. EXPORT_SYMBOL(my_sysaudit);

以上步骤做完就可以编译内核了,编译完后系统中就已经多了两个系统调用,一个是hello,world系统调用,另一个是读取内核缓冲区的,可以测试一下hello,world能不能用。这时候继续把模块编译一下,然后insmod加载模块。涉及到内核的部分就结束了。

功能(3):在用户空间编程,编写文件user_audit.c,内容如下:


 
 
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <sys/resource.h>
  7. #include <sys/syscall.h>
  8. struct syscall_buf{
  9. int serial;
  10. int syscall;
  11. int status;
  12. pid_t pid;
  13. char comm[ 16];
  14. };
  15. #define AUDIT_BUF_SIZE 100*sizeof(struct syscall_buf)
  16. int main(){
  17. char col_buf[AUDIT_BUF_SIZE];
  18. unsigned char reset= 1;
  19. int num= 0;
  20. struct syscall_buf *p;
  21. while( 1){
  22. num=syscall(__NR_myaudit, 0, col_buf, AUDIT_BUF_SIZE, reset);
  23. printf( "num: %d\n");
  24. char j= 0;
  25. int i;
  26. p=(struct syscall_buf *)col_buf;
  27. for(i= 0;i<num;i++){
  28. printf( "num[%d],serial: %d\t",i,p[i].serial);
  29. printf( "syscall:%d\n",p[i].syscall);
  30. printf( "status:%d\n",((struct syscall_buf *)col_buf)[i].status);
  31. printf( "status:%d\n",((struct syscall_buf *)col_buf)[i].status);
  32. printf( "status:%d\n",((struct syscall_buf *)col_buf)[i].status);
  33. }
  34. }
  35. }

编译测试监测效果。

以上代码适用于内核版本2.6.28,或者不要超过太多,否则会增加调BUG的难度。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值