Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f24544

Browse files
committedFeb 28, 2024
sess: stabilize -C stack-protector=all
Signed-off-by: David Wood <david@davidtw.co>
1 parent ef32456 commit 3f24544

19 files changed

+103
-33
lines changed
 

‎compiler/rustc_session/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ session_split_lto_unit_requires_lto = `-Zsplit-lto-unit` requires `-Clto`, `-Clt
104104
105105
session_target_requires_unwind_tables = target requires unwind tables, they cannot be disabled with `-C force-unwind-tables=no`
106106
107-
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
107+
session_target_stack_protector_not_supported = `-C stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
108108
109109
session_unleashed_feature_help_named = skipping check for `{$gate}` feature
110110
session_unleashed_feature_help_unnamed = skipping check that does not even have a feature gate

‎compiler/rustc_session/src/config.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::source_map::FilePathMapping;
1818
use rustc_span::symbol::{sym, Symbol};
1919
use rustc_span::{FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm};
2020
use rustc_target::abi::Align;
21-
use rustc_target::spec::LinkSelfContainedComponents;
21+
use rustc_target::spec::{LinkSelfContainedComponents, StackProtector};
2222
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, SplitDebuginfo};
2323
use rustc_target::spec::{Target, TargetTriple, TargetWarnings, TARGETS};
2424
use std::collections::btree_map::{
@@ -2736,6 +2736,22 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27362736
}
27372737
}
27382738

