Skip to content

Commit 6f67509

Browse files
authored
[monodroid] Add a handful of compiler options to harden code (#8551)
Context: https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html The following compile-time warning options are added: * `-Wformat=2`: Enable additional format function warnings. The `2` form of the option enables more extensive checks in calls to `printf`, `scanf` etc. These are compile-time only checks, no effect on runtime performance. * [`-Wimplicit-fallthrough`][1]: Warn when a switch case falls through. If this is an intended action, it can be marked with the `[[fallthrough]]` attribute. * `-Wtrampolines`: Enable warnings about trampolines that require executable stacks. The following options which affect the generated code are added: * `-fstack-clash-protection`: Enable run-time checks for variable-size stack allocation validity. This may affect performance if code allocates a lot of memory on the stack, but since we don't do that, we should be fine. * `-fstrict-flex-arrays=3`: Consider trailing array (at the end of struct) as flexible array only if declared as `[]` * (x86) `-fcf-protection=full`: Enable control flow protection to counter Return Oriented Programming (ROP) and Jump Oriented Programming (JOP) attacks on many x86 architectures * (arm64) `-mbranch-protection=standard`: Enable branch protection to counter Return Oriented Programming (ROP) and Jump Oriented Programming (JOP) attacks on AArch64 [1]: https://clang.llvm.org/docs/AttributeReference.html#fallthrough
1 parent 8fa5d99 commit 6f67509

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/monodroid/CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,37 @@ set(LOCAL_COMMON_COMPILER_ARGS
331331
-Werror=format-security
332332
-Werror=return-type
333333
-Wextra
334-
-Wformat
335334
-Wformat-security
335+
-Wformat=2
336+
-Wimplicit-fallthrough
336337
-Wmisleading-indentation
337338
-Wnull-dereference
338339
-Wpointer-arith
339340
-Wshadow
340341
-Wsign-compare
342+
-Wtrampolines
341343
-Wuninitialized
344+
-fstack-clash-protection
345+
-fstrict-flex-arrays=3
342346
)
343347

348+
# Add some options to increase security. They may mildly affect performance but they won't be big, because the features are
349+
# assisted by the hardware.
350+
if((CMAKE_ANDROID_ARCH_ABI STREQUAL "x86") OR (CMAKE_ANDROID_ARCH_ABI STREQUAL "x86_64"))
351+
# -fcf-protection=full: Enable control flow protection to counter Return Oriented Programming (ROP) and Jump Oriented Programming (JOP) attacks on many x86 architectures
352+
list(APPEND LOCAL_COMMON_COMPILER_ARGS
353+
-fcf-protection=full
354+
)
355+
endif()
356+
357+
if(CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a")
358+
# -mbranch-protection=standard: Enable branch protection to counter Return Oriented Programming (ROP) and Jump Oriented Programming (JOP) attacks on AArch64
359+
# In clang -mbranch-protection=standard is equivalent to -mbranch-protection=bti+pac-ret and invokes the AArch64 Branch Target Identification (BTI) and Pointer Authentication using key A (pac-ret)
360+
list(APPEND LOCAL_COMMON_COMPILER_ARGS
361+
-mbranch-protection=standard
362+
)
363+
endif()
364+
344365
if(COMPILER_DIAG_COLOR)
345366
list(APPEND LOCAL_COMMON_COMPILER_ARGS
346367
-fdiagnostics-color=always

0 commit comments

Comments
 (0)