yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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