Skip to contentSkip to conversation

igvm: Add NULL checks for igvm_get_buffer()

PATCH Superseded Oliver Steffen 2 participants 4 messages Feb 10 – Feb 11

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover igvm: Add NULL checks for igvm_get_buffer()

According to the documentation we are supposed to do a null-pointer check on the buffers returned by igvm_get_buffer() (part of the IGVM C library).
Add these checks in the IGVM backend.

Oliver Steffen (1):
igvm: Add NULL checks for igvm_get_buffer()

 backends/igvm.c | 86 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 56 insertions(+), 30 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/1 igvm: Add NULL checks for igvm_get_buffer()

According to the documentation we are supposed to do a null-pointer check on the buffers returned by igvm_get_buffer() (part of the IGVM C library).
Add these checks in the IGVM backend.

---
 backends/igvm.c | 86 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 56 insertions(+), 30 deletions(-)
backends/igvm.c +56 -30
index b01a19ba46..049cb36891 100644
@@ -152,9 +152,17 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
                 (int)header_handle);
             return -1;
         }
-        header_data = igvm_get_buffer(ctx->file, header_handle) +
-                      sizeof(IGVM_VHS_VARIABLE_HEADER);
-        result = handlers[handler].handler(ctx, header_data, errp);
+        header_data = igvm_get_buffer(ctx->file, header_handle);
+        if (header_data != NULL) {
+            header_data += sizeof(IGVM_VHS_VARIABLE_HEADER);
+            result = handlers[handler].handler(ctx, header_data, errp);
+        } else {
+            error_setg(errp,
+                    "IGVM: No buffer for handle %d: "
+                    "(type 0x%X)",
+                    header_handle, type);
+            result = -1;
+        }
         igvm_free_buffer(ctx->file, header_handle);
         return result;
     }
@@ -316,7 +324,13 @@ static int qigvm_process_mem_region(QIgvm *ctx, unsigned start_index,
                 return -1;
             }
             data = igvm_get_buffer(ctx->file, data_handle);
-            memcpy(&region[page_index * page_size], data, data_size);
+            if (data != NULL) {
+                memcpy(&region[page_index * page_size], data, data_size);
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);
+                igvm_free_buffer(ctx->file, data_handle);
+                return -1;
+            }
             igvm_free_buffer(ctx->file, data_handle);
         }
     }
@@ -426,6 +440,11 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
     }
 
     data = (uint8_t *)igvm_get_buffer(ctx->file, data_handle);
+    if (data == NULL) {
+        error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);
+        result = -1;
+        goto exit;
+    }
 
     if (ctx->machine_state->cgs) {
         result = ctx->cgsc->set_guest_state(
@@ -441,6 +460,7 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
         result = -1;
     }
 
+exit:
     igvm_free_buffer(ctx->file, data_handle);
     if (result < 0) {
         return result;
@@ -778,33 +798,39 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp)
             }
             platform =
                 (IGVM_VHS_SUPPORTED_PLATFORM *)(igvm_get_buffer(ctx->file,
-                                                                header_handle) +
-                                                sizeof(
-                                                    IGVM_VHS_VARIABLE_HEADER));
-            if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
-                ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_ES, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev_es = platform->compatibility_mask;
-                }
-            } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev = platform->compatibility_mask;
+                                                                header_handle));
+            if (platform != NULL) {
+                platform = (IGVM_VHS_SUPPORTED_PLATFORM *)((void *)platform
+                                            + sizeof(IGVM_VHS_VARIABLE_HEADER));
+                if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
+                    ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_ES, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_es = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type ==
+                            IGVM_PLATFORM_TYPE_SEV_SNP) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_SNP, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_snp = platform->compatibility_mask;
+                    }
+                } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
+                    compatibility_mask = platform->compatibility_mask;
                 }
-            } else if ((platform->platform_type ==
-                        IGVM_PLATFORM_TYPE_SEV_SNP) &&
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_SNP, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev_snp = platform->compatibility_mask;
-                }
-            } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
-                compatibility_mask = platform->compatibility_mask;
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", header_handle);
+                igvm_free_buffer(ctx->file, header_handle);
+                return -1;
             }
             igvm_free_buffer(ctx->file, header_handle);
         }
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#2 lore raw
Quoted 36 lines · Oliver Steffen

According to the documentation we are supposed to do a null-pointer check on the buffers returned by igvm_get_buffer() (part of the IGVM C library).
Add these checks in the IGVM backend.

---
backends/igvm.c | 86 ++++++++++++++++++++++++++++++++-----------------

