17

Use _FORTIFY_SOURCE

Compile with those flags:

-O2 -D_FORTIFY_SOURCE=2

 
to enable basic bounds checks for functions like memcpy or strcpy.

#SecureCoding #C/C++

Compiling with -O2 -D_FORTIFY_SOURCE=2 makes glibc functions like memcpy or strcpy do bounds checking for cases where the bounds are known at compile time, such as with statically defined, local buffers.

Higher fortify levels (currently from 1 up to 3) enable stricter behavior and add runtime checks (e.g. if the bounds are known to depend on a variable).

This of requires support for certain compiler builtins and optimization needs to be enabled.

Icon with a waving hand

Get in touch

sigma star gmbh
Eduard-Bodem-Gasse 6, 1st floor
6020 Innsbruck | Austria

sigma star gmbh logo