Sat Jun 27 08:50:46 2020 UTC ()
Add a simple test case that writes random garbage to (almost) every sysctl node.
This test reproduced already at least five unique panics in a few quick runs.
The test is skipped by default as it is not likely safe even without the panics.


(jruoho)
diff -r1.857 -r1.858 src/distrib/sets/lists/tests/mi
diff -r1.2 -r1.3 src/tests/sbin/sysctl/Makefile
diff -r0 -r1.1 src/tests/sbin/sysctl/t_random_garbage.sh

cvs diff -r1.857 -r1.858 src/distrib/sets/lists/tests/mi (expand / switch to context diff)
--- src/distrib/sets/lists/tests/mi 2020/06/27 06:57:44 1.857
+++ src/distrib/sets/lists/tests/mi 2020/06/27 08:50:46 1.858
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.857 2020/06/27 06:57:44 jruoho Exp $
+# $NetBSD: mi,v 1.858 2020/06/27 08:50:46 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -4032,6 +4032,7 @@
 ./usr/tests/sbin/sysctl/Atffile			tests-sbin-tests	compattestfile,atf
 ./usr/tests/sbin/sysctl/Kyuafile		tests-sbin-tests	compattestfile,atf,kyua
 ./usr/tests/sbin/sysctl/t_perm			tests-sbin-tests	compattestfile,atf
+./usr/tests/sbin/sysctl/t_random_garbage	tests-sbin-tests	compattestfile,atf
 ./usr/tests/sbin/sysctl/t_sysctl		tests-sbin-tests	compattestfile,atf
 ./usr/tests/share				tests-sys-tests	compattestfile,atf
 ./usr/tests/share/Atffile			tests-sys-tests		compattestfile,atf

cvs diff -r1.2 -r1.3 src/tests/sbin/sysctl/Makefile (expand / switch to context diff)
--- src/tests/sbin/sysctl/Makefile 2012/04/10 02:39:33 1.2
+++ src/tests/sbin/sysctl/Makefile 2020/06/27 08:50:46 1.3
@@ -1,9 +1,9 @@
-# $NetBSD: Makefile,v 1.2 2012/04/10 02:39:33 jruoho Exp $
+# $NetBSD: Makefile,v 1.3 2020/06/27 08:50:46 jruoho Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=	${TESTSBASE}/sbin/sysctl
 
-TESTS_SH=	t_perm t_sysctl
+TESTS_SH=	t_perm t_random_garbage t_sysctl
 
 .include <bsd.test.mk>

File Added: src/tests/sbin/sysctl/t_random_garbage.sh
# $NetBSD: t_random_garbage.sh,v 1.1 2020/06/27 08:50:46 jruoho Exp $
#
# Copyright (c) 2020 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Jukka Ruohonen.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
tmp="/tmp/sysctl.out"

getrint() {
	echo $(od -An -N2 -i /dev/urandom | sed 's/ //')
}

getrstr() {
	echo $(cat /dev/urandom | head -n 1 | base64)
}

atf_test_case random_garbage cleanup
random_garbage_head() {
	sysctl -a > $tmp
	atf_set "require.user" "root"
	atf_set "descr" "Test writing random garbage to sysctl nodes"
}

random_garbage_body() {

	atf_skip "The test is not safe"

	while read line; do

		var=$(echo $line | awk '{print $1}')

		case $var in
			kern.securelevel*)
			echo "Skipping $var"
			continue
			;;

			security*)
			echo "Skipping $var"
			continue
			;;
		esac

		val=$(getrint)
		echo "Write $var -> $val"
		sysctl -w $var=$val
		val=$(getrstr)
		echo "Write $var -> $val"
		sysctl -w $var=$val

	done < $tmp
}

random_garbage_cleanup() {

	atf_skip "The test is not safe"

	while read line; do
		var=$(echo $line | awk '{print $1}')
		val=$(echo $line | awk '{print $3}')
		echo "Restoring $var -> $val"
		sysctl -w $var=$val > /dev/null 2>&1
	done < $tmp

	rm $tmp
}

atf_init_test_cases() {
	atf_add_test_case random_garbage
}