我正在编写一个 char 设备,它使用 ioctl 函数指针和缓冲区指针作为输入。
我想修改用户机器上下文,以便回到用户模式,使用该缓冲区指针指向的新堆栈执行该函数。
我所做的如下:
long ioctl_funcs(struct file *filp,unsigned int cmd, unsigned long arg)
{
int ret = 0;
switch(cmd) {
case IOCTL_SET_FUN:
printk(KERN_INFO "start\n");
struct myarg* a;
a = (struct myarg*) arg;
struct pt_regs* regs = task_pt_regs(current);
regs->ip = a->func;// func is a function implemented in user space
regs->sp = a->stack;// stack is the buffer allocated in user space with malloc
break;
}
return ret;
}
好消息是函数被激活了,坏消息是堆栈是一样的(我用gdb测试过)。
特别是即使:regs->sp = 0;
新函数在它应该崩溃时执行,因为它应该没有堆栈。
用这种方式分配堆栈指针似乎是无效的。
为什么?我应该如何正确分配堆栈指针?
linux内核版本为:3.18.106,在Virtual Box上执行。