1 file changed, 56 insertions(+), 30 deletions(-)
backends/igvm.c +11 -3
index b01a19ba46..049cb36891 100644
@@ -152,9 +152,17 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
                (int)header_handle);
            return -1;
        }
-        header_data = igvm_get_buffer(ctx->file, header_handle) +
-                      sizeof(IGVM_VHS_VARIABLE_HEADER);
-        result = handlers[handler].handler(ctx, header_data, errp);
+        header_data = igvm_get_buffer(ctx->file, header_handle);
+        if (header_data != NULL) {
+            header_data += sizeof(IGVM_VHS_VARIABLE_HEADER);
+            result = handlers[handler].handler(ctx, header_data, errp);
+        } else {
+            error_setg(errp,
+                    "IGVM: No buffer for handle %d: "
+                    "(type 0x%X)",
+                    header_handle, type);
+            result = -1;
+        }
        igvm_free_buffer(ctx->file, header_handle);
        return result;
    }

backends/igvm.c static int qigvm_process_mem_region(QIgvm *ctx, unsigned start_index, · line 330

Quoted 8 lines
backends/igvm.c +4 -1
@@ -316,7 +324,13 @@ static int qigvm_process_mem_region(QIgvm *ctx, unsigned start_index,
                return -1;
            }
            data = igvm_get_buffer(ctx->file, data_handle);
-            memcpy(&region[page_index * page_size], data, data_size);
+            if (data != NULL) {
+                memcpy(&region[page_index * page_size], data, data_size);
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);

nit: space and semicolon can be removed

backends/igvm.c static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data, · line 444

Quoted 12 lines
backends/igvm.c +5 -0
+                igvm_free_buffer(ctx->file, data_handle);
+                return -1;
+            }
            igvm_free_buffer(ctx->file, data_handle);
        }
    }
@@ -426,6 +440,11 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
    }
 
    data = (uint8_t *)igvm_get_buffer(ctx->file, data_handle);
+    if (data == NULL) {
+        error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);

ditto

Quoted 65 lines
backends/igvm.c +32 -18
+        result = -1;
+        goto exit;
+    }
 
    if (ctx->machine_state->cgs) {
        result = ctx->cgsc->set_guest_state(
@@ -441,6 +460,7 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
        result = -1;
    }
 
+exit:
    igvm_free_buffer(ctx->file, data_handle);
    if (result < 0) {
        return result;
@@ -778,33 +798,39 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp)
            }
            platform =
                (IGVM_VHS_SUPPORTED_PLATFORM *)(igvm_get_buffer(ctx->file,
-                                                                header_handle) +
-                                                sizeof(
-                                                    IGVM_VHS_VARIABLE_HEADER));
-            if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
-                ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_ES, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev_es = platform->compatibility_mask;
-                }
-            } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev = platform->compatibility_mask;
+                                                                header_handle));
+            if (platform != NULL) {
+                platform = (IGVM_VHS_SUPPORTED_PLATFORM *)((void *)platform
+                                            + sizeof(IGVM_VHS_VARIABLE_HEADER));
+                if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
+                    ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_ES, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_es = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type ==
+                            IGVM_PLATFORM_TYPE_SEV_SNP) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_SNP, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_snp = platform->compatibility_mask;
+                    }
+                } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
+                    compatibility_mask = platform->compatibility_mask;
                }
-            } else if ((platform->platform_type ==
-                        IGVM_PLATFORM_TYPE_SEV_SNP) &&

backends/igvm.c static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp) · line 831

Quoted 10 lines
backends/igvm.c +2 -8
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_SNP, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev_snp = platform->compatibility_mask;
-                }
-            } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
-                compatibility_mask = platform->compatibility_mask;
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", header_handle);

ditto

Quoted 9 lines
backends/igvm.c +2 -0
+                igvm_free_buffer(ctx->file, header_handle);
+                return -1;
            }
            igvm_free_buffer(ctx->file, header_handle);
        }
Signature · 1 line
2.53.0

Thanks!

#3 lore raw
In reply to Oliver Steffen
Quoted 82 lines · Oliver Steffen

According to the documentation we are supposed to do a null-pointer check on the buffers returned by igvm_get_buffer() (part of the IGVM C library).
Add these checks in the IGVM backend.

---
backends/igvm.c | 86 ++++++++++++++++++++++++++++++++-----------------

1 file changed, 56 insertions(+), 30 deletions(-)
backends/igvm.c +24 -12
index b01a19ba46..049cb36891 100644
@@ -152,9 +152,17 @@ static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp)
                (int)header_handle);
            return -1;
        }
