CME-1320

Format-String-Safe API Enforcement (Compile-Time and Runtime Format Validation)

Description

Prevent exploitation of format string vulnerabilities by enforcing format-string-safe API usage across compiled and interpreted codebases. In C/C++, enable compiler diagnostics that reject non-literal format arguments (-Wformat-security, -Werror=format-security, /analyze on MSVC) and replace printf-family calls that accept user-controlled format specifiers with fixed-format alternatives (snprintf with literal format, or positional/typed formatting APIs). In managed languages, use parameterized logging (SLF4J {}, Python %s with logging module, .NET structured logging) instead of string interpolation with user input in format position. At the build system level, enforce these checks as CI gate failures so that format-string-unsafe patterns cannot merge. This control directly blocks the attacker's ability to use format specifiers (%n, %x, %s) to read or write arbitrary memory, leak stack contents, or achieve code execution via controlled writes.

CVSS Vector Impacts

Metric Transition Rationale
Attack Complexity (AC) L → H The attacker must find a code path where a user-controlled string reaches a format-specifier-interpreting function without compile-time or runtime neutralization — format-security compiler flags and parameterized APIs eliminate the most common paths, forcing the attacker to find an uncovered edge case or bypass the CI gate
Confidentiality (C) H → L Format string read primitives (%x, %s for stack/heap disclosure) are blocked because user input cannot inject format specifiers into printf-family calls — information disclosure is limited to what the application explicitly outputs

CWE Relationships

Verification

Verify that C/C++ build configurations enable format-string-security compiler warnings as errors, and that CI pipelines gate on these warnings. For managed languages, review logging configuration to confirm parameterized formatting is enforced.

$ grep -rE '(-Wformat-security|-Werror=format-security|/analyze)' Makefile CMakeLists.txt meson.build *.spec 2>/dev/null | head -10
# Expected: Build files contain -Wformat-security and -Werror=format-security (or MSVC /analyze equivalent)
Platform: any
$ gcc -Wformat-security -Werror=format-security -fsyntax-only -xc - <<< 'int main(){char *s="%x"; printf(s);}' 2>&1
# Expected: error: format not a string literal and no format arguments [-Werror=format-security]
Platform: linux
$ grep -rn 'printf\|fprintf\|sprintf\|snprintf' --include='*.c' --include='*.cpp' <source>/ | grep -v '"[^"]*"' | head -10
# Expected: No hits — all printf-family calls use literal format strings
Platform: any

Remediation

rhel RPM global build macros with format-security enforcement

printf '%%global optflags %%{optflags} -Wformat -Wformat-security -Werror=format-security\n' > /etc/rpm/macros.cme-1320-format-security
linux GCC specs file override for system-wide format-security enforcement

gcc -dumpspecs > /tmp/cme-1320-specs.tmp
sed 's/^\*cc1:$/&\n-Wformat -Wformat-security -Werror=format-security/' /tmp/cme-1320-specs.tmp > $(gcc -print-search-dirs | grep install | awk '{print $2}')specs
rm -f /tmp/cme-1320-specs.tmp
any CMake project format-security compiler flags

grep -q 'Werror=format-security' CMakeLists.txt 2>/dev/null || sed -i '1a add_compile_options(-Wformat -Wformat-security -Werror=format-security)' CMakeLists.txt

References

← CME-1319: Deserialization Type Allowlisting CME-1321: Trust Boundary Input Sanitization (Cross-Boundary Data Validation) →