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