Skip to content

Commit f66ae45

Browse files
abrownBlackHoleFox
andcommitted
Add support for control-flow protection
This change adds a flag for configuring control-flow protection in the LLVM backend. In Clang, this flag is exposed as `-fcf-protection` with options `none|branch|return|full`. This convention is followed for `rustc`, though as a codegen option: `rustc -Z cf-protection=<none|branch|return|full>`. Co-authored-by: BlackHoleFox <[email protected]>
1 parent e789f3a commit f66ae45

File tree

5 files changed

+136
-5
lines changed

5 files changed

+136
-5
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use rustc_middle::ty::layout::{
2121
};
2222
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2323
use rustc_middle::{bug, span_bug};
24-
use rustc_session::config::{BranchProtection, CFGuard, CrateType, DebugInfo, PAuthKey, PacRet};
24+
use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
25+
use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
2526
use rustc_session::Session;
2627
use rustc_span::source_map::Span;
2728
use rustc_span::symbol::Symbol;
@@ -287,6 +288,24 @@ pub unsafe fn create_module<'ll>(
287288
);
288289
}
289290

291+
// Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang).
292+
if let CFProtection::Branch | CFProtection::Full = sess.opts.debugging_opts.cf_protection {
293+
llvm::LLVMRustAddModuleFlag(
294+
llmod,
295+
llvm::LLVMModFlagBehavior::Override,
296+
"cf-protection-branch\0".as_ptr().cast(),
297+
1,
298+
)
299+
}
300+
if let CFProtection::Return | CFProtection::Full = sess.opts.debugging_opts.cf_protection {
301+
llvm::LLVMRustAddModuleFlag(
302+
llmod,
303+
llvm::LLVMModFlagBehavior::Override,
304+
"cf-protection-return\0".as_ptr().cast(),
305+
1,
306+
)
307+
}
308+
290309
llmod
291310
}
292311

compiler/rustc_session/src/config.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ pub enum CFGuard {
6363
Checks,
6464
}
6565

