我在linux内核模块中使用* call_usermodehelper * API时遇到问题。我在Kernel jprobes模块中使用此API来捕获* start_thread *函数。如果我提供* UMH_WAIT_EXEC *作为等待参数,则此API可以很好地工作,但是当* UMH_WAIT_PROC *作为参数传递时,它无法加载进程。
具体来说,以下代码可以正常运行。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
#include <linux/ptrace.h>
#include <linux/kmod.h>
#include <linux/syscalls.h>
#include <linux/delay.h>
static struct jprobe start_thread_jprobe;
static asmlinkage int kp_start_thread(struct pt_regs* regs,
unsigned long new_ip,unsigned long new_sp){
int retval;
char * envp[] = { NULL };
char* argv[]={"/bin/ls",NULL};
//Only modifying this for a process named test1
if(strcmp(current->comm,"test1")==0){
retval=call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
if(retval<0){
printk("Failed in starting! %d\n",retval);
}
else {
printk("Succeded in starting %d\n",retval);
}
}
jprobe_return();
/*NOTREACHED*/
return (0);
}
int init_module(void)
{
int ret;
start_thread_jprobe.entry = (kprobe_opcode_t *)kp_start_thread;
start_thread_jprobe.kp.addr = (kprobe_opcode_t *)
kallsyms_lookup_name("start_thread");
if (!start_thread_jprobe.kp.addr) {
printk("unable to lookup symbol\n");
return (-1);
}
if ((ret = register_jprobe(&start_thread_jprobe)) <0) {
printk("register_jprobe failed, returned %d\n", ret);
return (-1);
}
return (0);
}
但是如果我按如下方式更新对call_usemodehelper的调用。
retval=call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
上述调用无法使用此API加载 ls 。它给出了错误编号-14(EFAULT)。
一个简单的测试如下:
test1.c
#include <stdio.h>
int main(){
printf("This is test1\n");
return 0;
}
模块的Makefile:
obj-m := kp-start-thread.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -f *.mod.c *.ko *.o
测试:
gcc test1.c -o test1
make
sudo insmod ./kp-start-thread.ko
./test1
dmesg ---> Will demonstrate success message with EXEC and failure with WAIT.