Enable _FORTIFY_SOURCE=3 and Understand What It Checks
Turn on _FORTIFY_SOURCE=3 with -O2 on glibc 2.33 or later and see which libc calls get bounds checks, how level 3 differs from 2 and what it cannot catch.

_FORTIFY_SOURCE is a macro that asks the C library headers to swap risky functions for checked variants. When the compiler knows how big a destination buffer is, the checked variant compares the copy length with that size and aborts the program if the copy would overflow. In many cases it can reject the call at compile time with a warning. Level 3, available in glibc 2.33 and later with GCC 12 and Clang 17, extends the idea to buffers whose size is only known at run time, such as heap allocations. This post shows how to turn it on, what an overflow looks like with and without it, which functions are covered, how levels 1, 2 and 3 differ and the situations where the check silently does nothing. Examples target GCC 15 on x86-64 Linux 6.12 with glibc 2.41.
Turn it on
The flag is a preprocessor define, and it only works when the compiler optimizes, because the checks depend on the compiler's ability to compute object sizes:
gcc -std=c17 -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -o app app.cThe -U_FORTIFY_SOURCE first removes any value that the distribution's compiler already defined, which avoids the "_FORTIFY_SOURCE" redefined warning. Without optimization the header prints warning: _FORTIFY_SOURCE requires compiling with optimization (-O) and does nothing. Ubuntu 24.04 enables level 3 by default in its compilers, and other distributions use level 2 or none, so set it explicitly. Add -D_FORTIFY_SOURCE=3 to your project's common flags, and verify the binary has _chk imports, as shown at the end.
What it does to an overflow
Take a program with a stack buffer and a copy from user input:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char buf[16];
if (argc < 2)
return 1;
strcpy(buf, argv[1]);
printf("%s\n", buf);
return 0;
}Build it twice, with fortification off and on, and run both with a 40-character argument. Without it, and with the stack protector and other defaults disabled to isolate the effect, the program overruns the buffer and, depending on the layout, crashes or misbehaves. With -O2 -D_FORTIFY_SOURCE=3, GCC rewrites the call to __strcpy_chk(buf, argv[1], 16), passing the destination size as a third argument. The checked function compares the length of the source with 16 before copying:
$ ./fort_fortified AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
*** buffer overflow detected ***: terminated
Aborted (core dumped)The process dies before any byte is written past the buffer, which is the main difference from a stack canary, which notices after the fact. The message and the terminated word come from glibc's __fortify_fail, and the shell's second line differs by shell.
When the sizes are visible at compile time, the compiler warns you before the program ever runs. A constant source longer than the destination gives:
fort.c: In function 'main':
fort.c:8:5: warning: '__builtin_strcpy' writing 11 bytes into a region of size 8 overflows the destination [-Wstringop-overflow=]That warning belongs to -Wstringop-overflow, on by default in GCC, and it fires with or without fortification. The _chk machinery adds the run-time half.
Which calls are covered
The glibc headers define checked variants of about three dozen functions when fortification is on. The ones that matter most:
| Group | Functions |
|---|---|
| Memory | memcpy, memmove, mempcpy, memset, wmemcpy and friends |
| Strings | strcpy, stpcpy, strncpy, strcat, strncat |
| Formatted output | sprintf, snprintf, vsprintf, vsnprintf and the printf family with %n checks |
| Input | read, pread, recv, recvfrom, fgets, getcwd, readlink, realpath |
| Misc | poll (array size), fread, fdelt and others, FD_SET index checks |
Each is replaced, in the header, by an inline function that calls __builtin_object_size or, at level 3, __builtin_dynamic_object_size, and passes the result to the matching __xxx_chk function in libc.so. If the size is unknown, the checked function receives the maximum value and checks nothing. If it is known and the copy fits, the call is the same cost as before plus a comparison.
printf family fortification also adds checks on the format: with level 2 or higher, %n in a writable format string is rejected, and positional arguments must be dense, which closes some format-string tricks.
Levels 1, 2 and 3
| Level | Compile-time warnings | Run-time checks | Object sizes known from |
|---|---|---|---|
| 0 or unset | none | none | n/a |
| 1 | yes | only where the program is well-defined as written | __builtin_object_size type 0, whole object |
| 2 | yes | adds checks that can break conforming but sloppy code, such as memcpy into one member of a struct beyond that member | __builtin_object_size type 1, innermost member |
| 3 | yes | all of level 2, plus buffers of dynamic size | __builtin_dynamic_object_size |
Level 1 works for object sizes that are the whole object: if you copy into the middle of a struct, it checks against the whole struct. Level 2 checks against the member, which catches overflows from one field into the next and occasionally reports on legitimate code that treats a trailing array as a flexible one. Level 3 adds the size expressions the compiler can compute at run time, for example malloc(n) followed by a copy:
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
size_t n = (size_t)argc * 4; /* 8 for one argument */
char *p = malloc(n);
if (p == NULL)
return 1;
memcpy(p, "0123456789abcdef", 16); /* 16 bytes into an 8-byte block */
free(p);
return 0;
}With level 2, the size of p's block is not a compile-time constant, so __builtin_object_size reports unknown and the copy passes unchecked, overflowing the heap block. With level 3, the compiler tracks that p came from malloc(n) and passes n to __memcpy_chk:
$ ./dyn_l3
*** buffer overflow detected ***: terminated
Aborted (core dumped)The dynamic size is available only when the allocation and the use are visible to the compiler in the same function after inlining, and the allocator is annotated with __attribute__((alloc_size)), which glibc's malloc is. A pointer that passes through several function boundaries loses its size, and the check disappears. The GCC documentation describes the built-in in Object Size Checking.
What it does not catch
Unknown sizes. A
char *parameter has no size the compiler can see unless inlining exposes the caller's object. A library function that takes a pointer and a length and does the copy in a loop of your own is not fortified.Your own loops. Fortification covers library calls, not
for (i = 0; i <= n; i++) buf[i] = .... Use AddressSanitizer and-fstack-protector-strongfor those.Overflows inside a flexible array member or a struct hack at level 1, which cannot know the real size. Declare flexible arrays properly (
char data[];), and GCC 15 and Clang 18 support__attribute__((counted_by(len))), which gives the compiler the size.Reads that do not use the covered functions. Out-of-bounds reads through indexing are not checked.
Heap object overflows where the allocation is hidden, for example allocated in another translation unit and returned as an opaque pointer.
Cost
The added work is a size comparison per covered call, usually folded away when the compiler knows both sides, and a load of the size in the dynamic case. Most programs show no measurable change, and the method in the earlier post on measuring hardening costs applies if yours does. Code size grows slightly.
Verify it is on
The easiest check is to look for imported _chk functions in the built binary:
$ nm -D ./fort_fortified | grep _chk
U __strcpy_chk@GLIBC_2.3.4
$ nm -D ./fort_plain | grep _chkAn empty result for a program that calls strcpy on a known-size buffer means the fortification did not apply: the flag was missing, -O0 was in effect, or the compiler resolved the call without a check. The second reason is the most common in debug builds, so run the check on the release artifact. checksec reports the same in its FORTIFY column, as the earlier post describes.


