Blame SOURCES/kvm-pr-helper-avoid-error-on-PR-IN-command-with-zero-req.patch

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