Repository navigation
Commit 6823b69
authored
zephyr: sync: Disallow Mutex reentrancy to prevent unsound mutable aliasing (#181)
## Problem
`zephyr::sync::Mutex` wraps Zephyr's underlying `k_mutex`. In Zephyr,
`k_mutex` is recursive by design, allowing the owning thread to lock it
multiple times without blocking. Consequently, `Mutex::lock()` and
`Mutex::try_lock()` allowed reentrant acquisition from the same thread.
Because `MutexGuard` implements `DerefMut`, allowing reentrancy
permitted safe Rust code to obtain multiple concurrent mutable
references (`&mut T`) to the inner data:
```rust
let mutex = Mutex::new(0);
let mut g1 = mutex.lock().unwrap();
let mut g2 = mutex.lock().unwrap(); // Succeeded because k_mutex is recursive!
let r1: &mut i32 = &mut *g1;
let r2: &mut i32 = &mut *g2; // Undefined Behavior: concurrent mutable references to the same data
```
This violates Rust's fundamental aliasing rules ("aliasing XOR
mutability") and introduces undefined behavior in safe code.
## Solution
Track whether the mutex is currently locked by a guard using an
`AtomicBool`:
1. In `lock()`, acquire the underlying `sys::Mutex`. If `locked` was
already true (indicating recursive acquisition by the current thread),
immediately unlock the `sys::Mutex` to restore its lock count and panic
with `"cannot recursively lock Mutex"`.
2. In `try_lock()`, acquire the underlying `sys::Mutex`. If `locked` was
already true, immediately unlock and return
`Err(TryLockError::WouldBlock)`.
3. In `MutexGuard::drop()`, clear `locked` before unlocking
`sys::Mutex`.
4. In `Condvar::wait()`, clear `locked` before calling
`sys::Condvar::wait()` and re-assert `locked` upon re-acquiring the
mutex.
## Prior Issues / PRs
Checked the repository issues and pull requests; there are currently no
open or linked pull requests or issues addressing mutex reentrancy
soundness.1 file changed
Lines changed: 24 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
| 98 | + | |
96 | 99 | | |
97 | 100 | | |
98 | 101 | | |
| |||
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
120 | 128 | | |
121 | 129 | | |
122 | 130 | | |
| |||
125 | 133 | | |
126 | 134 | | |
127 | 135 | | |
128 | | - | |
| 136 | + | |
| 137 | + | |
129 | 138 | | |
130 | 139 | | |
131 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
132 | 148 | | |
133 | 149 | | |
134 | 150 | | |
| |||
163 | 179 | | |
164 | 180 | | |
165 | 181 | | |
| 182 | + | |
166 | 183 | | |
167 | 184 | | |
168 | 185 | | |
| |||
210 | 227 | | |
211 | 228 | | |
212 | 229 | | |
| 230 | + | |
213 | 231 | | |
| 232 | + | |
214 | 233 | | |
215 | 234 | | |
216 | 235 | | |
| |||
0 commit comments