Skip to contentSkip to conversation

s390x: even more hardening fixes

PATCH Superseded Eric Farman 4 participants 32 messages Sep 16 – Sep 21

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#18 lore raw

PATCH 6/9 hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl

In reply to Eric Farman

The ioctl to determine whether KVM_DEV_FLIC_CLEAR_IO_IRQ is present receives the contents of the attribute struct as its payload, rather than a pointer to the payload itself as is convention.

This may result in erroneous errors from the ioctl, which would result in clear_io_supported being false when it otherwise could be used (which is helpful in device recovery).

Fixes 9eccb8622c ("s390x/kvm: add interface for clearing IO irqs")
---
 hw/intc/s390_flic_kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +1 -1
index f833a3996a..5e6d422fba 100644
@@ -633,7 +633,7 @@ static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
     /* Check clear_io_irq support */
     test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
     flic_state->clear_io_supported = !ioctl(flic_state->fd,
-                                            KVM_HAS_DEVICE_ATTR, test_attr);
+                                            KVM_HAS_DEVICE_ATTR, &test_attr);
 }
 
 static void kvm_s390_flic_reset(DeviceState *dev)
Signature · 1 line
2.53.0
#29 lore raw

PATCH 9/9 s390x/sclp: Reject short headers in PV case

In reply to Eric Farman

An invalid length in an SCCB header correctly generates a specification exception for the regular (non-PV) codepath that processes an SCLP instruction intercept. The PV case doesn't, even though such a thing should already be sanitized. Since a short length will cause problems later down the line with the storage that will be allocated based on that size, put up a defensive check and return if it is too short.

Cc Janosch Frank <[email protected]>
Fixes c1db53a591 ("s390/sclp: read sccb from mem based on provided length")
---
 hw/s390x/sclp.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/s390x/sclp.c +6 -0
index d8b655efcc..ad8129a5d8 100644
@@ -277,6 +277,11 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
 
     s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
 
+    /* Do nothing if the header appears malformed */
+    if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
+        goto out_no_write;
+    }
+
     work_sccb = g_malloc0(be16_to_cpu(header.length));
     s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
                          be16_to_cpu(header.length));
@@ -290,6 +295,7 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
 out_write:
     s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
                           be16_to_cpu(header.length));
+out_no_write:
     sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
     return 0;
 }
Signature · 1 line
2.53.0
#10 lore raw

PATCH 3/9 virtio-ccw: Fix rc check in read configuration path

In reply to Eric Farman

Both the read and write config paths use wrappers around cds->op_handler, depending on the direction the I/O is flowing. But they expect opposite values in the returned variable to indicate success.

Fix this by unifying those checks, where CCW_CMD_READ_CONF needs to see !ret as success. The end result is some erroneous data in the SCSW response data, but the overall success/failure is correct.

Fixes d895d25ae2 ("s390x: css: report errors from ccw_dstream_read/write")
---
 hw/s390x/virtio-ccw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +1 -1
index d700db405b..4622c5d46d 100644
@@ -475,7 +475,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         } else {
             virtio_bus_get_vdev_config(&dev->bus, vdev->config);
             ret = ccw_dstream_write_buf(&sch->cds, vdev->config, len);
-            if (ret) {
+            if (!ret) {
                 sch->curr_status.scsw.count = ccw.count - len;
             }
         }
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/9 virtio-ccw: Limit ISC to MAX_ISC

In reply to Eric Farman

The interrupt subclass is often defined as a uint8, though in practice it will be within the range of just 0-7. Ensure that a guest-supplied subclass does not extend beyond its expected range, especially when used as an array index.

---
 hw/s390x/css.c        | 7 ++++++-
 hw/s390x/virtio-ccw.c | 6 ++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
hw/s390x/css.c +6 -1
index 76dbca3bb9..9da9128d88 100644
@@ -654,8 +654,13 @@ void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc)
     S390FLICState *fs = s390_get_flic();
     S390FLICStateClass *fsc = s390_get_flic_class(fs);
     uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
-    IoAdapter *adapter = channel_subsys.io_adapters[type][isc];
+    IoAdapter *adapter;
+
+    if (type >= CSS_IO_ADAPTER_TYPE_NUMS || isc > MAX_ISC) {
+        return;
+    }
 
+    adapter = channel_subsys.io_adapters[type][isc];
     if (!adapter) {
         return;
     }
hw/s390x/virtio-ccw.c +6 -0
index 30fb2c0681..eb6378ea4c 100644
@@ -46,6 +46,10 @@ static int virtio_ccw_dev_post_load(void *opaque, int version_id)
     CcwDevice *ccw_dev = CCW_DEVICE(dev);
     CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
 
