t_asan_uaf.sh revision 1.1
11.1Skamil#	$NetBSD: t_asan_uaf.sh,v 1.1 2018/04/04 23:53:26 kamil Exp $
21.1Skamil#
31.1Skamil# Copyright (c) 2018 The NetBSD Foundation, Inc.
41.1Skamil# All rights reserved.
51.1Skamil#
61.1Skamil# This code is derived from software contributed to The NetBSD Foundation
71.1Skamil# by Siddharth Muralee.
81.1Skamil#
91.1Skamil# Redistribution and use in source and binary forms, with or without
101.1Skamil# modification, are permitted provided that the following conditions
111.1Skamil# are met:
121.1Skamil# 1. Redistributions of source code must retain the above copyright
131.1Skamil#    notice, this list of conditions and the following disclaimer.
141.1Skamil# 2. Redistributions in binary form must reproduce the above copyright
151.1Skamil#    notice, this list of conditions and the following disclaimer in the
161.1Skamil#    documentation and/or other materials provided with the distribution.
171.1Skamil#
181.1Skamil# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
191.1Skamil# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
201.1Skamil# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
211.1Skamil# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
221.1Skamil# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
231.1Skamil# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
241.1Skamil# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
251.1Skamil# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
261.1Skamil# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
271.1Skamil# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
281.1Skamil# POSSIBILITY OF SUCH DAMAGE.
291.1Skamil#
301.1Skamil
311.1SkamilSUPPORT='n'
321.1Skamiltest_target() {
331.1Skamil	if uname -m | grep -q "amd64"; then
341.1Skamil		SUPPORT='y'
351.1Skamil	fi
361.1Skamil
371.1Skamil	if uname -m | grep -q "i386"; then
381.1Skamil		SUPPORT='y'
391.1Skamil	fi
401.1Skamil}
411.1Skamil
421.1Skamilatf_test_case uaf
431.1Skamiluaf_head() {
441.1Skamil	atf_set "descr" "compile and run \"Use After Free example\""
451.1Skamil	atf_set "require.progs" "c++ paxctl"
461.1Skamil}
471.1Skamil
481.1Skamilatf_test_case uaf_profile
491.1Skamiluaf_profile_head() {
501.1Skamil	atf_set "descr" "compile and run \"Use After Free example\" with profiling option"
511.1Skamil	atf_set "require.progs" "c++ paxctl"
521.1Skamil}
531.1Skamil
541.1Skamilatf_test_case uaf_pic
551.1Skamiluaf_pic_head() {
561.1Skamil	atf_set "descr" "compile and run PIC \"Use After Free example\""
571.1Skamil	atf_set "require.progs" "c++ paxctl"
581.1Skamil}
591.1Skamil
601.1Skamilatf_test_case uaf_pie
611.1Skamiluaf_pie_head() {
621.1Skamil	atf_set "descr" "compile and run position independent (PIE) \"Use After Free example\""
631.1Skamil	atf_set "require.progs" "c++ paxctl"
641.1Skamil}
651.1Skamil
661.1Skamilatf_test_case uaf32
671.1Skamiluaf32_head() {
681.1Skamil	atf_set "descr" "compile and run \"Use After Free example\" for/in netbsd32 emulation"
691.1Skamil	atf_set "require.progs" "c++ paxctl file diff cat"
701.1Skamil}
711.1Skamil
721.1Skamilatf_test_case target_not_supported
731.1Skamiltarget_not_supported_head()
741.1Skamil{
751.1Skamil	atf_set "descr" "Test forced skip"
761.1Skamil}
771.1Skamil
781.1Skamiluaf_body() {
791.1Skamil	cat > test.cpp << EOF
801.1Skamil#include <stdlib.h>
811.1Skamil#include <stdio.h>
821.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
831.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
841.1SkamilEOF
851.1Skamil	c++ -fsanitize=address -o test test.cpp
861.1Skamil	paxctl -a test
871.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./test
881.1Skamil}
891.1Skamil
901.1Skamiluaf_profile_body() {
911.1Skamil	cat > test.cpp << EOF
921.1Skamil#include <stdlib.h>
931.1Skamil#include <stdio.h>
941.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
951.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
961.1SkamilEOF
971.1Skamil	c++ -fsanitize=address -o test -pg test.cpp
981.1Skamil	paxctl +a test
991.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./test
1001.1Skamil}
1011.1Skamil
1021.1Skamiluaf_pic_body() {
1031.1Skamil	cat > test.cpp << EOF
1041.1Skamil#include <stdlib.h>
1051.1Skamil#include <stdio.h>
1061.1Skamilint foo();
1071.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
1081.1SkamilEOF
1091.1Skamil	cat > pic.cpp << EOF
1101.1Skamil#include <stdlib.h>
1111.1Skamil#include <stdio.h>
1121.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
1131.1SkamilEOF
1141.1Skamil
1151.1Skamil	c++ -fPIC -fsanitize=address -shared -o libtest.so pic.cpp
1161.1Skamil	c++ -o test test.cpp -fsanitize=address -L. -ltest
1171.1Skamil	paxctl +a test
1181.1Skamil
1191.1Skamil	export LD_LIBRARY_PATH=.
1201.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./test
1211.1Skamil}
1221.1Skamil
1231.1Skamiluaf_pie_body() {
1241.1Skamil	# check whether this arch supports -pice
1251.1Skamil	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
1261.1Skamil		atf_set_skip "c++ -pie not supported on this architecture"
1271.1Skamil	fi
1281.1Skamil	cat > test.cpp << EOF
1291.1Skamil#include <stdlib.h>
1301.1Skamil#include <stdio.h>
1311.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
1321.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
1331.1SkamilEOF
1341.1Skamil	c++ -fsanitize=address -fpie -pie -o test test.cpp
1351.1Skamil	paxctl +a test
1361.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./test
1371.1Skamil}
1381.1Skamil
1391.1Skamiluaf32_body() {
1401.1Skamil	# check whether this arch is 64bit
1411.1Skamil	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
1421.1Skamil		atf_skip "this is not a 64 bit architecture"
1431.1Skamil	fi
1441.1Skamil	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
1451.1Skamil		atf_skip "c++ -m32 not supported on this architecture"
1461.1Skamil	else
1471.1Skamil		if fgrep -q _LP64 ./def32; then
1481.1Skamil		atf_fail "c++ -m32 does not generate netbsd32 binaries"
1491.1Skamil	fi
1501.1Skamilfi
1511.1Skamil
1521.1Skamil	cat > test.cpp << EOF
1531.1Skamil#include <stdlib.h>
1541.1Skamil#include <stdio.h>
1551.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
1561.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
1571.1SkamilEOF
1581.1Skamil	c++ -fsanitize=address -o uaf32 -m32 test.cpp
1591.1Skamil	c++ -fsanitize=address -o uaf64 test.cpp
1601.1Skamil	file -b ./uaf32 > ./ftype32
1611.1Skamil	file -b ./uaf64 > ./ftype64
1621.1Skamil	if diff ./ftype32 ./ftype64 >/dev/null; then
1631.1Skamil		atf_fail "generated binaries do not differ"
1641.1Skamil	fi
1651.1Skamil	echo "32bit binaries on this platform are:"
1661.1Skamil	cat ./ftype32
1671.1Skamil	echo "While native (64bit) binaries are:"
1681.1Skamil	cat ./ftype64
1691.1Skamil	paxctl +a uaf32 
1701.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./uaf32
1711.1Skamil
1721.1Skamil# and another test with profile 32bit binaries
1731.1Skamil	cat > test.cpp << EOF
1741.1Skamil#include <stdlib.h>
1751.1Skamil#include <stdio.h>
1761.1Skamilint foo() {int *x = (int *)malloc(10 * sizeof(int)); free(x); return x[0];}
1771.1Skamilint main() {foo(); printf("CHECK\n"); exit(0);}
1781.1SkamilEOF
1791.1Skamil	c++ -o test -m32 -fsanitize=address -pg test.cpp
1801.1Skamil	paxctl +a test
1811.1Skamil	atf_check -s not-exit:0 -o not-match:"CHECK\n" -e match:"heap-use-after-free" ./test
1821.1Skamil}
1831.1Skamil
1841.1Skamiltarget_not_supported_body()
1851.1Skamil{
1861.1Skamil	atf_skip "Target is not supported"
1871.1Skamil}
1881.1Skamil
1891.1Skamilatf_init_test_cases()
1901.1Skamil{
1911.1Skamil	test_target
1921.1Skamil	test $SUPPORT = 'n' && {
1931.1Skamil		atf_add_test_case target_not_supported
1941.1Skamil		return 0
1951.1Skamil	}
1961.1Skamil
1971.1Skamil	atf_add_test_case uaf
1981.1Skamil	atf_add_test_case uaf_profile
1991.1Skamil	atf_add_test_case uaf_pic
2001.1Skamil	atf_add_test_case uaf_pie
2011.1Skamil	atf_add_test_case uaf32
2021.1Skamil	# static option not supported 
2031.1Skamil	# -static and -fsanitize=address can't be used together for compilation
2041.1Skamil	# (gcc version 5.4.0 and clang 7.1) tested on April 2nd 2018.
2051.1Skamil}
206