FreeRTOS / FreeRTOS-Kernel

FreeRTOS kernel files only, submoduled into https://github.com/FreeRTOS/FreeRTOS and various other repos.
https://www.FreeRTOS.org
MIT License
2.71k stars 1.11k forks source link

Add check for if the scheduler is running to MPU ports #954

Closed Skptak closed 8 months ago

Skptak commented 8 months ago

Description

Add a check for if the scheduler is running to xPortIsAuthorizedToAccessBuffer().

This fixes an issue where before the scheduler is started APIs can fail as the TCB pointed to by pxCurrentTCB may not have access to requested APIs.

Test Steps

Call xTimerStart(), which calls MPU_xTimerGenericCommandFromTask:

xReturn = MPU_xTimerGenericCommandFromTaskEntry( &( xParams ) );

Which calls MPU_xTimerGenericCommandFromTaskEntry

BaseType_t MPU_xTimerGenericCommandFromTaskEntry(
    const xTimerGenericCommandFromTaskParams_t * pxParams
 ) /* __attribute__ (( naked )) FREERTOS_SYSTEM_CALL */
    {
        __asm volatile
        (
            " .syntax unified                                               \n"
            " .extern MPU_xTimerGenericCommandFromTaskImpl                  \n"
            "                                                               \n"
            " push {r0}                                                     \n"
            " mrs r0, control                                               \n"
            " tst r0, #1                                                    \n"
            " bne MPU_xTimerGenericCommandFromTask_Unpriv                   \n"
            " MPU_xTimerGenericCommandFromTask_Priv:                        \n"
            "     pop {r0}                                                  \n"
            "     b MPU_xTimerGenericCommandFromTaskImpl                    \n"

This calls MPU_xTimerGenericCommandFromTaskImpl:

xAreParamsReadable = xPortIsAuthorizedToAccessBuffer( pxParams,
                                            sizeof( xTimerGenericCommandFromTaskParams_t ),
                                            tskMPU_READ_PERMISSION );
}

Which calls xPortIsAuthorizedToAccessBuffer This loads pxCurrentTCB to check if you are allowed to start the timer

BaseType_t xPortIsAuthorizedToAccessBuffer(
    const void * pvBuffer,
    uint32_t ulBufferLength,
    uint32_t ulAccessRequested
) /* PRIVILEGED_FUNCTION */
{
    uint32_t i, ulBufferStartAddress, ulBufferEndAddress;
    BaseType_t xAccessGranted = pdFALSE;
    const xMPU_SETTINGS * xTaskMpuSettings = xTaskGetMPUSettings( NULL ); /* Calling task's MPU settings. */

    if( ( xTaskMpuSettings->ulTaskFlags & portTASK_IS_PRIVILEGED_FLAG ) == portTASK_IS_PRIVILEGED_FLAG )
    {
            xAccessGranted = pdTRUE;
    }
    else
    {
        <CHECK_IF_ACCESS_ALLOWED>

However, there's no actual calling task that made this call. It just checks whatever TCB happens to be there.

A check is needed for when the scheduler is not running yet, as at this point the current TCB is irrelevant to the operation being performed.

Checklist:

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

codecov[bot] commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (cf2366c) 93.42% compared to head (921ea9a) 93.42%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #954 +/- ## ======================================= Coverage 93.42% 93.42% ======================================= Files 6 6 Lines 3196 3196 Branches 886 886 ======================================= Hits 2986 2986 Misses 103 103 Partials 107 107 ``` | [Flag](https://app.codecov.io/gh/FreeRTOS/FreeRTOS-Kernel/pull/954/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=FreeRTOS) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/FreeRTOS/FreeRTOS-Kernel/pull/954/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=FreeRTOS) | `93.42% <ø> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=FreeRTOS#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

sonarcloud[bot] commented 8 months ago

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud