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

Support Rust's stack-protector feature #1135

Merged
merged 2 commits into from
Feb 2, 2024

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Feb 2, 2024

While stack smashing is less of a problem in Rust it's still worth it to support Rust's stack-protector feature.

The stack canary is not zero-cost but it's also not too expensive (occupies one word in every stack frame and a few CPU cycles for every instrumented function)

Additionally, this can be also used to detect a possible stack overflow for the main stack by placing __stack_chk_guard near the bottom of the stack.

Example

Add this to rustflags in .cargo/config.toml

"-Z", "stack-protector=all",

Use this example (imports are for ESP32-C6)

#![no_std]
#![no_main]

use esp32c6_hal::{peripherals::Peripherals, prelude::*};
use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let _system = peripherals.SYSTEM.split();

    boom();

    loop {}
}

#[inline(never)]
fn boom() {
    deadly_recursion([0u8; 2048]);
}

#[ram]
#[allow(unconditional_recursion)]
fn deadly_recursion(data: [u8; 2048]) {
    static mut COUNTER: u32 = 0;

    println!(
        "Iteration {}, data {:02x?}...",
        unsafe { COUNTER },
        &data[0..10]
    );

    unsafe {
        COUNTER = COUNTER.wrapping_add(1);
    };

    deadly_recursion([0u8; 2048]);
}

It will result in this:
image

@MabezDev
Copy link
Member

MabezDev commented Feb 2, 2024

Whoa! This is very cool! Nice find!

Should we perhaps make it opt-in? I know the cost is quite small, but it is a cost. What do you think?

@bjoernQ
Copy link
Contributor Author

bjoernQ commented Feb 2, 2024

Whoa! This is very cool! Nice find!

Should we perhaps make it opt-in? I know the cost is quite small, but it is a cost. What do you think?

When not having "stack-protector" in rustflags we just have that one (unused) function and the symbol pointing into the stack. 🤔 Not sure if that is worth another feature but I was thinking about it

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, for the sake of one word, its probably not worth cfging it away :D

LGTM, thanks!

@bjoernQ bjoernQ added this pull request to the merge queue Feb 2, 2024
Merged via the queue into esp-rs:main with commit c0f9169 Feb 2, 2024
17 checks passed
@bjoernQ bjoernQ deleted the feature/support-stack-protector branch February 2, 2024 13:56
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.

2 participants