xref: /freebsd/contrib/netbsd-tests/sbin/sysctl/t_perm.sh (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1*640235e2SEnji Cooper# $NetBSD: t_perm.sh,v 1.7 2016/06/17 03:55:35 pgoyette Exp $
257718be8SEnji Cooper#
357718be8SEnji Cooper# Copyright (c) 2011 The NetBSD Foundation, Inc.
457718be8SEnji Cooper# All rights reserved.
557718be8SEnji Cooper#
657718be8SEnji Cooper# This code is derived from software contributed to The NetBSD Foundation
757718be8SEnji Cooper# by Jukka Ruohonen.
857718be8SEnji Cooper#
957718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without
1057718be8SEnji Cooper# modification, are permitted provided that the following conditions
1157718be8SEnji Cooper# are met:
1257718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright
1357718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer.
1457718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
1557718be8SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
1657718be8SEnji Cooper#    documentation and/or other materials provided with the distribution.
1757718be8SEnji Cooper#
1857718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1957718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2057718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2157718be8SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2257718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2357718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2457718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2557718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2657718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2757718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2857718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
2957718be8SEnji Cooper#
3057718be8SEnji Cooperfile="/tmp/d_sysctl.out"
3157718be8SEnji Cooper
3257718be8SEnji Cooperclean() {
3357718be8SEnji Cooper
3457718be8SEnji Cooper	if [ -f $file ]; then
3557718be8SEnji Cooper		rm $file
3657718be8SEnji Cooper	fi
3757718be8SEnji Cooper}
3857718be8SEnji Cooper
3957718be8SEnji Coopersysctl_write() {
4057718be8SEnji Cooper
4157718be8SEnji Cooper	deadbeef="3735928559"
42*640235e2SEnji Cooper	deadbeef_signed="-559038737"
4357718be8SEnji Cooper
4457718be8SEnji Cooper	sysctl $1 | cut -d= -f1 > $file
4557718be8SEnji Cooper
4657718be8SEnji Cooper	if [ ! -f $file ]; then
4757718be8SEnji Cooper		atf_fail "sysctl failed"
4857718be8SEnji Cooper	fi
4957718be8SEnji Cooper
5057718be8SEnji Cooper	while read line; do
5157718be8SEnji Cooper
5257718be8SEnji Cooper		node=$(echo $line)
5357718be8SEnji Cooper
5457718be8SEnji Cooper		case $node in
5557718be8SEnji Cooper
5657718be8SEnji Cooper		"$1."*)
5757718be8SEnji Cooper			atf_check -s not-exit:0 -e ignore \
5857718be8SEnji Cooper				-x sysctl -w $node=$deadbeef
5957718be8SEnji Cooper			;;
6057718be8SEnji Cooper		esac
6157718be8SEnji Cooper
6257718be8SEnji Cooper	done < $file
6357718be8SEnji Cooper
6457718be8SEnji Cooper	# A functional verification that $deadbeef
6557718be8SEnji Cooper	# was not actually written to the node.
6657718be8SEnji Cooper	#
67*640235e2SEnji Cooper	if [ ! -z $(sysctl $1 | grep -e $deadbeef -e $deadbeef_signed) ]; then
6857718be8SEnji Cooper		atf_fail "value was written"
6957718be8SEnji Cooper	fi
7057718be8SEnji Cooper}
7157718be8SEnji Cooper
7257718be8SEnji Cooper# ddb.
7357718be8SEnji Cooper#
7457718be8SEnji Cooperatf_test_case sysctl_ddb cleanup
7557718be8SEnji Coopersysctl_ddb_head() {
7657718be8SEnji Cooper	atf_set "require.user" "unprivileged"
7757718be8SEnji Cooper	atf_set "descr" "Test writing to 'ddb' sysctl node as an user"
7857718be8SEnji Cooper}
7957718be8SEnji Cooper
8057718be8SEnji Coopersysctl_ddb_body() {
8157718be8SEnji Cooper	sysctl_write "ddb"
8257718be8SEnji Cooper}
8357718be8SEnji Cooper
8457718be8SEnji Coopersysctl_ddb_cleanup() {
8557718be8SEnji Cooper	clean
8657718be8SEnji Cooper}
8757718be8SEnji Cooper
8857718be8SEnji Cooper# hw.
8957718be8SEnji Cooper#
9057718be8SEnji Cooperatf_test_case sysctl_hw cleanup
9157718be8SEnji Coopersysctl_hw_head() {
9257718be8SEnji Cooper	atf_set "require.user" "unprivileged"
9357718be8SEnji Cooper	atf_set "descr" "Test writing to 'hw' sysctl node as an user"
9457718be8SEnji Cooper}
9557718be8SEnji Cooper
9657718be8SEnji Coopersysctl_hw_body() {
9757718be8SEnji Cooper	sysctl_write "hw"
9857718be8SEnji Cooper}
9957718be8SEnji Cooper
10057718be8SEnji Coopersysctl_hw_cleanup() {
10157718be8SEnji Cooper	clean
10257718be8SEnji Cooper}
10357718be8SEnji Cooper
10457718be8SEnji Cooper# kern.
10557718be8SEnji Cooper#
10657718be8SEnji Cooperatf_test_case sysctl_kern cleanup
10757718be8SEnji Coopersysctl_kern_head() {
10857718be8SEnji Cooper	atf_set "require.user" "unprivileged"
10957718be8SEnji Cooper	atf_set "descr" "Test writing to 'kern' " \
11057718be8SEnji Cooper		"sysctl node as an user (PR kern/44946)"
11157718be8SEnji Cooper}
11257718be8SEnji Cooper
11357718be8SEnji Coopersysctl_kern_body() {
11457718be8SEnji Cooper	sysctl_write "kern"
11557718be8SEnji Cooper}
11657718be8SEnji Cooper
11757718be8SEnji Coopersysctl_kern_cleanup() {
11857718be8SEnji Cooper	clean
11957718be8SEnji Cooper}
12057718be8SEnji Cooper
12157718be8SEnji Cooper# machdep.
12257718be8SEnji Cooper#
12357718be8SEnji Cooperatf_test_case sysctl_machdep cleanup
12457718be8SEnji Coopersysctl_machdep_head() {
12557718be8SEnji Cooper	atf_set "require.user" "unprivileged"
12657718be8SEnji Cooper	atf_set "descr" "Test writing to 'machdep' sysctl node as an user"
12757718be8SEnji Cooper}
12857718be8SEnji Cooper
12957718be8SEnji Coopersysctl_machdep_body() {
13057718be8SEnji Cooper	sysctl_write "machdep"
13157718be8SEnji Cooper}
13257718be8SEnji Cooper
13357718be8SEnji Coopersysctl_machdep_cleanup() {
13457718be8SEnji Cooper	clean
13557718be8SEnji Cooper}
13657718be8SEnji Cooper
13757718be8SEnji Cooper# net.
13857718be8SEnji Cooper#
13957718be8SEnji Cooperatf_test_case sysctl_net cleanup
14057718be8SEnji Coopersysctl_net_head() {
14157718be8SEnji Cooper	atf_set "require.user" "unprivileged"
14257718be8SEnji Cooper	atf_set "descr" "Test writing to 'net' sysctl node as an user"
14357718be8SEnji Cooper}
14457718be8SEnji Cooper
14557718be8SEnji Coopersysctl_net_body() {
14657718be8SEnji Cooper	sysctl_write "net"
14757718be8SEnji Cooper}
14857718be8SEnji Cooper
14957718be8SEnji Coopersysctl_net_cleanup() {
15057718be8SEnji Cooper	clean
15157718be8SEnji Cooper}
15257718be8SEnji Cooper
15357718be8SEnji Cooper# security.
15457718be8SEnji Cooper#
15557718be8SEnji Cooperatf_test_case sysctl_security cleanup
15657718be8SEnji Coopersysctl_security_head() {
15757718be8SEnji Cooper	atf_set "require.user" "unprivileged"
15857718be8SEnji Cooper	atf_set "descr" "Test writing to 'security' sysctl node as an user"
15957718be8SEnji Cooper}
16057718be8SEnji Cooper
16157718be8SEnji Coopersysctl_security_body() {
16257718be8SEnji Cooper	sysctl_write "security"
16357718be8SEnji Cooper}
16457718be8SEnji Cooper
16557718be8SEnji Coopersysctl_security_cleanup() {
16657718be8SEnji Cooper	clean
16757718be8SEnji Cooper}
16857718be8SEnji Cooper
16957718be8SEnji Cooper# vfs.
17057718be8SEnji Cooper#
17157718be8SEnji Cooperatf_test_case sysctl_vfs cleanup
17257718be8SEnji Coopersysctl_vfs_head() {
17357718be8SEnji Cooper	atf_set "require.user" "unprivileged"
17457718be8SEnji Cooper	atf_set "descr" "Test writing to 'vfs' sysctl node as an user"
17557718be8SEnji Cooper}
17657718be8SEnji Cooper
17757718be8SEnji Coopersysctl_vfs_body() {
17857718be8SEnji Cooper	sysctl_write "vfs"
17957718be8SEnji Cooper}
18057718be8SEnji Cooper
18157718be8SEnji Coopersysctl_vfs_cleanup() {
18257718be8SEnji Cooper	clean
18357718be8SEnji Cooper}
18457718be8SEnji Cooper
18557718be8SEnji Cooper# vm.
18657718be8SEnji Cooper#
18757718be8SEnji Cooperatf_test_case sysctl_vm cleanup
18857718be8SEnji Coopersysctl_vm_head() {
18957718be8SEnji Cooper	atf_set "require.user" "unprivileged"
19057718be8SEnji Cooper	atf_set "descr" "Test writing to 'vm' sysctl node as an user"
19157718be8SEnji Cooper}
19257718be8SEnji Cooper
19357718be8SEnji Coopersysctl_vm_body() {
19457718be8SEnji Cooper	sysctl_write "vm"
19557718be8SEnji Cooper}
19657718be8SEnji Cooper
19757718be8SEnji Coopersysctl_vm_cleanup() {
19857718be8SEnji Cooper	clean
19957718be8SEnji Cooper}
20057718be8SEnji Cooper
20157718be8SEnji Cooperatf_init_test_cases() {
20257718be8SEnji Cooper	atf_add_test_case sysctl_ddb
20357718be8SEnji Cooper	atf_add_test_case sysctl_hw
20457718be8SEnji Cooper	atf_add_test_case sysctl_kern
20557718be8SEnji Cooper	atf_add_test_case sysctl_machdep
20657718be8SEnji Cooper	atf_add_test_case sysctl_net
20757718be8SEnji Cooper	atf_add_test_case sysctl_security
20857718be8SEnji Cooper	atf_add_test_case sysctl_vfs
20957718be8SEnji Cooper	atf_add_test_case sysctl_vm
21057718be8SEnji Cooper}
211