Skip to content

Commit aa15961

Browse files
committed
rewrite x86_64-fortanix-unknown-sgx-lvi to rmake
1 parent 80eb5a8 commit aa15961

File tree

6 files changed

+112
-2
lines changed

6 files changed

+112
-2
lines changed

src/tools/run-make-support/src/env.rs

+8
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ pub fn env_var_os(name: &str) -> OsString {
2424
pub fn no_debug_assertions() -> bool {
2525
std::env::var_os("NO_DEBUG_ASSERTIONS").is_some()
2626
}
27+
28+
#[track_caller]
29+
/// A wrapper around [`std::env::set_current_dir`] which includes the directory
30+
/// path in the panic message.
31+
pub fn set_current_dir<P: AsRef<std::path::Path>>(dir: P) {
32+
std::env::set_current_dir(dir.as_ref())
33+
.expect(&format!("the directory in path \"{}\" could not be read", dir.as_ref().display()));
34+
}

src/tools/run-make-support/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
6262
pub use diff::{diff, Diff};
6363

6464
/// Panic-on-fail [`std::env::var`] and [`std::env::var_os`] wrappers.
65-
pub use env::{env_var, env_var_os};
65+
pub use env::{env_var, env_var_os, set_current_dir};
6666

6767
/// Convenience helpers for running binaries and other commands.
6868
pub use run::{cmd, run, run_fail, run_with_args};

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@ run-make/split-debuginfo/Makefile
2323
run-make/symbol-mangling-hashed/Makefile
2424
run-make/sysroot-crates-are-unstable/Makefile
2525
run-make/translation/Makefile
26-
run-make/x86_64-fortanix-unknown-sgx-lvi/Makefile

tests/run-make/x86_64-fortanix-unknown-sgx-lvi/Makefile renamed to tests/run-make/x86_64-fortanix-unknown-sgx-lvi/_Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# FIXME(Oneirical): Disabled for now. Remove this if the rmake.rs replacement
2+
# is shown to work, or restore it if the rmake.rs replacement does not work.
3+
14
include ../tools.mk
25

36
#only-x86_64-fortanix-unknown-sgx

tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh renamed to tests/run-make/x86_64-fortanix-unknown-sgx-lvi/_script.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
2+
3+
# FIXME(Oneirical): Disabled for now. Remove this if the rmake.rs replacement
4+
# is shown to work, or restore it if the rmake.rs replacement does not work.
5+
26
set -exuo pipefail
37

48
function build {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 shows
6+
// lack of vulnerability to a Load Value Injection attack.
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+
check("unw_getcontext", "unw_getcontext.checks");
33+
check("__libunwind_Registers_x86_64_jumpto", "jumpto.checks");
34+
35+
check("std::io::stdio::_print::[[:alnum:]]+", "print.with_frame_pointers.checks");
36+
37+
check("st_plus_one_global_asm", "rust_plus_one_global_asm.checks");
38+
39+
check("_plus_one_c", "cc_plus_one_c.checks");
40+
check("_plus_one_c_asm", "cc_plus_one_c_asm.checks");
41+
check("_plus_one_cxx", "cc_plus_one_cxx.checks");
42+
check("_plus_one_cxx_asm", "cc_plus_one_cxx_asm.checks");
43+
check("_plus_one_asm", "cc_plus_one_asm.checks");
44+
45+
check("cmake_plus_one_c", "cmake_plus_one_c.checks");
46+
check("cmake_plus_one_c_asm", "cmake_plus_one_c_asm.checks");
47+
check("cmake_plus_one_c_global_asm", "cmake_plus_one_c_global_asm.checks");
48+
check("cmake_plus_one_cxx", "cmake_plus_one_cxx.checks");
49+
check("cmake_plus_one_cxx_asm", "cmake_plus_one_cxx_asm.checks");
50+
check("cmake_plus_one_cxx_global_asm", "cmake_plus_one_cxx_global_asm.checks");
51+
check("cmake_plus_one_asm", "cmake_plus_one_asm.checks");
52+
}
53+
54+
fn check(func_re: &str, checks: &str) {
55+
let dump = llvm_objdump()
56+
.input("enclave/target/x86_64-fortanix-unknown-sgx/debug/enclave")
57+
.args(&["--syms", "--demangle"])
58+
.run()
59+
.stdout_utf8();
60+
let re = regex::Regex::new(&format!("[[:blank:]]+{func_re}")).unwrap();
61+
let func = re.find_iter(&dump).map(|m| m.as_str().trim()).collect::<Vec<&str>>().join(",");
62+
let dump = llvm_objdump()
63+
.input("enclave/target/x86_64-fortanix-unknown-sgx/debug/enclave")
64+
.args(&["--syms", &format!("--disassemble-symbols={func}")])
65+
.run()
66+
.stdout_utf8();
67+
let dump = dump.as_bytes();
68+
69+
// Unique case, must succeed at one of two possible tests.
70+
if func_re == "std::io::stdio::_print::[[:alnum:]]+" {
71+
let output = llvm_filecheck().stdin(&dump).patterns(checks).run_unchecked();
72+
if !output.status().success() {
73+
llvm_filecheck().stdin(&dump).patterns("print.without_frame_pointers.checks").run();
74+
llvm_filecheck()
75+
.args(&["--implicit-check-not", "ret"])
76+
.stdin(dump)
77+
.patterns("print.without_frame_pointers.checks")
78+
.run();
79+
} else {
80+
llvm_filecheck()
81+
.args(&["--implicit-check-not", "ret"])
82+
.stdin(dump)
83+
.patterns(checks)
84+
.run();
85+
}
86+
return;
87+
}
88+
llvm_filecheck().stdin(&dump).patterns(checks).run();
89+
if !["rust_plus_one_global_asm", "cmake_plus_one_c_global_asm", "cmake_plus_one_cxx_global_asm"]
90+
.contains(&func_re)
91+
{
92+
// The assembler cannot avoid explicit `ret` instructions. Sequences
93+
// of `shlq $0x0, (%rsp); lfence; retq` are used instead.
94+
llvm_filecheck().args(&["--implicit-check-not", "ret"]).stdin(dump).patterns(checks).run();
95+
}
96+
}

0 commit comments

Comments
 (0)