t_fuzz.c revision 1.3 1 /* $NetBSD: t_fuzz.c,v 1.3 2010/08/16 10:46:20 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Fuzztest puffs mount. There are n different levels of testing:
31 * each one pours more and more sane garbage into the args to that
32 * the mount progresses further and further. Level 8 (at least when
33 * writing this comment) should be the one where mounting actually
34 * succeeds.
35 *
36 * Our metric of success is crash / no crash.
37 */
38
39 #include <sys/types.h>
40 #include <sys/mount.h>
41
42 #include <assert.h>
43 #include <atf-c.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <pthread.h>
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <stdlib.h>
52
53 #include <fs/puffs/puffs_msgif.h>
54
55 #include <rump/rump.h>
56 #include <rump/rump_syscalls.h>
57
58 #include "../../h_macros.h"
59
60 #define ITERATIONS 100
61
62 static void
63 fixversion(struct puffs_kargs *kargs)
64 {
65
66 kargs->pa_vers = PUFFSVERSION;
67 }
68
69 static void
70 fixkflag(struct puffs_kargs *kargs)
71 {
72
73 kargs->pa_flags &= PUFFS_KFLAG_MASK;
74 }
75
76 static void
77 fixfhflag(struct puffs_kargs *kargs)
78 {
79
80 kargs->pa_fhflags &= PUFFS_FHFLAG_MASK;
81 }
82
83 static void
84 fixspare(struct puffs_kargs *kargs)
85 {
86
87 memset(&kargs->pa_spare, 0, sizeof(kargs->pa_spare));
88 }
89
90 static void
91 fixhandsize(struct puffs_kargs *kargs)
92 {
93
94 kargs->pa_fhsize %= PUFFS_FHSIZE_MAX+4;
95 }
96
97 static void
98 fixhandsize2(struct puffs_kargs *kargs)
99 {
100
101 /* XXX: values */
102 if (kargs->pa_fhflags & PUFFS_FHFLAG_NFSV3)
103 kargs->pa_fhsize %= 60;
104 if (kargs->pa_fhflags & PUFFS_FHFLAG_NFSV2)
105 kargs->pa_fhsize %= 28;
106 }
107
108 static void
109 fixputter(struct puffs_kargs *kargs)
110 {
111
112 kargs->pa_fd = rump_sys_open("/dev/putter", O_RDWR);
113 if (kargs->pa_fd == -1)
114 atf_tc_fail_errno("open putter");
115 }
116
117 static void
118 fixroot(struct puffs_kargs *kargs)
119 {
120
121 kargs->pa_root_vtype %= VBAD;
122 }
123
124 static void
125 unfixputter(struct puffs_kargs *kargs)
126 {
127
128 rump_sys_close(kargs->pa_fd);
129 }
130
131 typedef void (*fixfn)(struct puffs_kargs *);
132 static fixfn fixstack[] = {
133 fixversion,
134 fixkflag,
135 fixfhflag,
136 fixspare,
137 fixhandsize,
138 fixhandsize2,
139 fixputter,
140 fixroot,
141 };
142
143 static void
144 fixup(int nfix, struct puffs_kargs *kargs)
145 {
146 int i;
147
148 assert(nfix <= __arraycount(fixstack));
149 for (i = 0; i < nfix; i++)
150 fixstack[i](kargs);
151 }
152
153 static void
154 unfixup(int nfix, struct puffs_kargs *kargs)
155 {
156
157 if (nfix >= 7)
158 unfixputter(kargs);
159 }
160
161 static pthread_mutex_t damtx;
162 static pthread_cond_t dacv;
163 static int dafd = -1;
164
165 static void *
166 respondthread(void *arg)
167 {
168 char buf[PUFFS_MSG_MAXSIZE];
169 struct puffs_req *preq = (void *)buf;
170 ssize_t n;
171
172 pthread_mutex_lock(&damtx);
173 for (;;) {
174 while (dafd == -1)
175 pthread_cond_wait(&dacv, &damtx);
176
177 while (dafd != -1) {
178 pthread_mutex_unlock(&damtx);
179 n = rump_sys_read(dafd, buf, sizeof(buf));
180 if (n <= 0) {
181 pthread_mutex_lock(&damtx);
182 break;
183 }
184
185 /* just say it was succesful */
186 preq->preq_rv = 0;
187 rump_sys_write(dafd, buf, n);
188 pthread_mutex_lock(&damtx);
189 }
190 }
191
192 return NULL;
193 }
194
195 static void
196 testbody(int nfix)
197 {
198 pthread_t pt;
199 struct puffs_kargs kargs;
200 unsigned long seed;
201 int i;
202
203 seed = time(NULL);
204 srandom(seed);
205 printf("test seeded RNG with %lu\n", seed);
206
207 pthread_mutex_init(&damtx, NULL);
208 pthread_cond_init(&dacv, NULL);
209 pthread_create(&pt, NULL, respondthread, NULL);
210
211 rump_init();
212 ATF_REQUIRE(rump_sys_mkdir("/mnt", 0777) == 0);
213
214 for (i = 0; i < ITERATIONS; i++) {
215 tests_makegarbage(&kargs, sizeof(kargs));
216 fixup(nfix, &kargs);
217 if (rump_sys_mount(MOUNT_PUFFS, "/mnt", 0,
218 &kargs, sizeof(kargs)) == 0) {
219 struct stat sb;
220
221 pthread_mutex_lock(&damtx);
222 dafd = kargs.pa_fd;
223 pthread_cond_signal(&dacv);
224 pthread_mutex_unlock(&damtx);
225
226 rump_sys_stat("/mnt", &sb);
227 rump_sys_unmount("/mnt", MNT_FORCE);
228 }
229 unfixup(nfix, &kargs);
230
231 pthread_mutex_lock(&damtx);
232 dafd = -1;
233 pthread_mutex_unlock(&damtx);
234 }
235 }
236
237 #define MAKETEST(_n_) \
238 ATF_TC(mountfuzz##_n_); \
239 ATF_TC_HEAD(mountfuzz##_n_, tc) \
240 {atf_tc_set_md_var(tc, "descr", "garbage kargs, " # _n_ " fix(es)");} \
241 ATF_TC_BODY(mountfuzz##_n_, tc) {testbody(_n_);}
242
243 MAKETEST(0);
244 MAKETEST(1);
245 MAKETEST(2);
246 MAKETEST(3);
247 MAKETEST(4);
248 MAKETEST(5);
249 MAKETEST(6);
250 MAKETEST(7);
251 MAKETEST(8);
252
253 ATF_TP_ADD_TCS(tp)
254 {
255
256 ATF_TP_ADD_TC(tp, mountfuzz0);
257 ATF_TP_ADD_TC(tp, mountfuzz1);
258 ATF_TP_ADD_TC(tp, mountfuzz2);
259 ATF_TP_ADD_TC(tp, mountfuzz3);
260 ATF_TP_ADD_TC(tp, mountfuzz4);
261 ATF_TP_ADD_TC(tp, mountfuzz5);
262 ATF_TP_ADD_TC(tp, mountfuzz6);
263 ATF_TP_ADD_TC(tp, mountfuzz7);
264 ATF_TP_ADD_TC(tp, mountfuzz8);
265
266 return atf_no_error();
267 }
268