Skip to content

Commit 7984036

Browse files
committed
sess: stabilize -C stack-protector=all
Signed-off-by: David Wood <[email protected]>
1 parent 60d1465 commit 7984036

20 files changed

+105
-34
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ fn test_codegen_options_tracking_hash() {
632632
tracked!(relro_level, Some(RelroLevel::Full));
633633
tracked!(soft_float, true);
634634
tracked!(split_debuginfo, Some(SplitDebuginfo::Packed));
635+
tracked!(stack_protector, StackProtector::All);
635636
tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
636637
tracked!(target_cpu, Some(String::from("abc")));
637638
tracked!(target_feature, String::from("all the features, all of them"));
@@ -844,7 +845,6 @@ fn test_unstable_options_tracking_hash() {
844845
tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));
845846
tracked!(split_lto_unit, Some(true));
846847
tracked!(src_hash_algorithm, Some(SourceFileHashAlgorithm::Sha1));
847-
tracked!(stack_protector, StackProtector::All);
848848
tracked!(teach, true);
849849
tracked!(thinlto, Some(true));
850850
tracked!(tiny_const_eval_limit, true);

compiler/rustc_session/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ session_split_lto_unit_requires_lto = `-Zsplit-lto-unit` requires `-Clto`, `-Clt
109109
110110
session_target_requires_unwind_tables = target requires unwind tables, they cannot be disabled with `-C force-unwind-tables=no`
111111
112-
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
112+
session_target_stack_protector_not_supported = `-C stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
113113
114114
session_unleashed_feature_help_named = skipping check for `{$gate}` feature
115115
session_unleashed_feature_help_unnamed = skipping check that does not even have a feature gate

compiler/rustc_session/src/config.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use rustc_span::edition::{Edition, DEFAULT_EDITION, EDITION_NAME_LIST, LATEST_ST
2424
use rustc_span::source_map::FilePathMapping;
2525
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm};
2626
use rustc_target::spec::{
27-
FramePointer, LinkSelfContainedComponents, LinkerFeatures, SplitDebuginfo, Target, TargetTriple,
27+
FramePointer, LinkSelfContainedComponents, LinkerFeatures, SplitDebuginfo, StackProtector,
28+
Target, TargetTriple,
2829
};
2930
use tracing::debug;
3031

@@ -2480,6 +2481,22 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24802481
}
24812482
}
24822483

