]> git.hungrycats.org Git - linux/commitdiff
scsi: sg: check for valid direction before starting the request
authorJohannes Thumshirn <jthumshirn@suse.de>
Fri, 7 Apr 2017 07:34:15 +0000 (09:34 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 22 Mar 2018 08:37:15 +0000 (09:37 +0100)
[ Upstream commit 28676d869bbb5257b5f14c0c95ad3af3a7019dd5 ]

Check for a valid direction before starting the request, otherwise we
risk running into an assertion in the scsi midlayer checking for valid
requests.

[mkp: fixed typo]

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Link: http://www.spinics.net/lists/linux-scsi/msg104400.html
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Hannes Reinecke <hare@suse.com>
Tested-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/scsi/sg.c

index 783040af3ead7c1b412621d19b4094923699f7fe..bba2dcd4f3241626ddade9c4ce85d6fa45e5c0d5 100644 (file)
@@ -674,18 +674,14 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
         * is a non-zero input_size, so emit a warning.
         */
        if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
-               static char cmd[TASK_COMM_LEN];
-               if (strcmp(current->comm, cmd)) {
-                       printk_ratelimited(KERN_WARNING
-                                          "sg_write: data in/out %d/%d bytes "
-                                          "for SCSI command 0x%x-- guessing "
-                                          "data in;\n   program %s not setting "
-                                          "count and/or reply_len properly\n",
-                                          old_hdr.reply_len - (int)SZ_SG_HEADER,
-                                          input_size, (unsigned int) cmnd[0],
-                                          current->comm);
-                       strcpy(cmd, current->comm);
-               }
+               printk_ratelimited(KERN_WARNING
+                                  "sg_write: data in/out %d/%d bytes "
+                                  "for SCSI command 0x%x-- guessing "
+                                  "data in;\n   program %s not setting "
+                                  "count and/or reply_len properly\n",
+                                  old_hdr.reply_len - (int)SZ_SG_HEADER,
+                                  input_size, (unsigned int) cmnd[0],
+                                  current->comm);
        }
        k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
        return (k < 0) ? k : count;
@@ -764,6 +760,29 @@ sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
        return count;
 }
 
+static bool sg_is_valid_dxfer(sg_io_hdr_t *hp)
+{
+       switch (hp->dxfer_direction) {
+       case SG_DXFER_NONE:
+               if (hp->dxferp || hp->dxfer_len > 0)
+                       return false;
+               return true;
+       case SG_DXFER_TO_DEV:
+       case SG_DXFER_FROM_DEV:
+       case SG_DXFER_TO_FROM_DEV:
+               if (!hp->dxferp || hp->dxfer_len == 0)
+                       return false;
+               return true;
+       case SG_DXFER_UNKNOWN:
+               if ((!hp->dxferp && hp->dxfer_len) ||
+                   (hp->dxferp && hp->dxfer_len == 0))
+                       return false;
+               return true;
+       default:
+               return false;
+       }
+}
+
 static int
 sg_common_write(Sg_fd * sfp, Sg_request * srp,
                unsigned char *cmnd, int timeout, int blocking)
@@ -784,6 +803,9 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
                        "sg_common_write:  scsi opcode=0x%02x, cmd_size=%d\n",
                        (int) cmnd[0], (int) hp->cmd_len));
 
+       if (!sg_is_valid_dxfer(hp))
+               return -EINVAL;
+
        k = sg_start_req(srp, cmnd);
        if (k) {
                SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,