fix(tasks): reject uxStackDepth that overflows the stack allocation size - #1489
Open
saikumar-mandaji wants to merge 1 commit into
Open
fix(tasks): reject uxStackDepth that overflows the stack allocation size#1489saikumar-mandaji wants to merge 1 commit into
saikumar-mandaji wants to merge 1 commit into
Conversation
prvCreateTask() computes the stack allocation size as (size_t) uxStackDepth * sizeof(StackType_t) with no overflow check. On 32-bit targets (where both size_t and the default configSTACK_DEPTH_TYPE == StackType_t are 32-bit, e.g. the Cortex-M ports), a caller-supplied uxStackDepth greater than SIZE_MAX / sizeof(StackType_t) makes this multiplication wrap, so pvPortMallocStack() allocates a much smaller buffer than the caller asked for. prvInitialiseNewTask() is unaware of the wrap: it still computes the top-of-stack and fills the stack using the original, oversized uxStackDepth (e.g. pxStack[uxStackDepth - 1]), which walks off the end of the undersized allocation. Fix: check for the overflow before attempting either allocation branch (portSTACK_GROWTH > 0 or < 0) and fail the task creation the same way a genuine pvPortMalloc()/pvPortMallocStack() failure is already handled (pxNewTCB left NULL), using the same 'SIZE_MAX / x >= y' overflow-check idiom already used for this exact purpose in queue.c (xQueueGenericReset(), xQueueGenericCreate()). Fixes FreeRTOS#1472
|
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.



Bug
prvCreateTask()computes the stack allocation size as(size_t) uxStackDepth * sizeof(StackType_t)with no overflow check (both theportSTACK_GROWTH > 0and< 0branches). On 32-bit targets — where bothsize_tand the defaultconfigSTACK_DEPTH_TYPE(== StackType_t) are 32-bit, e.g. the Cortex-M ports — a caller-supplieduxStackDepthgreater thanSIZE_MAX / sizeof(StackType_t)makes this multiplication wrap, sopvPortMallocStack()allocates a much smaller buffer than requested.prvInitialiseNewTask()is unaware of the wrap: it still computes the top-of-stack and fills the stack using the original, oversizeduxStackDepth(pxStack[uxStackDepth - 1], plus thememsetfill), which walks off the end of the undersized allocation. See #1472 for the full repro (0x40000020words wraps the allocation to 128 bytes).Fix
Check for the overflow before attempting either allocation branch, and fail task creation the same way a genuine
pvPortMalloc()/pvPortMallocStack()failure is already handled (pxNewTCBleftNULL). Uses the sameSIZE_MAX / x >= yoverflow-check idiom already used for exactly this purpose inqueue.c(xQueueGenericReset(),xQueueGenericCreate()), so it's consistent with existing project convention rather than a new idiom.Verification
tasks.cclean (-std=gnu99 -mcpu=cortex-m4 -mthumb -Wall -Wextra) against the realportable/GCC/ARM_CM4Fport headers with a minimalFreeRTOSConfig.h(dynamic allocation, no static allocation) — this exercises the exact 32-bitsize_t/StackType_tconfiguration the bug report describes, usingarm-none-eabi-gccsince no host-native compiler was available in this environment.queue.c), so I'm confident in the logic but flagging this honestly.Fixes #1472