Skip to content

Commit 09cb29c

Browse files
committed
Auto merge of rust-lang#93439 - abrown:cf-protection, r=nagisa
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>`. Tracking issue for future work is rust-lang#93754.
2 parents bfb2856 + 8d6c973 commit 09cb29c

File tree

5 files changed

+141
-5
lines changed

5 files changed

+141
-5
lines changed

compiler/rustc_codegen_llvm/src/context.rs

+20-1
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

+21-4
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
@@ -2630,11 +2646,11 @@ impl PpMode {
26302646
/// we have an opt-in scheme here, so one is hopefully forced to think about
26312647
/// how the hash should be calculated when adding a new command-line argument.
26322648
crate mod dep_tracking {
2633-
use super::LdImpl;
26342649
use super::{
2635-
BranchProtection, CFGuard, CrateType, DebugInfo, ErrorOutputType, InstrumentCoverage,
2636-
LinkerPluginLto, LocationDetail, LtoCli, OptLevel, OutputType, OutputTypes, Passes,
2637-
SourceFileHashAlgorithm, SwitchWithOptPath, SymbolManglingVersion, TrimmedDefPaths,
2650+
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, ErrorOutputType,
2651+
InstrumentCoverage, LdImpl, LinkerPluginLto, LocationDetail, LtoCli, OptLevel, OutputType,
2652+
OutputTypes, Passes, SourceFileHashAlgorithm, SwitchWithOptPath, SymbolManglingVersion,
2653+
TrimmedDefPaths,
26382654
};
26392655
use crate::lint;
26402656
use crate::options::WasiExecModel;
@@ -2715,6 +2731,7 @@ crate mod dep_tracking {
27152731
NativeLibKind,
27162732
SanitizerSet,
27172733
CFGuard,
2734+
CFProtection,
27182735
TargetTriple,
27192736
Edition,
27202737
LinkerPluginLto,

compiler/rustc_session/src/options.rs

+22
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ mod desc {
380380
pub const parse_sanitizer_memory_track_origins: &str = "0, 1, or 2";
381381
pub const parse_cfguard: &str =
382382
"either a boolean (`yes`, `no`, `on`, `off`, etc), `checks`, or `nochecks`";
383+
pub const parse_cfprotection: &str = "`none`|`no`|`n` (default), `branch`, `return`, or `full`|`yes`|`y` (equivalent to `branch` and `return`)";
383384
pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`";
384385
pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavor::one_of();
385386
pub const parse_optimization_fuel: &str = "crate=integer";
@@ -695,6 +696,25 @@ mod parse {
695696
true
696697
}
697698

699+
crate fn parse_cfprotection(slot: &mut CFProtection, v: Option<&str>) -> bool {
700+
if v.is_some() {
701+
let mut bool_arg = None;
702+
if parse_opt_bool(&mut bool_arg, v) {
703+
*slot = if bool_arg.unwrap() { CFProtection::Full } else { CFProtection::None };
704+
return true;
705+
}
706+
}
707+
708+
*slot = match v {
709+
None | Some("none") => CFProtection::None,
710+
Some("branch") => CFProtection::Branch,
711+
Some("return") => CFProtection::Return,
712+
Some("full") => CFProtection::Full,
713+
Some(_) => return false,
714+
};
715+
true
716+
}
717+
698718
crate fn parse_linker_flavor(slot: &mut Option<LinkerFlavor>, v: Option<&str>) -> bool {
699719
match v.and_then(LinkerFlavor::from_str) {
700720
Some(lf) => *slot = Some(lf),
@@ -1142,6 +1162,8 @@ options! {
11421162
"select which borrowck is used (`mir` or `migrate`) (default: `migrate`)"),
11431163
branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
11441164
"set options for branch target identification and pointer authentication on AArch64"),
1165+
cf_protection: CFProtection = (CFProtection::None, parse_cfprotection, [TRACKED],
1166+
"instrument control-flow architecture protection"),
11451167
cgu_partitioning_strategy: Option<String> = (None, parse_opt_string, [TRACKED],
11461168
"the codegen unit partitioning strategy to use"),
11471169
chalk: bool = (false, parse_bool, [TRACKED],
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

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#![feature(no_core, lang_items)]
14+
#![no_core]
15+
16+
#[lang="sized"]
17+
trait Sized { }
18+
19+
// A basic test function.
20+
pub fn test() {
21+
}
22+
23+
// undefined-NOT: !"cf-protection-branch"
24+
// undefined-NOT: !"cf-protection-return"
25+
26+
// none-NOT: !"cf-protection-branch"
27+
// none-NOT: !"cf-protection-return"
28+
29+
// branch-NOT: !"cf-protection-return"
30+
// branch: !"cf-protection-branch", i32 1
31+
// branch-NOT: !"cf-protection-return"
32+
33+
// return-NOT: !"cf-protection-branch"
34+
// return: !"cf-protection-return", i32 1
35+
// return-NOT: !"cf-protection-branch"
36+
37+
// full: !"cf-protection-branch", i32 1
38+
// full: !"cf-protection-return", i32 1

0 commit comments

Comments
 (0)