summaryrefslogtreecommitdiffstats
path: root/tools/regression/acltools/aclfuzzer.sh
blob: dff07d8218d7722126b6af17c5f366c095a6f714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
#
# Copyright (c) 2008, 2009 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
# All rights reserved.
#
# 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 AUTHOR 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 AUTHOR 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.
#
# $FreeBSD$
#

# This is an NFSv4 ACL fuzzer.  It expects to be run by non-root in a scratch
# directory on a filesystem with NFSv4 ACLs support.  Output it generates
# is expected to be fed to /usr/src/tools/regression/acltools/run script.

NUMBER_OF_COMMANDS=300

run_command()
{
	echo "\$ $1"
	eval $1 2>&1 | sed 's/^/> /'
}

rnd_from_0_to()
{
	max=`expr $1 + 1`
	rnd=`jot -r 1`
	rnd=`expr $rnd % $max`

	echo $rnd
}

rnd_path()
{
	rnd=`rnd_from_0_to 3`
	case $rnd in
		0) echo "$TMP/aaa" ;;
		1) echo "$TMP/bbb" ;;
		2) echo "$TMP/aaa/ccc" ;;
		3) echo "$TMP/bbb/ddd" ;;
	esac
}

f_prepend_random_acl_on()
{
	rnd=`rnd_from_0_to 4`
	case $rnd in
		0) u="owner@" ;;
		1) u="group@" ;;
		2) u="everyone@" ;;
		3) u="u:1138" ;;
		4) u="g:1138" ;;
	esac

	p=""
	while :; do
		rnd=`rnd_from_0_to 30`
		if [ -n "$p" -a $rnd -ge 14 ]; then
			break;
		fi

		case $rnd in
			0) p="${p}r" ;;
			1) p="${p}w" ;;
			2) p="${p}x" ;;
			3) p="${p}p" ;;
			4) p="${p}d" ;;
			5) p="${p}D" ;;
			6) p="${p}a" ;;
			7) p="${p}A" ;;
			8) p="${p}R" ;;
			9) p="${p}W" ;;
			10) p="${p}R" ;;
			11) p="${p}c" ;;
			12) p="${p}C" ;;
			13) p="${p}o" ;;
			14) p="${p}s" ;;
		esac
	done

	f=""
	while :; do
		rnd=`rnd_from_0_to 10`
		if [ $rnd -ge 6 ]; then
			break;
		fi

		case $rnd in
			0) f="${f}f" ;;
			1) f="${f}d" ;;
			2) f="${f}n" ;;
			3) f="${f}i" ;;
		esac
	done

	rnd=`rnd_from_0_to 1`
	case $rnd in
		0) x="allow" ;;
		1) x="deny" ;;
	esac

	acl="$u:$p:$f:$x"

	file=`rnd_path`
	run_command "setfacl -a0 $acl $file"
}

f_getfacl()
{
	file=`rnd_path`
	run_command "getfacl -qn $file"
}

f_ls_mode()
{
	file=`rnd_path`
	run_command "ls -al $file | sed -n '2p' | cut -d' ' -f1"
}

f_chmod()
{
	b1=`rnd_from_0_to 7`
	b2=`rnd_from_0_to 7`
	b3=`rnd_from_0_to 7`
	b4=`rnd_from_0_to 7`
	file=`rnd_path`

	run_command "chmod $b1$b2$b3$b4 $file $2"
}

f_touch()
{
	file=`rnd_path`
	run_command "touch $file"
}

f_rm()
{
	file=`rnd_path`
	run_command "rm -f $file"
}

f_mkdir()
{
	file=`rnd_path`
	run_command "mkdir $file"
}

f_rmdir()
{
	file=`rnd_path`
	run_command "rmdir $file"
}

f_mv()
{
	from=`rnd_path`
	to=`rnd_path`
	run_command "mv -f $from $to"
}

# XXX: To be implemented: chown(8), setting times with touch(1).

switch_to_random_user()
{
	# XXX: To be implemented.
}

execute_random_command()
{
	rnd=`rnd_from_0_to 20`

	case $rnd in
		0|10|11|12|13|15) cmd=f_prepend_random_acl_on ;;
		1) cmd=f_getfacl ;;
		2) cmd=f_ls_mode ;;
		3) cmd=f_chmod ;;
		4|18|19) cmd=f_touch ;;
		5) cmd=f_rm ;;
		6|16|17) cmd=f_mkdir ;;
		7) cmd=f_rmdir ;;
		8) cmd=f_mv ;;
	esac

	$cmd "XXX"
}

echo "# Fuzzing; will stop after $NUMBER_OF_COMMANDS commands."
TMP="aclfuzzer_`dd if=/dev/random bs=1k count=1 2>/dev/null | openssl md5`"

run_command "whoami"
umask 022
run_command "umask 022"
run_command "mkdir $TMP"

i=0;
while [ "$i" -lt "$NUMBER_OF_COMMANDS" ]; do
	switch_to_random_user
	execute_random_command
	i=`expr $i + 1`
done

run_command "find $TMP -exec setfacl -a0 everyone@:rxd:allow {} \;"
run_command "rm -rfv $TMP"

echo "# Fuzzed, thank you."

OpenPOWER on IntegriCloud