google / android-riscv64

Issues and discussions around RISC-V support in AOSP.
Apache License 2.0
226 stars 15 forks source link

kernel: software shadow call stack #55

Closed samitolvanen closed 10 months ago

samitolvanen commented 1 year ago

Linux doesn't have SCS support for RISC-V (CONFIG_SHADOW_CALL_STACK). Similarly to #52, we should support software SCS in the kernel until #14 is available and implemented.

enh-google commented 1 year ago

https://android-review.googlesource.com/q/topic:%22rv64-scs%22 seems to work.

still need to look at making setjmp() not leak x18, but i think that's it?

samitolvanen commented 1 year ago

Yes, it looks like that would do it for userspace. For the kernel, we'll first have to make sure the existing RISC-V assembly code doesn't use x18, and if it's not already done, we'll need to add code to save/restore x18 during context switch, CPU suspend etc. We probably also need a separate shadow call stack to match the shadow_stack the kernel switches to when a stack overflow occurs.

enh-google commented 1 year ago

i've updated setjmp()/longjmp() too (also on https://android-review.googlesource.com/q/topic:%22rv64-scs%22 ), but i'll wait until adelva's cuttlefish work is in CI before i submit this potentially disruptive topic...

samitolvanen commented 1 year ago

SCS kernel patches v1: https://lore.kernel.org/linux-riscv/20230811233556.97161-7-samitolvanen@google.com/

samitolvanen commented 1 year ago

CONFIG_SHADOW_CALL_STACK support was merged to v6.7: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=24005d184aaa80984e0511c4ec6e6a0860fdddb8

The feature still needs to be enabled in android-mainline once -rc1 is merged there, and possibly backported to older kernels depending on which kernel versions RISC-V GKI will support.

samitolvanen commented 10 months ago

Enabled in android-mainline: https://r.android.com/2912646