+    if (dev->thinint_isc > MAX_ISC) {
+        return -EINVAL;
+    }
+
     ccw_dev->sch->driver_data = dev;
     if (ccw_dev->sch->thinint_active) {
         dev->routes.adapter.adapter_id = css_get_adapter_id(
@@ -659,6 +663,8 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         } else {
             if (ccw_dstream_read(&sch->cds, thinint)) {
                 ret = -EFAULT;
+            } else if (thinint.isc > MAX_ISC) {
+                ret = -ENOSYS;
             } else {
                 thinint.ind_bit = be64_to_cpu(thinint.ind_bit);
                 thinint.summary_indicator =
Signature · 1 line
2.53.0
#26 lore raw

PATCH 8/9 s390x/css: Expand check against no-data-count CCWs

In reply to Eric Farman

The channel subsystem will reject channel programs that incorporate 256 CCWs without a data transfer, but the existing check for this does so against a data address of zero. Expand the check to include other CCWs that are valid but would transfer no data.

Fixes e8601dd5d0 ("s390x/css: catch ccw sequence errors")
---
 hw/s390x/css.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/css.c +3 -1
index 9da9128d88..c38549c7be 100644
@@ -1016,7 +1016,9 @@ static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr,
 
     check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
 
-    if (!ccw.cda) {
+    if (!ccw.cda ||
+        (ccw.flags & CCW_FLAG_SKIP) ||
+        (ccw.cmd_code == CCW_CMD_NOOP)) {
         if (sch->ccw_no_data_cnt == 255) {
             return -EINVAL;
         }
Signature · 1 line
2.53.0
#0 lore raw

cover s390x: even more hardening fixes

Hi Christian, Matt,

Here's a small pile of additional hardening fixes for consideration.

Took a stab at what should be cc'd to stable vs what shouldn't be, but open to other opinions.

Eric Farman (9):
virtio-ccw: Limit ISC to MAX_ISC
virtio-ccw: Do not overwrite existing indicators
virtio-ccw: Fix rc check in read configuration path vfio/ap: Clear error object pointer after non-fatal errors vfio/ccw: Clear error object pointer after non-fatal errors hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl hw/intc/s390x: Fix check after g_try_realloc s390x/css: Expand check against no-data-count CCWs s390x/sclp: Reject short headers in PV case

 hw/intc/s390_flic_kvm.c |  4 ++--
 hw/s390x/css.c          | 11 +++++++++--
 hw/s390x/sclp.c         |  6 ++++++
 hw/s390x/virtio-ccw.c   | 20 ++++++++++++++++++--
 hw/vfio/ap.c            |  4 ++++
 hw/vfio/ccw.c           |  2 ++
 6 files changed, 41 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0
#23 lore raw

PATCH 7/9 hw/intc/s390x: Fix check after g_try_realloc

The loop to store pending IRQs uses g_try_realloc() to expand the buffer being used, but the returned address is stored in a pointer-to-pointer. Thus, the check needs to examine its contents and not the pointer itself.

Fixes 3a553fc658 ("s390x/kvm: implement floating-interrupt controller device")
---
 hw/intc/s390_flic_kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +1 -1
index 5e6d422fba..ce9c720512 100644
@@ -264,7 +264,7 @@ static int __get_all_irqs(KVMS390FLICState *flic,
         }
         len *= 2;
         *buf = g_try_realloc(*buf, len);
-        if (!buf) {
+        if (!*buf) {
             return -ENOMEM;
         }
     } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER);
Signature · 1 line
2.53.0
#6 lore raw

PATCH 2/9 virtio-ccw: Do not overwrite existing indicators

In reply to Eric Farman

The various indicators pointers allow them to be overwritten, but leak any existing structs that might be there. The adapter indicators path checks that it isn't converting from regular to thin interrupts, but a check in the reverse direction isn't performed.

Rather than unwinding the existing allocations to allow additional assignments, just reject these duplicates.

---
 hw/s390x/virtio-ccw.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +11 -1
index eb6378ea4c..d700db405b 100644
@@ -565,6 +565,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
             ret = -EINVAL;
             break;
         }
+        if (dev->indicators) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
         if (sch->thinint_active) {
             /* Trigger a command reject. */
             ret = -ENOSYS;
@@ -599,6 +604,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
             ret = -EINVAL;
             break;
         }
+        if (dev->indicators2) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
         if (!ccw.cda) {
             ret = -EFAULT;
         } else {
@@ -657,7 +667,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         }
         if (!ccw.cda) {
             ret = -EFAULT;
-        } else if (dev->indicators && !sch->thinint_active) {
+        } else if (dev->indicators || dev->summary_indicator) {
             /* Trigger a command reject. */
             ret = -ENOSYS;
         } else {
Signature · 1 line
2.53.0
#15 lore raw

PATCH 5/9 vfio/ccw: Clear error object pointer after non-fatal errors

In reply to Eric Farman

Just like in vfio/ap, there is a non-fatal message that could be generated, with a stale pointer in the error object variable.

Unlike vfio/ap, there is no secondary message that could attempt to use it. Clear it in the same way, in case that ever changes.

Fixes 6178d4689a ("vfio-ccw: Permit missing IRQs")
---
 hw/vfio/ccw.c | 2 ++
 1 file changed, 2 insertions(+)
hw/vfio/ccw.c +2 -0
index c3dc7c1962..8e29a3ae61 100644
@@ -604,8 +604,10 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;
     }
 
     return;
Signature · 1 line
2.53.0
#12 lore raw

PATCH 4/9 vfio/ap: Clear error object pointer after non-fatal errors

In reply to Eric Farman

Registering a new IRQ notifier will allocate an error object if needed, and warn_report_err() will release that memory once it's finished. The pointer remains, however, so subsequent errors after the non-fatal errors in vfio_ap_realize() would cause an assertion failure for a non-NULL pointer.

Fix this by clearing the pointer once the warning has been reported, to reset the state of the error objects.

Cc Anthony Krowiak <[email protected]>
Fixes 1360b2ad1f ("s390x/ap: Wire up the device request notifier interface")
Fixes 0fb8a62fe4 ("hw/vfio/ap: notification handler for AP config changed event")
---
 hw/vfio/ap.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/ap.c +4 -0
index 6e2a1223ea..2926e04ad4 100644
@@ -247,8 +247,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;
     }
 
     if (!vfio_ap_register_irq_notifier(vapdev, VFIO_AP_CFG_CHG_IRQ_INDEX, &err))
@@ -256,8 +258,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;
     }
 
     return;
Signature · 1 line
2.53.0
Reviewed-by on v1 3/9
#11 lore raw

Re: [PATCH 3/9] virtio-ccw: Fix rc check in read configuration path

In reply to Eric Farman
Quoted 11 lines · Eric Farman

Both the read and write config paths use wrappers around cds->op_handler, depending on the direction the I/O is flowing. But they expect opposite values in the returned variable to indicate success.

Fix this by unifying those checks, where CCW_CMD_READ_CONF needs to see !ret as success. The end result is some erroneous data in the SCSW response data, but the overall success/failure is correct.

Fixes d895d25ae2 ("s390x: css: report errors from ccw_dstream_read/write")
Quoted 17 lines
---
 hw/s390x/virtio-ccw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +1 -1
index d700db405b..4622c5d46d 100644
@@ -475,7 +475,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         } else {
             virtio_bus_get_vdev_config(&dev->bus, vdev->config);
             ret = ccw_dstream_write_buf(&sch->cds, vdev->config, len);
-            if (ret) {
+            if (!ret) {
                 sch->curr_status.scsw.count = ccw.count - len;
             }
         }
Reviewed-by on v1 4/9
#13 lore raw

Re: [PATCH 4/9] vfio/ap: Clear error object pointer after non-fatal errors

In reply to Eric Farman
Quoted 34 lines · Eric Farman

Registering a new IRQ notifier will allocate an error object if needed, and warn_report_err() will release that memory once it's finished. The pointer remains, however, so subsequent errors after the non-fatal errors in vfio_ap_realize() would cause an assertion failure for a non-NULL pointer.

Fix this by clearing the pointer once the warning has been reported, to reset the state of the error objects.

Cc Anthony Krowiak <[email protected]>
Fixes 1360b2ad1f ("s390x/ap: Wire up the device request notifier interface")
Fixes 0fb8a62fe4 ("hw/vfio/ap: notification handler for AP config changed event")
---
 hw/vfio/ap.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/ap.c +2 -0
index 6e2a1223ea..2926e04ad4 100644
@@ -247,8 +247,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;
     }
 
     if (!vfio_ap_register_irq_notifier(vapdev, VFIO_AP_CFG_CHG_IRQ_INDEX, &err))

hw/vfio/ap.c static void vfio_ap_realize(DeviceState *dev, Error **errp) · line 264

Quoted 7 lines
hw/vfio/ap.c +2 -0
@@ -256,8 +258,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;

Technically this one is unnecessary since we're about to return, but I like it for future-proofing:

Reviewed-by on v1 5/9
#16 lore raw

Re: [PATCH 5/9] vfio/ccw: Clear error object pointer after non-fatal errors

