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

armv8-m: userspace: syscall return sequence needs to be atomic #22685

Closed
ioannisg opened this issue Feb 10, 2020 · 1 comment · Fixed by #22691
Closed

armv8-m: userspace: syscall return sequence needs to be atomic #22685

ioannisg opened this issue Feb 10, 2020 · 1 comment · Fixed by #22691
Assignees
Labels
area: ARM ARM (32-bit) Architecture area: Memory Protection area: Userspace Userspace bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Milestone

Comments

@ioannisg
Copy link
Member

ioannisg commented Feb 10, 2020

Describe the bug
The sequence of operations when returning from a Cortex-M system call is as follows:

  1. Stack Limit Register is cleared (if we are on ARMv8-M)
  2. Stack Pointer is set back to nPRIV (user) stack
  3. thread.mode variable is set to nPRIV
  4. CONTROL register nPRIV bit is set

The above sequence is executed in thread PRIV mode (cannot be executed in nPRIV mode, actually, as it leads to fault).

Now, if swap occurs between (1) and (2), or between (2) and (3) the PSPLIM will be set again to guard the privilege thread stack. This will cause a fault when the thread attempts to push to the user stack, which lies below the PRIV thread stack.

Note: ARMv7-M does not have this problem because it does not have stack pointer limit registers, so only steps (2), (3), (4) are executed.

For example: if swap occurs between (2) and (4) the processor returns to nPRIV mode, but the attempt to (over)write to the CONTROL Register is silently ignored; this is not a problem since the value is the correct one.

Impact
Undefined behavior and possible processor fault during system calls.

Solution

We cannot reverse the order of operations, because the PSP, PSPLIM cannot be written from nPRIV mode.

As a result, we must ensure SWAP cannot occur during this sequence of operations.

@ioannisg ioannisg added the bug The issue is a bug, or the PR is fixing a bug label Feb 10, 2020
@ioannisg ioannisg self-assigned this Feb 10, 2020
@ioannisg ioannisg added area: ARM ARM (32-bit) Architecture area: Memory Protection area: Userspace Userspace labels Feb 10, 2020
@ioannisg ioannisg added this to the v2.2.0 milestone Feb 10, 2020
@ioannisg
Copy link
Member Author

FYI @andrewboie @jhedberg

@jhedberg jhedberg added the priority: medium Medium impact/importance bug label Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ARM ARM (32-bit) Architecture area: Memory Protection area: Userspace Userspace bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants