Linux 内核行纪律 copy_from_user

Linux Kernel Line Discipline copy_from_user

我使用 Linux 内核的 v4.0.5,运行 在 Mint Linux 下开发了一个简单的线规程。

tty_ldisc_ops 结构如下所示:

static struct tty_ldisc_ops my_ldisc = {
    .owner          = THIS_MODULE,
    .magic          = TTY_LDISC_MAGIC,
    .name           = "my_ldisc",
    .open           = my_open,
    .close          = my_close,
    .read           = my_read,
    .write          = my_write,
    .ioctl          = my_ioctl,
    .poll           = my_poll,
    .receive_buf    = my_receive,
    .write_wakeup   = my_wakeup,
};

模块通过 insmod my_lkm.ko 添加。我知道它添加正确,因为我已经使用 printk 来指示它并且可以通过 dmesg 查看消息。此外,在启动时,我的用户space 应用程序使用 ioctl,我还通过 printk 验证了它的工作原理。

问题是,在 my_write 中,copy_from_user 总是 returns 一个非零值,表明它以某种方式失败了。

这里是 my_write():

static ssize_t my_write(struct tty_struct *tty, 
                        struct file *file,
                        const unsigned char *buf, 
                        size_t nr)
{
    int error = 0;
    unsigned char data[MAX]; //MAX is 256 

    if(!my_tty) {
        return -EIO;
    }
    if(nr > MAX) {      //too big       
        return -ENOMEM;
    }
    error = copy_from_user(data,buf,nr);    
    printk("copy_from_user returned %d\n",error);
    //here, error is always equal to nr 
    //(which is 12 in my example application)
    if(error==0) {
        printk("success\n"); //never get here
        return nr;
    }
    return error;
}

根据我的研究,copy_from_user 最终会调用 pa_memcpy 来验证所使用的指针。该验证失败,但我不知道为什么。我不知道 *buf 和数据如何重叠或会导致错误。

uname -a 的输出:Linux mint-linux 4.0.5-040005-generic #201506061639 SMP Sat Jun 6 16:40:45 UTC 2015 UTC x86_64 x86_64 x86_64 GNU/Linux

用户space申请的片段是:

#define OPEN_FLAGS      (O_RDWR|O_NONBLOCK)
int main(int argc, char **argv)
{
    int fd=-1;
    int bytes_written= 0;
    char device="/dev/ttyUSB0";
    unsigned char outbuffer[128]={0};
    fd=open(device,OPEN_FLAGS);
    //set baud rate, etc., switch to my_ldisc (using N_MOUSE)
    outbuffer[0]=0x01;
    outbuffer[1]=0x02;
    outbuffer[2]=0x03;
    outbuffer[3]=0x04;
    outbuffer[4]=0x05;
    outbuffer[5]=0x06;
    outbuffer[6]=0x07;
    outbuffer[7]=0x08;
    outbuffer[8]=0x09;
    outbuffer[9]=0x0A;
    outbuffer[10]=0x0B;
    outbuffer[11]=0x0C;
    bytes_written=write(fd,outbuffer,12);
    while(true) { 
      //...
      sleep(1);
    }
}

此外,对 my_write 中 buf 的任何访问都会导致 VM 不稳定。即使按照 o'reilly linux 驱动程序书中的 tty 驱动程序示例,也是这样:

printk(KERN_DEBUG "%s - ", __FUNCTION__);
for(i=0;i<nr;i++)
{       
    printk("%02x ",buf[i]);
}
printk("\n");

按照 Tsyvarev 的建议,我在用户 space 应用程序和内核模块中打印了指针。它们不同,这意味着我应该直接访问传入缓冲区。我在用户 space 中使用 printf("%p\n",outbuffer); 来执行此操作,在内核 space 中使用等效的 printk。

所以,放慢速度并逐行测试模块帮助我解决了最初的问题,结果证明这是用户 space 应用程序中的错误。

FWIW,编译器从来没有给我关于在原始代码中使用 __user 的警告。如果它按照 Tsyvarev 在编译时建议的方式工作,那么追踪起来就会容易得多。

.write method of struct file_operations, which accepts pointer to user data, .write 方法不同,struct tty_operations 接受 指向内核数据的指针 ,并且这些数据可以通过 [=12= 等常用方法访问] 甚至直接。

现代内核使用__user属性标记user-space数据,并且在访问数据时(在编译时)检查该属性。因此,启用编译器警告将揭示来源不正确的数据的使用情况。