In reply to Eric Farman
Quoted 18 lines · Eric Farman

Just like in vfio/ap, there is a non-fatal message that could be generated, with a stale pointer in the error object variable.

Unlike vfio/ap, there is no secondary message that could attempt to use it. Clear it in the same way, in case that ever changes.

Fixes 6178d4689a ("vfio-ccw: Permit missing IRQs")
---
 hw/vfio/ccw.c | 2 ++
 1 file changed, 2 insertions(+)
hw/vfio/ccw.c +0 -0
index c3dc7c1962..8e29a3ae61 100644

hw/vfio/ccw.c static void vfio_ccw_realize(DeviceState *dev, Error **errp) · line 610

Quoted 7 lines
hw/vfio/ccw.c +2 -0
@@ -604,8 +604,10 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)
         /*
          * Report this error, but do not make it a failing condition.
          * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
          */
         warn_report_err(err);
+        err = NULL;

OK, so this patch is ONLY a future-proofing change since err goes out of scope right after this.
I'm OK with the change from a future-proofing perspective, but IMO it does not merit a Fixes or a CC stable.

With those removed:

Quoted 3 lines
hw/vfio/ccw.c +0 -0
     }
 
     return;
Reviewed-by on v1 9/9
#30 lore raw

Re: [PATCH 9/9] s390x/sclp: Reject short headers in PV case

In reply to Eric Farman
Quoted 10 lines · Eric Farman

An invalid length in an SCCB header correctly generates a specification exception for the regular (non-PV) codepath that processes an SCLP instruction intercept. The PV case doesn't, even though such a thing should already be sanitized. Since a short length will cause problems later down the line with the storage that will be allocated based on that size, put up a defensive check and return if it is too short.

Cc Janosch Frank <[email protected]>
Fixes c1db53a591 ("s390/sclp: read sccb from mem based on provided length")

Seems like cc stable would make sense here, it sounds exploitable if the sanitization assumption were to fall apart.

Quoted 28 lines
---
 hw/s390x/sclp.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/s390x/sclp.c +6 -0
index d8b655efcc..ad8129a5d8 100644
@@ -277,6 +277,11 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
 
     s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
 
+    /* Do nothing if the header appears malformed */
+    if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
+        goto out_no_write;
+    }
+
     work_sccb = g_malloc0(be16_to_cpu(header.length));
     s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
                          be16_to_cpu(header.length));
@@ -290,6 +295,7 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
 out_write:
     s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
                           be16_to_cpu(header.length));
+out_no_write:
     sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
     return 0;
 }
Reviewed-by on v1 6/9
#19 lore raw

Re: [PATCH 6/9] hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl

In reply to Eric Farman
Quoted 20 lines · Eric Farman

The ioctl to determine whether KVM_DEV_FLIC_CLEAR_IO_IRQ is present receives the contents of the attribute struct as its payload, rather than a pointer to the payload itself as is convention.

This may result in erroneous errors from the ioctl, which would result in clear_io_supported being false when it otherwise could be used (which is helpful in device recovery).

Fixes 9eccb8622c ("s390x/kvm: add interface for clearing IO irqs")
---
 hw/intc/s390_flic_kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +0 -0
index f833a3996a..5e6d422fba 100644

hw/intc/s390_flic_kvm.c static void kvm_s390_flic_realize(DeviceState *dev, Error **errp) · line 636

Quoted 5 lines
hw/intc/s390_flic_kvm.c +1 -1
@@ -633,7 +633,7 @@ static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
     /* Check clear_io_irq support */
     test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
     flic_state->clear_io_supported = !ioctl(flic_state->fd,
-                                            KVM_HAS_DEVICE_ATTR, test_attr);
+                                            KVM_HAS_DEVICE_ATTR, &test_attr);

But AFAICT this means we've never actually driven KVM_DEV_FLIC_CLEAR_IO_IRQ and instead always took the -ENOYSYS in kvm_s390_clear_io_flic() -- so probably want to make sure we test this path.

Quoted 3 lines
hw/intc/s390_flic_kvm.c +0 -0
 }
 
 static void kvm_s390_flic_reset(DeviceState *dev)
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] virtio-ccw: Limit ISC to MAX_ISC

In reply to Eric Farman
Quoted 16 lines · Eric Farman

The interrupt subclass is often defined as a uint8, though in practice it will be within the range of just 0-7. Ensure that a guest-supplied subclass does not extend beyond its expected range, especially when used as an array index.

---
 hw/s390x/css.c        | 7 ++++++-
 hw/s390x/virtio-ccw.c | 6 ++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
hw/s390x/css.c +0 -0
index 76dbca3bb9..9da9128d88 100644

hw/s390x/css.c void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc) · line 657

Quoted 4 lines
hw/s390x/css.c +0 -1
@@ -654,8 +654,13 @@ void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc)
     S390FLICState *fs = s390_get_flic();
     S390FLICStateClass *fsc = s390_get_flic_class(fs);
     uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
-    IoAdapter *adapter = channel_subsys.io_adapters[type][isc];

I think this deserve a fixes/cc stable. This would have been an out-of-range access before this patch.

The initial support didn't have the concept of MAX_ISC, it showed up in

dde522bbc5 ("s390x: register I/O adapters per ISC during init")

Frankly I think at that point we should have started fencing registration > MAX_ISC already, but that patch DID locally fence its new array indexes against MAX_ISC. It wasn't until

25a08b8ded ("s390x/css: update css_adapter_interrupt")

when the above array index was added without the fence that you are now adding. So I think 25a08b8ded is really the culprit.

Quoted 15 lines
hw/s390x/css.c +6 -0
+    IoAdapter *adapter;
+
+    if (type >= CSS_IO_ADAPTER_TYPE_NUMS || isc > MAX_ISC) {
+        return;
+    }
 
+    adapter = channel_subsys.io_adapters[type][isc];
     if (!adapter) {
         return;
     }
hw/s390x/virtio-ccw.c +0 -0
index 30fb2c0681..eb6378ea4c 100644

hw/s390x/virtio-ccw.c static int virtio_ccw_dev_post_load(void *opaque, int version_id) · line 51

Quoted 6 lines
hw/s390x/virtio-ccw.c +3 -0
@@ -46,6 +46,10 @@ static int virtio_ccw_dev_post_load(void *opaque, int version_id)
     CcwDevice *ccw_dev = CCW_DEVICE(dev);
     CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
 
+    if (dev->thinint_isc > MAX_ISC) {
+        return -EINVAL;
+    }

Is this safe to check before thinint_active?

I guess if we get here before an ISC was ever set it would just be zero-initialized so it wouldn't erroneously trigger this check.

Assuming your reasoning matches mine:

