Skip to content

Commit ed900ae

Browse files
committed
Add -Werror=format-security to list of recommended options
Signed-off-by: Thomas Nyman <[email protected]>
1 parent 434d644 commit ed900ae

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ When compiling C or C++ code on compilers such as GCC and clang, turn on these f
2121

2222
~~~sh
2323
-O2 -Wall -Wformat -Wformat=2 -Wconversion -Wimplicit-fallthrough \
24+
-Werror=format-security \
2425
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 \
2526
-D_GLIBCXX_ASSERTIONS \
2627
-fstrict-flex-arrays=3 \
@@ -186,6 +187,7 @@ Table 1: Recommended compiler options that enable strictly compile-time checks.
186187
| [`-Wimplicit-fallthrough`](#-Wimplicit-fallthrough) | GCC 7<br>Clang 4.0 | Warn when a switch case falls through |
187188
| [`-Wbidi-chars=any`](#-Wbidi-chars=any) | GCC 12 | Enable warnings for possibly misleading Unicode bidirectional control characters |
188189
| [`-Werror`](#-Werror)<br/>[`-Werror=`*`<warning-flag>`*](#-Werror-flag) | GCC 2.95.3<br/>Clang 2.6 | Treat all or selected compiler warnings as errors. Use the blanket form `-Werror` only during development, not in source distribution. |
190+
| [`-Werror=format-security`](#-Werror=format-security) | GCC 2.95.3<br/>Clang 4.0 | Treat format strings that are not string literals and used without arguments as errors |
189191
| [`-Werror=implicit`](#-Werror=implicit)<br/>[`-Werror=incompatible-pointer-types`](#-Werror=incompatible-pointer-types)<br/>[`-Werror=int-conversion`](#-Werror=int-conversion)<br/> | GCC 2.95.3<br/>Clang 2.6 | Treat obsolete C constructs as errors |
190192

191193
Table 2: Recommended compiler options that enable run-time protection mechanisms.
@@ -399,6 +401,44 @@ Zero-warning policies can also be enforced at CI level. CI-based zero- or bounde
399401

400402
---
401403

404+
### Treat format strings that are not string literals and used without arguments as errors
405+
406+
| Compiler Flag | Supported since | Description |
407+
|:----------------------------------------------------------------------------------------- |:--------------------------:|:--------------------------------------------------------------------------------------|
408+
| <span id="-Werror=format-security">`-Werror=format-security`</span> | GCC 2.95.3<br/> Clang 4.0 | Treat format strings that are not string literals and used without arguments as errors |
409+
410+
#### Synopsis
411+
412+
Treat calls to printf- and scanf-family of functions where the format string is not a string literal and there are no additional format arguments as errors.
413+
414+
Format strings that can be influenced at run-time from outside the program are likely to cause format string vulnerabilities[^scut2001]. We recommend treating format strings that are not string literals and used without addition arguments as errors as invocations of the form:
415+
416+
~~~C
417+
printf(fmt);
418+
printf(gettext("Hello World\n"));
419+
fprintf(stderr, fmt);
420+
~~~
421+
422+
always indicates a bug and, if the format string can be controlled by external input, can be used in a format string attack. Code of this form where the format string `fmt` is not expected to contain format specifiers can be rewritten in a safe form using a fixed format string:
423+
424+
~~~C
425+
printf("%s", fmt);
426+
printf("%s", gettext("Hello World\n"));
427+
fprintf(stderr, "%s", fmt);
428+
~~~
429+
430+
Some Linux distributions, such as Arch Linux[^arch-buildflags], Fedora[^fedora-formatsecurityfaq], and Ubuntu[^ubuntu-compilerflags], are enforcing the use of `-Werror=format-security` when building software for distribution.
431+
432+
[^scut2001]: scut \[TESO\], [Exploiting Format String Vulnerabilities](https://web.archive.org/web/20240402183013/https://cs155.stanford.edu/papers/formatstring-1.2.pdf), version 1.2, 2001-09-01.
433+
434+
[^arch-buildflags]: Arch Linux, [rfc/0003-buildflags.rst](https://gitlab.archlinux.org/archlinux/rfcs/-/blob/2136adc4a86afe37f351f8f564af3dcc6d7681ae/rfcs/0003-buildflags.rstt), ArchLinux RFC, 2023-09-03.
435+
436+
[^fedora-formatsecurityfaq]: Fedora, [Format-Security-FAQ](https://fedoraproject.org/wiki/Format-Security-FAQ), Fedora Wiki, 2013-12-05.
437+
438+
[^ubuntu-compilerflags]: Ubuntu, [ToolChain/CompilerFlags](https://wiki.ubuntu.com/ToolChain/CompilerFlags#A-Wformat_-Wformat-security), Ubuntu Wiki, 2024-03-22.
439+
440+
---
441+
402442
### Treat obsolete C constructs as errors
403443

404444
| Compiler Flag | Supported since | Description |

0 commit comments

Comments
 (0)