1313498Sngie# $NetBSD: t_perm.sh,v 1.7 2016/06/17 03:55:35 pgoyette Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2011 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# This code is derived from software contributed to The NetBSD Foundation
7272343Sngie# by Jukka Ruohonen.
8272343Sngie#
9272343Sngie# Redistribution and use in source and binary forms, with or without
10272343Sngie# modification, are permitted provided that the following conditions
11272343Sngie# are met:
12272343Sngie# 1. Redistributions of source code must retain the above copyright
13272343Sngie#    notice, this list of conditions and the following disclaimer.
14272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
15272343Sngie#    notice, this list of conditions and the following disclaimer in the
16272343Sngie#    documentation and/or other materials provided with the distribution.
17272343Sngie#
18272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28272343Sngie# POSSIBILITY OF SUCH DAMAGE.
29272343Sngie#
30272343Sngiefile="/tmp/d_sysctl.out"
31272343Sngie
32272343Sngieclean() {
33272343Sngie
34272343Sngie	if [ -f $file ]; then
35272343Sngie		rm $file
36272343Sngie	fi
37272343Sngie}
38272343Sngie
39272343Sngiesysctl_write() {
40272343Sngie
41272343Sngie	deadbeef="3735928559"
42313498Sngie	deadbeef_signed="-559038737"
43272343Sngie
44272343Sngie	sysctl $1 | cut -d= -f1 > $file
45272343Sngie
46272343Sngie	if [ ! -f $file ]; then
47272343Sngie		atf_fail "sysctl failed"
48272343Sngie	fi
49272343Sngie
50272343Sngie	while read line; do
51272343Sngie
52272343Sngie		node=$(echo $line)
53272343Sngie
54272343Sngie		case $node in
55272343Sngie
56272343Sngie		"$1."*)
57272343Sngie			atf_check -s not-exit:0 -e ignore \
58272343Sngie				-x sysctl -w $node=$deadbeef
59272343Sngie			;;
60272343Sngie		esac
61272343Sngie
62272343Sngie	done < $file
63272343Sngie
64272343Sngie	# A functional verification that $deadbeef
65272343Sngie	# was not actually written to the node.
66272343Sngie	#
67313498Sngie	if [ ! -z $(sysctl $1 | grep -e $deadbeef -e $deadbeef_signed) ]; then
68272343Sngie		atf_fail "value was written"
69272343Sngie	fi
70272343Sngie}
71272343Sngie
72272343Sngie# ddb.
73272343Sngie#
74272343Sngieatf_test_case sysctl_ddb cleanup
75272343Sngiesysctl_ddb_head() {
76272343Sngie	atf_set "require.user" "unprivileged"
77272343Sngie	atf_set "descr" "Test writing to 'ddb' sysctl node as an user"
78272343Sngie}
79272343Sngie
80272343Sngiesysctl_ddb_body() {
81272343Sngie	sysctl_write "ddb"
82272343Sngie}
83272343Sngie
84272343Sngiesysctl_ddb_cleanup() {
85272343Sngie	clean
86272343Sngie}
87272343Sngie
88272343Sngie# hw.
89272343Sngie#
90272343Sngieatf_test_case sysctl_hw cleanup
91272343Sngiesysctl_hw_head() {
92272343Sngie	atf_set "require.user" "unprivileged"
93272343Sngie	atf_set "descr" "Test writing to 'hw' sysctl node as an user"
94272343Sngie}
95272343Sngie
96272343Sngiesysctl_hw_body() {
97272343Sngie	sysctl_write "hw"
98272343Sngie}
99272343Sngie
100272343Sngiesysctl_hw_cleanup() {
101272343Sngie	clean
102272343Sngie}
103272343Sngie
104272343Sngie# kern.
105272343Sngie#
106272343Sngieatf_test_case sysctl_kern cleanup
107272343Sngiesysctl_kern_head() {
108272343Sngie	atf_set "require.user" "unprivileged"
109272343Sngie	atf_set "descr" "Test writing to 'kern' " \
110272343Sngie		"sysctl node as an user (PR kern/44946)"
111272343Sngie}
112272343Sngie
113272343Sngiesysctl_kern_body() {
114272343Sngie	sysctl_write "kern"
115272343Sngie}
116272343Sngie
117272343Sngiesysctl_kern_cleanup() {
118272343Sngie	clean
119272343Sngie}
120272343Sngie
121272343Sngie# machdep.
122272343Sngie#
123272343Sngieatf_test_case sysctl_machdep cleanup
124272343Sngiesysctl_machdep_head() {
125272343Sngie	atf_set "require.user" "unprivileged"
126272343Sngie	atf_set "descr" "Test writing to 'machdep' sysctl node as an user"
127272343Sngie}
128272343Sngie
129272343Sngiesysctl_machdep_body() {
130272343Sngie	sysctl_write "machdep"
131272343Sngie}
132272343Sngie
133272343Sngiesysctl_machdep_cleanup() {
134272343Sngie	clean
135272343Sngie}
136272343Sngie
137272343Sngie# net.
138272343Sngie#
139272343Sngieatf_test_case sysctl_net cleanup
140272343Sngiesysctl_net_head() {
141272343Sngie	atf_set "require.user" "unprivileged"
142272343Sngie	atf_set "descr" "Test writing to 'net' sysctl node as an user"
143272343Sngie}
144272343Sngie
145272343Sngiesysctl_net_body() {
146272343Sngie	sysctl_write "net"
147272343Sngie}
148272343Sngie
149272343Sngiesysctl_net_cleanup() {
150272343Sngie	clean
151272343Sngie}
152272343Sngie
153272343Sngie# security.
154272343Sngie#
155272343Sngieatf_test_case sysctl_security cleanup
156272343Sngiesysctl_security_head() {
157272343Sngie	atf_set "require.user" "unprivileged"
158272343Sngie	atf_set "descr" "Test writing to 'security' sysctl node as an user"
159272343Sngie}
160272343Sngie
161272343Sngiesysctl_security_body() {
162272343Sngie	sysctl_write "security"
163272343Sngie}
164272343Sngie
165272343Sngiesysctl_security_cleanup() {
166272343Sngie	clean
167272343Sngie}
168272343Sngie
169272343Sngie# vfs.
170272343Sngie#
171272343Sngieatf_test_case sysctl_vfs cleanup
172272343Sngiesysctl_vfs_head() {
173272343Sngie	atf_set "require.user" "unprivileged"
174272343Sngie	atf_set "descr" "Test writing to 'vfs' sysctl node as an user"
175272343Sngie}
176272343Sngie
177272343Sngiesysctl_vfs_body() {
178272343Sngie	sysctl_write "vfs"
179272343Sngie}
180272343Sngie
181272343Sngiesysctl_vfs_cleanup() {
182272343Sngie	clean
183272343Sngie}
184272343Sngie
185272343Sngie# vm.
186272343Sngie#
187272343Sngieatf_test_case sysctl_vm cleanup
188272343Sngiesysctl_vm_head() {
189272343Sngie	atf_set "require.user" "unprivileged"
190272343Sngie	atf_set "descr" "Test writing to 'vm' sysctl node as an user"
191272343Sngie}
192272343Sngie
193272343Sngiesysctl_vm_body() {
194272343Sngie	sysctl_write "vm"
195272343Sngie}
196272343Sngie
197272343Sngiesysctl_vm_cleanup() {
198272343Sngie	clean
199272343Sngie}
200272343Sngie
201272343Sngieatf_init_test_cases() {
202272343Sngie	atf_add_test_case sysctl_ddb
203272343Sngie	atf_add_test_case sysctl_hw
204272343Sngie	atf_add_test_case sysctl_kern
205272343Sngie	atf_add_test_case sysctl_machdep
206272343Sngie	atf_add_test_case sysctl_net
207272343Sngie	atf_add_test_case sysctl_security
208272343Sngie	atf_add_test_case sysctl_vfs
209272343Sngie	atf_add_test_case sysctl_vm
210272343Sngie}
211