Skip to contentSkip to conversation

hw/cxl: Check for zero length features in cmd_features_set_feature()

PATCH In review Jonathan Cameron 3 participants 3 messages Nov 8, 2024 – Nov 12, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/cxl: Check for zero length features in cmd_features_set_feature()

Zero length data for features doesn't make any sense so exclude that case early. This fixes the undefined behavior reported by coverity for a zero length memcpy().

Resolves CID 1564900 and 1564901

---
 hw/cxl/cxl-mailbox-utils.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/cxl/cxl-mailbox-utils.c +4 -0
index 2d4d62c454..ce9aa18364 100644
@@ -1288,6 +1288,10 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
     set_feat_info->data_offset = hdr->offset;
     bytes_to_copy = len_in - sizeof(CXLSetFeatureInHeader);
 
+    if (bytes_to_copy == 0) {
+        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
+    }
+
     if (qemu_uuid_is_equal(&hdr->uuid, &patrol_scrub_uuid)) {
         if (hdr->version != CXL_MEMDEV_PS_SET_FEATURE_VERSION) {
             return CXL_MBOX_UNSUPPORTED;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jonathan Cameron

Zero length data for features doesn't make any sense so exclude that case early. This fixes the undefined behavior reported by coverity for a zero length memcpy().

Resolves CID 1564900 and 1564901

---
 hw/cxl/cxl-mailbox-utils.c | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 16 lines
hw/cxl/cxl-mailbox-utils.c +4 -0
index 2d4d62c454..ce9aa18364 100644
@@ -1288,6 +1288,10 @@ static CXLRetCode cmd_features_set_feature(const struct cxl_cmd *cmd,
     set_feat_info->data_offset = hdr->offset;
     bytes_to_copy = len_in - sizeof(CXLSetFeatureInHeader);
 
+    if (bytes_to_copy == 0) {
+        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
+    }
+
     if (qemu_uuid_is_equal(&hdr->uuid, &patrol_scrub_uuid)) {
         if (hdr->version != CXL_MEMDEV_PS_SET_FEATURE_VERSION) {
             return CXL_MBOX_UNSUPPORTED;
Signature · 1 line
Fan Ni
Tested-by on v1 1/1
#2 lore raw
In reply to Jonathan Cameron
Quoted 16 lines

-----Original Message-----
From: [email protected] <qemu-devel- [email protected]> On Behalf Of Jonathan Cameron via
Sent: 08 November 2024 17:58
To: Michael S . Tsirkin <[email protected]>; Fan Ni <[email protected]>; [email protected]; Peter Maydell <[email protected]>; [email protected]; [email protected]
Cc: Linuxarm <[email protected]>
Subject: [PATCH qemu] hw/cxl: Check for zero length features in cmd_features_set_feature()

Zero length data for features doesn't make any sense so exclude that case early. This fixes the undefined behavior reported by coverity for a zero length memcpy().

Quoted 8 lines

Resolves CID 1564900 and 1564901

---
hw/cxl/cxl-mailbox-utils.c | 4 ++++

1 file changed, 4 insertions(+)
Quoted 19 lines
hw/cxl/cxl-mailbox-utils.c index renamed from hw/cxl/cxl-mailbox-utils.c +0 -0

2d4d62c454..ce9aa18364 100644

hw/cxl/cxl-mailbox-utils.c +4 -0
@@ -1288,6 +1288,10 @@ static CXLRetCode cmd_features_set_feature(const
 struct cxl_cmd *cmd,
    set_feat_info->data_offset = hdr->offset;
    bytes_to_copy = len_in - sizeof(CXLSetFeatureInHeader);
 
+    if (bytes_to_copy == 0) {
+        return CXL_MBOX_INVALID_PAYLOAD_LENGTH;
+    }
+
    if (qemu_uuid_is_equal(&hdr->uuid, &patrol_scrub_uuid)) {
        if (hdr->version != CXL_MEMDEV_PS_SET_FEATURE_VERSION) {
            return CXL_MBOX_UNSUPPORTED;

--
2.43.0