在内核中访问用户空间结构的成员会给出错误的值

Posted

技术标签:

【中文标题】在内核中访问用户空间结构的成员会给出错误的值【英文标题】:Accessing members of a userspace struct in the kernel give wrong values 【发布时间】:2012-09-10 15:18:30 【问题描述】:

我在我的代码输出中看到了一个我不理解的奇怪现象。我在头文件中定义了一个结构。我在用户空间中填充一个结构,然后通过 ioctl 将其发送到内核模块。内核模块应该从用户那里复制它,然后报告用户存储的值。

结构定义为:

typedef struct Command_par 
    int cmd;            /**< special driver command */
    int target;         /**< special configuration target */
    unsigned long val1;     /**< 1. parameter for the target */
    unsigned long val2;     /**< 2. parameter for the target */
    int error;          /**< return value */
    unsigned long retval;   /**< return value */
 Command_par_t ;

用户空间代码只是一个快速测试程序:

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "can4linux.h"  //can4linux is the standard can driver that comes
                        //with the uCLinux kernel (where this code was originally
                        //running before this port to a desktop machine

#define CAN_COMMAND 0
void main()

  long ret;
  Command_par_t cmd;
  int fd;
  //set and open the file descriptor to the device

  cmd.cmd = 2;
  cmd.target = 9; 
  cmd.val1 = 8;
  cmd.val2 = 7;
  cmd.error = 6;
  cmd.retval = 5;
  ret = ioctl(fd, CAN_COMMAND, &cmd);

内核模块通过ioctl获取打开/发送数据:

long can_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

  Command_par_t *myptr;
  myptr = (void *)kmalloc(sizeof(Command_par_t)+1,GFP_KERNEL );

  copy_from_user((void *)myptr, (void *)arg, sizeof(Command_par_t));

  printk("cmd = %d, target = %d, val1 = %d, val2 = %d, error = %d, retval = %d\n", 
         myptr->cmd, myptr->target, myptr->val1, myptr->val2, myptr->error, myptr->retval);

这里是可能的 IOCTL 命令在 can4linux.h 头文件中定义的地方:

---------- IOCTL requests */

#define COMMAND      0  /**< IOCTL command request */
#define CONFIG       1  /**< IOCTL configuration request */
#define SEND         2  /**< IOCTL request */
#define RECEIVE      3  /**< IOCTL request */
#define CONFIGURERTR     4  /**< IOCTL request */

所以显然这只是测试代码,我正在尝试调试一个更大的问题,但现在即使这也无法正常工作......我得到的输出是:

[217088.860190] cmd = 2, target = 1, val1 = 3, val2 = 134514688, error = 134514480, retval = 0

cmd 设置正确,之后其他所有内容都倾斜了...看起来它只传递了一个 int 然后我正在访问我的结构之外的内存?有人看到我在这里做错了吗?

【问题讨论】:

它是 32 位还是 64 位内核?这可能会影响unsigned long 的大小以及它的正确格式字符串。 "%lu"? 这是一个 32 位内核。 (技术上是在 64 位 Windows 7 机器上的 VirtualBox 中运行的 32 位 OpenSUSE 12.1 操作系统) 只是为了确认:uname -aLinux linux-4puc.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) i686 i686 i386 GNU/Linux @BoPersson - 现在我正在考虑它......这真的不是问题吗?如果 unsigned long 打乱了我的电话,我们至少不会看到 cmd 和 target 正确出现吗? 你能显示ioctl号的定义吗?还有你在用户空间中使用的包含? 【参考方案1】:

问题在于如何定义 ioctl 编号。这是从运行在某些嵌入式硬件上的 uCLinux (2.4) 到 OpenSUSE 发行版 (3.1) 的移植。我猜发生的事情是在我们幸运的较小的嵌入式平台上发生的,并且 ioctl 数被定义为简单的整数:

#define COMMAND      0  /**< IOCTL command request */ 
#define CONFIG       1  /**< IOCTL configuration request */ 
#define SEND         2  /**< IOCTL request */ 
#define RECEIVE      3  /**< IOCTL request */ 
#define CONFIGURERTR     4  /**< IOCTL request */ 

足够独特,不会引起问题。现在我正试图在完整的 linux 发行版上重新编译代码,系统中发生了太多其他事情,我们的命令被践踏了。现在重新定义 ioctl 编号:

#define GC_CAN_IOC_MAGIC  'z'       //!< Magic number - I guess this magic number can be chosen freely
#define GC_CAN_IOC_WRITE_COMMAND        _IOW(GC_CAN_IOC_MAGIC,  0, Command_par_t)

从用户空间传递的值被正确记录:

[220546.535115] cmd = 2, target = 9, val1 = 8, val2 = 7, error = 6, retval = 5

如果有人能给我一个比“踩踏”更好的答案,我很乐意对这个问题有更清晰的了解。另外,如果我仍然错误地设置了 IOCTL 编号,请有人纠正我。

【讨论】:

以上是关于在内核中访问用户空间结构的成员会给出错误的值的主要内容,如果未能解决你的问题,请参考以下文章

c语言中怎样通过索引获取结构体成员的值

firewall之iptables ,SNAT,DNAT

C++ 类中由具有重载隐式转换的空结构成员计算的成员

Linux内核空间-理解高端内存

如何理解进程结构体中的mm和active_mm?

如何理解进程结构体中的mm和active_mm?