1197434Strasz#!/bin/sh
2197434Strasz#
3197434Strasz# Copyright (c) 2008, 2009 Edward Tomasz Napiera��a <trasz@FreeBSD.org>
4197434Strasz# All rights reserved.
5197434Strasz#
6197434Strasz# Redistribution and use in source and binary forms, with or without
7197434Strasz# modification, are permitted provided that the following conditions
8197434Strasz# are met:
9197434Strasz# 1. Redistributions of source code must retain the above copyright
10197434Strasz#    notice, this list of conditions and the following disclaimer.
11197434Strasz# 2. Redistributions in binary form must reproduce the above copyright
12197434Strasz#    notice, this list of conditions and the following disclaimer in the
13197434Strasz#    documentation and/or other materials provided with the distribution.
14197434Strasz#
15197434Strasz# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16197434Strasz# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17197434Strasz# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18197434Strasz# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19197434Strasz# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20197434Strasz# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21197434Strasz# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22197434Strasz# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23197434Strasz# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24197434Strasz# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25197434Strasz# SUCH DAMAGE.
26197434Strasz#
27197434Strasz# $FreeBSD: stable/11/tests/sys/acl/aclfuzzer.sh 324404 2017-10-07 23:10:16Z ngie $
28197434Strasz#
29197434Strasz
30197434Strasz# This is an NFSv4 ACL fuzzer.  It expects to be run by non-root in a scratch
31197434Strasz# directory on a filesystem with NFSv4 ACLs support.  Output it generates
32197434Strasz# is expected to be fed to /usr/src/tools/regression/acltools/run script.
33197434Strasz
34197434StraszNUMBER_OF_COMMANDS=300
35197434Strasz
36197434Straszrun_command()
37197434Strasz{
38197434Strasz	echo "\$ $1"
39197434Strasz	eval $1 2>&1 | sed 's/^/> /'
40197434Strasz}
41197434Strasz
42197434Straszrnd_from_0_to()
43197434Strasz{
44197434Strasz	max=`expr $1 + 1`
45197434Strasz	rnd=`jot -r 1`
46197434Strasz	rnd=`expr $rnd % $max`
47197434Strasz
48197434Strasz	echo $rnd
49197434Strasz}
50197434Strasz
51197434Straszrnd_path()
52197434Strasz{
53197434Strasz	rnd=`rnd_from_0_to 3`
54197434Strasz	case $rnd in
55197434Strasz		0) echo "$TMP/aaa" ;;
56197434Strasz		1) echo "$TMP/bbb" ;;
57197434Strasz		2) echo "$TMP/aaa/ccc" ;;
58197434Strasz		3) echo "$TMP/bbb/ddd" ;;
59197434Strasz	esac
60197434Strasz}
61197434Strasz
62197434Straszf_prepend_random_acl_on()
63197434Strasz{
64197434Strasz	rnd=`rnd_from_0_to 4`
65197434Strasz	case $rnd in
66197434Strasz		0) u="owner@" ;;
67197434Strasz		1) u="group@" ;;
68197434Strasz		2) u="everyone@" ;;
69197434Strasz		3) u="u:1138" ;;
70197434Strasz		4) u="g:1138" ;;
71197434Strasz	esac
72197434Strasz
73197434Strasz	p=""
74197434Strasz	while :; do
75197434Strasz		rnd=`rnd_from_0_to 30`
76197434Strasz		if [ -n "$p" -a $rnd -ge 14 ]; then
77197434Strasz			break;
78197434Strasz		fi
79197434Strasz
80197434Strasz		case $rnd in
81197434Strasz			0) p="${p}r" ;;
82197434Strasz			1) p="${p}w" ;;
83197434Strasz			2) p="${p}x" ;;
84197434Strasz			3) p="${p}p" ;;
85197434Strasz			4) p="${p}d" ;;
86197434Strasz			5) p="${p}D" ;;
87197434Strasz			6) p="${p}a" ;;
88197434Strasz			7) p="${p}A" ;;
89197434Strasz			8) p="${p}R" ;;
90197434Strasz			9) p="${p}W" ;;
91197434Strasz			10) p="${p}R" ;;
92197434Strasz			11) p="${p}c" ;;
93197434Strasz			12) p="${p}C" ;;
94197434Strasz			13) p="${p}o" ;;
95197434Strasz			14) p="${p}s" ;;
96197434Strasz		esac
97197434Strasz	done
98197434Strasz
99197434Strasz	f=""
100197434Strasz	while :; do
101197434Strasz		rnd=`rnd_from_0_to 10`
102197434Strasz		if [ $rnd -ge 6 ]; then
103197434Strasz			break;
104197434Strasz		fi
105197434Strasz
106197434Strasz		case $rnd in
107197434Strasz			0) f="${f}f" ;;
108197434Strasz			1) f="${f}d" ;;
109197434Strasz			2) f="${f}n" ;;
110197434Strasz			3) f="${f}i" ;;
111197434Strasz		esac
112197434Strasz	done
113197434Strasz
114197434Strasz	rnd=`rnd_from_0_to 1`
115197434Strasz	case $rnd in
116197434Strasz		0) x="allow" ;;
117197434Strasz		1) x="deny" ;;
118197434Strasz	esac
119197434Strasz
120197434Strasz	acl="$u:$p:$f:$x"
121197434Strasz
122197434Strasz	file=`rnd_path`
123197434Strasz	run_command "setfacl -a0 $acl $file"
124197434Strasz}
125197434Strasz
126197434Straszf_getfacl()
127197434Strasz{
128197434Strasz	file=`rnd_path`
129197434Strasz	run_command "getfacl -qn $file"
130197434Strasz}
131197434Strasz
132197434Straszf_ls_mode()
133197434Strasz{
134197434Strasz	file=`rnd_path`
135197434Strasz	run_command "ls -al $file | sed -n '2p' | cut -d' ' -f1"
136197434Strasz}
137197434Strasz
138197434Straszf_chmod()
139197434Strasz{
140197434Strasz	b1=`rnd_from_0_to 7`
141197434Strasz	b2=`rnd_from_0_to 7`
142197434Strasz	b3=`rnd_from_0_to 7`
143197434Strasz	b4=`rnd_from_0_to 7`
144197434Strasz	file=`rnd_path`
145197434Strasz
146197434Strasz	run_command "chmod $b1$b2$b3$b4 $file $2"
147197434Strasz}
148197434Strasz
149197434Straszf_touch()
150197434Strasz{
151197434Strasz	file=`rnd_path`
152197434Strasz	run_command "touch $file"
153197434Strasz}
154197434Strasz
155197434Straszf_rm()
156197434Strasz{
157197434Strasz	file=`rnd_path`
158197434Strasz	run_command "rm -f $file"
159197434Strasz}
160197434Strasz
161197434Straszf_mkdir()
162197434Strasz{
163197434Strasz	file=`rnd_path`
164197434Strasz	run_command "mkdir $file"
165197434Strasz}
166197434Strasz
167197434Straszf_rmdir()
168197434Strasz{
169197434Strasz	file=`rnd_path`
170197434Strasz	run_command "rmdir $file"
171197434Strasz}
172197434Strasz
173197434Straszf_mv()
174197434Strasz{
175197434Strasz	from=`rnd_path`
176197434Strasz	to=`rnd_path`
177197434Strasz	run_command "mv -f $from $to"
178197434Strasz}
179197434Strasz
180197434Strasz# XXX: To be implemented: chown(8), setting times with touch(1).
181197434Strasz
182197434Straszswitch_to_random_user()
183197434Strasz{
184197434Strasz	# XXX: To be implemented.
185197434Strasz}
186197434Strasz
187197434Straszexecute_random_command()
188197434Strasz{
189197434Strasz	rnd=`rnd_from_0_to 20`
190197434Strasz
191197434Strasz	case $rnd in
192197434Strasz		0|10|11|12|13|15) cmd=f_prepend_random_acl_on ;;
193197434Strasz		1) cmd=f_getfacl ;;
194197434Strasz		2) cmd=f_ls_mode ;;
195197434Strasz		3) cmd=f_chmod ;;
196197434Strasz		4|18|19) cmd=f_touch ;;
197197434Strasz		5) cmd=f_rm ;;
198197434Strasz		6|16|17) cmd=f_mkdir ;;
199197434Strasz		7) cmd=f_rmdir ;;
200197434Strasz		8) cmd=f_mv ;;
201197434Strasz	esac
202197434Strasz
203197434Strasz	$cmd "XXX"
204197434Strasz}
205197434Strasz
206197434Straszecho "# Fuzzing; will stop after $NUMBER_OF_COMMANDS commands."
207197434StraszTMP="aclfuzzer_`dd if=/dev/random bs=1k count=1 2>/dev/null | openssl md5`"
208197434Strasz
209197434Straszrun_command "whoami"
210197434Straszumask 022
211197434Straszrun_command "umask 022"
212197434Straszrun_command "mkdir $TMP"
213197434Strasz
214197434Straszi=0;
215197434Straszwhile [ "$i" -lt "$NUMBER_OF_COMMANDS" ]; do
216197434Strasz	switch_to_random_user
217197434Strasz	execute_random_command
218197434Strasz	i=`expr $i + 1`
219197434Straszdone
220197434Strasz
221197434Straszrun_command "find $TMP -exec setfacl -a0 everyone@:rxd:allow {} \;"
222197434Straszrun_command "rm -rfv $TMP"
223197434Strasz
224197434Straszecho "# Fuzzed, thank you."
225197434Strasz
226