Skip to content

Commit c64ef8a

Browse files
committed
Prevent access to per-core variables without proper macro
Without this patch, it's possible to declare a per-core variable like so: declare_per_core! { static mut TIMER_WHEEL: Option<TimerWheel> = None; } Then access the variable like: unsafe { TIMER_WHEEL = Some(value); } But this will always mutate the value on the BSP core (and is not thread-safe). This patch generates a new symbol for the actual static variable, which effectively requires the usage of the 'get_per_core' or 'get_per_core_mut' and prevents this category of error.
1 parent 06535b8 commit c64ef8a

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

mythril/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mythril/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ linked_list_allocator = "0.8.6"
2020
log = { version = "0.4.8", default-features = false }
2121
multiboot = "0.3.0"
2222
multiboot2 = "0.9.0"
23+
paste = "1.0"
2324
raw-cpuid = "8.1.1"
2425
rlibc = "1.0.0"
2526
serde = {version = "^1", default-features = false, features = ["alloc", "derive"] }

mythril/src/percore.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ macro_rules! get_per_core {
127127
($name:ident) => {
128128
#[allow(unused_unsafe)]
129129
unsafe {
130-
$crate::percore::get_per_core_impl(&$name)
130+
$crate::percore::get_per_core_impl(&paste::paste! {
131+
[<PERCORE_ $name>]
132+
})
131133
}
132134
};
133135
}
@@ -137,7 +139,9 @@ macro_rules! get_per_core_mut {
137139
($name:ident) => {
138140
#[allow(unused_unsafe)]
139141
unsafe {
140-
$crate::percore::get_per_core_mut_impl(&mut $name)
142+
$crate::percore::get_per_core_mut_impl(&mut paste::paste! {
143+
[<PERCORE_ $name>]
144+
})
141145
}
142146
};
143147
}
@@ -147,14 +151,19 @@ macro_rules! get_per_core_mut {
147151
#[doc(hidden)]
148152
macro_rules! __declare_per_core_internal {
149153
($(#[$attr:meta])* ($($vis:tt)*) static mut $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
150-
#[link_section = ".per_core"]
151-
$($vis)* static mut $N: $T = $e;
154+
paste::paste! {
155+
#[link_section = ".per_core"]
156+
$($vis)* static mut [<PERCORE_ $N>]: $T = $e;
157+
}
152158

153159
declare_per_core!($($t)*);
154160
};
155161
($(#[$attr:meta])* ($($vis:tt)*) static $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
156-
#[link_section = ".per_core"]
157-
$($vis)* static $N: $T = $e;
162+
use paste::paste;
163+
paste! {
164+
#[link_section = ".per_core"]
165+
$($vis)* static [<PERCORE_ $N>]: $T = $e;
166+
}
158167

159168
declare_per_core!($($t)*);
160169
};

0 commit comments

Comments
 (0)