Proposal
This MCP proposes adding a new unstable -Z stack-protector-guard target
modifier to configure how the stack canary is accessed, equivalent to
Clang's -mstack-protector-guard=* options.
Rust nightly already supports stack smashing protection via
-Z stack-protector. However, rustc currently always generates code that
reads the canary from the global __stack_chk_guard symbol. This is
incompatible with Linux kernel configurations that enable
CONFIG_STACKPROTECTOR_PER_TASK, where the canary is stored in a
per-task location (e.g. a system register on AArch64, or TLS on x86)
rather than a global variable.
LLVM already supports customizing the guard location via module-level
metadata (stack-protector-guard, stack-protector-guard-offset,
stack-protector-guard-reg, stack-protector-guard-symbol). Clang
exposes this through -mstack-protector-guard=* flags. This MCP proposes
exposing the same functionality in rustc.
The proposed flag uses a comma-separated syntax:
-Z stack-protector-guard=,offset=N,reg=R
mode: global, tls, or sysreg
offset=N: offset from the guard base
reg=R: TLS/system register (e.g. fs, gs, sp_el0)
symbol=S: custom guard symbol name
Architecture validation:
- x86/x86_64:
tls, global
- AArch64:
sysreg, global
- ARM:
tls, global
- RISC-V 32/64:
sysreg, global
- Other architectures: error
This is a target modifier, ensuring consistency across the crate graph
- without it, mismatches would only be caught during LTO.
The implementation follows the same pattern as -Z branch-protection.
Prototype PR: rust-lang/rust#159165
Mentors or Reviewers
@rcvalle
Process
The main points of the Major Change Process are as follows:
You can read more about Major Change Proposals on forge.
Proposal
This MCP proposes adding a new unstable
-Z stack-protector-guardtargetmodifier to configure how the stack canary is accessed, equivalent to
Clang's
-mstack-protector-guard=*options.Rust nightly already supports stack smashing protection via
-Z stack-protector. However, rustc currently always generates code thatreads the canary from the global
__stack_chk_guardsymbol. This isincompatible with Linux kernel configurations that enable
CONFIG_STACKPROTECTOR_PER_TASK, where the canary is stored in aper-task location (e.g. a system register on AArch64, or TLS on x86)
rather than a global variable.
LLVM already supports customizing the guard location via module-level
metadata (
stack-protector-guard,stack-protector-guard-offset,stack-protector-guard-reg,stack-protector-guard-symbol). Clangexposes this through
-mstack-protector-guard=*flags. This MCP proposesexposing the same functionality in rustc.
The proposed flag uses a comma-separated syntax:
-Z stack-protector-guard=,offset=N,reg=R
mode:global,tls, orsysregoffset=N: offset from the guard basereg=R: TLS/system register (e.g.fs,gs,sp_el0)symbol=S: custom guard symbol nameArchitecture validation:
tls,globalsysreg,globaltls,globalsysreg,globalThis is a target modifier, ensuring consistency across the crate graph
The implementation follows the same pattern as
-Z branch-protection.Prototype PR: rust-lang/rust#159165
Mentors or Reviewers
@rcvalle
Process
The main points of the Major Change Process are as follows:
@rustbot secondor kickoff a team FCP with@rfcbot fcp $RESOLUTION.You can read more about Major Change Proposals on forge.