xref: /freebsd/contrib/netbsd-tests/sbin/sysctl/t_perm.sh (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
1# $NetBSD: t_perm.sh,v 1.7 2016/06/17 03:55:35 pgoyette Exp $
2#
3# Copyright (c) 2011 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jukka Ruohonen.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30file="/tmp/d_sysctl.out"
31
32clean() {
33
34	if [ -f $file ]; then
35		rm $file
36	fi
37}
38
39sysctl_write() {
40
41	deadbeef="3735928559"
42	deadbeef_signed="-559038737"
43
44	sysctl $1 | cut -d= -f1 > $file
45
46	if [ ! -f $file ]; then
47		atf_fail "sysctl failed"
48	fi
49
50	while read line; do
51
52		node=$(echo $line)
53
54		case $node in
55
56		"$1."*)
57			atf_check -s not-exit:0 -e ignore \
58				-x sysctl -w $node=$deadbeef
59			;;
60		esac
61
62	done < $file
63
64	# A functional verification that $deadbeef
65	# was not actually written to the node.
66	#
67	if [ ! -z $(sysctl $1 | grep -e $deadbeef -e $deadbeef_signed) ]; then
68		atf_fail "value was written"
69	fi
70}
71
72# ddb.
73#
74atf_test_case sysctl_ddb cleanup
75sysctl_ddb_head() {
76	atf_set "require.user" "unprivileged"
77	atf_set "descr" "Test writing to 'ddb' sysctl node as an user"
78}
79
80sysctl_ddb_body() {
81	sysctl_write "ddb"
82}
83
84sysctl_ddb_cleanup() {
85	clean
86}
87
88# hw.
89#
90atf_test_case sysctl_hw cleanup
91sysctl_hw_head() {
92	atf_set "require.user" "unprivileged"
93	atf_set "descr" "Test writing to 'hw' sysctl node as an user"
94}
95
96sysctl_hw_body() {
97	sysctl_write "hw"
98}
99
100sysctl_hw_cleanup() {
101	clean
102}
103
104# kern.
105#
106atf_test_case sysctl_kern cleanup
107sysctl_kern_head() {
108	atf_set "require.user" "unprivileged"
109	atf_set "descr" "Test writing to 'kern' " \
110		"sysctl node as an user (PR kern/44946)"
111}
112
113sysctl_kern_body() {
114	sysctl_write "kern"
115}
116
117sysctl_kern_cleanup() {
118	clean
119}
120
121# machdep.
122#
123atf_test_case sysctl_machdep cleanup
124sysctl_machdep_head() {
125	atf_set "require.user" "unprivileged"
126	atf_set "descr" "Test writing to 'machdep' sysctl node as an user"
127}
128
129sysctl_machdep_body() {
130	sysctl_write "machdep"
131}
132
133sysctl_machdep_cleanup() {
134	clean
135}
136
137# net.
138#
139atf_test_case sysctl_net cleanup
140sysctl_net_head() {
141	atf_set "require.user" "unprivileged"
142	atf_set "descr" "Test writing to 'net' sysctl node as an user"
143}
144
145sysctl_net_body() {
146	sysctl_write "net"
147}
148
149sysctl_net_cleanup() {
150	clean
151}
152
153# security.
154#
155atf_test_case sysctl_security cleanup
156sysctl_security_head() {
157	atf_set "require.user" "unprivileged"
158	atf_set "descr" "Test writing to 'security' sysctl node as an user"
159}
160
161sysctl_security_body() {
162	sysctl_write "security"
163}
164
165sysctl_security_cleanup() {
166	clean
167}
168
169# vfs.
170#
171atf_test_case sysctl_vfs cleanup
172sysctl_vfs_head() {
173	atf_set "require.user" "unprivileged"
174	atf_set "descr" "Test writing to 'vfs' sysctl node as an user"
175}
176
177sysctl_vfs_body() {
178	sysctl_write "vfs"
179}
180
181sysctl_vfs_cleanup() {
182	clean
183}
184
185# vm.
186#
187atf_test_case sysctl_vm cleanup
188sysctl_vm_head() {
189	atf_set "require.user" "unprivileged"
190	atf_set "descr" "Test writing to 'vm' sysctl node as an user"
191}
192
193sysctl_vm_body() {
194	sysctl_write "vm"
195}
196
197sysctl_vm_cleanup() {
198	clean
199}
200
201atf_init_test_cases() {
202	atf_add_test_case sysctl_ddb
203	atf_add_test_case sysctl_hw
204	atf_add_test_case sysctl_kern
205	atf_add_test_case sysctl_machdep
206	atf_add_test_case sysctl_net
207	atf_add_test_case sysctl_security
208	atf_add_test_case sysctl_vfs
209	atf_add_test_case sysctl_vm
210}
211