gpt4 book ai didi

c - 为什么在 ioctl 命令中从用户空间复制结构失败?

转载 作者:太空宇宙 更新时间:2023-11-04 05:31:21 25 4
gpt4 key购买 nike

我正在开发设备驱动程序并且需要使用 IOCTL。不幸的是我不能从用户空间复制一个结构。这是代码(经过简化,删除了错误处理):

结构

struct secvault_createoptions {
int secvaultId;
long dataSize;
char key[SECVAULT_KEYSIZE];
};

申请

void createSecvault(int secvaultId)
{
struct secvault_createoptions creationOptions;
/* fill with data */
sendIoctlCommand(SECVAULT_IOCTL_CREATE, &creationOptions);
}

void sendIoctlCommand(int command, void *arg)
{
FILE *stream;
int fd, err;

stream = fopen(SECVAULT_DEV_CONTROL, "r");
fd = fileno(stream);
ioctl(fd, command, arg);
fclose(stream);
}

内核模块

int control_device_ioctl(struct inode *node, struct file *filp, unsigned int cmd, unsigned long arg)
{
struct secvault_createoptions creationOptions;
int returnCode;

switch (cmd)
{
case SECVAULT_IOCTL_CREATE:
if (copy_from_user(&creationOptions, (void*)arg, sizeof(struct secvault_createoptions)) != sizeof(struct secvault_createoptions))
{
/* Always this branch gets executed */
printk(KERN_ALERT "Copying secure vault creation options from user space failed.\n");
returnCode = -EFAULT;
break;
}
printk(KERN_ALERT "2 IOCTL create request on control device received: secvaultId = %d, dataSize = %ld.\n",
creationOptions.secvaultId, creationOptions.dataSize);

returnCode = createDataDevice(&creationOptions);
break;
}
return returnCode;
}

最好的问候,
奥利弗·哈纳皮

最佳答案

您的 copy_from_user 调用是错误的。它不返回复制的字节数,而是返回未复制的字节数。你想要的是

if (copy_from_user(...) != 0)
return -EFAULT;

(您可以在代码段中跳过对 ret 的分配。)

关于c - 为什么在 ioctl 命令中从用户空间复制结构失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com