Quoted 13 lines
hw/s390x/virtio-ccw.c +3 -0
+
     ccw_dev->sch->driver_data = dev;
     if (ccw_dev->sch->thinint_active) {
         dev->routes.adapter.adapter_id = css_get_adapter_id(
@@ -659,6 +663,8 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         } else {
             if (ccw_dstream_read(&sch->cds, thinint)) {
                 ret = -EFAULT;
+            } else if (thinint.isc > MAX_ISC) {
+                ret = -ENOSYS;
             } else {
                 thinint.ind_bit = be64_to_cpu(thinint.ind_bit);
                 thinint.summary_indicator =
#24 lore raw

Re: [PATCH 7/9] hw/intc/s390x: Fix check after g_try_realloc

In reply to Eric Farman
Quoted 17 lines · Eric Farman

The loop to store pending IRQs uses g_try_realloc() to expand the buffer being used, but the returned address is stored in a pointer-to-pointer. Thus, the check needs to examine its contents and not the pointer itself.

Fixes 3a553fc658 ("s390x/kvm: implement floating-interrupt controller device")
---
 hw/intc/s390_flic_kvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +0 -0
index 5e6d422fba..ce9c720512 100644

hw/intc/s390_flic_kvm.c static int __get_all_irqs(KVMS390FLICState *flic, · line 266

Quoted 3 lines
hw/intc/s390_flic_kvm.c +0 -0
@@ -264,7 +264,7 @@ static int __get_all_irqs(KVMS390FLICState *flic,
         }
         len *= 2;
         *buf = g_try_realloc(*buf, len);

Aren't we also leaking the old *buf if this fails?

I think you need a local void *foo here to catch the return..

hw/intc/s390_flic_kvm.c static int __get_all_irqs(KVMS390FLICState *flic, · line 269

Quoted 4 lines
hw/intc/s390_flic_kvm.c +1 -1
-        if (!buf) {
+        if (!*buf) {
             return -ENOMEM;
         }

And then here a *buf = foo; to only overwrite *buf in the success case.

Quoted 1 line
hw/intc/s390_flic_kvm.c +0 -0
     } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER);
Reviewed-by on v1 2/9
#7 lore raw

Re: [PATCH 2/9] virtio-ccw: Do not overwrite existing indicators

In reply to Eric Farman
Quoted 5 lines · Eric Farman

The various indicators pointers allow them to be overwritten, but leak any existing structs that might be there. The adapter indicators path checks that it isn't converting from regular to thin interrupts, but a check in the reverse direction isn't performed.

guest-triggered memory leak = fixes/stable?

I think
7bca3892cb ("s390x/virtio-ccw: reference-counted indicators") when we started doing dynamic allocation

Quoted 46 lines

Rather than unwinding the existing allocations to allow additional assignments, just reject these duplicates.

---
 hw/s390x/virtio-ccw.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +11 -1
index eb6378ea4c..d700db405b 100644
@@ -565,6 +565,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
             ret = -EINVAL;
             break;
         }
+        if (dev->indicators) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
         if (sch->thinint_active) {
             /* Trigger a command reject. */
             ret = -ENOSYS;
@@ -599,6 +604,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
             ret = -EINVAL;
             break;
         }
+        if (dev->indicators2) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
         if (!ccw.cda) {
             ret = -EFAULT;
         } else {
@@ -657,7 +667,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
         }
         if (!ccw.cda) {
             ret = -EFAULT;
-        } else if (dev->indicators && !sch->thinint_active) {
+        } else if (dev->indicators || dev->summary_indicator) {
             /* Trigger a command reject. */
             ret = -ENOSYS;
         } else {
#27 lore raw

Re: [PATCH 8/9] s390x/css: Expand check against no-data-count CCWs

In reply to Eric Farman
Quoted 6 lines · Eric Farman

The channel subsystem will reject channel programs that incorporate 256 CCWs without a data transfer, but the existing check for this does so against a data address of zero. Expand the check to include other CCWs that are valid but would transfer no data.

Fixes e8601dd5d0 ("s390x/css: catch ccw sequence errors")

guest-triggerable, cc stable

Quoted 10 lines
---
 hw/s390x/css.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/css.c +0 -0
index 9da9128d88..c38549c7be 100644

hw/s390x/css.c static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr, · line 1021

Quoted 7 lines
hw/s390x/css.c +3 -1
@@ -1016,7 +1016,9 @@ static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr,
 
     check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
 
-    if (!ccw.cda) {
+    if (!ccw.cda ||
+        (ccw.flags & CCW_FLAG_SKIP) ||
+        (ccw.cmd_code == CCW_CMD_NOOP)) {

I'm not sure if this list is complete...

I wonder if we could we key off of the number of bytes transferred e.g. sch->cds.at_byte == 0 instead? Would have to happen at the end of this function e.g. in the ret == 0 check maybe?

Quoted 3 lines
hw/s390x/css.c +0 -0
         if (sch->ccw_no_data_cnt == 255) {
             return -EINVAL;
         }
#8 lore raw

Re: [PATCH 2/9] virtio-ccw: Do not overwrite existing indicators

In reply to Eric Farman

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 43 lines

The various indicators pointers allow them to be overwritten, but leak any existing structs that might be there. The adapter indicators path checks that it isn't converting from regular to thin interrupts, but a check in the reverse direction isn't performed.

Rather than unwinding the existing allocations to allow additional assignments, just reject these duplicates.

---
  hw/s390x/virtio-ccw.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +10 -0
index eb6378ea4c..d700db405b 100644
@@ -565,6 +565,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
              ret = -EINVAL;
              break;
          }
+        if (dev->indicators) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
          if (sch->thinint_active) {
              /* Trigger a command reject. */
              ret = -ENOSYS;
@@ -599,6 +604,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
              ret = -EINVAL;
              break;
          }
+        if (dev->indicators2) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
          if (!ccw.cda) {
              ret = -EFAULT;
          } else {

hw/s390x/virtio-ccw.c static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) · line 673

Quoted 8 lines
hw/s390x/virtio-ccw.c +1 -1
@@ -657,7 +667,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
          }
          if (!ccw.cda) {
              ret = -EFAULT;
-        } else if (dev->indicators && !sch->thinint_active) {
+        } else if (dev->indicators || dev->summary_indicator) {
              /* Trigger a command reject. */
              ret = -ENOSYS;
          } else {

I think we have to do the full unwind.
The Linux driver deregisters indicators by issuing SET_IND with address 0, or SET_IND_ADAPTER with device indicator 0. The virtio drivers reset the device before del_vqs, so the deregistration lands on a clean state and QEMU records an indicator at address 0. A later rebind or module reload does not reset again, so the next SET_IND_ADAPTER and the SET_IND fallback are both rejected and the probe fails. The old condition on SET_IND_ADAPTER existed precisely to let the deregistration through while thinint is active. The fix should release the existing indicator before overwriting, or treat address 0 as deregistration. Releasing before overwriting also handles the find_vqs error path in the guest, which deregisters without a reset in between.

#3 lore raw

Re: [PATCH 1/9] virtio-ccw: Limit ISC to MAX_ISC

In reply to Matthew Rosato

Am 17.09.26 um 01:15 schrieb Matthew Rosato:

Quoted 25 lines · Eric Farman
Quoted 16 lines

The interrupt subclass is often defined as a uint8, though in practice it will be within the range of just 0-7. Ensure that a guest-supplied subclass does not extend beyond its expected range, especially when used as an array index.

---
  hw/s390x/css.c        | 7 ++++++-
  hw/s390x/virtio-ccw.c | 6 ++++++
  2 files changed, 12 insertions(+), 1 deletion(-)
hw/s390x/css.c +0 -0
index 76dbca3bb9..9da9128d88 100644

hw/s390x/css.c void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc) · line 657

Quoted 4 lines
hw/s390x/css.c +0 -1
@@ -654,8 +654,13 @@ void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc)
      S390FLICState *fs = s390_get_flic();
      S390FLICStateClass *fsc = s390_get_flic_class(fs);
      uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
-    IoAdapter *adapter = channel_subsys.io_adapters[type][isc];

I think this deserve a fixes/cc stable. This would have been an out-of-range access before this patch.

Quoted 3 lines

The initial support didn't have the concept of MAX_ISC, it showed up in

dde522bbc5 ("s390x: register I/O adapters per ISC during init")

Yes.

#20 lore raw

Re: [PATCH 6/9] hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl

In reply to Eric Farman

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 20 lines

The ioctl to determine whether KVM_DEV_FLIC_CLEAR_IO_IRQ is present receives the contents of the attribute struct as its payload, rather than a pointer to the payload itself as is convention.

This may result in erroneous errors from the ioctl, which would result in clear_io_supported being false when it otherwise could be used (which is helpful in device recovery).

Fixes 9eccb8622c ("s390x/kvm: add interface for clearing IO irqs")
---
  hw/intc/s390_flic_kvm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +0 -0
index f833a3996a..5e6d422fba 100644

hw/intc/s390_flic_kvm.c static void kvm_s390_flic_realize(DeviceState *dev, Error **errp) · line 639

Quoted 8 lines
hw/intc/s390_flic_kvm.c +1 -1
@@ -633,7 +633,7 @@ static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
      /* Check clear_io_irq support */
      test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
      flic_state->clear_io_supported = !ioctl(flic_state->fd,
-                                            KVM_HAS_DEVICE_ATTR, test_attr);
+                                            KVM_HAS_DEVICE_ATTR, &test_attr);
  }
 
  static void kvm_s390_flic_reset(DeviceState *dev)

Certainly a correct fix but maybe its not as bad. Aggregates > 8 byte are passed as reference. Looks like the third argument is a pointer to a stack copy when testing, so the old code might have worked by accident.

     f8c:       d2 0f f0 a8 f0 d8       mvc     168(16,%r15),216(%r15) <--- stack copy
     f92:       b9 14 00 20             lgfr    %r2,%r0
     f96:       41 40 f0 a8             la      %r4,168(%r15) <-- pointer
     f9a:       c0 31 40 18 ae e3       lgfi    %r3,1075359459
     fa0:       d2 07 f0 b8 f0 e8       mvc     184(8,%r15),232(%r15)
     fa6:       c0 e5 00 00 00 00       brasl   %r14,fa6 <kvm_s390_flic_realize+0x136>
                        fa8: R_390_PLT32DBL     ioctl+0x2
                                            KVM_HAS_DEVICE_ATTR, test_attr);
