iFlashRead() overflows caller buffer by up to 7 bytes on final unaligned iteration#27
Open
merkelmarrow wants to merge 1 commit intoXilinx:mainfrom
Open
Conversation
On the final iteration of an unaligned iFlashRead(), ulBytesToRead was trimmed to (ulByteCount % OSPI_READ_BUFFER_SIZE) *after* the pvOSAL_MemCpy into the caller's buffer, so the copy overran pucReadBfrPtr by up to 7 bytes for any read with a length not a multiple of 8. If stack hardening isn't enabled at compile-time, this is easy to go unnoticed. On every V80 cold boot, iLoadFptPartition() asks for exactly 12 bytes, and the overflow writes 4 bytes which overwrites iStatus with zeros. This does nothing on a successful read since iStatus returns zero anyway. The fix is to compute the trim before memcpy from a snapshot of the post-clamp length taken before the unaligned branch overwrites it. Signed-off-by: Marco Blackwell <mblackwe@amd.com>
38fc880 to
0e6a3cd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context
iFlashRead()(fw/AMC/src/device_drivers/ospi/aved/ospi.c) reads flash into a caller buffer. In the function there is one variable, ulBytesToRead, that is reused for three different purposes during a single call:The bug is that step 3 happens after the memcpy it was supposed to constrain. By that point the overflow has already occurred.
Impact
The copy overruns pucReadBfrPtr by up to 7 bytes for any read with a length not a multiple of 8. On every V80 cold boot, iLoadFptPartition() asks for exactly 12 bytes (
sizeof(APC_PROXY_DRIVER_FPT_PARTITION) == 12), and the overflow writes 4 bytes, overwritingiLoadFptPartition'siStatuswith zeros (verified via disassembly + DWARF, the overwrite value is always 0x00 because of the waygen_fpt.pyzero-initialises the FPT image).This does nothing on a successful read since iStatus returns zero anyway. On a failed read the memcpy never gets triggered.
But when the firmware is compiled with stack protections, this bug surfaces as
NO_AMCinami_tool overviewdue to the stack canary trip during boot.Stack frame layout is unspecified by the C standard so this bug could be more consequential in other setups. Any future changes to the code could also result in other stack locals being silently overwritten.
Fix
The fix is to snapshot the post-clamp length (
ulEffectiveBytes) before the unaligned branch overwritesulBytesToRead, and on the final iteration computeulBytesToRead = ulEffectiveBytes − (i * OSPI_READ_BUFFER_SIZE)before thememcpy. Using the post-clamp length rather thanulByteCount % 8keeps the trim correct in the stacked-flash-tail case (>256 Mbit flashes).