66+
/// The different settings that the `-Z cf-protection` flag can have.
67+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
68+
pub enum CFProtection {
69+
/// Do not enable control-flow protection
70+
None,
71+
72+
/// Emit control-flow protection for branches (enables indirect branch tracking).
73+
Branch,
74+
75+
/// Emit control-flow protection for returns.
76+
Return,
77+
78+
/// Emit control-flow protection for both branches and returns.
79+
Full,
80+
}
81+
6682
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
6783
pub enum OptLevel {
6884
No, // -O0
@@ -2632,11 +2648,11 @@ impl PpMode {
26322648
/// we have an opt-in scheme here, so one is hopefully forced to think about
26332649
/// how the hash should be calculated when adding a new command-line argument.
26342650
crate mod dep_tracking {
2635-
use super::LdImpl;
26362651
use super::{
2637-
BranchProtection, CFGuard, CrateType, DebugInfo, ErrorOutputType, InstrumentCoverage,
2638-
LinkerPluginLto, LocationDetail, LtoCli, OptLevel, OutputType, OutputTypes, Passes,
2639-
SourceFileHashAlgorithm, SwitchWithOptPath, SymbolManglingVersion, TrimmedDefPaths,
2652+
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, ErrorOutputType,
2653+
InstrumentCoverage, LdImpl, LinkerPluginLto, LocationDetail, LtoCli, OptLevel, OutputType,
2654+
OutputTypes, Passes, SourceFileHashAlgorithm, SwitchWithOptPath, SymbolManglingVersion,
2655+
TrimmedDefPaths,
26402656
};
26412657
use crate::lint;
26422658
use crate::options::WasiExecModel;
@@ -2717,6 +2733,7 @@ crate mod dep_tracking {
27172733
NativeLibKind,
27182734
SanitizerSet,
27192735
CFGuard,
2736+
CFProtection,
27202737
TargetTriple,
27212738
Edition,
27222739
LinkerPluginLto,

compiler/rustc_session/src/options.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ mod desc {
384384
pub const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2";
385385
pub const parse_cfguard: &str =
386386
"either a boolean (`yes`, `no`, `on`, `off`, etc), `checks`, or `nochecks`";
387+
pub const parse_cfprotection: &str = "`none`|`no`|`n` (default), `branch`, `return`, or `full`|`yes`|`y` (equivalent to `branch` and `return`)";
387388
pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`";
388389
pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavor::one_of();
389390
pub const parse_optimization_fuel: &str = "crate=integer";
@@ -699,6 +700,25 @@ mod parse {
699700
true
700701
}
701702

703+
crate fn parse_cfprotection(slot: &mut CFProtection, v: Option<&str>) -> bool {
704+
if v.is_some() {
705+
let mut bool_arg = None;
706+
if parse_opt_bool(&mut bool_arg, v) {
707+
*slot = if bool_arg.unwrap() { CFProtection::Full } else { CFProtection::None };
708+
return true;
709+
}
710+
}
711+
712+
*slot = match v {
713+
None | Some("none") => CFProtection::None,
714+
Some("branch") => CFProtection::Branch,
715+
Some("return") => CFProtection::Return,
716+
Some("full") => CFProtection::Full,
717+
Some(_) => return false,
718+
};
719+
true
720+
}
721+
702722
crate fn parse_linker_flavor(slot: &mut Option<LinkerFlavor>, v: Option<&str>) -> bool {
703723
match v.and_then(LinkerFlavor::from_str) {
704724
Some(lf) => *slot = Some(lf),
@@ -1146,6 +1166,8 @@ options! {
11461166
"select which borrowck is used (`mir` or `migrate`) (default: `migrate`)"),
11471167
branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
11481168
"set options for branch target identification and pointer authentication on AArch64"),
1169+
cf_protection: CFProtection = (CFProtection::None, parse_cfprotection, [TRACKED],
1170+
"instrument control-flow architecture protection"),
11491171
cgu_partitioning_strategy: Option<String> = (None, parse_opt_string, [TRACKED],
11501172
"the codegen unit partitioning strategy to use"),
11511173
chalk: bool = (false, parse_bool, [TRACKED],
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# `cf-protection`
2+
3+
This option enables control-flow enforcement technology (CET) on x86; a more detailed description of
4+
CET is available [here]. Similar to `clang`, this flag takes one of the following values:
5+
6+
- `none` - Disable CET completely (this is the default).
7+
- `branch` - Enable indirect branch tracking (`IBT`).
8+
- `return` - Enable shadow stack (`SHSTK`).
9+
- `full` - Enable both `branch` and `return`.
10+
11+
[here]: https://www.intel.com/content/www/us/en/develop/articles/technical-look-control-flow-enforcement-technology.html
12+
13+
This flag only applies to the LLVM backend: it sets the `cf-protection-branch` and
14+
`cf-protection-return` flags on LLVM modules. Note, however, that all compiled modules linked
15+
together must have the flags set for the compiled output to be CET-enabled. Currently, Rust's
16+
standard library does not ship with CET enabled by default, so you may need to rebuild all standard
17+
modules with a `cargo` command like:
18+
19+
```sh
20+
$ RUSTFLAGS="-Z cf-protection=full" RUSTC="rustc-custom" cargo +nightly build -Z build-std --target x86_64-unknown-linux-gnu
21+
```
22+
23+
### Detection
24+
25+
An ELF binary is CET-enabled if it has the `IBT` and `SHSTK` tags, e.g.:
26+
27+
```sh
28+
$ readelf -a target/x86_64-unknown-linux-gnu/debug/example | grep feature:
29+
Properties: x86 feature: IBT, SHSTK
30+
```
31+
32+
### Troubleshooting
33+
34+
To display modules that are not CET enabled, examine the linker errors available when `cet-report` is enabled:
35+
36+
```sh
37+
$ RUSTC_LOG=rustc_codegen_ssa::back::link=info rustc-custom -v -Z cf-protection=full -C link-arg="-Wl,-z,cet-report=warning" -o example example.rs
38+
...
39+
/usr/bin/ld: /.../build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-d73f7266be14cb8b.rlib(std-d73f7266be14cb8b.std.f7443020-cgu.12.rcgu.o): warning: missing IBT and SHSTK properties
40+
```

src/test/codegen/cf-protection.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Test that the correct module flags are emitted with different control-flow protection flags.
2+
3+
// revisions: undefined none branch return full
4+
// needs-llvm-components: x86
5+
// [undefined] compile-flags:
6+
// [none] compile-flags: -Z cf-protection=none
7+
// [branch] compile-flags: -Z cf-protection=branch
8+
// [return] compile-flags: -Z cf-protection=return
9+
// [full] compile-flags: -Z cf-protection=full
10+
// compile-flags: --target x86_64-unknown-linux-gnu
11+
12+
#![crate_type = "lib"]
13+
14+
// A basic test function.
15+
pub fn test() {
16+
}
17+
18+
// undefined-NOT: !"cf-protection-branch"
19+
// undefined-NOT: !"cf-protection-return"
20+
21+
// none-NOT: !"cf-protection-branch"
22+
// none-NOT: !"cf-protection-return"
23+
24+
// branch-NOT: !"cf-protection-return"
25+
// branch: !"cf-protection-branch", i32 1
26+
// branch-NOT: !"cf-protection-return"
27+
28+
// return-NOT: !"cf-protection-branch"
29+
// return: !"cf-protection-return", i32 1
30+
// return-NOT: !"cf-protection-branch"
31+
32+
// full: !"cf-protection-branch", i32 1
33+
// full: !"cf-protection-return", i32 1

0 commit comments

Comments
 (0)