rust-lang / backtrace-rs

Backtraces in Rust
https://docs.rs/backtrace
Other
524 stars 240 forks source link

Adjust frame IP in SGX relative to image base #566

Closed mzohreva closed 10 months ago

mzohreva commented 11 months ago

The backtraces printed by panic! are broken in SGX, this PR is a prerequisite for fixing the issue in the standard library.

The SGX-only API added in this PR (backtrace::set_image_base) enables the standard library to set the correct image base address before calling trace_unsynchronized here: https://github.com/rust-lang/rust/blob/1.72.1/library/std/src/sys_common/backtrace.rs#L65.

cc @jethrogb

workingjubilee commented 10 months ago

Is there no way to automatically recover the base image address in this scenario?

mzohreva commented 10 months ago

Is there no way to automatically recover the base image address in this scenario?

No, due to ASLR the base address changes every time the enclave is loaded into memory.

workingjubilee commented 10 months ago

Then how does std know? Apologies if this is an obvious/silly question, I am not familiar with the particulars of the SGX target, as opposed to my familiarity with x86.

mzohreva commented 10 months ago

Then how does std know? Apologies if this is an obvious/silly question, I am not familiar with the particulars of the SGX target, as opposed to my familiarity with x86.

The relevant code is here: https://github.com/rust-lang/rust/blob/1.72.1/library/std/src/sys/sgx/abi/mem.rs#L37 it's using inline assembly (that's why I don't think it's a good idea to duplicate that code in this crate or elsewhere)

workingjubilee commented 10 months ago

I see.

// Do not remove inline: will result in relocation failure

That... seems like a compiler bug? Is there a bug tracking this concern?

Can you mark the exposed APIs #[doc(hidden)]? I want to retain plausible deniability about whether this is a sanctioned usage of this crate, here.

mzohreva commented 10 months ago

I see.

// Do not remove inline: will result in relocation failure

That... seems like a compiler bug? Is there a bug tracking this concern?

I'm not aware. @jethrogb might know.

Can you mark the exposed APIs #[doc(hidden)]? I want to retain plausible deniability about whether this is a sanctioned usage of this crate, here.

Sure, I'll update the PR

workingjubilee commented 10 months ago

With that, happy to merge this once the question about the relocation thing is settled. Maybe it's a bug but unfixable? Either way, I'd like to know so I'm sure there's no loose ends, or er... more like the loose ends are properly loosened? I don't know.

jethrogb commented 10 months ago

SGX doesn't have a dynamic loader, so the Rust entry code needs to do the relocations manually. Any code that's used to compute the relocations (which includes fn image_base()) shouldn't itself contain relocations.

workingjubilee commented 10 months ago

Oh, I see!

That's kinda wild.

mzohreva commented 10 months ago

@workingjubilee the CI failures seem unrelated to the changes, is there a separate effort to address those? How should we proceed with this PR?

github-actions[bot] commented 10 months ago

Code size changes for a hello-world Rust program linked with libstd with backtrace:

On platform ubuntu-latest: