rust: add flags for shadow call stack sanitizer
Alice Ryhl
reviewerPATCH v2 1/1 rust: add flags for shadow call stack sanitizer
Add flags to support the shadow call stack sanitizer, both in the dynamic and non-dynamic modes.
Right now, the compiler will emit the warning "unknown feature specified for -Ctarget-feature: reserve-x18". However, the compiler still passes it to the codegen backend, so the flag will work just fine. Once rustc starts recognizing the flag (or provides another way to enable the feature), it will stop emitting this warning. See [1] for the relevant issue.
Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. However, I still think it is reasonable to add the flag now, as it will at least fail the build when using an invalid configuration, until the Rust compiler is fixed to list -Zsanitizer=shadow-call-stack as supported for the target. See [2] for the feature request to add this.
I have tested this change with Rust Binder on an Android device using CONFIG_DYNAMIC_SCS. Without the -Ctarget-feature=+reserve-x18 flag, the phone crashes immediately on boot, and with the flag, the phone appears to work normally.
This contains a TODO to add the -Zuse-sync-unwind=n flag. The flag defaults to n, so it isn't a problem today, but the flag is unstable, so the default could change in a future compiler release.
---
This patch raises the question of whether we should change the Rust
aarch64 support to use a custom target.json specification. If we do
that, then we can fix both the warning for dynamic SCS and the
build-failure for non-dynamic SCS without waiting for a new version of
rustc with the mentioned issues fixed.
---
Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- Makefile | 1 + arch/arm64/Makefile | 4 ++++ 2 files changed, 5 insertions(+)
Makefile +1 -0
@@ -936,6 +936,7 @@ ifdef CONFIG_SHADOW_CALL_STACK ifndef CONFIG_DYNAMIC_SCS CC_FLAGS_SCS := -fsanitize=shadow-call-stack KBUILD_CFLAGS += $(CC_FLAGS_SCS) +KBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack endif export CC_FLAGS_SCS endif
arch/arm64/Makefile +4 -0
@@ -48,9 +48,12 @@ KBUILD_AFLAGS += $(call cc-option,-mabi=lp64) ifneq ($(CONFIG_UNWIND_TABLES),y) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables KBUILD_AFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=n else KBUILD_CFLAGS += -fasynchronous-unwind-tables KBUILD_AFLAGS += -fasynchronous-unwind-tables +# TODO: Pass -Zuse-sync-unwind=n once we upgrade to Rust 1.77.0 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
@@ -103,6 +106,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Ctarget-feature=+reserve-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
---
base-commit: 90d35da658da8cff0d4ecbb5113f5fac9d00eb72
change-id: 20240304-shadow-call-stack-9c197a4361d9
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Quoted 31 lines · Alice Ryhl
Add flags to support the shadow call stack sanitizer, both in the dynamic and non-dynamic modes.
Right now, the compiler will emit the warning "unknown feature specified for
-Ctarget-feature:reserve-x18". However, the compiler still passes it to the codegen backend, so the flag will work just fine. Once rustc starts recognizing the flag (or provides another way to enable the feature), it will stop emitting this warning. See [1] for the relevant issue.Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. However, I still think it is reasonable to add the flag now, as it will at least fail the build when using an invalid configuration, until the Rust compiler is fixed to list -Zsanitizer=shadow-call-stack as supported for the target. See [2] for the feature request to add this.
I have tested this change with Rust Binder on an Android device using
CONFIG_DYNAMIC_SCS. Without the -Ctarget-feature=+reserve-x18 flag, the phone crashes immediately on boot, and with the flag, the phone appears to work normally.This contains a TODO to add the -Zuse-sync-unwind=n flag. The flag defaults to n, so it isn't a problem today, but the flag is unstable, so the default could change in a future compiler release.
Signed-off-by Alice Ryhl <[email protected]>
Quoted 6 lines
---
This patch raises the question of whether we should change the Rust aarch64 support to use a custom target.json specification. If we do that, then we can fix both the warning for dynamic SCS and the build-failure for non-dynamic SCS without waiting for a new version of rustc with the mentioned issues fixed.
If the arm64 maintainers are OK with the warning being triggered in that case:
Otherwise partially reverting to the target.json approach sounds good too.
I added the -Zuse-sync-unwind=n to the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.
Cheers,
Miguel
Quoted 64 lines
Add flags to support the shadow call stack sanitizer, both in the dynamic and non-dynamic modes.
Right now, the compiler will emit the warning "unknown feature specified for
-Ctarget-feature:reserve-x18". However, the compiler still passes it to the codegen backend, so the flag will work just fine. Once rustc starts recognizing the flag (or provides another way to enable the feature), it will stop emitting this warning. See [1] for the relevant issue.Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. However, I still think it is reasonable to add the flag now, as it will at least fail the build when using an invalid configuration, until the Rust compiler is fixed to list -Zsanitizer=shadow-call-stack as supported for the target. See [2] for the feature request to add this.
I have tested this change with Rust Binder on an Android device using
CONFIG_DYNAMIC_SCS. Without the -Ctarget-feature=+reserve-x18 flag, the phone crashes immediately on boot, and with the flag, the phone appears to work normally.This contains a TODO to add the -Zuse-sync-unwind=n flag. The flag defaults to n, so it isn't a problem today, but the flag is unstable, so the default could change in a future compiler release.
Signed-off-by Alice Ryhl <[email protected]>---
This patch raises the question of whether we should change the Rust
aarch64 support to use a custom target.json specification. If we do
that, then we can fix both the warning for dynamic SCS and the
build-failure for non-dynamic SCS without waiting for a new version of
rustc with the mentioned issues fixed.
---
Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/r/[email protected]
---Makefile | 1 + arch/arm64/Makefile | 4 ++++ 2 files changed, 5 insertions(+)Makefile +1
-0@@ -936,6 +936,7 @@ ifdef CONFIG_SHADOW_CALL_STACK ifndef CONFIG_DYNAMIC_SCS CC_FLAGS_SCS := -fsanitize=shadow-call-stack KBUILD_CFLAGS += $(CC_FLAGS_SCS) +KBUILD_RUSTFLAGS += -Zsanitizer=shadow-call-stack endif export CC_FLAGS_SCS endifarch/arm64/Makefile +0
-0@@ -48,9 +48,12 @@ KBUILD_AFLAGS += $(call cc-option,-mabi=lp64) ifneq ($(CONFIG_UNWIND_TABLES),y)
arch/arm64/Makefile KBUILD_AFLAGS += $(call cc-option,-mabi=lp64) · line 58
Quoted 10 lines
arch/arm64/Makefile +3
-0KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables KBUILD_AFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=n else KBUILD_CFLAGS += -fasynchronous-unwind-tables KBUILD_AFLAGS += -fasynchronous-unwind-tables +# TODO: Pass -Zuse-sync-unwind=n once we upgrade to Rust 1.77.0 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y endif
That's the setup I used for my previous testing at [1], offering:
- Best Valentin
arch/arm64/Makefile endif · line 112
Quoted 9 lines
arch/arm64/Makefile +1
-0ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)@@ -103,6 +106,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Ctarget-feature=+reserve-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 30 lines · Alice Ryhl
Add flags to support the shadow call stack sanitizer, both in the dynamic and non-dynamic modes.
Right now, the compiler will emit the warning "unknown feature specified for
-Ctarget-feature:reserve-x18". However, the compiler still passes it to the codegen backend, so the flag will work just fine. Once rustc starts recognizing the flag (or provides another way to enable the feature), it will stop emitting this warning. See [1] for the relevant issue.Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. However, I still think it is reasonable to add the flag now, as it will at least fail the build when using an invalid configuration, until the Rust compiler is fixed to list -Zsanitizer=shadow-call-stack as supported for the target. See [2] for the feature request to add this.
I have tested this change with Rust Binder on an Android device using
CONFIG_DYNAMIC_SCS. Without the -Ctarget-feature=+reserve-x18 flag, the phone crashes immediately on boot, and with the flag, the phone appears to work normally.This contains a TODO to add the -Zuse-sync-unwind=n flag. The flag defaults to n, so it isn't a problem today, but the flag is unstable, so the default could change in a future compiler release.
Signed-off-by Alice Ryhl <[email protected]>
Quoted 10 lines
---
This patch raises the question of whether we should change the Rust
aarch64 support to use a custom target.json specification. If we do
that, then we can fix both the warning for dynamic SCS and the
build-failure for non-dynamic SCS without waiting for a new version of
rustc with the mentioned issues fixed.
---
Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/r/[email protected]
Sami
Will Deacon
maintainerQuoted 39 lines · Miguel Ojeda
Quoted 31 lines · Alice Ryhl
Add flags to support the shadow call stack sanitizer, both in the dynamic and non-dynamic modes.
Right now, the compiler will emit the warning "unknown feature specified for
-Ctarget-feature:reserve-x18". However, the compiler still passes it to the codegen backend, so the flag will work just fine. Once rustc starts recognizing the flag (or provides another way to enable the feature), it will stop emitting this warning. See [1] for the relevant issue.Currently, the compiler thinks that the aarch64-unknown-none target doesn't support -Zsanitizer=shadow-call-stack, so the build will fail if you enable shadow call stack in non-dynamic mode. However, I still think it is reasonable to add the flag now, as it will at least fail the build when using an invalid configuration, until the Rust compiler is fixed to list -Zsanitizer=shadow-call-stack as supported for the target. See [2] for the feature request to add this.
I have tested this change with Rust Binder on an Android device using
CONFIG_DYNAMIC_SCS. Without the -Ctarget-feature=+reserve-x18 flag, the phone crashes immediately on boot, and with the flag, the phone appears to work normally.This contains a TODO to add the -Zuse-sync-unwind=n flag. The flag defaults to n, so it isn't a problem today, but the flag is unstable, so the default could change in a future compiler release.
Signed-off-by Alice Ryhl <[email protected]>Quoted 6 lines
---
This patch raises the question of whether we should change the Rust aarch64 support to use a custom target.json specification. If we do that, then we can fix both the warning for dynamic SCS and the build-failure for non-dynamic SCS without waiting for a new version of rustc with the mentioned issues fixed.
Quoted 1 line
If the arm64 maintainers are OK with the warning being triggered in that case:
Sorry, I meant to reply on this at the time...
Quoted 7 lines
Acked-by Miguel Ojeda <[email protected]>Otherwise partially reverting to the
target.jsonapproach sounds good too.I added the
-Zuse-sync-unwind=nto the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
So, please can we fix this in rustc and just have SCS dependent on that?
Cheers,
Will
Quoted 5 lines · Will Deacon
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
Yeah, it sounds fair, thanks!
After the warning is fixed (i.e. -Zfixed-x18 or similar is implemented etc.), I would recommend adding support to the kernel for the -Z (unstable) flags, similar to this patch, in order to test them easily and getting rustc to stabilize them. Then the only change required should be a name change to -C or similar.
Cheers,
Miguel
Alice Ryhl
reviewerQuoted 7 lines · Will Deacon
Quoted 5 lines · Miguel Ojeda
Otherwise partially reverting to the
target.jsonapproach sounds good too.I added the
-Zuse-sync-unwind=nto the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.
Quoted 6 lines
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
So, please can we fix this in rustc and just have SCS dependent on that?
Hi Will,
Just to keep you in the loop, I've posted a PR to make rustc recognize the reserve-x18 target feature, so that the -Ctarget-feature=+reserve-x18 flag stops emitting a warning.
This should be sufficient for adding support for CONFIG_DYNAMIC_SCS.
You can find it here:
https://github.com/rust-lang/rust/pull/124323
As for non-dynamic SCS, I plan to tackle that after the PR is merged. See the "Future possibilities" section in the linked PR for more info on that.
Alice
Will Deacon
maintainerQuoted 19 lines · Alice Ryhl
Quoted 7 lines · Will Deacon
Quoted 5 lines · Miguel Ojeda
Otherwise partially reverting to the
target.jsonapproach sounds good too.I added the
-Zuse-sync-unwind=nto the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.Quoted 6 lines
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
So, please can we fix this in rustc and just have SCS dependent on that?
Just to keep you in the loop, I've posted a PR to make rustc recognize the reserve-x18 target feature, so that the -Ctarget-feature=+reserve-x18 flag stops emitting a warning.
Quoted 8 lines
This should be sufficient for adding support for
CONFIG_DYNAMIC_SCS.You can find it here:
https://github.com/rust-lang/rust/pull/124323As for non-dynamic SCS, I plan to tackle that after the PR is merged. See the "Future possibilities" section in the linked PR for more info on that.
Thanks for persevering with this, Alice. I read the pull request above, but it looks like you went with:
https://github.com/rust-lang/rust/pull/124655
instead, which was merged (hurrah!). Do we need anything else?
Will
Alice Ryhl
reviewerQuoted 30 lines · Will Deacon
Quoted 19 lines · Alice Ryhl
Quoted 7 lines · Will Deacon
Quoted 5 lines · Miguel Ojeda
Otherwise partially reverting to the
target.jsonapproach sounds good too.I added the
-Zuse-sync-unwind=nto the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.Quoted 6 lines
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
So, please can we fix this in rustc and just have SCS dependent on that?
Just to keep you in the loop, I've posted a PR to make rustc recognize the reserve-x18 target feature, so that the -Ctarget-feature=+reserve-x18 flag stops emitting a warning.
Quoted 8 lines
This should be sufficient for adding support for
CONFIG_DYNAMIC_SCS.You can find it here:
https://github.com/rust-lang/rust/pull/124323As for non-dynamic SCS, I plan to tackle that after the PR is merged. See the "Future possibilities" section in the linked PR for more info on that.
Quoted 6 lines
Thanks for persevering with this, Alice. I read the pull request above, but it looks like you went with:
https://github.com/rust-lang/rust/pull/124655
instead, which was merged (hurrah!). Do we need anything else?
Yeah, it took a while, but I've managed to get a -Zfixed-x18 flag in. It will be available starting with Rust 1.80, which will be released on the 25th of July.
A few things:
1. The -Zsanitizer=shadow-call-stack flag still doesn't work because the compiler thinks that the target doesn't support it. I'll fix this eventually, but at least CONFIG_DYNAMIC_SCS works now.
2. I haven't convinced the Rust maintainers that -Zfixed-x18 is the way to go long term (flags starting with -Z are unstable and may change). Some of the maintainers want to instead add a x18-available target feature (that is, the inverse of the current reserve-x18 target feature), that you can disable with -Ctarget-feature=-x18-available.
And a few questions for you:
By the time support for 1.80 goes in, we are probably supporting more than one Rust compiler. For pre-1.80 compilers, should we fall back to -Ctarget-feature=+reserve-x18 (which emits a warning, but works), or fail compilation?
Similarly, we should probably submit a fix to the stable branches so that SCS+Rust doesn't silently break in a hard-to-debug way. Do you prefer a backport with -Ctarget-feature=+reserve-x18 or one that fails compilation?
Alice
Will Deacon
maintainerHi Alice,
Quoted 33 lines · Alice Ryhl
Quoted 32 lines · Will Deacon
Quoted 19 lines · Alice Ryhl
Quoted 7 lines · Will Deacon
Quoted 5 lines · Miguel Ojeda
Otherwise partially reverting to the
target.jsonapproach sounds good too.I added the
-Zuse-sync-unwind=nto the list at
https://github.com/Rust-for-Linux/linux/issues/2. Given the default is what we want, I have put it in the "Good to have" section.Quoted 6 lines
I think we have time to do this properly, like we did for the clang enablement a few years ago. In hindsight, avoiding hacks for the early toolchains back then was a really good idea because it meant we could rely on a solid baseline set of compiler features from the start.
So, please can we fix this in rustc and just have SCS dependent on that?
Just to keep you in the loop, I've posted a PR to make rustc recognize the reserve-x18 target feature, so that the -Ctarget-feature=+reserve-x18 flag stops emitting a warning.
Quoted 8 lines
This should be sufficient for adding support for
CONFIG_DYNAMIC_SCS.You can find it here:
https://github.com/rust-lang/rust/pull/124323As for non-dynamic SCS, I plan to tackle that after the PR is merged. See the "Future possibilities" section in the linked PR for more info on that.
Thanks for persevering with this, Alice. I read the pull request above, but it looks like you went with:
Quoted 7 lines
Quoted 3 lines
https://github.com/rust-lang/rust/pull/124655
instead, which was merged (hurrah!). Do we need anything else?
Yeah, it took a while, but I've managed to get a -Zfixed-x18 flag in. It will be available starting with Rust 1.80, which will be released on the 25th of July.
Great, thank you!
Quoted 12 lines
A few things:
1. The -Zsanitizer=shadow-call-stack flag still doesn't work because the compiler thinks that the target doesn't support it. I'll fix this eventually, but at least
CONFIG_DYNAMIC_SCSworks now.2. I haven't convinced the Rust maintainers that -Zfixed-x18 is the way to go long term (flags starting with -Z are unstable and may change). Some of the maintainers want to instead add a x18-available target feature (that is, the inverse of the current reserve-x18 target feature), that you can disable with -Ctarget-feature=-x18-available.
Quoted 6 lines
And a few questions for you:
By the time support for 1.80 goes in, we are probably supporting more than one Rust compiler. For pre-1.80 compilers, should we fall back to -Ctarget-feature=+reserve-x18 (which emits a warning, but works), or fail compilation?
I think we should just prevent the Kconfig option from being enabled if the toolchain doesn't give us what we need. So there's no need to fail compilation, but SCS =n. See how e.g. CONFIG_ARM64_BTI_KERNEL depends on CONFIG_CC_HAS_BRANCH_PROT_PAC_RET_BTI.
Quoted 4 lines
Similarly, we should probably submit a fix to the stable branches so that SCS+Rust doesn't silently break in a hard-to-debug way. Do you prefer a backport with -Ctarget-feature=+reserve-x18 or one that fails compilation?
As above, I think we should disable the feature in those cases.
Make sense?
Will