-
Notifications
You must be signed in to change notification settings - Fork 23
Automatic ASLR disablement #66
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
Open
LebedevRI
wants to merge
1
commit into
criterion-rs:master
Choose a base branch
from
LebedevRI:aslr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,9 @@ tokio = { version = "1.0", default-features = false, features = [ | |
| [target.'cfg(any(windows, unix))'.dependencies] | ||
| alloca = "0.4" | ||
|
|
||
| [target.'cfg(unix)'.dependencies] | ||
| nix = { version = "0.31.2", default-features = false, features = [], optional = true } | ||
|
|
||
| [dependencies.plotters] | ||
| version = "^0.3.2" | ||
| optional = true | ||
|
|
@@ -85,7 +88,7 @@ stable = [ | |
| "async_smol", | ||
| "async_tokio", | ||
| ] | ||
| default = ["rayon", "plotters", "cargo_bench_support"] | ||
| default = ["rayon", "plotters", "cargo_bench_support", "deaslr"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you be okay with not making this a default feature? If the feature works well and doesn't cause problems, then we could include it as default in a future release. |
||
|
|
||
| # This is a legacy feature that no longer does anything, but removing it would be a semver break. | ||
| real_blackbox = [] | ||
|
|
@@ -115,6 +118,10 @@ cargo_bench_support = [] | |
| # cargo-criterion's --message-format=json option. | ||
| csv_output = ["dep:csv"] | ||
|
|
||
| # Support disabling Address-Space-Layout Randomization at runtime, | ||
| # thus eliminating some of exec-to-exec noise. | ||
| deaslr = ["dep:nix", "nix/personality", "nix/process"] | ||
|
|
||
| [[bench]] | ||
| name = "bench_main" | ||
| harness = false | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Disabling ASLR # | ||
|
|
||
| Address-space layout randomization (ASLR) security hardening feature | ||
| is a known source of performance variance, it is a good idea | ||
| to make sure that it is not enabled when performing benchmarking. | ||
|
|
||
| `criterion` crate, when built with with `deaslr` feature, | ||
| will provide a `criterion::deaslr::maybe_reenter_without_aslr()` function, | ||
| that will automatically try to disable ASLR for the current process, | ||
| and, if successful, re-execute the binary. | ||
|
|
||
| If you are using `criterion_main!` macro, you do not need to do anything, | ||
| however, if you have your own `main` function, then you need to add | ||
| ``` | ||
| criterion::deaslr::maybe_reenter_without_aslr(); | ||
| ``` | ||
| as the first line of your `main()` function. | ||
|
|
||
| Note that `personality(2)` may be forbidden by e.g. seccomp (which happens | ||
| by default if you are running in a Docker container). | ||
|
|
||
| To globally disable ASLR on Linux, run | ||
| ``` | ||
| echo 0 > /proc/sys/kernel/randomize_va_space | ||
| ``` | ||
| or | ||
| ``` | ||
| sysctl -w kernel.randomize_va_space=0 | ||
| ``` | ||
| ... or, you can add `norandmaps` to you linux kernel's command-line. | ||
|
|
||
| To run a single benchmark with ASLR disabled on Linux, do: | ||
| ``` | ||
| setarch `uname -m` -R ./a_benchmark | ||
| ``` | ||
|
|
||
| Note that for the information on how to disable ASLR on other operating systems, | ||
| please refer to their documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Benchmarking Tips | ||
|
|
||
| This chapter covers the tips that you can use to minimize performance variance, | ||
| and thus make benchmarking more deterministic. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| //! This module deals with disablement of address-space-layout randomization | ||
| //! security hardening feature for the benchmark process, | ||
| //! which, for the purposes of benchmarking, would cause | ||
| //! subtle unreproducible uncontrollable randomness factors, | ||
| //! that would deteriorate quality-of-life of the benchmarks. | ||
|
|
||
| /// This function does nothing on non-UNIX systems. | ||
| #[cfg(not(all(unix, feature = "deaslr")))] | ||
| pub fn maybe_reenter_without_aslr() { | ||
| // No-op. | ||
| } | ||
|
|
||
| /// Disables ASLR for the current process and restarts the binary. | ||
| /// | ||
| /// If you using [`criterion_main!`](crate::criterion_main) macro, | ||
| /// it is done automatically, otherwise you may want to call | ||
| /// this function first thing in your `main` function. | ||
| #[cfg(all(unix, feature = "deaslr"))] | ||
| pub fn maybe_reenter_without_aslr() { | ||
| use nix::sys::personality; | ||
| use nix::sys::personality::Persona; | ||
| use std::ffi::CString; | ||
| use std::os::unix::ffi::OsStrExt; | ||
|
|
||
| let Some(argc) = std::env::current_exe().ok() else { | ||
| // We are not guaranteed to know what our current executable is. | ||
| // On e.g. Hexagon simulator, argv may be NULL. | ||
| return; | ||
| }; | ||
|
|
||
| let Ok(curr_personality) = personality::get() else { | ||
| // We should never fail to read-only query the current personality, | ||
| // but let's be cautious. | ||
| return; | ||
| }; | ||
|
|
||
| if curr_personality.contains(Persona::ADDR_NO_RANDOMIZE) { | ||
| // If ASLR is already disabled, we have nothing more to do. | ||
| return; | ||
| } | ||
|
|
||
| let proposed_personality = curr_personality.union(Persona::ADDR_NO_RANDOMIZE); | ||
|
|
||
| let Ok(_prev_personality) = personality::set(proposed_personality) else { | ||
| // Have we failed to change the personality? That may happen. | ||
| return; | ||
| }; | ||
|
|
||
| // Actually read what the new personality is. | ||
| let Ok(new_personality) = personality::get() else { | ||
| // We should never fail to read-only query the current personality, | ||
| // but let's be cautious. | ||
| return; | ||
| }; | ||
|
|
||
| // Make sure the persona has been updated with the no-ASLR flag, | ||
| // otherwise we will try to reenter infinitely. | ||
| // This seems impossible, but can happen in some docker configurations. | ||
| if !new_personality.contains(Persona::ADDR_NO_RANDOMIZE) { | ||
| return; | ||
| } | ||
|
|
||
| // We've succeeded in altering our personality, and can now [`execv`]. | ||
| // But it takes null-terminated [`CStr`]'s, | ||
| // whereas Rust's [`std::env::args_os`] are null-stripped. | ||
|
|
||
| let argc = | ||
| CString::new(argc.into_os_string().as_bytes()).expect("executable name contained 0 byte"); | ||
|
|
||
| let argv = std::env::args_os() | ||
| .skip(1) | ||
| .map(|s| CString::new(s.as_bytes()).expect("argument contained 0 byte")) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let Err(_) = nix::unistd::execv(&argc, &argv); | ||
| // The exec() functions return only if an error has occurred, | ||
| // in which case we want to just continue as-is. | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be under "unreleased".