From 162fc8a015d35b88ee930d6654cac6807b677f09 Mon Sep 17 00:00:00 2001 From: Bruno Ribeiro Date: Thu, 12 Dec 2024 12:02:14 -0300 Subject: [PATCH] Fix SMBIOS version validation --- README.md | 6 +++--- smbios.c | 8 +++++--- smbios.h | 1 + smbios_decode.c | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 40ab45d..5a0711e 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,11 @@ The pointer to the current SMBIOS entry is stored in the variable specified as t |TYPE_MANAGEMENT_DEVICE_THRESHOLD_DATA | management_device_threshold_data | |TYPE_ONBOARD_DEVICES_EXTENDED_INFO | onboard_devices_extended_info | -The library do not make heap allocations: everything is done in-place using the provided SMBIOS buffer and the context. +The library do not make heap allocations; everything is done in-place using the provided SMBIOS buffer and the context. ## API -The following functions are available. If you're using the library in a C++ code, the functions will be defined in the `smbios` namespace. +The following functions are available. ### smbios_initialize @@ -93,7 +93,7 @@ If the actual version of the SMBIOS data is smaller than the value of the parame * **context**: Parser context. * **data**: SMBIOS data. * **size**: Size of the SMBIOS data. -* **version**: Preferred SMBIOS version. +* **version**: Preferred SMBIOS version. If set with `SMBIOS_ANY`, the latest version will be used (currently 3.0). The function returns SMBERR_OK on success or a negative error code. diff --git a/smbios.c b/smbios.c index 13d3e2b..0848ef5 100644 --- a/smbios.c +++ b/smbios.c @@ -50,7 +50,7 @@ int smbios_initialize(struct ParserContext *context, const uint8_t *data, size_t memset(context, 0, sizeof(struct ParserContext)); context->ptr = NULL; - context->sversion = VALID_VERSION(version) ? SMBIOS_3_0 : version; + context->sversion = VALID_VERSION(version) ? version : SMBIOS_3_0; // we have a valid SMBIOS entry point? #ifndef _WIN32 @@ -102,10 +102,12 @@ int smbios_initialize(struct ParserContext *context, const uint8_t *data, size_t context->size = smBiosData->Length; #endif - if (!VALID_VERSION(context->oversion)) - return SMBERR_INVALID_DATA; if (context->sversion > context->oversion) + { + if (!VALID_VERSION(context->oversion)) + return SMBERR_INVALID_DATA; context->sversion = context->oversion; + } return SMBERR_OK; } diff --git a/smbios.h b/smbios.h index c8a0088..8063b83 100644 --- a/smbios.h +++ b/smbios.h @@ -382,6 +382,7 @@ struct Entry enum SpecVersion { + SMBIOS_ANY = 0, SMBIOS_2_0 = 0x0200, SMBIOS_2_1 = 0x0201, SMBIOS_2_2 = 0x0202, diff --git a/smbios_decode.c b/smbios_decode.c index 27b0977..cb018b9 100644 --- a/smbios_decode.c +++ b/smbios_decode.c @@ -101,6 +101,8 @@ bool printSMBIOS( struct ParserContext *parser, FILE *output ) if (smbios_get_version(parser, &version, NULL) != SMBERR_OK) return false; + fprintf(output, "SMBIOS version %d.%d\n", version >> 8, version & 0xFF); + const struct Entry *entry = NULL; while (true) { @@ -480,7 +482,7 @@ int main(int argc, char ** argv) } struct ParserContext parser; - if (smbios_initialize(&parser, buffer, size, SMBIOS_3_0) == SMBERR_OK) + if (smbios_initialize(&parser, buffer, size, SMBIOS_ANY) == SMBERR_OK) printSMBIOS(&parser, stdout); else fputs("Invalid SMBIOS data", stderr);