Blame SOURCES/kvm-s390-sclp-read-sccb-from-mem-based-on-provided-lengt.patch

a19a21
From 212c129b82f0a53725a4167303de2ee0a865f82d Mon Sep 17 00:00:00 2001
a19a21
From: Thomas Huth <thuth@redhat.com>
a19a21
Date: Wed, 11 Nov 2020 12:03:08 -0500
a19a21
Subject: [PATCH 08/18] s390/sclp: read sccb from mem based on provided length
a19a21
a19a21
RH-Author: Thomas Huth <thuth@redhat.com>
a19a21
Message-id: <20201111120316.707489-5-thuth@redhat.com>
a19a21
Patchwork-id: 99501
a19a21
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 04/12] s390/sclp: read sccb from mem based on provided length
a19a21
Bugzilla: 1798506
a19a21
RH-Acked-by: Jens Freimann <jfreimann@redhat.com>
a19a21
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
a19a21
RH-Acked-by: David Hildenbrand <david@redhat.com>
a19a21
a19a21
From: Collin Walling <walling@linux.ibm.com>
a19a21
a19a21
The header contained within the SCCB passed to the SCLP service call
a19a21
contains the actual length of the SCCB. Instead of allocating a static
a19a21
4K size for the work sccb, let's allow for a variable size determined
a19a21
by the value in the header. The proper checks are already in place to
a19a21
ensure the SCCB length is sufficent to store a full response and that
a19a21
the length does not cross any explicitly-set boundaries.
a19a21
a19a21
Signed-off-by: Collin Walling <walling@linux.ibm.com>
a19a21
Reviewed-by: Thomas Huth <thuth@redhat.com>
a19a21
Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
a19a21
Message-Id: <20200915194416.107460-4-walling@linux.ibm.com>
a19a21
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
a19a21
(cherry picked from commit c1db53a5910f988eeb32f031c53a50f3373fd824)
a19a21
Signed-off-by: Thomas Huth <thuth@redhat.com>
a19a21
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
a19a21
---
a19a21
 hw/s390x/event-facility.c |  2 +-
a19a21
 hw/s390x/sclp.c           | 55 ++++++++++++++++++++++-----------------
a19a21
 include/hw/s390x/sclp.h   |  2 +-
a19a21
 3 files changed, 33 insertions(+), 26 deletions(-)
a19a21
a19a21
diff --git a/hw/s390x/event-facility.c b/hw/s390x/event-facility.c
a19a21
index 66205697ae7..8aa7017f06b 100644
a19a21
--- a/hw/s390x/event-facility.c
a19a21
+++ b/hw/s390x/event-facility.c
a19a21
@@ -215,7 +215,7 @@ static uint16_t handle_sccb_read_events(SCLPEventFacility *ef, SCCB *sccb,
a19a21
 
a19a21
     event_buf = &red->ebh;
a19a21
     event_buf->length = 0;
a19a21
-    slen = sizeof(sccb->data);
a19a21
+    slen = sccb_data_len(sccb);
a19a21
 
a19a21
     rc = SCLP_RC_NO_EVENT_BUFFERS_STORED;
a19a21
 
a19a21
diff --git a/hw/s390x/sclp.c b/hw/s390x/sclp.c
a19a21
index 38278497319..cf1292beb22 100644
a19a21
--- a/hw/s390x/sclp.c
a19a21
+++ b/hw/s390x/sclp.c
a19a21
@@ -231,25 +231,29 @@ int sclp_service_call_protected(CPUS390XState *env, uint64_t sccb,
a19a21
 {
a19a21
     SCLPDevice *sclp = get_sclp_device();
a19a21
     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
a19a21
-    SCCB work_sccb;
a19a21
-    hwaddr sccb_len = sizeof(SCCB);
a19a21
+    SCCBHeader header;
a19a21
+    g_autofree SCCB *work_sccb = NULL;
a19a21
 
a19a21
-    s390_cpu_pv_mem_read(env_archcpu(env), 0, &work_sccb, sccb_len);
a19a21
+    s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
a19a21
+
a19a21
+    work_sccb = g_malloc0(be16_to_cpu(header.length));
a19a21
+    s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
a19a21
+                         be16_to_cpu(header.length));
a19a21
 
a19a21
     if (!sclp_command_code_valid(code)) {
a19a21
-        work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
a19a21
+        work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
a19a21
         goto out_write;
a19a21
     }
a19a21
 
a19a21
-    if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb.h.length))) {
a19a21
-        work_sccb.h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
a19a21
+    if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb->h.length))) {
a19a21
+        work_sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
a19a21
         goto out_write;
a19a21
     }
