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

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