Skip to contentSkip to conversation

s390x: more interface hardening

PATCH In review Christian Borntraeger 3 participants 6 messages Jul 9 – Jul 10

lore
Share & tools
lore
#0 lore raw

cover s390x: more interface hardening

Some more fixes in the sclp resiliency

Christian Borntraeger (2):
s390x/sclp: prevent re-reading the sclp header s390x/sclpcpi: check event length field before reading from buffer

 hw/s390x/sclp.c    | 4 +++-
 hw/s390x/sclpcpi.c | 5 +++++
 2 files changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 s390x/sclp: prevent re-reading the sclp header

We verify the sccb length and then allocate based on that length. The following access re-reads the sccb again. This can race against other vCPUs overwriting the length field.

sclp_service_call_protected does not need a change as the ultravisor provides a consistent snapshot.

Fixes c1db53a5910f ("s390/sclp: read sccb from mem based on provided length")
---
 hw/s390x/sclp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/sclp.c +3 -1
index 2d2cde7803..3c8cb16488 100644
@@ -329,7 +329,8 @@ int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
     /*
      * we want to work on a private copy of the sccb, to prevent guests
      * from playing dirty tricks by modifying the memory content after
-     * the host has checked the values
+     * the host has checked the values.
+     * Reuse the previously fetched header
      */
     work_sccb = g_malloc0(be16_to_cpu(header.length));
     ret = address_space_read(as, sccb, attrs,
@@ -337,6 +338,7 @@ int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
     if (ret != MEMTX_OK) {
         return -PGM_ADDRESSING;
     }
+    work_sccb->h = header;
 
     if (!sclp_command_code_valid(code)) {
         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] s390x/sclp: prevent re-reading the sclp header

Quoted 7 lines · Christian Borntraeger

We verify the sccb length and then allocate based on that length. The following access re-reads the sccb again. This can race against other vCPUs overwriting the length field.

sclp_service_call_protected does not need a change as the ultravisor provides a consistent snapshot.

Quoted 7 lines
Fixes c1db53a5910f ("s390/sclp: read sccb from mem based on provided length")
---
 hw/s390x/sclp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] s390x/sclp: prevent re-reading the sclp header

In reply to Christian Borntraeger
Quoted 30 lines · Christian Borntraeger

We verify the sccb length and then allocate based on that length. The following access re-reads the sccb again. This can race against other vCPUs overwriting the length field.

sclp_service_call_protected does not need a change as the ultravisor provides a consistent snapshot.

Fixes c1db53a5910f ("s390/sclp: read sccb from mem based on provided length")
---
 hw/s390x/sclp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/sclp.c +2 -1
index 2d2cde7803..3c8cb16488 100644
@@ -329,7 +329,8 @@ int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
     /*
      * we want to work on a private copy of the sccb, to prevent guests
      * from playing dirty tricks by modifying the memory content after
-     * the host has checked the values
+     * the host has checked the values.
+     * Reuse the previously fetched header
      */
     work_sccb = g_malloc0(be16_to_cpu(header.length));
     ret = address_space_read(as, sccb, attrs,

hw/s390x/sclp.c int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code) · line 344

Quoted 7 lines
hw/s390x/sclp.c +1 -0
@@ -337,6 +338,7 @@ int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
     if (ret != MEMTX_OK) {
         return -PGM_ADDRESSING;
     }
+    work_sccb->h = header;
 
     if (!sclp_command_code_valid(code)) {
         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);

I guess the alternative (and messier) approach would be to re-validate the new header. This proposal looks good to me.

Signature · 2 lines
Regards,
  Collin
#4 lore raw

PATCH 2/2 s390x/sclpcpi: check event length field before reading from buffer

In reply to Christian Borntraeger

A guest might send a too short SCCB with SCLP_EVENT_CTRL_PGM_ID. QEMU would fill its data structures with garbage data. Check for the precise length of the CBI data structure and reject otherwise.

Fixes f345978f24be ("hw/s390x: add Control-Program Identification to QOM")
---
 hw/s390x/sclpcpi.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/s390x/sclpcpi.c +5 -0
index 68fc1b809b..ec4bdf2350 100644
@@ -97,6 +97,11 @@ static int write_event_data(SCLPEvent *event, EventBufferHeader *evt_buf_hdr)
                                              ebh);
     SCLPEventCPI *e = SCLP_EVENT_CPI(event);
 
+    /* Caller checks sccb length, buffer header checking is our duty */
+    if (be16_to_cpu(evt_buf_hdr->length) != sizeof(ControlProgramIdMsg)) {
+        return SCLP_RC_INCONSISTENT_LENGTHS;
+    }
+
     ascii_put(e->system_type, (char *)cpim->data.system_type,
               sizeof(cpim->data.system_type));
     ascii_put(e->system_name, (char *)cpim->data.system_name,
Signature · 1 line
2.53.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] s390x/sclpcpi: check event length field before reading from buffer

Quoted 4 lines · Christian Borntraeger

A guest might send a too short SCCB with SCLP_EVENT_CTRL_PGM_ID. QEMU would fill its data structures with garbage data. Check for the precise length of the CBI data structure and reject otherwise.

Quoted 8 lines
Fixes f345978f24be ("hw/s390x: add Control-Program Identification to QOM")
---
 hw/s390x/sclpcpi.c | 5 +++++
 1 file changed, 5 insertions(+)