From 3067c28ceb744b2bafe9202023084f762871e844 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 27 Sep 2023 23:28:50 +0200 Subject: Add `printf` hardening --- include/stdio.h | 12 ++++++++++++ tests/Makefile | 1 + tests/test_printf.c | 7 +++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/test_printf.c diff --git a/include/stdio.h b/include/stdio.h index b407d0f..c363edc 100644 --- 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 index 4e5ee00..a628a2a 100644 --- 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 new file mode 100644 index 0000000..d444a52 --- /dev/null +++ b/tests/test_printf.c @@ -0,0 +1,7 @@ +#include "common.h" + +#include + +int main(int argc, char** argv) { + printf("%s", "1234567"); +} -- cgit v1.3