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

Document how to determine the right value to pass for pages argument #22

Open
phayes opened this issue Feb 25, 2020 · 1 comment
Open

Comments

@phayes
Copy link

phayes commented Feb 25, 2020

Hi there,

Thanks for this awesome crate! I was wondering if you could document how to find the right value to pass for the pages argument for clear_stack_on_return, clear_stack_on_return_fnonce.

@hiroki-chen
Copy link

hiroki-chen commented Aug 12, 2022

I encountered this issue too when I was trying to zeroize the stack and general purpose registers of a sensitive function, and my thought was to keep the code of the sensitive function tight and concise so that the stack size is always delimited within an expected range, say 4096 bytes.

The value of page is directly linked to the maximum stack usage of the closure. I am afraid that it is very awkward to determine the size of page correctly when using static analysis because the original code can be compiled differently based on compiler versions and platforms; therefore the stack layout may vary significantly too. In my opinion, it is better to check the stack usage of a function by its runtime profile, but this could introduce extra labor because we have to run the binary first and then find the precise value for page.

Another way to do this at the source code level may be the inline assembly by which one gets the stack pointer and then estimates the current stack size, which could be imprecise, but this may be the most convenient approach.

fn get_stack_pointer() -> usize {
    let sp: usize;
    unsafe {
        asm!("mov {:r}, rsp", out(reg) x);
    }
    sp
}

I am not sure the following link would help because it only provides a rudimentary solution, and it cannot work with no_std, but I hope it may be enlightening.

https://stackoverflow.com/a/38953349/14875612

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

No branches or pull requests

2 participants