Linux内核学习8——添加系统调用

基于linux 5.0内核添加一个系统调用,但是单纯添加一个系统调用会显得有些单调,所以这里把系统调用,工作队列,修改内核,内核编译,内核模块的编写,插入等结合起来。

要添加的是一个系统调用日志收集系统

系统调用是用户程序与系统打交道的入口,系统调用的安全直接关系到系统的安全,假设一个用户它恶意的不断的调用fork,将会导致系统负载增加,所以,如果能收集到是谁调用了一些有危险的系统调用,以及系统调用的时间和其它信息,将有助于系统管理员进行事后的追踪,从而提高系统的安全性,下面这张图就是本次要添加的系统调用日志收集系统示意图。

当用户进程进行系统调用的时候,当执行到do_syscall_64,我们判断是否是我们需要记录的系统调用,如果是则拦截需要记录的系统调用,通过my_audit这一函数,将相关信息写入到内核中的buffer里,同时我们编写用户测试程序。

在用户测试程序中,通过我们本次添加的335号系统调用执行my_sysaudit这一函数,该函数把内核buffer里的信息拿到我们的用户buffer当中,其中my_audit和my_sysaudit这两个函数是钩子函数,并且在我们的my_audit内核模块当中进行具体的实现,接下来看具体的实现步骤

img

添加系统调用第一步:打开系统调用表表项文件,增加系统调用表表项

/arch/x86/entry/syscalls/syscall_64.tbl

在这里插入图片描述

img

要按照上面的格式添加。

第二步,添加系统调用函数。/arch/x86/kernel/myaudit.c

在这里插入图片描述

#include <linux/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/syscalls.h>
#include <linux/kernel.h>
#include <asm/current.h>

void (*my_audit) (int,int) = 0;

int(*my_sysaudit)(u8,u8 *,u16,u8) = 0;
SYSCALL_DEFINE4(myaudit,u8,type,u8 *,us_buf,u16,us_buf_size,u8,reset)
{
        if (my_audit){
                printk("IN KENEL:my system call sys_myaudit() working\n");
                return (*my_sysaudit)(type,us_buf,us_buf_size,reset);

        } else
                printk("my_audit is not exist\n");
        return 1;

}

EXPORT_SYMBOL(my_audit);
EXPORT_SYMBOL(my_sysaudit);

这里使用内核所给的SYSCALL_DEFINE宏,定义了系统调用在内核的入口。同时声明了两个钩子函数在内核模块中等待实现。

第三步,修改Makefile文件,把myaudit.c添加到内核编译中去

/arch/x86/kernel/Makefile

img

也就是这里, obj-y += myaudit.o

img

第四步,增加函数声明

img

img

第五步,拦截相关系统调用

img

首先找到do_syscall_64这个函数,

在这里插入图片描述

在这个地方加入对系统调用号nr的判断

img

如果是我们需要拦截的系统调用号我们就将它拦截下来

我们可以看到,要记录的系统调用号有2号open,3号close,39号get_pid,56号clone,57号fork,59号exit,如果是我们需要记录的系统调用函数,我们就调用之前的钩子函数my_audit,如果我们还没有在内核模块中实现my_audit,就打印日志my_audit is not exist,然后保存退出

#ifdef CONFIG_X86_64
__visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
{
	struct thread_info *ti;
 
	enter_from_user_mode();
	local_irq_enable();
	ti = current_thread_info();
	if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY)
		nr = syscall_trace_enter(regs);
 
	/*
	 * NB: Native and x32 syscalls are dispatched from the same
	 * table.  The only functional difference is the x32 bit in
	 * regs->orig_ax, which changes the behavior of some syscalls.
	 */
	nr &= __SYSCALL_MASK;
	if (likely(nr < NR_syscalls)) {
		nr = array_index_nospec(nr, NR_syscalls);
		regs->ax = sys_call_table[nr](regs);
	}
 
	syscall_return_slowpath(regs);
}
#endif

接下来编译内核,当前内核是4.1.15,我们把当前内核的.config文件copy到当前目录下,之后执行make menuconfig

在这里插入图片描述

加载.config文件,

img

img

img

img

顺序是load,ok,save,ok,exit,exit

接着执行make olddefconfig

img

之后开始编译内核

img

如果直接使用的是ubuntu系统,直接make menuconfig有时会报错,需要安装几个依赖的库

sudo make modules

装内核,并修改内核引导

sudo make modules_install

sudo make install

sudo update-grub2

sudo reboot

现在,内核版本就替换位5.0,接下来要做的是添加实现钩子函数的内核模块。

进入相应的目录下打开my_audit.c

img

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/uaccess.h>

#define COMM_SIZE 16
#define AUDIT_BUF_SIZE 20

MODULE_LICENSE("GPL v2");

struct syscall_buf{
u32 serial;
u64 ts_sec;
u64 ts_micro;
u32 syscall;
u32 status;
pid_t pid;
uid_t uid;
u8 comm[COMM_SIZE];	
};

DECLARE_WAIT_QUEUE_HEAD(buffer_wait);

static struct  syscall_buf  audit_buf[AUDIT_BUF_SIZE];
static int current_pos = 0;
static u32 serial = 0;

void syscall_audit(int syscall,int return_status)
{	
	struct syscall_buf *ppb_temp;
	struct timespec64 nowtime;
	
	ktime_get_real_ts64(&nowtime);
	if(current_pos<AUDIT_BUF_SIZE){
		ppb_temp = &audit_buf[current_pos];
		ppb_temp->serial = serial++;
		ppb_temp->ts_sec = nowtime.tv_sec;
		ppb_temp->ts_micro = nowtime.tv_nsec;
		ppb_temp->syscall = syscall;
		ppb_temp->status = return_status;
		ppb_temp->pid = current->pid;
		ppb_temp->uid = current->tgid; 
		
		memcpy(ppb_temp->comm,current->comm,COMM_SIZE);
		if(++current_pos ==AUDIT_BUF_SIZE * 6/10)
			{
				printk("IN MODULE_AUDIT:yes,it near full\n");
				wake_up_interruptible(&buffer_wait);
			}
		}
	
}

int sys_audit(u8 type,u8 *us_buf,u16 us_buf_size,u8 reset)
{
	int ret = 0;
		if(!type){
			if(clear_user(us_buf,us_buf_size)){
				printk("Error:clear_user\n");
				return 0;
			}
			printk("IN MODULE_systemcall:starting...\n");
			ret = wait_event_interruptible(buffer_wait,current_pos >= AUDIT_BUF_SIZE *6/10);
			printk("IN MODULE_systemcall:over,current_pos is %d\n",current_pos);
			if(copy_to_user(us_buf,audit_buf,(current_pos)*sizeof(struct syscall_buf))){
				printk("Error:copy error\n");
				return 0;
			}
			ret = current_pos;
			current_pos = 0;

		}
		return ret;		
}

extern void(*my_audit)(int,int);
extern int (*my_sysaudit)(u8,u8*,u16,u8);
static int __init audit_init(void)
{
	my_sysaudit = sys_audit;
	my_audit = syscall_audit;
	printk("Exiting System Call Auditing\n");
	return;
}

module_init(audit_init);

static void __exit audit_exit(void)
{
	my_audit = NULL;
	my_sysaudit = NULL;
	printk("Exiting System Calling Auditing\n");
	return;
}

module_exit(audit_exit);

先定义了一个结构体,这个结构体就存放了我们需要获取的一些系统调用的属性,

接下来实现了两个函数,syscall_audit这个函数,实现了把拦截下来的系统调用参数赋值给我们定义的结构体,

sys_audit这个函数则实现了把内核的buffer拿到用户的buffer中,

my_sysaudit,my_audit这两个函数就挂在我们之前在内核中定义的两个钩子函数上。

可以看到模块加载的时候会把它赋过去,模块卸载的时候就会把这个钩子函数重新置为空

编写Makefile

img

把my_audit.c编译成内核模块。接着对my_audit.c进行编译make,加载模块

img

查看日志,demesg

最后一行可以看到,内核buffer快满了

img

编写用户态测试程序

vim test_syscall.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/types.h>

#define COMM_SIZE 16

typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;

struct syscall_buf{
	u32 serial;
	u64 ts_sec;
	u64 ts_micro;
	u32 syscall;
	u32 status;
	pid_t pid;
	uid_t uid;
	u8 comm[COMM_SIZE];
};

#define AUDIT_BUF_SIZE (20*sizeof(struct syscall_buf))

int main(int argc,char *argv[])
{	
	u8 col_buf[AUDIT_BUF_SIZE];
	unsigned char reset = 1;
	int num = 0;
	int i,j;
	struct syscall_buf *p;
	while(1){
		num = syscall(335,0,col_buf,AUDIT_BUF_SIZE,reset);
		printf("num :%d\n",num);
		p = (struct syscall_buf *)col_buf;
		for(i=0;i<num;i++){
			printf("num[%d],serial:%d,\t syscall :%d,\t pid:%d,\t comm:%s,\t time:%s\n",i,p[i].serial,p[i].syscall,p[i].pid,p[i].comm,ctime(&p[i].ts_sec));
		}
	}
	return 1;
}

这里有相同的结构体

img

img

img

img

查看执行结果

img

img

./test_syscall

img

我们可以看到测试程序一运行就打印出了许多拦截到的系统调用的相关日志,并随着系统的运行,也在不断的记录一些信息,也在不断的打印

img

用户进行我们需要记录的系统调用时,会被my_audit进行拦截,存在内核的buffer中,我们使用用户态测试程序进行335号系统调用,把内核buffer里面的信息拿出到用户态buffer里面进行打印,这就是我们本次添加日志收集系统的全过程。

如果对您有帮助,麻烦点赞、收藏或者关注哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值