Our fortification macros don't wrap these stdio functions (since there's already lots of variability, whether calling directly to the CRT's stdio functions, or if calling the __USE_MINGW_ANSI_STDIO replacements), but this at least provides token implementations if someone implements fortification of them later.
Not implementing __stpcpy_chk, since mingw-w64 doesn't provide the stpcpy function to begin with. Signed-off-by: Martin Storsjö <[email protected]> --- Not sure if we should add these at this point, since it doesn't solve how to redirect them to the right underlying stdio functions, but this is at least a sample implementation if someone looks into this at a later point. --- mingw-w64-crt/Makefile.am | 3 +++ mingw-w64-crt/ssp/gets_chk.c | 42 +++++++++++++++++++++++++++++++ mingw-w64-crt/ssp/snprintf_chk.c | 30 ++++++++++++++++++++++ mingw-w64-crt/ssp/sprintf_chk.c | 34 +++++++++++++++++++++++++ mingw-w64-crt/ssp/vsnprintf_chk.c | 23 +++++++++++++++++ mingw-w64-crt/ssp/vsprintf_chk.c | 31 +++++++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 mingw-w64-crt/ssp/gets_chk.c create mode 100644 mingw-w64-crt/ssp/snprintf_chk.c create mode 100644 mingw-w64-crt/ssp/sprintf_chk.c create mode 100644 mingw-w64-crt/ssp/vsnprintf_chk.c create mode 100644 mingw-w64-crt/ssp/vsprintf_chk.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index e7786c592..b85090b54 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -566,6 +566,9 @@ src_libmingwex=\ ssp/memset_chk.c ssp/stack_chk_fail.c ssp/stack_chk_guard.c ssp/strcat_chk.c \ ssp/strcpy_chk.c ssp/strncat_chk.c ssp/strncpy_chk.c \ \ + ssp/gets_chk.c ssp/snprintf_chk.c ssp/sprintf_chk.c ssp/vsnprintf_chk.c \ + ssp/vsprintf_chk.c \ + \ stdio/mingw_pformat.h \ stdio/scanf2-argcount-char.c stdio/scanf2-argcount-wchar.c \ stdio/vfscanf2.S stdio/vfwscanf2.S stdio/vscanf2.S stdio/vsscanf2.S stdio/vswscanf2.S \ diff --git a/mingw-w64-crt/ssp/gets_chk.c b/mingw-w64-crt/ssp/gets_chk.c new file mode 100644 index 000000000..9c85083f9 --- /dev/null +++ b/mingw-w64-crt/ssp/gets_chk.c @@ -0,0 +1,42 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +char *__cdecl __gets_chk(char *dst, size_t bufsize); + +char *__cdecl __gets_chk(char *dst, size_t bufsize) +{ + char *buf; + size_t n; + if (bufsize >= (size_t) INT_MAX) + return gets(dst); + + buf = malloc(bufsize + 1); + if (!buf) + return gets(dst); + + if (!fgets(buf, (int)(bufsize + 1), stdin)) { + free(buf); + return NULL; + } + + n = strlen(buf); + if (n > 0 && buf[n - 1] == '\n') + n--; + + if (n >= bufsize) + __chk_fail(); + + memcpy(dst, buf, n); + dst[n] = '\0'; + free(buf); + return dst; +} diff --git a/mingw-w64-crt/ssp/snprintf_chk.c b/mingw-w64-crt/ssp/snprintf_chk.c new file mode 100644 index 000000000..53fabd9f0 --- /dev/null +++ b/mingw-w64-crt/ssp/snprintf_chk.c @@ -0,0 +1,30 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <string.h> +#include <stdio.h> +#include <stdarg.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +int __cdecl __snprintf_chk(char *dst, size_t n, int flags, size_t bufsize, const char *format, ...); + +int __cdecl __snprintf_chk(char *dst, size_t n, int flags, size_t bufsize, const char *format, ...) +{ + va_list ap; + int ret; + + (void)flags; + + if (n > bufsize) + __chk_fail(); + + va_start(ap, format); + ret = vsnprintf(dst, n, format, ap); + va_end(ap); + + return ret; +} diff --git a/mingw-w64-crt/ssp/sprintf_chk.c b/mingw-w64-crt/ssp/sprintf_chk.c new file mode 100644 index 000000000..7d70e1cc5 --- /dev/null +++ b/mingw-w64-crt/ssp/sprintf_chk.c @@ -0,0 +1,34 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <string.h> +#include <stdio.h> +#include <stdarg.h> +#include <limits.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +int __cdecl __sprintf_chk(char *dst, int flags, size_t bufsize, const char *format, ...); + +int __cdecl __sprintf_chk(char *dst, int flags, size_t bufsize, const char *format, ...) +{ + va_list ap; + int ret; + + (void)flags; + + va_start(ap, format); + if (bufsize > (size_t) INT_MAX) { + ret = vsprintf(dst, format, ap); + } else { + ret = vsnprintf(dst, bufsize, format, ap); + if (ret >= 0 && (size_t)ret >= bufsize) + __chk_fail(); + } + va_end(ap); + + return ret; +} diff --git a/mingw-w64-crt/ssp/vsnprintf_chk.c b/mingw-w64-crt/ssp/vsnprintf_chk.c new file mode 100644 index 000000000..ba6fc8eca --- /dev/null +++ b/mingw-w64-crt/ssp/vsnprintf_chk.c @@ -0,0 +1,23 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <string.h> +#include <stdio.h> +#include <stdarg.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +int __cdecl __vsnprintf_chk(char *dst, size_t n, int flags, size_t bufsize, const char *format, va_list ap); + +int __cdecl __vsnprintf_chk(char *dst, size_t n, int flags, size_t bufsize, const char *format, va_list ap) +{ + (void)flags; + + if (n > bufsize) + __chk_fail(); + + return vsnprintf(dst, n, format, ap); +} diff --git a/mingw-w64-crt/ssp/vsprintf_chk.c b/mingw-w64-crt/ssp/vsprintf_chk.c new file mode 100644 index 000000000..582a93925 --- /dev/null +++ b/mingw-w64-crt/ssp/vsprintf_chk.c @@ -0,0 +1,31 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <string.h> +#include <stdio.h> +#include <stdarg.h> +#include <limits.h> + +void __cdecl __chk_fail(void) __attribute__((__noreturn__)); + +int __cdecl __vsprintf_chk(char *dst, int flags, size_t bufsize, const char *format, va_list ap); + +int __cdecl __vsprintf_chk(char *dst, int flags, size_t bufsize, const char *format, va_list ap) +{ + int ret; + + (void)flags; + + if (bufsize > (size_t) INT_MAX) { + ret = vsprintf(dst, format, ap); + } else { + ret = vsnprintf(dst, bufsize, format, ap); + if (ret >= 0 && (size_t)ret >= bufsize) + __chk_fail(); + } + + return ret; +} -- 2.25.1 _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
