MdePkg: Fix overflow issue in BasePeCoffLib: PeCoffLoaderRelocateImage #6249
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The RelocDir->Size is a UINT32 value, and RelocDir->VirtualAddress is also a UINT32 value. The current code does not check for overflow when adding RelocDir->Size to RelocDir->VirtualAddress. This patch adds a check to ensure that the addition does not overflow.
How This Was Tested
In BasePeCoff.c, the PeCoffLoaderRelocateImage() does RelocDir→VirtualAddress + ReloDir→Size- 1
inside the function was overflowing and causing memory corruption.
so added the below check for avoiding the memory corruption before calculating the RelocBase and RelocBaseEnd.
if ((RelocDir != NULL) && (RelocDir->Size > 0) && (RelocDir->Size -1 < MAX_UINT32 - RelocDir->VirtualAddress))
With this condition added the max value while adding size and address is always less than MAX_UINT32.
Hence there won’t be integer overflow with possible values for RelocDir->VirtualAddress and RelocDir->Size.
Have tested the fix in real platform and confirmed the image is booting fine.
Integration Instructions
N/A