2484+
// Check for unstable values of `-C stack-protector`.
2485+
// This is what prevents them from being used on stable compilers.
2486+
match cg.stack_protector {
2487+
// Stable values:
2488+
StackProtector::All | StackProtector::None => {}
2489+
// Unstable values:
2490+
StackProtector::Basic | StackProtector::Strong => {
2491+
if !unstable_opts.unstable_options {
2492+
early_dcx.early_fatal(
2493+
"`-C stack-protector=basic` and `-C stack-protector=strong` \
2494+
require `-Z unstable-options`",
2495+
);
2496+
}
2497+
}
2498+
}
2499+
24832500
if cg.instrument_coverage != InstrumentCoverage::No {
24842501
if cg.profile_generate.enabled() || cg.profile_use.is_some() {
24852502
early_dcx.early_fatal(

compiler/rustc_session/src/options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,9 @@ options! {
16021602
#[rustc_lint_opt_deny_field_access("use `Session::split_debuginfo` instead of this field")]
16031603
split_debuginfo: Option<SplitDebuginfo> = (None, parse_split_debuginfo, [TRACKED],
16041604
"how to handle split-debuginfo, a platform-specific option"),
1605+
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]
1606+
stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED],
1607+
"control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"),
16051608
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
16061609
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
16071610
symbol_mangling_version: Option<SymbolManglingVersion> = (None,
@@ -2014,9 +2017,6 @@ written to standard error output)"),
20142017
"enable LTO unit splitting (default: no)"),
20152018
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
20162019
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
2017-
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]
2018-
stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED],
2019-
"control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"),
20202020
staticlib_allow_rdylib_deps: bool = (false, parse_bool, [TRACKED],
20212021
"allow staticlibs to have rust dylib dependencies"),
20222022
staticlib_prefer_dynamic: bool = (false, parse_bool, [TRACKED],

compiler/rustc_session/src/session.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ impl Session {
787787

788788
pub fn stack_protector(&self) -> StackProtector {
789789
if self.target.options.supports_stack_protector {
790-
self.opts.unstable_opts.stack_protector
790+
self.opts.cg.stack_protector
791791
} else {
792792
StackProtector::None
793793
}
@@ -1274,10 +1274,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
12741274
}
12751275
}
12761276

1277-
if sess.opts.unstable_opts.stack_protector != StackProtector::None {
1277+
if sess.opts.cg.stack_protector != StackProtector::None {
12781278
if !sess.target.options.supports_stack_protector {
12791279
sess.dcx().emit_warn(errors::StackProtectorNotSupportedForTarget {
1280-
stack_protector: sess.opts.unstable_opts.stack_protector,
1280+
stack_protector: sess.opts.cg.stack_protector,
12811281
target_triple: &sess.opts.target_triple,
12821282
});
12831283
}

src/doc/rustc/src/codegen-options/index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,26 @@ Note that all three options are supported on Linux and Apple platforms,
565565
Attempting to use an unsupported option requires using the nightly channel
566566
with the `-Z unstable-options` flag.
567567

568+
## stack-protector
569+
570+
The option `-C stack-protector=val` controls stack smashing protection. See [Stack smashing
571+
protection][stack-smashing] for more details.
572+
573+
Supported values for this option are:
574+
575+
- `none` - no stack protectors
576+
- `all` - force use of stack protectors for all functions
577+
578+
Unstable options for this value are:
579+
580+
- `basic` - enable stack protectors for functions potentially vulnerable to stack smashing (basic
581+
heuristic)
582+
- `strong` - enable stack protectors for functions potentially vulnerable to stack smashing (strong
583+
heuristic)
584+
585+
`basic` and `strong` values for `-C stack-protector` require using the nightly channel with the
586+
`-Z unstable-options` flag.
587+
568588
## strip
569589

570590
The option `-C strip=val` controls stripping of debuginfo and similar auxiliary
@@ -667,3 +687,4 @@ effective only for x86 targets.
667687
[instrumentation-based code coverage]: ../instrument-coverage.md
668688
[profile-guided optimization]: ../profile-guided-optimization.md
669689
[option-g-debug]: ../command-line-arguments.md#option-g-debug
690+
[stack-smashing]: ../exploit-mitigations.md#stack-smashing-protection

src/doc/rustc/src/exploit-mitigations.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ equivalent.
6363
| Stack clashing protection | Yes | 1.20.0 (2017-08-31) |
6464
| Read-only relocations and immediate binding | Yes | 1.21.0 (2017-10-12) |
6565
| Heap corruption protection | Yes | 1.32.0 (2019-01-17) (via operating system default or specified allocator) |
66-
| Stack smashing protection | Yes | Nightly |
66+
| Stack smashing protection | Yes | 1.78.0 (2024-05-02) |
6767
| Forward-edge control flow protection | Yes | Nightly |
6868
| Backward-edge control flow protection (e.g., shadow and safe stack) | Yes | Nightly |
6969

@@ -357,7 +357,8 @@ instruction pointer, and checking if this value has changed when returning from
357357
a function. This is also known as “Stack Protector” or “Stack Smashing
358358
Protector (SSP)”.
359359

360-
The Rust compiler supports stack smashing protection on nightly builds[40].
360+
The Rust compiler supports stack smashing protection with the `-C stack-protector=all`
361+
flag since version 1.78.0 (2024-05-02)[40], [47].
361362

362363
![Screenshot of IDA Pro listing cross references to __stack_chk_fail in hello-rust.](images/image3.png "Cross references to __stack_chk_fail in hello-rust.")
363364
Fig. 14. IDA Pro listing cross references to `__stack_chk_fail` in hello-rust.
@@ -627,3 +628,6 @@ to `READ_IMPLIES_EXEC`).
627628

628629
46. “SafeStack.” The Rust Unstable Book.
629630
[https://doc.rust-lang/org/unstable-book/compiler-flags/sanitizer.html#safestack](../unstable-book/compiler-flags/sanitizer.html#safestack).
631+
632+
47. D. Wood. “sess: stabilize stack-protector=all #121742” GitHub.
633+
<https://github.com/rust-lang/rust/pull/121742>

tests/assembly/stack-protector/stack-protector-heuristics-effect-windows-32bit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//@ only-windows
44
//@ only-msvc
55
//@ ignore-64bit 64-bit table based SEH has slightly different behaviors than classic SEH
6-
//@ [all] compile-flags: -Z stack-protector=all
7-
//@ [strong] compile-flags: -Z stack-protector=strong
8-
//@ [basic] compile-flags: -Z stack-protector=basic
9-
//@ [none] compile-flags: -Z stack-protector=none
6+
//@ [all] compile-flags: -C stack-protector=all
7+
//@ [strong] compile-flags: -C stack-protector=strong -Z unstable-options
8+
//@ [basic] compile-flags: -C stack-protector=basic -Z unstable-options
9+
//@ [none] compile-flags: -C stack-protector=none
1010
//@ compile-flags: -C opt-level=2 -Z merge-functions=disabled
1111

1212
#![crate_type = "lib"]

tests/assembly/stack-protector/stack-protector-heuristics-effect-windows-64bit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//@ only-windows
44
//@ only-msvc
55
//@ ignore-32bit 64-bit table based SEH has slightly different behaviors than classic SEH
6-
//@ [all] compile-flags: -Z stack-protector=all
7-
//@ [strong] compile-flags: -Z stack-protector=strong
8-
//@ [basic] compile-flags: -Z stack-protector=basic
9-
//@ [none] compile-flags: -Z stack-protector=none
6+
//@ [all] compile-flags: -C stack-protector=all
7+
//@ [strong] compile-flags: -C stack-protector=strong -Z unstable-options
8+
//@ [basic] compile-flags: -C stack-protector=basic -Z unstable-options
9+
//@ [none] compile-flags: -C stack-protector=none
1010
//@ compile-flags: -C opt-level=2 -Z merge-functions=disabled
1111

1212
#![crate_type = "lib"]

tests/assembly/stack-protector/stack-protector-heuristics-effect.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
//@ ignore-msvc stack check code uses different function names
55
//@ ignore-nvptx64 stack protector is not supported
66
//@ ignore-wasm32-bare
7-
//@ [all] compile-flags: -Z stack-protector=all
8-
//@ [strong] compile-flags: -Z stack-protector=strong
9-
//@ [basic] compile-flags: -Z stack-protector=basic
10-
//@ [none] compile-flags: -Z stack-protector=none
7+
//@ [all] compile-flags: -C stack-protector=all
8+
//@ [strong] compile-flags: -C stack-protector=strong -Z unstable-options
9+
//@ [basic] compile-flags: -C stack-protector=basic -Z unstable-options
10+
//@ [none] compile-flags: -C stack-protector=none
1111
//@ compile-flags: -C opt-level=2 -Z merge-functions=disabled
1212
//@ min-llvm-version: 17.0.2
1313

0 commit comments

Comments
 (0)