Acked-by on v1 9/9
#31 lore raw

Re: [PATCH 9/9] s390x/sclp: Reject short headers in PV case

In reply to Eric Farman

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 6 lines

An invalid length in an SCCB header correctly generates a specification exception for the regular (non-PV) codepath that processes an SCLP instruction intercept. The PV case doesn't, even though such a thing should already be sanitized. Since a short length will cause problems later down the line with the storage that will be allocated based on that size, put up a defensive check and return if it is too short.

I think the ultravisor will already sanitize this, but better save than sorry

Quoted 32 lines
Cc Janosch Frank <[email protected]>
Fixes c1db53a591 ("s390/sclp: read sccb from mem based on provided length")
---
  hw/s390x/sclp.c | 6 ++++++
  1 file changed, 6 insertions(+)
hw/s390x/sclp.c +6 -0
index d8b655efcc..ad8129a5d8 100644
@@ -277,6 +277,11 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
 
      s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
 
+    /* Do nothing if the header appears malformed */
+    if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
+        goto out_no_write;
+    }
+
      work_sccb = g_malloc0(be16_to_cpu(header.length));
      s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
                           be16_to_cpu(header.length));
@@ -290,6 +295,7 @@ int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
  out_write:
      s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
                            be16_to_cpu(header.length));
+out_no_write:
      sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
      return 0;
  }
#4 lore raw

Re: [PATCH 1/9] virtio-ccw: Limit ISC to MAX_ISC

In reply to Matthew Rosato
Quoted 12 lines · Matthew Rosato
Quoted 11 lines · Eric Farman

The interrupt subclass is often defined as a uint8, though in practice it will be within the range of just 0-7. Ensure that a guest-supplied subclass does not extend beyond its expected range, especially when used as an array index.

---
  hw/s390x/css.c        | 7 ++++++-
  hw/s390x/virtio-ccw.c | 6 ++++++
  2 files changed, 12 insertions(+), 1 deletion(-)
Quoted 12 lines

hw/s390x/css.c void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc) · line 657

Quoted 9 lines
hw/s390x/css.c +0 -1
index 76dbca3bb9..9da9128d88 100644
@@ -654,8 +654,13 @@ void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc)
      S390FLICState *fs = s390_get_flic();
      S390FLICStateClass *fsc = s390_get_flic_class(fs);
      uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
-    IoAdapter *adapter = channel_subsys.io_adapters[type][isc];

I think this deserve a fixes/cc stable. This would have been an out-of-range access before this patch.

Ack.

Quoted 5 lines

The initial support didn't have the concept of MAX_ISC, it showed up in

dde522bbc5 ("s390x: register I/O adapters per ISC during init")

Quoted 8 lines

Frankly I think at that point we should have started fencing registration > MAX_ISC already, but that patch DID locally fence its new array indexes against MAX_ISC. It wasn't until

25a08b8ded ("s390x/css: update css_adapter_interrupt")

when the above array index was added without the fence that you are now adding. So I think 25a08b8ded is really the culprit.

I was considering 25a08b8ded, since the array indexing was the impetus for this patch. So will go with that.

Quoted 19 lines
Quoted 19 lines
hw/s390x/css.c +6 -0
+    IoAdapter *adapter;
+
+    if (type >= CSS_IO_ADAPTER_TYPE_NUMS || isc > MAX_ISC) {
+        return;
+    }
 
+    adapter = channel_subsys.io_adapters[type][isc];
      if (!adapter) {
          return;
      }
