All Projects → airbus-seclab → c-compiler-security

airbus-seclab / c-compiler-security

Licence: CC-BY-SA-4.0 license
Security-related flags and options for C compilers

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to c-compiler-security

Embedded Ide
IDE for C embedded development centered on bare-metal ARM systems
Stars: ✭ 127 (+1.6%)
Mutual labels:  gcc, clang
Polymcu
An open framework for micro-controller software
Stars: ✭ 173 (+38.4%)
Mutual labels:  gcc, clang
Arm Cmake Toolchains
CMake toolchain configurations for ARM
Stars: ✭ 148 (+18.4%)
Mutual labels:  gcc, clang
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (-12.8%)
Mutual labels:  gcc, clang
xcross
"Zero Setup" cross-compilation for C/C++. Supports numerous architectures, build systems, C standard libraries, vcpkg, and Conan.
Stars: ✭ 29 (-76.8%)
Mutual labels:  gcc, clang
Vector
➿ A supercharged std::vector implementation (minus Allocator)
Stars: ✭ 118 (-5.6%)
Mutual labels:  gcc, clang
Libosmscout
Libosmscout is a C++ library for offline map rendering, routing and location lookup based on OpenStreetMap data
Stars: ✭ 159 (+27.2%)
Mutual labels:  gcc, clang
Avalonstudio
Cross platform IDE and Shell
Stars: ✭ 1,132 (+805.6%)
Mutual labels:  gcc, clang
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+2132.8%)
Mutual labels:  gcc, clang
Cmake Scripts
A selection of useful scripts for use in CMake projects, include code coverage, sanitizers, and dependency graph generation.
Stars: ✭ 202 (+61.6%)
Mutual labels:  gcc, clang
Cmakepchcompiler
CMake precompiled header support via custom PCH compiler extension
Stars: ✭ 105 (-16%)
Mutual labels:  gcc, clang
perses
Language-agnostic program reducer.
Stars: ✭ 57 (-54.4%)
Mutual labels:  gcc, clang
Cvise
Super-parallel Python port of the C-Reduce
Stars: ✭ 77 (-38.4%)
Mutual labels:  gcc, clang
minilib
A c standard system library with a focus on size, headeronly, "singlefile", intended for static linking. 187 Bytes for "Hello World"(regular elf), compiled with the standard gcc toolchain.
Stars: ✭ 29 (-76.8%)
Mutual labels:  gcc, clang
Ccache
ccache – a fast compiler cache
Stars: ✭ 1,128 (+802.4%)
Mutual labels:  gcc, clang
C
Compile and execute C "scripts" in one go!
Stars: ✭ 1,920 (+1436%)
Mutual labels:  gcc, clang
Pfr
std::tuple like methods for user defined types without any macro or boilerplate code
Stars: ✭ 896 (+616.8%)
Mutual labels:  gcc, clang
C2goasm
C to Go Assembly
Stars: ✭ 1,072 (+757.6%)
Mutual labels:  gcc, clang
Fixed point
C++ Binary Fixed-Point Arithmetic
Stars: ✭ 199 (+59.2%)
Mutual labels:  gcc, clang
FrameOfReference
C++ library to pack and unpack vectors of integers having a small range of values using a technique called Frame of Reference
Stars: ✭ 36 (-71.2%)
Mutual labels:  gcc, clang

Getting the maximum of your C compiler, for security

Introduction

This guide is intended to help you determine which flags you should use to compile your C Code using GCC, Clang or MSVC, in order to:

  • detect the maximum number of bugs or potential security problems.
  • enable security mitigations in the produced binaries.
  • enable runtime sanitizers to detect errors (overflows, race conditions, etc.) and make fuzzing more efficient.

Disclaimer:

The flags selected and recommended here were chosen to maximize the number of classes of detected errors which could have a security benefit when enabled. Code generation options (such as -fstack-protector-strong) can also have performance impacts. It is up to you to assess the impact on your code base and choose the right set of command line options.

Comments are of course welcome.

GCC TL;DR

Detailed page

Always use the following warnings and flags on the command line:

-O2
-Werror
-Wall -Wextra -Wpedantic -Wformat=2 -Wformat-overflow=2 -Wformat-truncation=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wtrampolines -Walloca -Wvla -Warray-bounds=2 -Wimplicit-fallthrough=3 -Wtraditional-conversion -Wshift-overflow=2 -Wcast-qual -Wstringop-overflow=4 -Wconversion -Warith-conversion -Wlogical-op -Wduplicated-cond -Wduplicated-branches -Wformat-signedness -Wshadow -Wstrict-overflow=4 -Wundef -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wstack-usage=1000000 -Wcast-align=strict
-D_FORTIFY_SOURCE=2
-fstack-protector-strong -fstack-clash-protection -fPIE 
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code

On legacy code bases, some of the warnings may produce some false positives. On code where the behavior is intended, pragmas can be used to disable the specific warning locally.

Run debug/test builds with sanitizers (in addition to the flags above): AddressSanitizer + UndefinedBehaviorSanitizer:

-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=bounds-strict -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow
export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:detect_invalid_pointer_pairs=2

If your program is multi-threaded, run with -fsanitize=thread (incompatible with ASan).

Finally, use -fanalyzer to spot potential issues.

Clang TL;DR

Detailed page

First compile with:

-O2
-Werror
-Walloca -Wcast-qual -Wconversion -Wformat=2 -Wformat-security -Wnull-dereference -Wstack-protector -Wvla -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wconversion -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wswitch-enum -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma
-D_FORTIFY_SOURCE=2
-fstack-protector-strong -fsanitize=safe-stack -fPIE -fstack-clash-protection
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,separate-code

On legacy code bases, some of the warnings may produce some false positives. On code where the behavior is intended, pragmas can be used to disable the specific warning locally.

Run debug/test builds with sanitizers, in addition to the flags above (and after removing -fsanitize=safe-stack, which is incompatible with LeakSanitizer):

AddressSanitizer + UndefinedBehaviorSanitizer:

-fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -fsanitize=undefined  -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=integer
export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:detect_invalid_pointer_pairs=2

If your program is multi-threaded, run with -fsanitize=thread (incompatible with ASan).

Finally, use scan-build to spot potential issues.

In addition, you can build production code with -fsanitize=integer -fsanitize-minimal-runtime -fno-sanitize-recover to catch integer overflows.

Microsoft Visual Studio 2019 TL;DR

Detailed page

  • Compile with /Wall /sdl /guard:cf /guard:ehcont /CETCOMPAT
  • Use ASan with /fsanitize=address
  • Analyze your code with /analyze

Tips

References

Written by Raphaël Rigo and reviewed by Sarah Zennou @ Airbus Security lab, 2021.

Contributing

Please open an issue if you notice any error, imprecision or have comments or improvements ideas.

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].