Skip to content

Commit

Permalink
fix: varint: read records per erase block and fix leak
Browse files Browse the repository at this point in the history
When the write pointer moves to write a new record, it
takes care not to straddle erase blocks; when reading back
account for this.
Fix a memory leak in the NvVarIntLibrary.

Bug 4788402
Change-Id: I5ff718c066860ca9f20a7bfa4a006b57405f1911
Signed-off-by: Girish Mahadevan <[email protected]>
Reviewed-on: https://git-master.nvidia.com/r/c/tegra/bootloader/uefi/edk2-nvidia/+/3244202
Reviewed-by: svcacv <[email protected]>
Reviewed-by: Ashish Singhal <[email protected]>
GVS: buildbot_gerritrpt <[email protected]>
Reviewed-by: svc-sw-mobile-l4t <[email protected]>
  • Loading branch information
gmahadevan committed Nov 16, 2024
1 parent 78a917e commit b0847a5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
74 changes: 42 additions & 32 deletions Silicon/NVIDIA/Drivers/FvbNorFlashDxe/VarIntCheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ GetLastValidMeasurements (
UINT64 StartOffset;
UINT64 EndOffset;
UINT64 CurOffset;
UINT64 BlockOffset;
UINT64 BlockEnd;
UINT64 NumValidRecords;
UINT8 *ReadBuf;

Expand All @@ -499,50 +501,58 @@ GetLastValidMeasurements (
EndOffset = StartOffset + VarInt->PartitionSize;

CurOffset = StartOffset;
BlockOffset = CurOffset;
BlockEnd = BlockOffset + VarInt->BlockSize;
NumValidRecords = 0;
*NumRecords = 0;

while (CurOffset < EndOffset) {
Status = NorFlash->Read (
NorFlash,
CurOffset,
VarInt->MeasurementSize,
ReadBuf
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"%a: NorFlash Read Failed at %lu offset %r\n",
__FUNCTION__,
CurOffset,
Status
));
goto ExitGetLastValidMeasuremets;
}

if ((ReadBuf[0] == VAR_INT_VALID) ||
(ReadBuf[0] == VAR_INT_PENDING))
{
NumValidRecords++;
if (NumValidRecords > MAX_VALID_RECORDS) {
while (BlockOffset < BlockEnd) {
Status = NorFlash->Read (
NorFlash,
BlockOffset,
VarInt->MeasurementSize,
ReadBuf
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_ERROR,
"%a: More than %d Valid measurements found %x\n",
"%a: NorFlash Read Failed at %lu offset %r\n",
__FUNCTION__,
MAX_VALID_RECORDS,
ReadBuf[0]
BlockOffset,
Status
));
Status = EFI_DEVICE_ERROR;
goto ExitGetLastValidMeasuremets;
} else {
DEBUG ((DEBUG_INFO, "Found Record at %lu Header %x\n", CurOffset, ReadBuf[0]));
CopyMem (Records[(NumValidRecords - 1)]->Measurement, ReadBuf, VarInt->MeasurementSize);
*NumRecords += 1;
Records[(NumValidRecords - 1)]->ByteOffset = CurOffset;
}

if ((ReadBuf[0] == VAR_INT_VALID) ||
(ReadBuf[0] == VAR_INT_PENDING))
{
NumValidRecords++;
if (NumValidRecords > MAX_VALID_RECORDS) {
DEBUG ((
DEBUG_ERROR,
"%a: More than %d Valid measurements found %x\n",
__FUNCTION__,
MAX_VALID_RECORDS,
ReadBuf[0]
));
Status = EFI_DEVICE_ERROR;
goto ExitGetLastValidMeasuremets;
} else {
DEBUG ((DEBUG_INFO, "Found Record at %lu Header %x\n", BlockOffset, ReadBuf[0]));
CopyMem (Records[(NumValidRecords - 1)]->Measurement, ReadBuf, VarInt->MeasurementSize);
*NumRecords += 1;
Records[(NumValidRecords - 1)]->ByteOffset = BlockOffset;
}
}

BlockOffset += VarInt->MeasurementSize;
}

CurOffset += VarInt->MeasurementSize;
CurOffset += VarInt->BlockSize;
BlockEnd += VarInt->BlockSize;
BlockOffset = CurOffset;
}

ExitGetLastValidMeasuremets:
Expand Down
22 changes: 9 additions & 13 deletions Silicon/NVIDIA/Library/NvVarIntLibrary/NvVarIntLibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,16 @@ MeasureBootVars (

DEBUG ((DEBUG_INFO, "Adding %s Size %u %p\n", BootOptionName, BootOptionSize, BootOptions[Index]));
HashApiUpdate (HashContext, BootOptions[Index], BootOptionSize);
FreePool (BootOptions[Index]);
}

ExitMeasureBootVars:

if (BootOptions != NULL) {
FreePool (BootOptions);
BootOptions = NULL;
}

if ((UpdatingBootOrder == TRUE)) {
BootOrder = NULL;
BootCount = 0;
Expand Down Expand Up @@ -598,7 +605,6 @@ ComputeVarMeasurement (
)
{
EFI_STATUS Status;
UINTN Index;

if (HashContext == NULL) {
HashContext = AllocateRuntimeZeroPool (HashApiGetContextSize ());
Expand All @@ -625,22 +631,12 @@ ComputeVarMeasurement (
Status = EFI_DEVICE_ERROR;
}

if ((BootCount != 0) && (BootOptions != NULL)) {
for (Index = 0; Index < BootCount; Index++) {
if (BootOptions[Index] != NULL) {
FreePool (BootOptions[Index]);
}
}

FreePool (BootOptions);
BootCount = 0;
BootOptions = NULL;
}

if (BootOrder != NULL) {
FreePool (BootOrder);
}

BootCount = 0;

Status = EFI_SUCCESS;

ExitComputeVarMeasurement:
Expand Down

0 comments on commit b0847a5

Please sign in to comment.