fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 3067c28ceb744b2bafe9202023084f762871e844
parent 81ef1fd8c55aae65d321b2730ff5da6b73d1e707
Author: jvoisin <julien.voisin@dustri.org>
Date:   Wed, 27 Sep 2023 23:28:50 +0200

Add `printf` hardening

Diffstat:
Minclude/stdio.h | 12++++++++++++
Mtests/Makefile | 1+
Atests/test_printf.c | 7+++++++
3 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h @@ -38,6 +38,7 @@ extern "C" { #undef popen #undef vsnprintf #undef vsprintf +#undef printf __access(read_only, 2) #if __has_builtin(__builtin_fdopen) @@ -241,6 +242,17 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) #endif } +__format(printf, 1, 2) +__access(read_only, 1) +_FORTIFY_FN(printf) int printf(const char *__f, ...) +{ +#if __has_builtin(__builtin___printf_chk) && USE_NATIVE_CHK + return __builtin___printf_chk(_FORTIFY_SOURCE, __f, __builtin_va_arg_pack()); +#else + return __orig_printf(__f, __builtin_va_arg_pack()); +#endif +} + #endif /* __has_builtin(__builtin_va_arg_pack) */ #ifdef __cplusplus diff --git a/tests/Makefile b/tests/Makefile @@ -64,6 +64,7 @@ RUNTIME_TARGETS= \ test_ppoll_static \ test_pread_dynamic \ test_pread_static \ + test_printf \ test_pwrite_dynamic \ test_pwrite_static \ test_read_dynamic \ diff --git a/tests/test_printf.c b/tests/test_printf.c @@ -0,0 +1,7 @@ +#include "common.h" + +#include <stdio.h> + +int main(int argc, char** argv) { + printf("%s", "1234567"); +}