26ba25
From aec7894ed126514877d145ad4c3615a0b29e3443 Mon Sep 17 00:00:00 2001
26ba25
From: Paolo Bonzini <pbonzini@redhat.com>
26ba25
Date: Fri, 6 Jul 2018 17:56:57 +0200
26ba25
Subject: [PATCH 197/268] pr-helper: avoid error on PR IN command with zero
26ba25
 request size
26ba25
26ba25
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
26ba25
Message-id: <20180706175659.30615-8-pbonzini@redhat.com>
26ba25
Patchwork-id: 81250
26ba25
O-Subject: [RHEL7.6 qemu-kvm-rhev PATCH 7/9] pr-helper: avoid error on PR IN command with zero request size
26ba25
Bugzilla: 1533158
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Michal Privoznik <mprivozn@redhat.com>
26ba25
26ba25
After reading a PR IN command with zero request size in prh_read_request,
26ba25
the resp->result field will be uninitialized and the resp.sz field will
26ba25
be also uninitialized when returning to prh_co_entry.
26ba25
26ba25
If resp->result == GOOD (from a previous successful reply or just luck),
26ba25
then the assert in prh_write_response might not be triggered and
26ba25
uninitialized response will be sent.
26ba25
26ba25
The fix is to remove the whole handling of sz == 0 in prh_co_entry.
26ba25
Those errors apply only to PR OUT commands and it's perfectly okay to
26ba25
catch them later in do_pr_out and multipath_pr_out; the check for
26ba25
too-short parameters in fact doesn't apply in the easy SG_IO case, as
26ba25
it can be left to the target firmware even.
26ba25
26ba25
The result is that prh_read_request does not fail requests anymore and
26ba25
prh_co_entry becomes simpler.
26ba25
26ba25
Reported-by: Dima Stepanov <dimastep@yandex-team.ru>
26ba25
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
26ba25
(cherry picked from commit ee8c13b81474e002db083e9692b11c0e106a9c7f)
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 scsi/qemu-pr-helper.c | 63 ++++++++++++++++++++++++---------------------------
26ba25
 1 file changed, 30 insertions(+), 33 deletions(-)
26ba25
26ba25
diff --git a/scsi/qemu-pr-helper.c b/scsi/qemu-pr-helper.c
26ba25
index 0218d65..c89a446 100644
26ba25
--- a/scsi/qemu-pr-helper.c
26ba25
+++ b/scsi/qemu-pr-helper.c
26ba25
@@ -455,6 +455,14 @@ static int multipath_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
26ba25
     char transportids[PR_HELPER_DATA_SIZE];
26ba25
     int r;
26ba25
 
26ba25
+    if (sz < PR_OUT_FIXED_PARAM_SIZE) {
26ba25
+        /* Illegal request, Parameter list length error.  This isn't fatal;
26ba25
+         * we have read the data, send an error without closing the socket.
26ba25
+         */
26ba25
+        scsi_build_sense(sense, SENSE_CODE(INVALID_PARAM_LEN));
26ba25
+        return CHECK_CONDITION;
26ba25
+    }
26ba25
+
26ba25
     switch (rq_servact) {
26ba25
     case MPATH_PROUT_REG_SA:
26ba25
     case MPATH_PROUT_RES_SA:
26ba25
@@ -574,6 +582,12 @@ static int do_pr_out(int fd, const uint8_t *cdb, uint8_t *sense,
26ba25
                      const uint8_t *param, int sz)
26ba25
 {
26ba25
     int resp_sz;
26ba25
+
26ba25
+    if ((fcntl(fd, F_GETFL) & O_ACCMODE) == O_RDONLY) {
26ba25
+        scsi_build_sense(sense, SENSE_CODE(INVALID_OPCODE));
26ba25
+        return CHECK_CONDITION;
26ba25
+    }
26ba25
+
26ba25
 #ifdef CONFIG_MPATH
26ba25
     if (is_mpath(fd)) {
26ba25
         return multipath_pr_out(fd, cdb, sense, param, sz);
26ba25
@@ -690,21 +704,6 @@ static int coroutine_fn prh_read_request(PRHelperClient *client,
26ba25
                                  errp) < 0) {
26ba25
             goto out_close;
26ba25
         }
26ba25
-        if ((fcntl(client->fd, F_GETFL) & O_ACCMODE) == O_RDONLY) {
26ba25
-            scsi_build_sense(resp->sense, SENSE_CODE(INVALID_OPCODE));
26ba25
-            sz = 0;
26ba25
-        } else if (sz < PR_OUT_FIXED_PARAM_SIZE) {
26ba25
-            /* Illegal request, Parameter list length error.  This isn't fatal;
26ba25
-             * we have read the data, send an error without closing the socket.
26ba25
-             */
26ba25
-            scsi_build_sense(resp->sense, SENSE_CODE(INVALID_PARAM_LEN));
26ba25
-            sz = 0;
26ba25
-        }
26ba25
-        if (sz == 0) {
26ba25
-            resp->result = CHECK_CONDITION;
26ba25
-            close(client->fd);
26ba25
-            client->fd = -1;
26ba25
-        }
26ba25
     }
26ba25
 
26ba25
     req->fd = client->fd;
26ba25
@@ -785,25 +784,23 @@ static void coroutine_fn prh_co_entry(void *opaque)
26ba25
             break;
26ba25
         }
26ba25
 
26ba25
-        if (sz > 0) {
26ba25
-            num_active_sockets++;
26ba25
-            if (req.cdb[0] == PERSISTENT_RESERVE_OUT) {
26ba25
-                r = do_pr_out(req.fd, req.cdb, resp.sense,
26ba25
-                              client->data, sz);
26ba25
-                resp.sz = 0;
26ba25
-            } else {
26ba25
-                resp.sz = sizeof(client->data);
26ba25
-                r = do_pr_in(req.fd, req.cdb, resp.sense,
26ba25
-                             client->data, &resp.sz);
26ba25
-                resp.sz = MIN(resp.sz, sz);
26ba25
-            }
26ba25
-            num_active_sockets--;
26ba25
-            close(req.fd);
26ba25
-            if (r == -1) {
26ba25
-                break;
26ba25
-            }
26ba25
-            resp.result = r;
26ba25
+        num_active_sockets++;
26ba25
+        if (req.cdb[0] == PERSISTENT_RESERVE_OUT) {
26ba25
+            r = do_pr_out(req.fd, req.cdb, resp.sense,
26ba25
+                          client->data, sz);
26ba25
+            resp.sz = 0;
26ba25
+        } else {
26ba25
+            resp.sz = sizeof(client->data);
26ba25
+            r = do_pr_in(req.fd, req.cdb, resp.sense,
26ba25
+                         client->data, &resp.sz);
26ba25
+            resp.sz = MIN(resp.sz, sz);
26ba25
+        }
26ba25
+        num_active_sockets--;
26ba25
+        close(req.fd);
26ba25
+        if (r == -1) {
26ba25
+            break;
26ba25
         }
26ba25
+        resp.result = r;
26ba25
 
26ba25
         if (prh_write_response(client, &req, &resp, &local_err) < 0) {
26ba25
             break;
26ba25
-- 
26ba25
1.8.3.1
26ba25