rust: support for shadow call stack sanitizer
Alice Ryhl
reviewerPATCH v6 1/1 rust: support for shadow call stack sanitizer
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates Kconfig to allow only configurations that work.
The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in Kconfig.
When CONFIG_DYNAMIC_SCS is enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.
In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in Kconfig.
It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on select HAVE_RUST to avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes to CONFIG_RUST to result in any mitigations being changed or turned off.
At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
---
---
Changes in v6:
- Move Kconfig requirements into arch/*/Kconfig.
- List non-dynamic SCS as supported on 1.82. This reflects newly added
things in rustc.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Rebase series on v6.11-rc2.
- The first patch is no longer included as it was merged in v6.11-rc2.
- The commit message is rewritten from scratch.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Move depends on to CONFIG_RUST.
- Rewrite commit messages to include more context.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Use -Zfixed-x18.
- Add logic to reject unsupported rustc versions.
- Also include a fix to be backported.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/
Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/
--- Makefile | 1 + arch/arm64/Kconfig | 7 ++++++- arch/arm64/Makefile | 3 +++ arch/riscv/Kconfig | 7 ++++++- init/Kconfig | 1 - 5 files changed, 16 insertions(+), 3 deletions(-)
Makefile +1 -0
@@ -927,6 +927,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/Kconfig +6 -1
@@ -231,7 +231,7 @@ config ARM64 select HAVE_FUNCTION_ARG_ACCESS_API select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN + select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES
@@ -265,6 +265,11 @@ config ARM64 help ARM 64-bit (AArch64) Linux support. +config RUSTC_SUPPORTS_ARM64 + def_bool y + depends on CPU_LITTLE_ENDIAN + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS + config CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1507
arch/arm64/Makefile +3 -0
@@ -57,9 +57,11 @@ 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 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
@@ -114,6 +116,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Zfixed-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)
arch/riscv/Kconfig +6 -1
@@ -172,7 +172,7 @@ config RISCV select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RETHOOK if !XIP_KERNEL select HAVE_RSEQ - select HAVE_RUST if 64BIT + select HAVE_RUST if RUSTC_SUPPORTS_RISCV select HAVE_SAMPLE_FTRACE_DIRECT select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR
@@ -202,6 +202,11 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT +config RUSTC_SUPPORTS_RISCV + def_bool y + depends on 64BIT + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 + config CLANG_SUPPORTS_DYNAMIC_FTRACE def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1817
init/Kconfig +0 -1
@@ -1909,7 +1909,6 @@ config RUST depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT - depends on !SHADOW_CALL_STACK depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE help Enables Rust support in the kernel.
---
base-commit: 5953fd808fca8116a91678ee5fac00fc198ad93d
change-id: 20240304-shadow-call-stack-9c197a4361d9
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Quoted 29 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.
Quoted 5 lines
At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>
Thanks for continuing to chase this down.
Signature · 1 line
Kees Cook
Hi Alice,
Quoted 30 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.
Quoted 5 lines
At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>
Sami
Quoted 63 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>---
---
Changes in v6:
- MoveKconfigrequirements intoarch/*/Kconfig.
- List non-dynamic SCS as supported on 1.82. This reflects newly added
things in rustc.
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Rebase series on v6.11-rc2.
- The first patch is no longer included as it was merged in v6.11-rc2.
- The commit message is rewritten from scratch.
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- Movedepends ontoCONFIG_RUST.
- Rewrite commit messages to include more context.
- Link to v3: https://lore.kernel.org/r/[email protected]Changes in v3:
- Use -Zfixed-x18.
- Add logic to reject unsupported rustc versions.
- Also include a fix to be backported.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 7 lines
--- Makefile | 1 + arch/arm64/Kconfig | 7 ++++++- arch/arm64/Makefile | 3 +++ arch/riscv/Kconfig | 7 ++++++- init/Kconfig | 1 - 5 files changed, 16 insertions(+), 3 deletions(-)
For the arm64 parts:
In which tree do you plan to merge this?
Cheers,
Will
Quoted 6 lines · Will Deacon
For the arm64 parts:
Acked-by Will Deacon <[email protected]>In which tree do you plan to merge this?
If it goes in this cycle, and that is OK with you, then I think it would be easiest to put it into Rust, since it depends on RUSTC_VERSION which we will want to put there too since other things will be placed on top too.
Thanks!
Cheers,
Miguel
Will Deacon
maintainerQuoted 4 lines · Miguel Ojeda
Quoted 3 lines · Will Deacon
For the arm64 parts:
Quoted 8 lines
Quoted 3 lines
Acked-by Will Deacon <[email protected]>In which tree do you plan to merge this?
If it goes in this cycle, and that is OK with you, then I think it would be easiest to put it into Rust, since it depends on
RUSTC_VERSIONwhich we will want to put there too since other things will be placed on top too.
Fine by me! I just wanted to make sure you didn't need me to do anything else.
Will
Quoted 98 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>---
---
Changes in v6:
- MoveKconfigrequirements intoarch/*/Kconfig.
- List non-dynamic SCS as supported on 1.82. This reflects newly added
things in rustc.
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Rebase series on v6.11-rc2.
- The first patch is no longer included as it was merged in v6.11-rc2.
- The commit message is rewritten from scratch.
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- Movedepends ontoCONFIG_RUST.
- Rewrite commit messages to include more context.
- Link to v3: https://lore.kernel.org/r/[email protected]Changes in v3:
- Use -Zfixed-x18.
- Add logic to reject unsupported rustc versions.
- Also include a fix to be backported.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/--- Makefile | 1 + arch/arm64/Kconfig | 7 ++++++- arch/arm64/Makefile | 3 +++ arch/riscv/Kconfig | 7 ++++++- init/Kconfig | 1 - 5 files changed, 16 insertions(+), 3 deletions(-)Makefile +1
-0@@ -927,6 +927,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/Kconfig +1
-1@@ -231,7 +231,7 @@ config ARM64 select HAVE_FUNCTION_ARG_ACCESS_API select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN + select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES
arch/arm64/Kconfig config ARM64 · line 268
Quoted 4 lines
arch/arm64/Kconfig +1
-0@@ -265,6 +265,11 @@ config ARM64 help ARM 64-bit (AArch64) Linux support. +config RUSTC_SUPPORTS_ARM64
Nit: could we choose a better name here? ARCH_HAVE_RUST perhaps?
arch/arm64/Kconfig config ARM64 · line 272
Quoted 4 lines
arch/arm64/Kconfig +4
-0+ def_bool y + depends on CPU_LITTLE_ENDIAN + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS +
This is a bit opaque, so I'd prefer to have a comment here, explaining that rustc 1.82 supports emitting the instrumentation statically, but 1.80 is needed to get the X18 reservation, which the DWARF based patching logic relies on.
Quoted 41 lines
arch/arm64/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1507arch/arm64/Makefile +3
-0@@ -57,9 +57,11 @@ 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 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)@@ -114,6 +116,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Zfixed-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)arch/riscv/Kconfig +1
-1@@ -172,7 +172,7 @@ config RISCV select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RETHOOK if !XIP_KERNEL select HAVE_RSEQ - select HAVE_RUST if 64BIT + select HAVE_RUST if RUSTC_SUPPORTS_RISCV select HAVE_SAMPLE_FTRACE_DIRECT select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR
arch/riscv/Kconfig config RISCV · line 209
Quoted 8 lines
arch/riscv/Kconfig +5
-0@@ -202,6 +202,11 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT +config RUSTC_SUPPORTS_RISCV + def_bool y + depends on 64BIT + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 +
Same nit as above. Also, if this enables shadow call stack on RISC-V too, please mention it in the commit log more clearly, as it only mentions arm64 by name.
Quoted 17 lines
arch/riscv/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1817init/Kconfig +0
-2@@ -1909,7 +1909,6 @@ config RUST depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT - depends on !SHADOW_CALL_STACK depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE help Enables Rust support in the kernel. ---
Quoted 3 lines
base-commit:
5953fd808fca8116a91678ee5fac00fc198ad93d
change-id: 20240304-shadow-call-stack-9c197a4361d9
With the comments addressed (or refuted)
Thanks,
Alice Ryhl
reviewerQuoted 103 lines · Ard Biesheuvel
Quoted 101 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>---
---
Changes in v6:
- MoveKconfigrequirements intoarch/*/Kconfig.
- List non-dynamic SCS as supported on 1.82. This reflects newly added
things in rustc.
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Rebase series on v6.11-rc2.
- The first patch is no longer included as it was merged in v6.11-rc2.
- The commit message is rewritten from scratch.
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- Movedepends ontoCONFIG_RUST.
- Rewrite commit messages to include more context.
- Link to v3: https://lore.kernel.org/r/[email protected]Changes in v3:
- Use -Zfixed-x18.
- Add logic to reject unsupported rustc versions.
- Also include a fix to be backported.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/--- Makefile | 1 + arch/arm64/Kconfig | 7 ++++++- arch/arm64/Makefile | 3 +++ arch/riscv/Kconfig | 7 ++++++- init/Kconfig | 1 - 5 files changed, 16 insertions(+), 3 deletions(-)Makefile +1
-0@@ -927,6 +927,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/Kconfig +1
-1@@ -231,7 +231,7 @@ config ARM64 select HAVE_FUNCTION_ARG_ACCESS_API select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN + select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES@@ -265,6 +265,11 @@ config ARM64 help ARM 64-bit (AArch64) Linux support.
Quoted 3 lines
arch/arm64/Kconfig config ARM64 · line 268
Quoted 1 line
arch/arm64/Kconfig +1
-0+config RUSTC_SUPPORTS_ARM64Nit: could we choose a better name here?
ARCH_HAVE_RUSTperhaps?
This is the name suggested by Will Deacon in the previous version. I'm happy to change it if you prefer, but I'm wondering if that name is too close to HAVE_RUST? Perhaps ARCH_SUPPORTS_RUST? Ultimately I think that the current name is okay.
Quoted 9 lines
arch/arm64/Kconfig config ARM64 · line 272
Quoted 4 lines
arch/arm64/Kconfig +4
-0+ def_bool y + depends on CPU_LITTLE_ENDIAN + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS +This is a bit opaque, so I'd prefer to have a comment here, explaining that rustc 1.82 supports emitting the instrumentation statically, but 1.80 is needed to get the X18 reservation, which the DWARF based patching logic relies on.
Hmm. I tried a few different wordings and ended on this:
config RUSTC_SUPPORTS_ARM64
def_bool y
depends on CPU_LITTLE_ENDIAN
# Shadow call stack is only supported on some versions of rustc. #
# When using the UNWIND_PATCH_PAC_INTO_SCS option, rustc version 1.80+ is # required due to use of the -Zfixed-x18 flag.
#
# Otherwise, rustc version 1.82+ is required due to use of the # -Zsanitizer=shadow-call-stack flag.
depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 ||RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS
This wording avoids getting into the weeds of how SCS works. Do you prefer something that gets into more detail than this?
Quoted 44 lines
Quoted 44 lines
arch/arm64/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1507arch/arm64/Makefile +3
-0@@ -57,9 +57,11 @@ 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 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)@@ -114,6 +116,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Zfixed-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)arch/riscv/Kconfig +1
-1@@ -172,7 +172,7 @@ config RISCV select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RETHOOK if !XIP_KERNEL select HAVE_RSEQ - select HAVE_RUST if 64BIT + select HAVE_RUST if RUSTC_SUPPORTS_RISCV select HAVE_SAMPLE_FTRACE_DIRECT select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR@@ -202,6 +202,11 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BIT
Quoted 9 lines
arch/riscv/Kconfig config RISCV · line 209
Quoted 5 lines
arch/riscv/Kconfig +5
-0+config RUSTC_SUPPORTS_RISCV + def_bool y + depends on 64BIT + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 +Same nit as above. Also, if this enables shadow call stack on RISC-V too, please mention it in the commit log more clearly, as it only mentions arm64 by name.
Same question as above. I came up with:
config RUSTC_SUPPORTS_RISCV
def_bool y
depends on 64BIT
# Shadow call stack requires rustc version 1.82+ due to use of the # -Zsanitizer=shadow-call-stack flag.
depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200
Quoted 16 lines
Quoted 16 lines
arch/riscv/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1817init/Kconfig +0
-1@@ -1909,7 +1909,6 @@ config RUST depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT - depends on !SHADOW_CALL_STACK depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE help Enables Rust support in the kernel.
Quoted 8 lines
Quoted 4 lines
---
base-commit:5953fd808fca8116a91678ee5fac00fc198ad93d
change-id: 20240304-shadow-call-stack-9c197a4361d9With the comments addressed (or refuted)
Reviewed-by Ard Biesheuvel <[email protected]>
Thanks for the review!
Alice
Quoted 105 lines · Alice Ryhl
Quoted 103 lines · Ard Biesheuvel
Quoted 101 lines · Alice Ryhl
This patch adds all of the flags that are needed to support the shadow call stack (SCS) sanitizer with Rust, and updates
Kconfigto allow only configurations that work.The -Zfixed-x18 flag is required to use SCS on arm64, and requires rustc version 1.80.0 or greater. This restriction is reflected in
Kconfig.When
CONFIG_DYNAMIC_SCSis enabled, the build will be configured to include unwind tables in the build artifacts. Dynamic SCS uses the unwind tables at boot to find all places that need to be patched. The -Cforce-unwind-tables=y flag ensures that unwind tables are available for Rust code.In non-dynamic mode, the -Zsanitizer=shadow-call-stack flag is what enables the SCS sanitizer. Using this flag requires rustc version 1.82.0 or greater on the targets used by Rust in the kernel. This restriction is reflected in
Kconfig.It is possible to avoid the requirement of rustc 1.80.0 by using -Ctarget-feature=+reserve-x18 instead of -Zfixed-x18. However, this flag emits a warning during the build, so this patch does not add support for using it and instead requires 1.80.0 or greater.
The dependency is placed on
select HAVE_RUSTto avoid a situation where enabling Rust silently turns off the sanitizer. Instead, turning on the sanitizer results in Rust being disabled. We generally do not want changes toCONFIG_RUSTto result in any mitigations being changed or turned off.At the time of writing, rustc 1.82.0 only exists via the nightly release channel. There is a chance that the -Zsanitizer=shadow-call-stack flag will end up needing 1.83.0 instead, but I think it is small.
Signed-off-by Alice Ryhl <[email protected]>---
---
Changes in v6:
- MoveKconfigrequirements intoarch/*/Kconfig.
- List non-dynamic SCS as supported on 1.82. This reflects newly added
things in rustc.
- Link to v5: https://lore.kernel.org/r/[email protected]Changes in v5:
- Rebase series on v6.11-rc2.
- The first patch is no longer included as it was merged in v6.11-rc2.
- The commit message is rewritten from scratch.
- Link to v4: https://lore.kernel.org/r/[email protected]Changes in v4:
- Movedepends ontoCONFIG_RUST.
- Rewrite commit messages to include more context.
- Link to v3: https://lore.kernel.org/r/[email protected]Changes in v3:
- Use -Zfixed-x18.
- Add logic to reject unsupported rustc versions.
- Also include a fix to be backported.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/Changes in v2:
- Add -Cforce-unwind-tables flag.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/--- Makefile | 1 + arch/arm64/Kconfig | 7 ++++++- arch/arm64/Makefile | 3 +++ arch/riscv/Kconfig | 7 ++++++- init/Kconfig | 1 - 5 files changed, 16 insertions(+), 3 deletions(-)Makefile +1
-0@@ -927,6 +927,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/Kconfig +1
-1@@ -231,7 +231,7 @@ config ARM64 select HAVE_FUNCTION_ARG_ACCESS_API select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ - select HAVE_RUST if CPU_LITTLE_ENDIAN + select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES@@ -265,6 +265,11 @@ config ARM64 help ARM 64-bit (AArch64) Linux support.
Quoted 9 lines
Quoted 3 lines
arch/arm64/Kconfig config ARM64 · line 268
Quoted 1 line
arch/arm64/Kconfig +1
-0+config RUSTC_SUPPORTS_ARM64Nit: could we choose a better name here?
ARCH_HAVE_RUSTperhaps?This is the name suggested by Will Deacon in the previous version. I'm happy to change it if you prefer, but I'm wondering if that name is too close to
HAVE_RUST? PerhapsARCH_SUPPORTS_RUST? Ultimately I think that the current name is okay.
Fair enough. Let's leave it as-is.
Quoted 25 lines
Quoted 9 lines
arch/arm64/Kconfig config ARM64 · line 272
Quoted 4 lines
arch/arm64/Kconfig +4
-0+ def_bool y + depends on CPU_LITTLE_ENDIAN + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 || RUSTC_VERSION >= 108000 && UNWIND_PATCH_PAC_INTO_SCS +This is a bit opaque, so I'd prefer to have a comment here, explaining that rustc 1.82 supports emitting the instrumentation statically, but 1.80 is needed to get the X18 reservation, which the DWARF based patching logic relies on.
Hmm. I tried a few different wordings and ended on this:
config
RUSTC_SUPPORTS_ARM64
def_booly
depends onCPU_LITTLE_ENDIAN
# Shadow call stack is only supported on some versions of rustc. #
# When using theUNWIND_PATCH_PAC_INTO_SCSoption, rustc version 1.80+ is # required due to use of the -Zfixed-x18 flag.
#
# Otherwise, rustc version 1.82+ is required due to use of the # -Zsanitizer=shadow-call-stack flag.
depends on !SHADOW_CALL_STACK ||RUSTC_VERSION>= 108200 ||RUSTC_VERSION>= 108000 &&UNWIND_PATCH_PAC_INTO_SCS
Quoted 3 lines
This wording avoids getting into the weeds of how SCS works. Do you prefer something that gets into more detail than this?
This looks fine to me.
Quoted 54 lines
Quoted 44 lines
Quoted 44 lines
arch/arm64/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE_WITH_ARGS def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1507arch/arm64/Makefile +3
-0@@ -57,9 +57,11 @@ 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 +KBUILD_RUSTFLAGS += -Cforce-unwind-tables=y -Zuse-sync-unwind=n endif ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)@@ -114,6 +116,7 @@ endif ifeq ($(CONFIG_SHADOW_CALL_STACK), y) KBUILD_CFLAGS += -ffixed-x18 +KBUILD_RUSTFLAGS += -Zfixed-x18 endif ifeq ($(CONFIG_CPU_BIG_ENDIAN), y)arch/riscv/Kconfig +1
-1@@ -172,7 +172,7 @@ config RISCV select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RETHOOK if !XIP_KERNEL select HAVE_RSEQ - select HAVE_RUST if 64BIT + select HAVE_RUST if RUSTC_SUPPORTS_RISCV select HAVE_SAMPLE_FTRACE_DIRECT select HAVE_SAMPLE_FTRACE_DIRECT_MULTI select HAVE_STACKPROTECTOR@@ -202,6 +202,11 @@ config RISCV select UACCESS_MEMCPY if !MMU select ZONE_DMA32 if 64BITQuoted 9 lines
arch/riscv/Kconfig config RISCV · line 209
Quoted 5 lines
arch/riscv/Kconfig +5
-0+config RUSTC_SUPPORTS_RISCV + def_bool y + depends on 64BIT + depends on !SHADOW_CALL_STACK || RUSTC_VERSION >= 108200 +Same nit as above. Also, if this enables shadow call stack on RISC-V too, please mention it in the commit log more clearly, as it only mentions arm64 by name.
Quoted 9 lines
Same question as above. I came up with:
config
RUSTC_SUPPORTS_RISCV
def_booly
depends on 64BIT
# Shadow call stack requires rustc version 1.82+ due to use of the # -Zsanitizer=shadow-call-stack flag.
depends on !SHADOW_CALL_STACK ||RUSTC_VERSION>= 108200
Fine too.
Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
arch/riscv/Kconfig +0
-0config CLANG_SUPPORTS_DYNAMIC_FTRACE def_bool CC_IS_CLANG # https://github.com/ClangBuiltLinux/linux/issues/1817init/Kconfig +0
-1@@ -1909,7 +1909,6 @@ config RUST depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT - depends on !SHADOW_CALL_STACK depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE help Enables Rust support in the kernel.Quoted 8 lines
Quoted 4 lines
---
base-commit:5953fd808fca8116a91678ee5fac00fc198ad93d
change-id: 20240304-shadow-call-stack-9c197a4361d9With the comments addressed (or refuted)
Reviewed-by Ard Biesheuvel <[email protected]>Thanks for the review!
Alice