bytecodealliance / wasmtime

A fast and secure runtime for WebAssembly
https://wasmtime.dev/
Apache License 2.0
15.47k stars 1.31k forks source link

Wasmtime is not using the recommended approach to set the RA state register on Linux AArch64 with PAC enabled #9290

Open matlo607 opened 2 months ago

matlo607 commented 2 months ago

Description

DWARF for the Arm® 64-bit Architecture (AArch64) specifies that the RA state should be set with DW_CFA_AARCH64_negate_ra_state.

The same result can be obtained using DW_CFA_val_expression.

// RA_SIGN_STATE corresponds to the DWARF register number 34.
DW_CFA_val_expression 34, 1, DW_OP_lit1

This behavior was added to libgcc to support unwinding when the RA state is set with DW_CFA_val_expression (Bug 104689).

Current implementation in Wasmtime: https://github.com/bytecodealliance/wasmtime/blob/37ed724c9da2ee5a516923cf221ade19b78f63eb/cranelift/codegen/src/isa/unwind/systemv.rs#L115-L128

Issue

Armv9.5-A introduced a new extension to PAC for return address signing: PAuth_LR. It adds a new diversifier PC, aside to SP. The value of PC used by PAC2 algorithm corresponds to its value at the time of signing, i.e. the address of the paci<k>sppc instruction, or paci<k>sp if NOP-space instructions are used.

A new DWARF directive DW_CFA_AARCH64_negate_ra_state_with_pc was introduced to record the position of signing and authentication, and to express that PACv2 algorithm was used (vs PACv1 relying on DW_CFA_AARCH64_negate_ra_state). During unwinding, this directive saves the PC diversifier into the frame state, to later use it to authenticate the return address.

In my understanding, the approach based on DW_CFA_val_expression is not working anymore in this context, as the libgcc unwinder cannot know the difference between a DW_CFA_val_expression used for PAC, and another one used for something else, and so cannot save PC at the time of signing.

  1. Is it an issue for your use case ? Do you plan to add support for PAuth_LR ?
  2. Would it be possible to change the CFI directive to the recommended one ?

Links

alexcrichton commented 2 months ago

Thanks for the report! AFAIK we don't have strict needs around this related to Wasmtime or Cranelift per se, and so long as things work and pass tests changing should be fine. I believe we unfortunately don't have automated testing for this in that our QEMU emulation in CI I don't think has this enabled, but it might not be too too hard to set up.