hw/s390x/virtio-ccw.c +0 -0
index 30fb2c0681..eb6378ea4c 100644
@@ -46,6 +46,10 @@ static int virtio_ccw_dev_post_load(void *opaque, int version_id)
      CcwDevice *ccw_dev = CCW_DEVICE(dev);
      CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
 
Quoted 8 lines

hw/s390x/virtio-ccw.c static int virtio_ccw_dev_post_load(void *opaque, int version_id) · line 51

Quoted 3 lines
hw/s390x/virtio-ccw.c +3 -0
+    if (dev->thinint_isc > MAX_ISC) {
+        return -EINVAL;
+    }

Is this safe to check before thinint_active?

I guess if we get here before an ISC was ever set it would just be zero-initialized so it wouldn't erroneously trigger this check.

That was my reasoning. I thought that adding it outside the thinint_active block would highlight potential shenanigans, even though that variable would end up being ignored in that case. Of course...

Quoted 9 lines

Assuming your reasoning matches mine:

Quoted 4 lines
hw/s390x/virtio-ccw.c +1 -0
+
      ccw_dev->sch->driver_data = dev;
      if (ccw_dev->sch->thinint_active) {
          dev->routes.adapter.adapter_id = css_get_adapter_id(

... the above css_get_adapter_id() call will already check that it's within MAX_ISC, so it's probably unnecessary.

Quoted 10 lines
Quoted 9 lines
hw/s390x/virtio-ccw.c +2 -0
@@ -659,6 +663,8 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
          } else {
              if (ccw_dstream_read(&sch->cds, thinint)) {
                  ret = -EFAULT;
+            } else if (thinint.isc > MAX_ISC) {
+                ret = -ENOSYS;
              } else {
                  thinint.ind_bit = be64_to_cpu(thinint.ind_bit);
                  thinint.summary_indicator =
#21 lore raw

Re: [PATCH 6/9] hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl

In reply to Christian Borntraeger
Quoted 29 lines · Christian Borntraeger

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 26 lines

The ioctl to determine whether KVM_DEV_FLIC_CLEAR_IO_IRQ is present receives the contents of the attribute struct as its payload, rather than a pointer to the payload itself as is convention.

This may result in erroneous errors from the ioctl, which would result in clear_io_supported being false when it otherwise could be used (which is helpful in device recovery).

Fixes 9eccb8622c ("s390x/kvm: add interface for clearing IO irqs")

---
  hw/intc/s390_flic_kvm.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/s390_flic_kvm.c +0 -1
index f833a3996a..5e6d422fba 100644
@@ -633,7 +633,7 @@ static void kvm_s390_flic_realize(DeviceState 
 *dev, Error **errp)
       /* Check clear_io_irq support */
       test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
       flic_state->clear_io_supported = !ioctl(flic_state->fd,
-                                            KVM_HAS_DEVICE_ATTR, 
 test_attr);
Quoted 8 lines
Quoted 4 lines
hw/intc/s390_flic_kvm.c +1 -0
+                                            KVM_HAS_DEVICE_ATTR, 
 &test_attr);

  }
  static void kvm_s390_flic_reset(DeviceState *dev)

Certainly a correct fix but maybe its not as bad. Aggregates > 8 byte are passed as reference. Looks like the third argument is a pointer to a stack copy when testing, so the old code might have worked by accident.

Ah, thanks for the assembler, I should have looked at that.

As I was testing this, I became worried that this whole part of error recovery was broken, but I was finding that clear_io_supported was already true even without this patch. I presumed ioctl magic, and that seems to be the case.

Maybe drop the stable tags from this one then, since it's working "by design"

Quoted 13 lines

     f8c:       d2 0f f0 a8 f0 d8       mvc     168(16,%r15),216(%r15)
<--- stack copy
     f92:       b9 14 00 20             lgfr    %r2,%r0
     f96:       41 40 f0 a8             la      %r4,168(%r15) <-- pointer f9a:       c0 31 40 18 ae e3       lgfi    %r3,1075359459 fa0:       d2 07 f0 b8 f0 e8       mvc     184(8,%r15),232(%r15) fa6:       c0 e5 00 00 00 00       brasl   %r14,fa6
<kvm_s390_flic_realize+0x136>
                        fa8: R_390_PLT32DBL     ioctl+0x2 KVM_HAS_DEVICE_ATTR,
test_attr);

#17 lore raw

Re: [PATCH 5/9] vfio/ccw: Clear error object pointer after non-fatal errors

In reply to Matthew Rosato
Quoted 23 lines · Matthew Rosato
Quoted 22 lines · Eric Farman

Just like in vfio/ap, there is a non-fatal message that could be generated, with a stale pointer in the error object variable.

Unlike vfio/ap, there is no secondary message that could attempt to use it. Clear it in the same way, in case that ever changes.

Fixes 6178d4689a ("vfio-ccw: Permit missing IRQs")
---
  hw/vfio/ccw.c | 2 ++
  1 file changed, 2 insertions(+)
hw/vfio/ccw.c +1 -0
index c3dc7c1962..8e29a3ae61 100644
@@ -604,8 +604,10 @@ static void vfio_ccw_realize(DeviceState *dev, Error **errp)
          /*
           * Report this error, but do not make it a failing condition.
           * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
Quoted 8 lines

hw/vfio/ccw.c static void vfio_ccw_realize(DeviceState *dev, Error **errp) · line 610

Quoted 3 lines
hw/vfio/ccw.c +1 -0
           */
          warn_report_err(err);
+        err = NULL;

OK, so this patch is ONLY a future-proofing change since err goes out of scope right after this.
I'm OK with the change from a future-proofing perspective, but IMO it does not merit a Fixes or a CC stable.

Fair enough. Gone.

Quoted 4 lines

With those removed:

Thanks!

Quoted 5 lines
Quoted 4 lines
hw/vfio/ccw.c +0 -0
      }
 
      return;
#22 lore raw

Re: [PATCH 6/9] hw/intc/s390x: Fix payload to KVM_HAS_DEVICE_ATTR ioctl

In reply to Eric Farman

Am 17.09.26 um 14:15 schrieb Eric Farman:

Quoted 38 lines · Christian Borntraeger
Quoted 18 lines

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 15 lines

The ioctl to determine whether KVM_DEV_FLIC_CLEAR_IO_IRQ is present receives the contents of the attribute struct as its payload, rather than a pointer to the payload itself as is convention.

This may result in erroneous errors from the ioctl, which would result in clear_io_supported being false when it otherwise could be used (which is helpful in device recovery).

Fixes 9eccb8622c ("s390x/kvm: add interface for clearing IO irqs")

---
  hw/intc/s390_flic_kvm.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

hw/intc/s390_flic_kvm.c static void kvm_s390_flic_realize(DeviceState *dev, Error **errp) · line 638

