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

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