Guard xTaskCreate() stack allocation against size_t overflow - #1488
Open
JacksonBopp wants to merge 1 commit into
Open
Guard xTaskCreate() stack allocation against size_t overflow#1488JacksonBopp wants to merge 1 commit into
JacksonBopp wants to merge 1 commit into
Conversation
uxStackDepth * sizeof(StackType_t) was computed without an overflow check before being passed to pvPortMallocStack(). On a target where size_t and StackType_t are both 32 bits, a large enough uxStackDepth wraps the multiplication to a small allocation, while prvInitialiseNewTask() still uses the original, larger uxStackDepth for stack pointer arithmetic (pxStack[uxStackDepth - 1] and the end-of-stack pointer), producing an out-of-bounds access. Add tskSTACK_DEPTH_WILL_OVERFLOW() and check it at both dynamic allocation sites in prvCreateTask() (portSTACK_GROWTH > 0 and <= 0), falling back to the existing NULL-allocation failure path rather than calling pvPortMallocStack() with a size that has already wrapped. Verified with a standalone reproduction of the exact 32-bit arithmetic against the depth from the report (0x40000020 words), confirming the wrap without the guard and rejection with it, and compiled tasks.c clean under the POSIX/Linux port (portSTACK_GROWTH <= 0 branch).
|
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.



Fixes #1472.
prvCreateTask()computes the dynamic stack allocation size asuxStackDepth * sizeof(StackType_t)with no overflow check. On a target wheresize_tandStackType_tare both 32 bits, a large enoughuxStackDepthwraps the multiplication to a small allocation.prvInitialiseNewTask()then continues to use the original, largeruxStackDepthfor stack pointer arithmetic (pxStack[uxStackDepth - 1]and the end-of-stack pointer), which reads and writes past the undersized allocation.This adds
tskSTACK_DEPTH_WILL_OVERFLOW()and checks it at both dynamic allocation sites inprvCreateTask()(theportSTACK_GROWTH > 0and<= 0branches), falling back to the existing NULL-allocation failure path instead of callingpvPortMallocStack()with a size that has already wrapped.Verification: I reproduced the exact 32-bit wraparound described in the issue (depth
0x40000020words) in a standalone program modeling the same arithmetic, confirming the allocation silently wraps to 128 bytes without the guard and is correctly rejected with it, with no effect on a normal in-range depth. I also compiled the patchedtasks.cclean (-Wall -Wextra, no warnings) against the POSIX/Linux port, which exercises theportSTACK_GROWTH <= 0branch. I wasn't able to run the full CMock unit test suite in this environment (FreeRTOS/Test/CMocklives in the umbrellaFreeRTOS/FreeRTOSrepo and needs Ruby + make, neither of which I had available), so I'm flagging that gap rather than claiming coverage I don't have.