Quoted 12 lines
hw/intc/s390_flic_kvm.c +1 -1
index f833a3996a..5e6d422fba 100644
@@ -633,7 +633,7 @@ static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
       /* Check clear_io_irq support */
       test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
       flic_state->clear_io_supported = !ioctl(flic_state->fd,
-                                            KVM_HAS_DEVICE_ATTR, test_attr);
+                                            KVM_HAS_DEVICE_ATTR, &test_attr);
   }
   static void kvm_s390_flic_reset(DeviceState *dev)

Certainly a correct fix but maybe its not as bad. Aggregates > 8 byte are passed as reference. Looks like the third argument is a pointer to a stack copy when testing, so the old code might have worked by accident.

Quoted 5 lines

Ah, thanks for the assembler, I should have looked at that.

As I was testing this, I became worried that this whole part of error recovery was broken, but I was finding that clear_io_supported was already true even without this patch. I presumed ioctl magic, and that seems to be the case.

Maybe drop the stable tags from this one then, since it's working "by design"

working by accident, not by design. Yes we can drop cc stable.

Quoted 12 lines
Quoted 11 lines

      f8c:       d2 0f f0 a8 f0 d8       mvc     168(16,%r15),216(%r15) <--- stack copy f92:       b9 14 00 20             lgfr    %r2,%r0
      f96:       41 40 f0 a8             la      %r4,168(%r15) <-- pointer f9a:       c0 31 40 18 ae e3       lgfi    %r3,1075359459
      fa0:       d2 07 f0 b8 f0 e8       mvc     184(8,%r15),232(%r15) fa6:       c0 e5 00 00 00 00       brasl   %r14,fa6 <kvm_s390_flic_realize+0x136>
                         fa8: R_390_PLT32DBL     ioctl+0x2 KVM_HAS_DEVICE_ATTR, test_attr);

Reviewed-by on v1 4/9
#14 lore raw

Re: [PATCH 4/9] vfio/ap: Clear error object pointer after non-fatal errors

In reply to Eric Farman
Quoted 6 lines · Eric Farman

Registering a new IRQ notifier will allocate an error object if needed, and warn_report_err() will release that memory once it's finished. The pointer remains, however, so subsequent errors after the non-fatal errors in vfio_ap_realize() would cause an assertion failure for a non-NULL pointer.

Quoted 8 lines

Fix this by clearing the pointer once the warning has been reported, to reset the state of the error objects.

Cc Anthony Krowiak <[email protected]>
Fixes 1360b2ad1f ("s390x/ap: Wire up the device request notifier interface")
Fixes 0fb8a62fe4 ("hw/vfio/ap: notification handler for AP config changed event")
Quoted 30 lines
---
  hw/vfio/ap.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/vfio/ap.c +4 -0
index 6e2a1223ea..2926e04ad4 100644
@@ -247,8 +247,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
          /*
           * Report this error, but do not make it a failing condition.
           * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
           */
          warn_report_err(err);
+        err = NULL;
      }
 
      if (!vfio_ap_register_irq_notifier(vapdev, VFIO_AP_CFG_CHG_IRQ_INDEX, &err))
@@ -256,8 +258,10 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
          /*
           * Report this error, but do not make it a failing condition.
           * Lack of this IRQ in the host does not prevent normal operation.
+         * Clear the pointer, since the warning will release it.
           */
          warn_report_err(err);
+        err = NULL;
      }
 
      return;
#25 lore raw

Re: [PATCH 7/9] hw/intc/s390x: Fix check after g_try_realloc

In reply to Matthew Rosato
Quoted 13 lines · Matthew Rosato
Quoted 12 lines · Eric Farman

The loop to store pending IRQs uses g_try_realloc() to expand the buffer being used, but the returned address is stored in a pointer-to-pointer. Thus, the check needs to examine its contents and not the pointer itself.

Fixes 3a553fc658 ("s390x/kvm: implement floating-interrupt controller device")
---
  hw/intc/s390_flic_kvm.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

hw/intc/s390_flic_kvm.c static int __get_all_irqs(KVMS390FLICState *flic, · line 266

Quoted 8 lines
hw/intc/s390_flic_kvm.c +0 -0
index 5e6d422fba..ce9c720512 100644
@@ -264,7 +264,7 @@ static int __get_all_irqs(KVMS390FLICState *flic,
          }
          len *= 2;
          *buf = g_try_realloc(*buf, len);

Aren't we also leaking the old *buf if this fails?

Argh, yeah of course, the caller is the one doing the malloc/free, so we don't want to silently nuke that on error. Will fix.

Quoted 12 lines

I think you need a local void *foo here to catch the return..

hw/intc/s390_flic_kvm.c static int __get_all_irqs(KVMS390FLICState *flic, · line 269

Quoted 4 lines
hw/intc/s390_flic_kvm.c +1 -1
-        if (!buf) {
+        if (!*buf) {
              return -ENOMEM;
          }

And then here a *buf = foo; to only overwrite *buf in the success case.

Quoted 1 line
hw/intc/s390_flic_kvm.c +0 -0
      } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER);
#28 lore raw

Re: [PATCH 8/9] s390x/css: Expand check against no-data-count CCWs

In reply to Matthew Rosato
Quoted 21 lines · Matthew Rosato
Quoted 6 lines · Eric Farman

The channel subsystem will reject channel programs that incorporate 256 CCWs without a data transfer, but the existing check for this does so against a data address of zero. Expand the check to include other CCWs that are valid but would transfer no data.

Fixes e8601dd5d0 ("s390x/css: catch ccw sequence errors")

guest-triggerable, cc stable

Quoted 11 lines
---
  hw/s390x/css.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
hw/s390x/css.c +0 -0
index 9da9128d88..c38549c7be 100644
@@ -1016,7 +1016,9 @@ static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr,
 
Quoted 8 lines

hw/s390x/css.c static int css_interpret_ccw(SubchDev *sch, hwaddr ccw_addr, · line 1021

Quoted 6 lines
hw/s390x/css.c +3 -1
      check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC));
 
