From e6e6dad3324a03134a8814e18ccd61c3a7190a0a Mon Sep 17 00:00:00 2001 From: martynas Date: Sat, 28 Dec 2013 02:14:32 +0000 Subject: [PATCH] Add regression tests to check whether GCC folds unsafe builtins and actually shows the security warning. Also add some other cases where GCC used to yield the warning but shouldn't (e.g. strncat simplified into strcat). --- regress/gnu/egcs/Makefile | 3 ++- regress/gnu/egcs/gcc-builtins/Makefile | 26 +++++++++++++++++++ regress/gnu/egcs/gcc-builtins/sprintf-1.c | 13 ++++++++++ regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/sprintf-2.c | 12 +++++++++ regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/sprintf-3.c | 12 +++++++++ regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/stpcpy-1.c | 13 ++++++++++ regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/stpcpy-2.c | 12 +++++++++ regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/strcat-1.c | 13 ++++++++++ regress/gnu/egcs/gcc-builtins/strcat-1.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/strcat-2.c | 12 +++++++++ regress/gnu/egcs/gcc-builtins/strcat-2.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/strcpy-1.c | 13 ++++++++++ regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/strcpy-2.c | 12 +++++++++ regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/strncat-1.c | 13 ++++++++++ regress/gnu/egcs/gcc-builtins/strncat-1.c.exp | 0 regress/gnu/egcs/gcc-builtins/vsprintf-1.c | 25 ++++++++++++++++++ .../gnu/egcs/gcc-builtins/vsprintf-1.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/vsprintf-2.c | 25 ++++++++++++++++++ .../gnu/egcs/gcc-builtins/vsprintf-2.c.exp | 2 ++ regress/gnu/egcs/gcc-builtins/vsprintf-3.c | 25 ++++++++++++++++++ .../gnu/egcs/gcc-builtins/vsprintf-3.c.exp | 2 ++ 28 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 regress/gnu/egcs/gcc-builtins/Makefile create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-2.c create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-3.c create mode 100644 regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/stpcpy-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/stpcpy-2.c create mode 100644 regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/strcat-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/strcat-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/strcat-2.c create mode 100644 regress/gnu/egcs/gcc-builtins/strcat-2.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/strcpy-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/strcpy-2.c create mode 100644 regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/strncat-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/strncat-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-1.c create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-2.c create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-3.c create mode 100644 regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp diff --git a/regress/gnu/egcs/Makefile b/regress/gnu/egcs/Makefile index a7f3c47e707..962ff9d1188 100644 --- a/regress/gnu/egcs/Makefile +++ b/regress/gnu/egcs/Makefile @@ -1,9 +1,10 @@ -# $OpenBSD: Makefile,v 1.6 2013/08/01 21:26:30 kettenis Exp $ +# $OpenBSD: Makefile,v 1.7 2013/12/28 02:14:32 martynas Exp $ SUBDIR+= gcc libiberty .if defined(REGRESS_FULL) SUBDIR += gcc-bounds +SUBDIR += gcc-builtins .endif install: diff --git a/regress/gnu/egcs/gcc-builtins/Makefile b/regress/gnu/egcs/gcc-builtins/Makefile new file mode 100644 index 00000000000..ecfb1d35239 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/Makefile @@ -0,0 +1,26 @@ +# $OpenBSD: Makefile,v 1.1 2013/12/28 02:14:32 martynas Exp $ + +GCC_BUILTINS= sprintf-1 sprintf-2 sprintf-3 stpcpy-1 stpcpy-2 strcat-1 \ + strcat-2 strcpy-1 strcpy-2 strncat-1 vsprintf-1 vsprintf-2 \ + vsprintf-3 + +TCC= gcc +TCFLAGS= -O2 +TCXXFLAGS= ${TCFLAGS} + +.for i in ${GCC_BUILTINS} +REGRESS_TARGETS+= c-${i} +c-${i}: + ${TCC} ${TCFLAGS} -o /dev/null ${i}.c 2>&1 | cut -d: -f2- | diff -u - ${.CURDIR}/${i}.c.exp +.endfor + +generate: +.for i in ${REGRESS_TARGETS} + -${TCC} ${TCFLAGS} -o /dev/null ${i:C/^c-//g}.c 2>&1 | cut -d: -f2- >${i:C/^c-//g}.c.exp +.endfor + +.PHONY: ${REGRESS_TARGETS} generate regress + +NOOBJ= Yes + +.include diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-1.c b/regress/gnu/egcs/gcc-builtins/sprintf-1.c new file mode 100644 index 00000000000..6294490d438 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-1.c @@ -0,0 +1,13 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[512]; + volatile int rv; + + /* This expression cannot be folded. */ + rv = sprintf(buf, "%s", argv[0]); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp new file mode 100644 index 00000000000..6d3879edbd3 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-1.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: sprintf() is often misused, please use snprintf() diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-2.c b/regress/gnu/egcs/gcc-builtins/sprintf-2.c new file mode 100644 index 00000000000..49974e1db4a --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-2.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[10]; + + /* This expression can be folded. */ + sprintf(buf, "%s", "foo"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp new file mode 100644 index 00000000000..6d3879edbd3 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-2.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: sprintf() is often misused, please use snprintf() diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-3.c b/regress/gnu/egcs/gcc-builtins/sprintf-3.c new file mode 100644 index 00000000000..cac6fa0d5fe --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-3.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[10]; + + /* This expression can be folded. */ + sprintf(buf, "baz"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp b/regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp new file mode 100644 index 00000000000..6d3879edbd3 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/sprintf-3.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: sprintf() is often misused, please use snprintf() diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-1.c b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c new file mode 100644 index 00000000000..d9506280903 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c @@ -0,0 +1,13 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[512]; + volatile char *rv; + + /* This expression cannot be folded. */ + rv = stpcpy(buf, argv[0]); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp new file mode 100644 index 00000000000..ef88ade942d --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/stpcpy-1.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: stpcpy() is dangerous GNU crap; don't use it diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-2.c b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c new file mode 100644 index 00000000000..5700ea99952 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[10]; + + /* This expression can be folded. */ + stpcpy(buf, "foo"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp new file mode 100644 index 00000000000..ef88ade942d --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/stpcpy-2.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: stpcpy() is dangerous GNU crap; don't use it diff --git a/regress/gnu/egcs/gcc-builtins/strcat-1.c b/regress/gnu/egcs/gcc-builtins/strcat-1.c new file mode 100644 index 00000000000..3694d7c2324 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcat-1.c @@ -0,0 +1,13 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[512]; + volatile char *rv; + + /* This expression cannot be folded. */ + rv = strcat(buf, argv[0]); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/strcat-1.c.exp b/regress/gnu/egcs/gcc-builtins/strcat-1.c.exp new file mode 100644 index 00000000000..e0fe608bca9 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcat-1.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: strcat() is almost always misused, please use strlcat() diff --git a/regress/gnu/egcs/gcc-builtins/strcat-2.c b/regress/gnu/egcs/gcc-builtins/strcat-2.c new file mode 100644 index 00000000000..52364a7018c --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcat-2.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[10]; + + /* This expression can be folded. */ + strcat(buf, "foo"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/strcat-2.c.exp b/regress/gnu/egcs/gcc-builtins/strcat-2.c.exp new file mode 100644 index 00000000000..e0fe608bca9 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcat-2.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: strcat() is almost always misused, please use strlcat() diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-1.c b/regress/gnu/egcs/gcc-builtins/strcpy-1.c new file mode 100644 index 00000000000..1c87e6aa50a --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcpy-1.c @@ -0,0 +1,13 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[512]; + volatile char *rv; + + /* This expression cannot be folded. */ + rv = strcpy(buf, argv[0]); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp b/regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp new file mode 100644 index 00000000000..2528526b848 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcpy-1.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: strcpy() is almost always misused, please use strlcpy() diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-2.c b/regress/gnu/egcs/gcc-builtins/strcpy-2.c new file mode 100644 index 00000000000..3862ff16ff6 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcpy-2.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char **argv) +{ + char buf[10]; + + /* This expression can be folded. */ + strcpy(buf, "foo"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp b/regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp new file mode 100644 index 00000000000..2528526b848 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strcpy-2.c.exp @@ -0,0 +1,2 @@ + In function `main': + warning: strcpy() is almost always misused, please use strlcpy() diff --git a/regress/gnu/egcs/gcc-builtins/strncat-1.c b/regress/gnu/egcs/gcc-builtins/strncat-1.c new file mode 100644 index 00000000000..389dd3fc7d0 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/strncat-1.c @@ -0,0 +1,13 @@ +#include + +int +main(int argc, char **argv) +{ + char foo[10]; + const char bar[] = "bar"; + + /* The compiler should not simplify this into strcat. */ + strncat(foo, bar, sizeof(foo)); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/strncat-1.c.exp b/regress/gnu/egcs/gcc-builtins/strncat-1.c.exp new file mode 100644 index 00000000000..e69de29bb2d diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-1.c b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c new file mode 100644 index 00000000000..f0a4340e39f --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c @@ -0,0 +1,25 @@ +#include +#include + +void +test_vsprintf(int unused, ...) +{ + char buf[512]; + volatile int rv; + va_list ap; + + va_start(ap, unused); + + /* This expression cannot be folded. */ + rv = vsprintf(buf, "%s", ap); + + va_end(ap); +} + +int +main(int argc, char **argv) +{ + test_vsprintf(0, argv[0]); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp new file mode 100644 index 00000000000..836db0696bc --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-1.c.exp @@ -0,0 +1,2 @@ + In function `test_vsprintf': + warning: vsprintf() is often misused, please use vsnprintf() diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-2.c b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c new file mode 100644 index 00000000000..fc8c77260c4 --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c @@ -0,0 +1,25 @@ +#include +#include + +void +test_vsprintf(int unused, ...) +{ + char buf[10]; + volatile int rv; + va_list ap; + + va_start(ap, unused); + + /* This expression can be folded. */ + rv = vsprintf(buf, "%s", ap); + + va_end(ap); +} + +int +main(int argc, char **argv) +{ + test_vsprintf(0, "foo"); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp new file mode 100644 index 00000000000..836db0696bc --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-2.c.exp @@ -0,0 +1,2 @@ + In function `test_vsprintf': + warning: vsprintf() is often misused, please use vsnprintf() diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-3.c b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c new file mode 100644 index 00000000000..a0d5b9e3b5d --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c @@ -0,0 +1,25 @@ +#include +#include + +void +test_vsprintf(int unused, ...) +{ + char buf[10]; + volatile int rv; + va_list ap; + + va_start(ap, unused); + + /* This expression can be folded. */ + rv = vsprintf(buf, "bar", ap); + + va_end(ap); +} + +int +main(int argc, char **argv) +{ + test_vsprintf(0); + + return (1); +} diff --git a/regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp new file mode 100644 index 00000000000..836db0696bc --- /dev/null +++ b/regress/gnu/egcs/gcc-builtins/vsprintf-3.c.exp @@ -0,0 +1,2 @@ + In function `test_vsprintf': + warning: vsprintf() is often misused, please use vsnprintf() -- 2.20.1