-        header_data = igvm_get_buffer(ctx->file, header_handle) +
-                      sizeof(IGVM_VHS_VARIABLE_HEADER);
-        result = handlers[handler].handler(ctx, header_data, errp);
+        header_data = igvm_get_buffer(ctx->file, header_handle);
+        if (header_data != NULL) {
+            header_data += sizeof(IGVM_VHS_VARIABLE_HEADER);
+            result = handlers[handler].handler(ctx, header_data, errp);
+        } else {
+            error_setg(errp,
+                    "IGVM: No buffer for handle %d: "
+                    "(type 0x%X)",
+                    header_handle, type);
+            result = -1;
+        }
        igvm_free_buffer(ctx->file, header_handle);
        return result;
    }
@@ -316,7 +324,13 @@ static int qigvm_process_mem_region(QIgvm *ctx, unsigned start_index,
                return -1;
            }
            data = igvm_get_buffer(ctx->file, data_handle);
-            memcpy(&region[page_index * page_size], data, data_size);
+            if (data != NULL) {
+                memcpy(&region[page_index * page_size], data, data_size);
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);
+                igvm_free_buffer(ctx->file, data_handle);
+                return -1;
+            }
            igvm_free_buffer(ctx->file, data_handle);
        }
    }
@@ -426,6 +440,11 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
    }
 
    data = (uint8_t *)igvm_get_buffer(ctx->file, data_handle);
+    if (data == NULL) {
+        error_setg(errp, "IGVM: No buffer for handle %d: ", data_handle);
+        result = -1;
+        goto exit;
+    }
 
    if (ctx->machine_state->cgs) {
        result = ctx->cgsc->set_guest_state(
@@ -441,6 +460,7 @@ static int qigvm_directive_vp_context(QIgvm *ctx, const uint8_t *header_data,
        result = -1;
    }
 
+exit:
    igvm_free_buffer(ctx->file, data_handle);
    if (result < 0) {
        return result;
@@ -778,33 +798,39 @@ static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp)
            }
            platform =
                (IGVM_VHS_SUPPORTED_PLATFORM *)(igvm_get_buffer(ctx->file,
-                                                                header_handle) +
-                                                sizeof(
-                                                    IGVM_VHS_VARIABLE_HEADER));
-            if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
-                ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_ES, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {

backends/igvm.c static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp) · line 802

Quoted 10 lines
backends/igvm.c +2 -8
-                    compatibility_mask_sev_es = platform->compatibility_mask;
-                }
-            } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev = platform->compatibility_mask;
+                                                                header_handle));
+            if (platform != NULL) {

on a second thought: if you have to send a v2, what about reversing if and else? With an early return, we can remove the else clause entirely.

Quoted 48 lines
backends/igvm.c +30 -10
+                platform = (IGVM_VHS_SUPPORTED_PLATFORM *)((void *)platform
+                                            + sizeof(IGVM_VHS_VARIABLE_HEADER));
+                if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV_ES) &&
+                    ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_ES, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_es = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type == IGVM_PLATFORM_TYPE_SEV) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev = platform->compatibility_mask;
+                    }
+                } else if ((platform->platform_type ==
+                            IGVM_PLATFORM_TYPE_SEV_SNP) &&
+                        ctx->machine_state->cgs) {
+                    if (ctx->cgsc->check_support(
+                            CGS_PLATFORM_SEV_SNP, platform->platform_version,
+                            platform->highest_vtl, platform->shared_gpa_boundary)) {
+                        compatibility_mask_sev_snp = platform->compatibility_mask;
+                    }
+                } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
+                    compatibility_mask = platform->compatibility_mask;
                }
-            } else if ((platform->platform_type ==
-                        IGVM_PLATFORM_TYPE_SEV_SNP) &&
-                       ctx->machine_state->cgs) {
-                if (ctx->cgsc->check_support(
-                        CGS_PLATFORM_SEV_SNP, platform->platform_version,
-                        platform->highest_vtl, platform->shared_gpa_boundary)) {
-                    compatibility_mask_sev_snp = platform->compatibility_mask;
-                }
-            } else if (platform->platform_type == IGVM_PLATFORM_TYPE_NATIVE) {
-                compatibility_mask = platform->compatibility_mask;
+            } else {
+                error_setg(errp, "IGVM: No buffer for handle %d: ", header_handle);
+                igvm_free_buffer(ctx->file, header_handle);
+                return -1;
            }
            igvm_free_buffer(ctx->file, header_handle);
        }
Signature · 1 line
2.53.0