-    if (!ccw.cda) {
+    if (!ccw.cda ||
+        (ccw.flags & CCW_FLAG_SKIP) ||
+        (ccw.cmd_code == CCW_CMD_NOOP)) {

I'm not sure if this list is complete...

Surely the list of opcodes for virtio is far smaller than, say, actual DASD, but I'm not sure it's missing much. Maybe ccw.count == 0; that's valid for format-1 CCWs (we correctly reject it earlier than here for format-0), but would not move data.

Quoted 4 lines

I wonder if we could we key off of the number of bytes transferred e.g. sch->cds.at_byte == 0 instead? Would have to happen at the end of this function e.g. in the ret == 0 check maybe?

But wouldn't that only be looking at just a single CCW? That's totally fine by itself, whereas this is trying to set an upper bound of chained CCWs in a single program that don't do meaningful work.

Quoted 5 lines
Quoted 4 lines
hw/s390x/css.c +0 -0
          if (sch->ccw_no_data_cnt == 255) {
              return -EINVAL;
          }
#9 lore raw

Re: [PATCH 2/9] virtio-ccw: Do not overwrite existing indicators

In reply to Christian Borntraeger
Quoted 8 lines · Christian Borntraeger

Am 16.09.26 um 22:32 schrieb Eric Farman:

Quoted 5 lines

The various indicators pointers allow them to be overwritten, but leak any existing structs that might be there. The adapter indicators path checks that it isn't converting from regular to thin interrupts, but a check in the reverse direction isn't performed.

I missed the check actually exists in the reverse direction, it's just not as involved and blended in with other checks.

Quoted 40 lines
Quoted 40 lines

Rather than unwinding the existing allocations to allow additional assignments, just reject these duplicates.

---
  hw/s390x/virtio-ccw.c | 12 +++++++++++-

  1 file changed, 11 insertions(+), 1 deletion(-)
hw/s390x/virtio-ccw.c +10 -0
index eb6378ea4c..d700db405b 100644
@@ -565,6 +565,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
               ret = -EINVAL;
               break;
           }
+        if (dev->indicators) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
           if (sch->thinint_active) {
               /* Trigger a command reject. */
               ret = -ENOSYS;
@@ -599,6 +604,11 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
               ret = -EINVAL;
               break;
           }
+        if (dev->indicators2) {
+            /* Do not set without un-setting first */
+            ret = -ENOSYS;
+            break;
+        }
           if (!ccw.cda) {
               ret = -EFAULT;
           } else {
@@ -657,7 +667,7 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
           }
           if (!ccw.cda) {
Quoted 8 lines

hw/s390x/virtio-ccw.c static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) · line 673

Quoted 6 lines
hw/s390x/virtio-ccw.c +1 -1
               ret = -EFAULT;
-        } else if (dev->indicators && !sch->thinint_active) {
+        } else if (dev->indicators || dev->summary_indicator) {
               /* Trigger a command reject. */
               ret = -ENOSYS;
           } else {

I think we have to do the full unwind.

Will do; thanks

Quoted 10 lines

The Linux driver deregisters indicators by issuing SET_IND with address 0, or SET_IND_ADAPTER with device indicator 0. The virtio drivers reset the device before del_vqs, so the deregistration lands on a clean state and QEMU records an indicator at address 0. A later rebind or module reload does not reset again, so the next SET_IND_ADAPTER and the SET_IND fallback are both rejected and the probe fails. The old condition on SET_IND_ADAPTER existed precisely to let the deregistration through while thinint is active. The fix should release the existing indicator before overwriting, or treat address 0 as deregistration. Releasing before overwriting also handles the find_vqs error path in the guest, which deregisters without a reset in between.

#5 lore raw

Re: [PATCH 1/9] virtio-ccw: Limit ISC to MAX_ISC

In reply to Eric Farman
Quoted 71 lines · Eric Farman
Quoted 12 lines · Matthew Rosato
Quoted 11 lines · Eric Farman

The interrupt subclass is often defined as a uint8, though in practice it will be within the range of just 0-7. Ensure that a guest-supplied subclass does not extend beyond its expected range, especially when used as an array index.

---
  hw/s390x/css.c        | 7 ++++++-
  hw/s390x/virtio-ccw.c | 6 ++++++

  2 files changed, 12 insertions(+), 1 deletion(-)
Quoted 13 lines

hw/s390x/css.c void css_adapter_interrupt(CssIoAdapterType · line 658

Quoted 10 lines
hw/s390x/css.c +0 -1
index 76dbca3bb9..9da9128d88 100644
@@ -654,8 +654,13 @@ void css_adapter_interrupt(CssIoAdapterType 
 type, uint8_t isc)
       S390FLICState *fs = s390_get_flic();
       S390FLICStateClass *fsc = s390_get_flic_class(fs);
       uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI;
-    IoAdapter *adapter = channel_subsys.io_adapters[type][isc];

I think this deserve a fixes/cc stable.  This would have been an out-of-range access before this patch.

Ack.

Quoted 5 lines

The initial support didn't have the concept of MAX_ISC, it showed up in

dde522bbc5 ("s390x: register I/O adapters per ISC during init")

Quoted 8 lines

Frankly I think at that point we should have started fencing registration > MAX_ISC already, but that patch DID locally fence its new array indexes against MAX_ISC.  It wasn't until

25a08b8ded ("s390x/css: update css_adapter_interrupt")

when the above array index was added without the fence that you are now adding.  So I think 25a08b8ded is really the culprit.

I was considering 25a08b8ded, since the array indexing was the impetus for this patch. So will go with that.

Quoted 24 lines
Quoted 15 lines
hw/s390x/css.c +6 -0
+    IoAdapter *adapter;
+
+    if (type >= CSS_IO_ADAPTER_TYPE_NUMS || isc > MAX_ISC) {
+        return;
+    }
+    adapter = channel_subsys.io_adapters[type][isc];
       if (!adapter) {
           return;
       }
hw/s390x/virtio-ccw.c +0 -0
index 30fb2c0681..eb6378ea4c 100644

hw/s390x/virtio-ccw.c static int virtio_ccw_dev_post_load(void *opaque, · line 51

Quoted 6 lines
hw/s390x/virtio-ccw.c +3 -0
@@ -46,6 +46,10 @@ static int virtio_ccw_dev_post_load(void *opaque, 
 int version_id)
       CcwDevice *ccw_dev = CCW_DEVICE(dev);
       CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
+    if (dev->thinint_isc > MAX_ISC) {
+        return -EINVAL;
+    }

Is this safe to check before thinint_active?

Quoted 9 lines
Quoted 2 lines

I guess if we get here before an ISC was ever set it would just be zero-initialized so it wouldn't erroneously trigger this check.

That was my reasoning. I thought that adding it outside the thinint_active block would highlight potential shenanigans, even though that variable would end up being ignored in that case. Of course...

Quoted 4 lines

Assuming your reasoning matches mine:

Ack; thanks.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
hw/s390x/virtio-ccw.c +1 -0
+
       ccw_dev->sch->driver_data = dev;
       if (ccw_dev->sch->thinint_active) {

          dev->routes.adapter.adapter_id = css_get_adapter_id(

... the above css_get_adapter_id() call will already check that it's within MAX_ISC, so it's probably unnecessary.

Just to revisit this comment of my own, this won't reference outside of the array, but it would leave the adapter_id set to -1, which is going to be a bit confusing. So blocking it as I have it above is more correct.

Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
hw/s390x/virtio-ccw.c +2 -0
@@ -659,6 +663,8 @@ static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw)
           } else {
               if (ccw_dstream_read(&sch->cds, thinint)) {
                   ret = -EFAULT;
+            } else if (thinint.isc > MAX_ISC) {
+                ret = -ENOSYS;
               } else {
                   thinint.ind_bit = be64_to_cpu(thinint.ind_bit);

                  thinint.summary_indicator =