Skip to content

Commit d8a7904

Browse files
committed
LVI hardening tests for cmake
1 parent 64811ed commit d8a7904

File tree

11 files changed

+120
-0
lines changed

11 files changed

+120
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CHECK: cmake_plus_one_c
2+
CHECK: lfence
3+
CHECK: popq
4+
CHECK-NEXT: popq [[REGISTER:%[a-z]+]]
5+
CHECK-NEXT: lfence
6+
CHECK-NEXT: jmpq *[[REGISTER]]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CHECK: cmake_plus_one_c_asm
2+
CHECK: lfence
3+
CHECK: lfence
4+
CHECK: lfence
5+
CHECK: lfence
6+
CHECK: movl
7+
CHECK: lfence
8+
CHECK-NEXT: incl
9+
CHECK-NEXT: jmp 0x{{[[:xdigit:]]+}} <cmake_plus_one_c_asm+0x{{[[:xdigit:]]+}}>
10+
CHECK-NEXT: shlq $0, (%rsp)
11+
CHECK-NEXT: lfence
12+
CHECK-NEXT: retq
13+
CHECK: popq
14+
CHECK-NEXT: popq [[REGISTER:%[a-z]+]]
15+
CHECK-NEXT: lfence
16+
CHECK-NEXT: jmpq *[[REGISTER]]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CHECK: cmake_plus_one_cxx
2+
CHECK: lfence
3+
CHECK: popq
4+
CHECK-NEXT: popq [[REGISTER:%[a-z]+]]
5+
CHECK-NEXT: lfence
6+
CHECK-NEXT: jmpq *[[REGISTER]]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CHECK: cmake_plus_one_cxx_asm
2+
CHECK: lfence
3+
CHECK: lfence
4+
CHECK: lfence
5+
CHECK: lfence
6+
CHECK: movl
7+
CHECK: lfence
8+
CHECK-NEXT: incl
9+
CHECK-NEXT: jmp 0x{{[[:xdigit:]]+}} <cmake_plus_one_cxx_asm+0x{{[[:xdigit:]]+}}>
10+
CHECK-NEXT: shlq $0, (%rsp)
11+
CHECK-NEXT: lfence
12+
CHECK-NEXT: retq
13+
CHECK: popq
14+
CHECK-NEXT: popq [[REGISTER:%[a-z]+]]
15+
CHECK-NEXT: lfence
16+
CHECK-NEXT: jmpq *[[REGISTER]]

src/test/run-make/x86_64-fortanix-unknown-sgx-lvi/enclave/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ edition = "2018"
1010

1111
[build-dependencies]
1212
cc = "1.0"
13+
cmake = "0.1"

src/test/run-make/x86_64-fortanix-unknown-sgx-lvi/enclave/build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,19 @@ fn main() {
88
.cpp_set_stdlib(None)
99
.file("foo_cxx.cpp")
1010
.compile("foo_cxx");
11+
12+
// When the cmake crate detects the clang compiler, it passes the
13+
// "--target" argument to the linker which subsequently fails. The
14+
// `CMAKE_C_COMPILER_FORCED` option makes sure that `cmake` does not
15+
// tries to test the compiler. From version 3.6 the option
16+
// `CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY` can be used
17+
// https://cmake.org/cmake/help/v3.5/module/CMakeForceCompiler.html
18+
let dst = cmake::Config::new("libcmake_foo")
19+
.build_target("cmake_foo")
20+
.define("CMAKE_C_COMPILER_FORCED", "1")
21+
.define("CMAKE_CXX_COMPILER_FORCED", "1")
22+
.define("CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY", "1")
23+
.build();
24+
println!("cargo:rustc-link-search=native={}/build/", dst.display());
25+
println!("cargo:rustc-link-lib=static=cmake_foo");
1126
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_library(cmake_foo STATIC
2+
src/foo.c
3+
src/foo_cxx.cpp
4+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
int cmake_plus_one_c(int *arg) {
2+
return *arg + 1;
3+
}
4+
5+
int cmake_plus_one_c_asm(int *arg) {
6+
int value = 0;
7+
8+
asm volatile ( " movl (%1), %0\n"
9+
" inc %0\n"
10+
" jmp 1f\n"
11+
" retq\n" // never executed, but a shortcut to determine how the assembler deals with `ret` instructions
12+
"1:\n"
13+
: "=r"(value)
14+
: "r"(arg) );
15+
16+
return value;
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extern "C" int cmake_plus_one_cxx(int *arg);
2+
extern "C" int cmake_plus_one_cxx_asm(int *arg);
3+
4+
int cmake_plus_one_cxx(int *arg) {
5+
return *arg + 1;
6+
}
7+
8+
int cmake_plus_one_cxx_asm(int *arg) {
9+
int value = 0;
10+
11+
asm volatile ( " movl (%1), %0\n"
12+
" inc %0\n"
13+
" jmp 1f\n"
14+
" retq\n" // never executed, but a shortcut to determine how the assembler deals with `ret` instructions
15+
"1:\n"
16+
: "=r"(value)
17+
: "r"(arg) );
18+
19+
return value;
20+
}

src/test/run-make/x86_64-fortanix-unknown-sgx-lvi/enclave/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ extern {
33
fn cc_plus_one_c_asm(arg : &u32) -> u32;
44
fn cc_plus_one_cxx(arg : &u32) -> u32;
55
fn cc_plus_one_cxx_asm(arg : &u32) -> u32;
6+
fn cmake_plus_one_c(arg : &u32) -> u32;
7+
fn cmake_plus_one_c_asm(arg : &u32) -> u32;
8+
fn cmake_plus_one_cxx(arg : &u32) -> u32;
9+
fn cmake_plus_one_cxx_asm(arg : &u32) -> u32;
610
}
711

812
fn main() {
@@ -13,5 +17,10 @@ fn main() {
1317
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cc_plus_one_c_asm(&value));
1418
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cc_plus_one_cxx(&value));
1519
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cc_plus_one_cxx_asm(&value));
20+
21+
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cmake_plus_one_c(&value));
22+
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cmake_plus_one_c_asm(&value));
23+
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cmake_plus_one_cxx(&value));
24+
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cmake_plus_one_cxx_asm(&value));
1625
}
1726
}

src/test/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ build
3939
#check "libunwind::Registers_x86_64::jumpto()" jumpto.checks
4040

4141
check "std::io::stdio::_print::h87f0c238421c45bc" print.checks
42+
#TODO: the current passes cannot handle module level assembly!
43+
# No checks are implemented
4244
check cc_plus_one_c cc_plus_one_c.checks
4345
check cc_plus_one_c_asm cc_plus_one_c_asm.checks
4446
check cc_plus_one_cxx cc_plus_one_cxx.checks
4547
check cc_plus_one_cxx_asm cc_plus_one_cxx_asm.checks
48+
49+
check cmake_plus_one_c cmake_plus_one_c.checks
50+
check cmake_plus_one_c_asm cmake_plus_one_c_asm.checks
51+
check cmake_plus_one_cxx cmake_plus_one_cxx.checks
52+
check cmake_plus_one_cxx_asm cmake_plus_one_cxx_asm.checks
53+
54+
#WARNING clang/clang++ use an integrated assembler when given an assembly file.
55+
# LVI patches are *not* applied

0 commit comments

Comments
 (0)