IB/hfi1: Split copy_to_user data copy for better security
authorMichael J. Ruhl <michael.j.ruhl@intel.com>
Mon, 24 Jul 2017 14:46:42 +0000 (07:46 -0700)
committerDoug Ledford <dledford@redhat.com>
Mon, 31 Jul 2017 19:18:37 +0000 (15:18 -0400)
A copy_to_user() call assumes that two members of a data structure
are sequential.  Since this may not always be true, separate the copies
to ensure a safe copy.

Reviewed-by: Dennis Dalessandro <dennis.dalessandro@intel.com>
Signed-off-by: Michael J. Ruhl <michael.j.ruhl@intel.com>
Signed-off-by: Dennis Dalessandro <dennis.dalessandro@intel.com>
Signed-off-by: Doug Ledford <dledford@redhat.com>
drivers/infiniband/hw/hfi1/file_ops.c

index 7be75e0d4f7eebf4c149e4ae042ccf4b8fce83ca..650c1e578775ea608b4f98e407620ee589b6b00d 100644 (file)
@@ -268,12 +268,14 @@ static long hfi1_file_ioctl(struct file *fp, unsigned int cmd,
                        /*
                         * Copy the number of tidlist entries we used
                         * and the length of the buffer we registered.
-                        * These fields are adjacent in the structure so
-                        * we can copy them at the same time.
                         */
                        addr = arg + offsetof(struct hfi1_tid_info, tidcnt);
                        if (copy_to_user((void __user *)addr, &tinfo.tidcnt,
-                                        sizeof(tinfo.tidcnt) +
+                                        sizeof(tinfo.tidcnt)))
+                               return -EFAULT;
+
+                       addr = arg + offsetof(struct hfi1_tid_info, length);
+                       if (copy_to_user((void __user *)addr, &tinfo.length,
                                         sizeof(tinfo.length)))
                                ret = -EFAULT;
                }