个模块感染另一个模块。
--[ 3 - 玩转 loadable kernel modules 下面这段给出了动态加载模块程序的源代码。了解了这个,我们就能学会在模块中插入代
码了。 ----[ 3.1 - 模块加载 内核模块的加载是通过insmod这个用户空间工具实现的。insmod包含在modutils包里[6]。
我们感兴趣的东西是insmod.c文件里的init_module()函数。 static int init_module(const char *m_name, struct obj_file *f,
unsigned long m_size, const char *blob_name,
unsigned int noload, unsigned int flag_load_map)
{
(1) struct module *module;
struct obj_section *sec;
void *image;
int ret = 0;
tgt_long m_addr; .... (2) module->init = obj_symbol_final_value(f,
obj_find_symbol(f, "init_module"));
(3) module->cleanup = obj_symbol_final_value(f,
obj_find_symbol(f, "cleanup_module")); .... if (ret == 0 && !noload) {
fflush(stdout); /* Flush any debugging output */
(4) ret = sys_init_module(m_name, (struct module *) image);
if (ret) {
error("init_module: %m");
lprintf(
"Hint: insmod errors can be caused by incorrect module parameters, "
"including invalid IO or IRQ parameters.n"
"You may find more information in syslog or the output from dmesg");
}
} 在 (1) 里,函数向一个结构体模块(struct module)填充了加载模块必须的数据。
需要关注的部分是 init_module 和 cleanup_module。这是两个函数指针,分别指向
被加载模块的 init_module() 和 cleanup_module() 函数。(2)里面的 obj_find_symbol()
函数遍历符号列表查找名字为init_module的符号,然后提取这个结构体符号(struct
symbol)并把它传递给 obj_symbol_final_value()。后者从这个结构体符号提取出
init_module函数的地址。同理,在 (3) 里这个工作对于cleanup_module()又重复了一
遍。需要牢记的是,对于模块初始化或结束时调用的函数,他们在 .strtab section 的入口
分别对应着 init_module 和 cleanup_module。 当结构体模块填充完毕后,(4) 使用了 sys_init_module() 这个系统调用(syscall)
通知内核加载相应模块。 模块加载过程中程序调用了 sys_init_module(),其中有我们感兴趣的部分。这个函
数的代码可以在 /usr/src/linux/kernel/module.c 里找到: asmlinkage long
sys_init_module(const char *name_user, struct module *mod_user)
{
struct module mod_tmp, *mod;
char *name, *n_name, *name_tmp = NULL;
long namelen, n_namelen, i, error;
unsigned long mod_user_size;
struct module_ref *dep; /* 很多sanity checks */
.....
/* 好了,上面是我们可以忍受的所有的sanity checks;剩下的拷贝如下。*/ (1) if (copy_from_user((char *)mod mod_user_size,
(char *)mod_user mod_user_size,
mod->size-mod_user_size)) {
error = -EFAULT;
goto err3;
} /* 其他的sanity checks */ .... /* 初始化模块 */
atomic_set(&mod->uc.usecount,1);
mod->flags |= MOD_INITIALIZING;
(2) if (mod->init && (error = mod->init()) != 0) {
atomic_set(&mod->uc.usecount,0);
mod->flags &= ~MOD_INITIALIZING;
if (error > 0) /* Buggy module */
error = -EBUSY;
goto err0;
}
atomic_dec(&mod->uc.usecount); 在一些sanity check之后,结构体模块被 (1) 里的copy_from_user()从用户空间拷贝
到内核空间。然后 (2) 里通过使用 mod->init() 函数指针调用了被加载模块的
init_module() 函数。而 mod->init() 这个指针的值是由 insmod 这个工具填充的。
----[ 3.2 - 修改 .strtab section 前面已经提到了,通过检查 .strtab section 里的字符串,我们可以定位模块中init函数的
地址。而通过修改这个字符串,我们可以在模块被加载的时候执行其他的函数。 修改 .strtab section 入口有几种办法。ld 的 -wrap 参数可以做到。不过这个方法和我们
后面要用到的 -r 参数(章节3.3)不兼容。在章节5.1里,我们会看到如何用xxd完成这
个工作。我写了一个工具(章节9.1)自动执行这些。 下面是一个简单的例子: $ cat test.c
#define MODULE
#define __KERNEL__ #include <linux/module.h>
#include <linux/kernel.h> int init_module(void)
{
printk ("<1> Into init_module()n");
return 0;
} int evil_module(void)
{
printk ("<1> Into evil_module()n");
return 0;
} int cleanup_module(void)
{
printk ("<1> Into cleanup_module()n");
return 0;
} $ cc -O2 -c test.c 让我们看看 .symtab 和 .strtab 这两个section: $ objdump -t test.o test.o: file format elf32-i386 SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 test.c
0000000000000000 l d .text 0000000000000000
0000000000000000 l d .data 0000000000000000
0000000000000000 l d .bss 0000000000000000
0000000000000000 l d .modinfo 0000000000000000
0000000000000000 l O .modinfo 0000000000000016 __module_kernel_version
0000000000000000 l d .rodata 0000000000000000
0000000000000000 l d .comment 0000000000000000
0000000000000000 g F .text 0000000000000014 init_module
0000000000000000 *UND* 0000000000000000 printk
0000000000000014 g F .text 0000000000000014 evil_module
0000000000000028 g F .text 0000000000000014 cleanup_module 我们马上要修改 .strtab section 的两个入口以便把 evil_module 的符号名改成 init_module。
首先必须把 init_module 这个符号重命名。在同一个ELF object文件里,两个性质相同的符
号名字不能重复。下面是操作过程: 重命名
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




