Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does copying a struct from user space fails in an ioctl command?
    text
    copied!<p>I'm developing a device driver and need to make use of IOCTL. Unfortunately I cannot copy a struct from user space. Here is the code (simplified, error handling removed):</p> <p><strong>Structure</strong></p> <pre><code>struct secvault_createoptions { int secvaultId; long dataSize; char key[SECVAULT_KEYSIZE]; }; </code></pre> <p><strong>Application</strong></p> <pre><code>void createSecvault(int secvaultId) { struct secvault_createoptions creationOptions; /* fill with data */ sendIoctlCommand(SECVAULT_IOCTL_CREATE, &amp;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); } </code></pre> <p><strong>Kernel Module</strong></p> <pre><code>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(&amp;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(&amp;creationOptions); break; } return returnCode; } </code></pre> <p>Best Regards,<br> Oliver Hanappi</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload