Skip to content

Commit c010af3

Browse files
committed
Add stack guard
1 parent 3ad1300 commit c010af3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

armv7a-unknown-xous-elf.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"features": "+v7,+thumb2,+thumb-mode,+vfp4d16,+d32,+neon,+strict-align",
1616
"executables": true,
1717
"relocation-model": "static",
18+
"supports-stack-protector": true,
1819
"os": "xous"
1920
}

library/std/src/sys/pal/xous/os.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ mod eh_unwinding {
1818
#[cfg(not(test))]
1919
mod c_compat {
2020
use crate::os::xous::ffi::exit;
21+
use crate::sync::atomic::{AtomicU32, Ordering};
22+
2123
unsafe extern "C" {
2224
fn main() -> u32;
2325
}
@@ -28,14 +30,16 @@ mod c_compat {
2830
}
2931

3032
#[unsafe(no_mangle)]
31-
pub extern "C" fn _start(_eh_frame: usize, params_address: usize) {
33+
pub extern "C" fn _start(_eh_frame: usize, params_address: usize, rnd_seed: usize) {
3234
#[cfg(feature = "panic_unwind")]
3335
{
3436
// TODO
3537
// unsafe { super::eh_unwinding::EH_FRAME_ADDRESS = eh_frame };
3638
// unwind::set_custom_eh_frame_finder(&super::eh_unwinding::EH_FRAME_SETTINGS).ok();
3739
}
3840

41+
init_stack_guard(rnd_seed as u32);
42+
3943
if params_address != 0 {
4044
let params_address = crate::ptr::with_exposed_provenance_mut::<u8>(params_address);
4145
if unsafe {
@@ -46,6 +50,19 @@ mod c_compat {
4650
}
4751
exit(unsafe { main() });
4852
}
53+
54+
pub fn init_stack_guard(rnd_seed: u32) {
55+
unsafe extern "C" {
56+
static __stack_chk_guard: AtomicU32;
57+
}
58+
59+
// Ensure at least one 0 byte to reduce certain string-overflow exploits
60+
let canary = rnd_seed & 0xFFFF_FF00;
61+
62+
unsafe {
63+
__stack_chk_guard.store(canary, Ordering::Relaxed);
64+
}
65+
}
4966
}
5067

5168
pub fn errno() -> i32 {

0 commit comments

Comments
 (0)