Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arm: nxp: mpu: Fix Coverity issue #441

Merged
merged 2 commits into from
Jun 9, 2017

Conversation

MaureenHelm
Copy link
Member

Fixes a Coverity issue in the NXP MPU driver, as well as a second similar issue in the NXP and ARM MPU drivers found by inspection.

Neither of these issues are causing any actual runtime failures, and given we are so close to releasing 1.8, I suggest we do not include this in the release. I don't want any surprises like #261.

The NXP MPU requires special handling of region descriptor 0 to
guarantee that the debugger has access to the entire address space. It
does not allow writes from the core to affect the start or end
addresses, or the permissions associated with the debugger.

The original implementation of this driver attempted to work around
region descriptor 0, resulting in an off-by-1 error caught by Coverity.

Instead, define region descriptor 0 explicitly in the mpu_regions array,
and add some asserts to ensure that one doesn't try to change its start
or end addresses. This has an added benefit such that more permissions
can be enabled in region 0 if desired, whereas the previous
implementation always forced all writable permissions to be cleared.

Coverity-CID: 170473
Jira: ZEP-2258

Signed-off-by: Maureen Helm <[email protected]>
Both the ARM and NXP MPU drivers incorrectly calculated the region index
by assuming the region type (e.g., THREAD_STACK_GUARD_REGION) was
zero-indexed, when in reality it is one-indexed. This had the effect of
wasting one region.

Signed-off-by: Maureen Helm <[email protected]>
@galak galak merged this pull request into zephyrproject-rtos:arm Jun 9, 2017
@stephensmalley
Copy link
Collaborator

After 07e0a78, attempting to execute from RAM no longer triggers a MPU-caused bus fault on frdm_k64f, as can be seen from mpu_test run - you'll get a usage fault / invalid use of the EPSR instead of a bus fault. Also my own tests of stack/heap/data execution all successfully execute code from those regions without causing any fault at all after this change; those test actual execution of code written to RAM. Looking at the MPU configuration before and after this change, the only difference seems to be that previously region 0 was disabled via a direct write to RGD0_WORD2, causing the valid mask to also be cleared, whereas now you are configuring it through RGDAAC0 and therefore leaving it marked valid. Otherwise, before and after the change, the region attributes seem to be identical (0x7c0). I would have thought that would be ok since you are clearing the non-debugger permissions, but for some reason it doesn't seem to be working AFAICS as long as the valid mask remains set.

@stephensmalley
Copy link
Collaborator

FWIW, my tests are here:
https://github.com/stephensmalley/zephyr/commit/3d5b9dfaee61f5236d02e01962471711cf9edde0
Don't know if they would be useful to submit. exec_stack for example before and after this change shows the difference in behavior caused by this change, although you can see it even with mpu_test; it just isn't as obvious (bus fault -> usage fault there versus a bus fault -> FAIL).

@MaureenHelm
Copy link
Member Author

Thanks for catching this. It turns out that clearing certain bits in WORD2/RGDAAC0 actually enables access. Specifically, the MxSM register fields:

Bus Master 3 Supervisor Mode Access Control
Defines the access controls for bus master 3 in Supervisor mode.
00 r/w/x; read, write and execute allowed
01 r/x; read and execute allowed, but no write
10 r/w; read and write allowed, but no execute
11 Same as User mode defined in M3UM

I've got a fix I'm testing now.

@MaureenHelm
Copy link
Member Author

@stephensmalley, please have a look at #484. I like your test and think it's worth submitting, perhaps merge the new functionality into the existing mpu_test.

@stephensmalley
Copy link
Collaborator

Submitted as #493

@MaureenHelm MaureenHelm deleted the zep-2258 branch June 15, 2017 00:49
nagineni pushed a commit to nagineni/zephyr that referenced this pull request Nov 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants