|
| 1 | +// ignore-tidy-linelength |
| 2 | +// Reason: intel.com link |
| 3 | + |
| 4 | +// This security test checks that the disassembled form of certain symbols |
| 5 | +// is "hardened" - that means, the assembly instructions match a pattern that |
| 6 | +// mitigate potential Load Value Injection vulnerabilities. |
| 7 | +// To do so, a test crate is compiled, and certain symbols are found, disassembled |
| 8 | +// and checked one by one. |
| 9 | +// See https://github.com/rust-lang/rust/pull/77008 |
| 10 | + |
| 11 | +// On load value injection: |
| 12 | +// https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/technical-documentation/load-value-injection.html |
| 13 | + |
| 14 | +//@ only-x86_64-fortanix-unknown-sgx |
| 15 | + |
| 16 | +use run_make_support::{cmd, cwd, llvm_filecheck, llvm_objdump, regex, set_current_dir, target}; |
| 17 | + |
| 18 | +fn main() { |
| 19 | + let main_dir = cwd(); |
| 20 | + set_current_dir("enclave"); |
| 21 | + // HACK(eddyb) sets `RUSTC_BOOTSTRAP=1` so Cargo can accept nightly features. |
| 22 | + // These come from the top-level Rust workspace, that this crate is not a |
| 23 | + // member of, but Cargo tries to load the workspace `Cargo.toml` anyway. |
| 24 | + cmd("cargo") |
| 25 | + .env("RUSTC_BOOTSTRAP", "1") |
| 26 | + .arg("-v") |
| 27 | + .arg("run") |
| 28 | + .arg("--target") |
| 29 | + .arg(target()) |
| 30 | + .run(); |
| 31 | + set_current_dir(&main_dir); |
| 32 | + // Rust has various ways of adding code to a binary: |
| 33 | + // - Rust code |
| 34 | + // - Inline assembly |
| 35 | + // - Global assembly |
| 36 | + // - C/C++ code compiled as part of Rust crates |
| 37 | + // For those different kinds, we do have very small code examples that should be |
| 38 | + // mitigated in some way. Mostly we check that ret instructions should no longer be present. |
| 39 | + check("unw_getcontext", "unw_getcontext.checks"); |
| 40 | + check("__libunwind_Registers_x86_64_jumpto", "jumpto.checks"); |
| 41 | + |
| 42 | + check("std::io::stdio::_print::[[:alnum:]]+", "print.with_frame_pointers.checks"); |
| 43 | + |
| 44 | + check("st_plus_one_global_asm", "rust_plus_one_global_asm.checks"); |
| 45 | + |
| 46 | + check("_plus_one_c", "cc_plus_one_c.checks"); |
| 47 | + check("_plus_one_c_asm", "cc_plus_one_c_asm.checks"); |
| 48 | + check("_plus_one_cxx", "cc_plus_one_cxx.checks"); |
| 49 | + check("_plus_one_cxx_asm", "cc_plus_one_cxx_asm.checks"); |
| 50 | + check("_plus_one_asm", "cc_plus_one_asm.checks"); |
| 51 | + |
| 52 | + check("cmake_plus_one_c", "cmake_plus_one_c.checks"); |
| 53 | + check("cmake_plus_one_c_asm", "cmake_plus_one_c_asm.checks"); |
| 54 | + check("cmake_plus_one_c_global_asm", "cmake_plus_one_c_global_asm.checks"); |
| 55 | + check("cmake_plus_one_cxx", "cmake_plus_one_cxx.checks"); |
| 56 | + check("cmake_plus_one_cxx_asm", "cmake_plus_one_cxx_asm.checks"); |
| 57 | + check("cmake_plus_one_cxx_global_asm", "cmake_plus_one_cxx_global_asm.checks"); |
| 58 | + check("cmake_plus_one_asm", "cmake_plus_one_asm.checks"); |
| 59 | +} |
| 60 | + |
| 61 | +fn check(func_re: &str, mut checks: &str) { |
| 62 | + let dump = llvm_objdump() |
| 63 | + .input("enclave/target/x86_64-fortanix-unknown-sgx/debug/enclave") |
| 64 | + .args(&["--syms", "--demangle"]) |
| 65 | + .run() |
| 66 | + .stdout_utf8(); |
| 67 | + let re = regex::Regex::new(&format!("[[:blank:]]+{func_re}")).unwrap(); |
| 68 | + let func = re.find_iter(&dump).map(|m| m.as_str().trim()).collect::<Vec<&str>>().join(","); |
| 69 | + let dump = llvm_objdump() |
| 70 | + .input("enclave/target/x86_64-fortanix-unknown-sgx/debug/enclave") |
| 71 | + .args(&["--syms", &format!("--disassemble-symbols={func}")]) |
| 72 | + .run() |
| 73 | + .stdout_utf8(); |
| 74 | + let dump = dump.as_bytes(); |
| 75 | + |
| 76 | + // Unique case, must succeed at one of two possible tests. |
| 77 | + // This is because frame pointers are optional, and them being enabled requires |
| 78 | + // an additional `popq` in the pattern checking file. |
| 79 | + if func_re == "std::io::stdio::_print::[[:alnum:]]+" { |
| 80 | + let output = llvm_filecheck().stdin(&dump).patterns(checks).run_unchecked(); |
| 81 | + if !output.status().success() { |
| 82 | + checks = "print.without_frame_pointers.checks"; |
| 83 | + llvm_filecheck().stdin(&dump).patterns(checks).run(); |
| 84 | + } |
| 85 | + } else { |
| 86 | + llvm_filecheck().stdin(&dump).patterns(checks).run(); |
| 87 | + } |
| 88 | + if !["rust_plus_one_global_asm", "cmake_plus_one_c_global_asm", "cmake_plus_one_cxx_global_asm"] |
| 89 | + .contains(&func_re) |
| 90 | + { |
| 91 | + // The assembler cannot avoid explicit `ret` instructions. Sequences |
| 92 | + // of `shlq $0x0, (%rsp); lfence; retq` are used instead. |
| 93 | + llvm_filecheck().args(&["--implicit-check-not", "ret"]).stdin(dump).patterns(checks).run(); |
| 94 | + } |
| 95 | +} |
0 commit comments