Enable Stack Protection for Shared Library

I am trying to build a shared library. I want it to have a stack canary. I have installed rustc 1.82.0-beta.5 (6a3b69c6b 2024-09-27).

cargo build --release
error: failed to run rustc to learn about target-specific information .rustup/toolchains/beta-aarch64-apple-darwin/bin/rustc - --crate-name ___ --print=file-names -C stack-protector=strong --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg(exit status: 1) --- stderr error: unknown codegen option: stack-protector`

Its nightly only and a -Z option.

1 Like

Thank you for you reply.

cargo-features = ["profile-rustflags"]
[package]
name = "sample_stack_canary"
version = "0.1.0"
edition = "2021"

[dependencies]
[profile.release]
rustflags = ["-Z", "stack-protector=strong"]

Having the above in cargo.toml had added stack canary protection. As you have mentioned I had '-C' instead of '-Z'.

It worked fine with a simple binary.
But when I tried to build a Shared Library for Android it did not accept the -Z flag and stack-protector attribute.
I used cargo-ndk to build a shared library for android. So what could be the possible solution to enable stack canary for an android so when we build with Cargo.

As I worked further I did not find a way to pass the stack-protector flag to cargo-ndk. However, I found a way to build the Android shared library by configuring the ndk toolchain manually. The below article helped me in building an 'so' without cargo-ndk

https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.