diff --git a/.gitattributes b/.gitattributes index 37d9e0189717d64a622edf5c1c022a290c077789..346eae720208800830d1440a8417521f13370af2 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1253,6 +1253,8 @@ tools/transforms/globals.h -text tools/transforms/integertransformdriver.cpp -text tools/transforms/nulltransform.cpp -text tools/transforms/sample_meds_int.annot -text +tools/transforms/tests/test_buffer_overflow.c -text +tools/transforms/tests/test_buffer_overflow.sh -text tools/transforms/transformutils.cpp -text tools/transforms/transformutils.h -text xform/Makefile.in -text diff --git a/tools/transforms/tests/test_buffer_overflow.c b/tools/transforms/tests/test_buffer_overflow.c new file mode 100644 index 0000000000000000000000000000000000000000..39c151a8889006f3d9e035af96f840ef7cbbe661 --- /dev/null +++ b/tools/transforms/tests/test_buffer_overflow.c @@ -0,0 +1,19 @@ +#include <stdio.h> +#include <string.h> + +#define BUFSIZE 32 + +void do_overflow(char *s) +{ + char tmp[BUFSIZE]; + strcpy(tmp, s); + printf("target string is: %s\n", tmp); +} + +int main(int argc, char **argv) +{ + if (argc > 1) + do_overflow(argv[1]); + else + printf("specify long string on command line as argument to overflow (bufsize=%d)\n", BUFSIZE); +} diff --git a/tools/transforms/tests/test_buffer_overflow.sh b/tools/transforms/tests/test_buffer_overflow.sh new file mode 100755 index 0000000000000000000000000000000000000000..3a6628cb2b2e9b1603e49fa2dd2852702eca01db --- /dev/null +++ b/tools/transforms/tests/test_buffer_overflow.sh @@ -0,0 +1,114 @@ +#!/bin/bash + +do_p1() +{ + if [[ -f $2 ]]; then + echo "Eliding rebuild of $2" + else + if [ -z "$3" ]; then + $PSZ $1 $2 --step p1transform=on + else + $PSZ $1 $2 --step p1transform=on --step-option p1transform:"$3" + fi + fi +} + +get_correct() +{ + ./test_buffer_overflow.exe > correct + ./test_buffer_overflow.exe abc >> correct +} + +test_functional() +{ + ./$1 > out + ./$1 abc >> out + + cmp out correct + if [ $? = 1 ]; then + fails=$(expr $fails + 1 ) + echo test failed $1 $2 $3 + echo "=== out ===" + cat out + echo "======" + else + passes=$(expr $passes + 1 ) + echo test passed. + fi +} + +test_detection() +{ + ./$1 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + exitcode=$? + if [ $exitcode -eq $2 ]; then + passes=$(expr $passes + 1 ) + echo test passed. + else + fails=$(expr $fails + 1 ) + echo "test failed: does not detect overflow or wrong exit code: $1 (expected $2, got $exitcode)" + fi +} + +build() +{ + gcc -o test_buffer_overflow.exe test_buffer_overflow.c -fno-stack-protector +} + + +protect() +{ + do_p1 ./test_buffer_overflow.exe test_buffer_overflow.exe.p1.189 + do_p1 ./test_buffer_overflow.exe test_buffer_overflow.exe.p1.188 "--detection_policy exit --detection_exit_code 188" + do_p1 ./test_buffer_overflow.exe test_buffer_overflow.exe.p1.hlt "--detection_policy halt" +} + +clean() +{ + rm out 2>/dev/null + rm correct 2>/dev/null + rm -Rf test_buffer_overflow.exe* peasoup_exe* 2>/dev/null +} + +report () +{ + total=$(expr $passes + $fails) + echo "Passes: $passes / $total" + echo "Fails : $fails / $total" +} + +main() +{ + clean + build + protect + get_correct + + echo "Test functionality" + test_functional test_buffer_overflow.exe # unprotected - should pass! + test_functional test_buffer_overflow.exe.p1.189 + test_functional test_buffer_overflow.exe.p1.188 + test_functional test_buffer_overflow.exe.p1.hlt + + report + + echo "Test detection" + passes=0 + fails=0 + test_detection test_buffer_overflow.exe.p1.189 189 + test_detection test_buffer_overflow.exe.p1.188 188 + test_detection test_buffer_overflow.exe.p1.hlt 139 + + report + + if [[ $1 == "-k" ]] ; then + echo "Skipping cleanup" + else + clean + fi +} + +passes=0 +fails=0 + +main $*