2739+
// Check for unstable values of `-C stack-protector`.
2740+
// This is what prevents them from being used on stable compilers.
2741+
match cg.stack_protector {
2742+
// Stable values:
2743+
StackProtector::All | StackProtector::None => {}
2744+
// Unstable values:
2745+
StackProtector::Basic | StackProtector::Strong => {
2746+
if !unstable_opts.unstable_options {
2747+
early_dcx.early_fatal(
2748+
"`-C stack-protector=basic` and `-C stack-protector=strong` \
2749+
require `-Z unstable-options`",
2750+
);
2751+
}
2752+
}
2753+
}
2754+
27392755
if cg.instrument_coverage != InstrumentCoverage::Off {
27402756
if cg.profile_generate.enabled() || cg.profile_use.is_some() {
27412757
early_dcx.early_fatal(

‎compiler/rustc_session/src/options.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,9 @@ options! {
15051505
#[rustc_lint_opt_deny_field_access("use `Session::split_debuginfo` instead of this field")]
15061506
split_debuginfo: Option<SplitDebuginfo> = (None, parse_split_debuginfo, [TRACKED],
15071507
"how to handle split-debuginfo, a platform-specific option"),
1508+
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]
1509+
stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED],
1510+
"control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"),
15081511
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
15091512
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
15101513
symbol_mangling_version: Option<SymbolManglingVersion> = (None,
@@ -1897,9 +1900,6 @@ written to standard error output)"),
18971900
"enable LTO unit splitting (default: no)"),
18981901
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
18991902
"hash algorithm of source files in debug info (`md5`, `sha1`, or `sha256`)"),
1900-
#[rustc_lint_opt_deny_field_access("use `Session::stack_protector` instead of this field")]
1901-
stack_protector: StackProtector = (StackProtector::None, parse_stack_protector, [TRACKED],
1902-
"control stack smash protection strategy (`rustc --print stack-protector-strategies` for details)"),
19031903
staticlib_allow_rdylib_deps: bool = (false, parse_bool, [TRACKED],
19041904
"allow staticlibs to have rust dylib dependencies"),
19051905
staticlib_prefer_dynamic: bool = (false, parse_bool, [TRACKED],

‎compiler/rustc_session/src/session.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ impl Session {
774774

775775
pub fn stack_protector(&self) -> StackProtector {
776776
if self.target.options.supports_stack_protector {
777-
self.opts.unstable_opts.stack_protector
777+
self.opts.cg.stack_protector
778778
} else {
779779
StackProtector::None
780780
}
@@ -1314,10 +1314,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
13141314
}
13151315
}
13161316

1317-
if sess.opts.unstable_opts.stack_protector != StackProtector::None {
1317+
if sess.opts.cg.stack_protector != StackProtector::None {
13181318
if !sess.target.options.supports_stack_protector {
13191319
sess.dcx().emit_warn(errors::StackProtectorNotSupportedForTarget {
1320-
stack_protector: sess.opts.unstable_opts.stack_protector,
1320+
stack_protector: sess.opts.cg.stack_protector,
13211321
target_triple: &sess.opts.target_triple,
13221322
});
13231323
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,26 @@ Note that all three options are supported on Linux and Apple platforms,
541541
Attempting to use an unsupported option requires using the nightly channel
542542
with the `-Z unstable-options` flag.
543543

544+
## stack-protector
545+
546+
The option `-C stack-protector=val` controls stack smashing protection. See [Stack smashing
547+
protection][stack-smashing] for more details.
548+
549+
Supported values for this option are:
550+
551+
- `none` - no stack protectors
552+
- `all` - force use of stack protectors for all functions
553+
554+
Unstable options for this value are:
555+
556+
- `basic` - enable stack protectors for functions potentially vulnerable to stack smashing (basic
557+
heuristic)
558+
- `strong` - enable stack protectors for functions potentially vulnerable to stack smashing (strong
559+
heuristic)
560+
561+
`basic` and `strong` values for `-C stack-protector` require using the nightly channel with the
562+
`-Z unstable-options` flag.
563+
544564
## strip
545565

546566
The option `-C strip=val` controls stripping of debuginfo and similar auxiliary
@@ -634,3 +654,4 @@ effective only for x86 targets.
634654
[instrumentation-based code coverage]: ../instrument-coverage.md
635655
[profile-guided optimization]: ../profile-guided-optimization.md
636656
[option-g-debug]: ../command-line-arguments.md#option-g-debug
657+
[stack-smashing]: ../exploit-mitigations.md#stack-smashing-protection

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

+6-2
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

+4-4
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

+4-4
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

+4-4
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

‎tests/assembly/stack-protector/stack-protector-target-support.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
//@ [r84] needs-llvm-components: x86
176176
//@ [r85] compile-flags: --target x86_64-unknown-redox
177177
//@ [r85] needs-llvm-components: x86
178-
//@ compile-flags: -Z stack-protector=all
178+
//@ compile-flags: -C stack-protector=all
179179
//@ compile-flags: -C opt-level=2
180180

181181
#![crate_type = "lib"]

‎tests/codegen/stack-protector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//@ revisions: all strong basic none
22
//@ ignore-nvptx64 stack protector not supported
3-
//@ [all] compile-flags: -Z stack-protector=all
4-
//@ [strong] compile-flags: -Z stack-protector=strong
5-
//@ [basic] compile-flags: -Z stack-protector=basic
3+
//@ [all] compile-flags: -C stack-protector=all
4+
//@ [strong] compile-flags: -C stack-protector=strong -Z unstable-options
5+
//@ [basic] compile-flags: -C stack-protector=basic -Z unstable-options
66

77
#![crate_type = "lib"]
88

‎tests/ui/abi/stack-protector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ run-pass
22
//@ only-x86_64-unknown-linux-gnu
33
//@ revisions: ssp no-ssp
4-
//@ [ssp] compile-flags: -Z stack-protector=all
4+
//@ [ssp] compile-flags: -C stack-protector=all
55
//@ compile-flags: -C opt-level=2
66
//@ compile-flags: -g
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: `-C stack-protector=basic` and `-C stack-protector=strong` require `-Z unstable-options`
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ revisions: all strong strong-ok basic basic-ok
2+
//@ compile-flags: --target x86_64-unknown-linux-gnu
3+
//@ needs-llvm-components: x86
4+
//@ [all] check-pass
5+
//@ [all] compile-flags: -C stack-protector=all
6+
//@ [strong] check-fail
7+
//@ [strong] compile-flags: -C stack-protector=strong
8+
//@ [strong-ok] check-pass
9+
//@ [strong-ok] compile-flags: -C stack-protector=strong -Z unstable-options
10+
//@ [basic] check-fail
11+
//@ [basic] compile-flags: -C stack-protector=basic
12+
//@ [basic-ok] check-pass
13+
//@ [basic-ok] compile-flags: -C stack-protector=basic -Z unstable-options
14+
15+
#![crate_type = "lib"]
16+
#![feature(no_core, lang_items)]
17+
#![no_std]
18+
#![no_core]
19+
20+
#[lang = "sized"]
21+
trait Sized {}
22+
#[lang = "copy"]
23+
trait Copy {}
24+
25+
pub fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: `-C stack-protector=basic` and `-C stack-protector=strong` require `-Z unstable-options`
2+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: `-Z stack-protector=all` is not supported for target nvptx64-nvidia-cuda and will be ignored
1+
warning: `-C stack-protector=all` is not supported for target nvptx64-nvidia-cuda and will be ignored
22

33
warning: 1 warning emitted
44

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: `-Z stack-protector=basic` is not supported for target nvptx64-nvidia-cuda and will be ignored
1+
warning: `-C stack-protector=basic` is not supported for target nvptx64-nvidia-cuda and will be ignored
22

33
warning: 1 warning emitted
44

‎tests/ui/stack-protector/warn-stack-protector-unsupported.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//@ revisions: all strong basic
33
//@ compile-flags: --target nvptx64-nvidia-cuda
44
//@ needs-llvm-components: nvptx
5-
//@ [all] compile-flags: -Z stack-protector=all
6-
//@ [strong] compile-flags: -Z stack-protector=strong
7-
//@ [basic] compile-flags: -Z stack-protector=basic
5+
//@ [all] compile-flags: -C stack-protector=all
6+
//@ [strong] compile-flags: -C stack-protector=strong -Z unstable-options
7+
//@ [basic] compile-flags: -C stack-protector=basic -Z unstable-options
88

99
#![crate_type = "lib"]
1010
#![feature(no_core, lang_items)]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: `-Z stack-protector=strong` is not supported for target nvptx64-nvidia-cuda and will be ignored
1+
warning: `-C stack-protector=strong` is not supported for target nvptx64-nvidia-cuda and will be ignored
22

33
warning: 1 warning emitted
44

0 commit comments

Comments
 (0)
Failed to load comments.