a19a21
 
a19a21
-    sclp_c->execute(sclp, &work_sccb, code);
a19a21
+    sclp_c->execute(sclp, work_sccb, code);
a19a21
 out_write:
a19a21
-    s390_cpu_pv_mem_write(env_archcpu(env), 0, &work_sccb,
a19a21
-                          be16_to_cpu(work_sccb.h.length));
a19a21
+    s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
a19a21
+                          be16_to_cpu(work_sccb->h.length));
a19a21
     sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
a19a21
     return 0;
a19a21
 }
a19a21
@@ -258,9 +262,8 @@ int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
a19a21
 {
a19a21
     SCLPDevice *sclp = get_sclp_device();
a19a21
     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
a19a21
-    SCCB work_sccb;
a19a21
-
a19a21
-    hwaddr sccb_len = sizeof(SCCB);
a19a21
+    SCCBHeader header;
a19a21
+    g_autofree SCCB *work_sccb = NULL;
a19a21
 
a19a21
     /* first some basic checks on program checks */
a19a21
     if (env->psw.mask & PSW_MASK_PSTATE) {
a19a21
@@ -274,32 +277,36 @@ int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
a19a21
         return -PGM_SPECIFICATION;
a19a21
     }
a19a21
 
a19a21
+    /* the header contains the actual length of the sccb */
a19a21
+    cpu_physical_memory_read(sccb, &header, sizeof(SCCBHeader));
a19a21
+
a19a21
+    /* Valid sccb sizes */
a19a21
+    if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
a19a21
+        return -PGM_SPECIFICATION;
a19a21
+    }
a19a21
+
a19a21
     /*
a19a21
      * we want to work on a private copy of the sccb, to prevent guests
a19a21
      * from playing dirty tricks by modifying the memory content after
a19a21
      * the host has checked the values
a19a21
      */
a19a21
-    cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
a19a21
-
a19a21
-    /* Valid sccb sizes */
a19a21
-    if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader)) {
a19a21
-        return -PGM_SPECIFICATION;
a19a21
-    }
a19a21
+    work_sccb = g_malloc0(be16_to_cpu(header.length));
a19a21
+    cpu_physical_memory_read(sccb, work_sccb, be16_to_cpu(header.length));
a19a21
 
a19a21
     if (!sclp_command_code_valid(code)) {
a19a21
-        work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
a19a21
+        work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
a19a21
         goto out_write;
a19a21
     }
a19a21
 
a19a21
-    if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb.h.length))) {
a19a21
-        work_sccb.h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
a19a21
+    if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb->h.length))) {
a19a21
+        work_sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
a19a21
         goto out_write;
a19a21
     }
a19a21
 
a19a21
-    sclp_c->execute(sclp, &work_sccb, code);
a19a21
+    sclp_c->execute(sclp, work_sccb, code);
a19a21
 out_write:
a19a21
-    cpu_physical_memory_write(sccb, &work_sccb,
a19a21
-                              be16_to_cpu(work_sccb.h.length));
a19a21
+    cpu_physical_memory_write(sccb, work_sccb,
a19a21
+                              be16_to_cpu(work_sccb->h.length));
a19a21
 
a19a21
     sclp_c->service_interrupt(sclp, sccb);
a19a21
 
a19a21
diff --git a/include/hw/s390x/sclp.h b/include/hw/s390x/sclp.h
a19a21
index c0a3faa37d7..55f53a46540 100644
a19a21
--- a/include/hw/s390x/sclp.h
a19a21
+++ b/include/hw/s390x/sclp.h
a19a21
@@ -177,7 +177,7 @@ typedef struct IoaCfgSccb {
a19a21
 
a19a21
 typedef struct SCCB {
a19a21
     SCCBHeader h;
a19a21
-    char data[SCCB_DATA_LEN];
a19a21
+    char data[];
a19a21
  } QEMU_PACKED SCCB;
a19a21
 
a19a21
 #define TYPE_SCLP "sclp"
a19a21
-- 
a19a21
2.27.0
a19a21