Skip to content

Commit

Permalink
Fix SMBIOS version validation
Browse files Browse the repository at this point in the history
  • Loading branch information
brunexgeek committed Dec 12, 2024
1 parent 75c24b7 commit 162fc8a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions smbios.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions smbios.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ struct Entry

enum SpecVersion
{
SMBIOS_ANY = 0,
SMBIOS_2_0 = 0x0200,
SMBIOS_2_1 = 0x0201,
SMBIOS_2_2 = 0x0202,
Expand Down
4 changes: 3 additions & 1 deletion smbios_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 162fc8a

Please sign in to comment.