1*63d1fd59SEnji Cooper /* $NetBSD: t_fuzz.c,v 1.6 2017/01/13 21:30:40 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper
2957718be8SEnji Cooper /*
3057718be8SEnji Cooper * Fuzztest puffs mount. There are n different levels of testing:
3157718be8SEnji Cooper * each one pours more and more sane garbage into the args to that
3257718be8SEnji Cooper * the mount progresses further and further. Level 8 (at least when
3357718be8SEnji Cooper * writing this comment) should be the one where mounting actually
3457718be8SEnji Cooper * succeeds.
3557718be8SEnji Cooper *
3657718be8SEnji Cooper * Our metric of success is crash / no crash.
3757718be8SEnji Cooper */
3857718be8SEnji Cooper
3957718be8SEnji Cooper #include <sys/types.h>
4057718be8SEnji Cooper #include <sys/mount.h>
4157718be8SEnji Cooper #include <sys/poll.h>
4257718be8SEnji Cooper
4357718be8SEnji Cooper #include <assert.h>
4457718be8SEnji Cooper #include <atf-c.h>
4557718be8SEnji Cooper #include <err.h>
4657718be8SEnji Cooper #include <errno.h>
4757718be8SEnji Cooper #include <fcntl.h>
4857718be8SEnji Cooper #include <pthread.h>
4957718be8SEnji Cooper #include <stdio.h>
5057718be8SEnji Cooper #include <unistd.h>
5157718be8SEnji Cooper #include <string.h>
5257718be8SEnji Cooper #include <stdlib.h>
5357718be8SEnji Cooper
5457718be8SEnji Cooper #include <fs/puffs/puffs_msgif.h>
5557718be8SEnji Cooper
5657718be8SEnji Cooper #include <rump/rump.h>
5757718be8SEnji Cooper #include <rump/rump_syscalls.h>
5857718be8SEnji Cooper
59*63d1fd59SEnji Cooper #include "h_macros.h"
6057718be8SEnji Cooper
6157718be8SEnji Cooper #define ITERATIONS 100
6257718be8SEnji Cooper
6357718be8SEnji Cooper static void
fixversion(struct puffs_kargs * kargs)6457718be8SEnji Cooper fixversion(struct puffs_kargs *kargs)
6557718be8SEnji Cooper {
6657718be8SEnji Cooper
6757718be8SEnji Cooper kargs->pa_vers = PUFFSVERSION;
6857718be8SEnji Cooper }
6957718be8SEnji Cooper
7057718be8SEnji Cooper static void
fixkflag(struct puffs_kargs * kargs)7157718be8SEnji Cooper fixkflag(struct puffs_kargs *kargs)
7257718be8SEnji Cooper {
7357718be8SEnji Cooper
7457718be8SEnji Cooper kargs->pa_flags &= PUFFS_KFLAG_MASK;
7557718be8SEnji Cooper
7657718be8SEnji Cooper /*
7757718be8SEnji Cooper * PUFFS_KFLAG_CACHE_FS_TTL require extended behavior
7857718be8SEnji Cooper * from the filesystem for which we have no test right now.
7957718be8SEnji Cooper */
8057718be8SEnji Cooper kargs->pa_flags &= ~PUFFS_KFLAG_CACHE_FS_TTL;
8157718be8SEnji Cooper }
8257718be8SEnji Cooper
8357718be8SEnji Cooper static void
fixfhflag(struct puffs_kargs * kargs)8457718be8SEnji Cooper fixfhflag(struct puffs_kargs *kargs)
8557718be8SEnji Cooper {
8657718be8SEnji Cooper
8757718be8SEnji Cooper kargs->pa_fhflags &= PUFFS_FHFLAG_MASK;
8857718be8SEnji Cooper }
8957718be8SEnji Cooper
9057718be8SEnji Cooper static void
fixspare(struct puffs_kargs * kargs)9157718be8SEnji Cooper fixspare(struct puffs_kargs *kargs)
9257718be8SEnji Cooper {
9357718be8SEnji Cooper
9457718be8SEnji Cooper memset(&kargs->pa_spare, 0, sizeof(kargs->pa_spare));
9557718be8SEnji Cooper }
9657718be8SEnji Cooper
9757718be8SEnji Cooper static void
fixhandsize(struct puffs_kargs * kargs)9857718be8SEnji Cooper fixhandsize(struct puffs_kargs *kargs)
9957718be8SEnji Cooper {
10057718be8SEnji Cooper
10157718be8SEnji Cooper kargs->pa_fhsize %= PUFFS_FHSIZE_MAX+4;
10257718be8SEnji Cooper }
10357718be8SEnji Cooper
10457718be8SEnji Cooper static void
fixhandsize2(struct puffs_kargs * kargs)10557718be8SEnji Cooper fixhandsize2(struct puffs_kargs *kargs)
10657718be8SEnji Cooper {
10757718be8SEnji Cooper
10857718be8SEnji Cooper /* XXX: values */
10957718be8SEnji Cooper if (kargs->pa_fhflags & PUFFS_FHFLAG_NFSV3)
11057718be8SEnji Cooper kargs->pa_fhsize %= 60;
11157718be8SEnji Cooper if (kargs->pa_fhflags & PUFFS_FHFLAG_NFSV2)
11257718be8SEnji Cooper kargs->pa_fhsize %= 28;
11357718be8SEnji Cooper }
11457718be8SEnji Cooper
11557718be8SEnji Cooper static void
fixputter(struct puffs_kargs * kargs)11657718be8SEnji Cooper fixputter(struct puffs_kargs *kargs)
11757718be8SEnji Cooper {
11857718be8SEnji Cooper
11957718be8SEnji Cooper kargs->pa_fd = rump_sys_open("/dev/putter", O_RDWR);
12057718be8SEnji Cooper if (kargs->pa_fd == -1)
12157718be8SEnji Cooper atf_tc_fail_errno("open putter");
12257718be8SEnji Cooper }
12357718be8SEnji Cooper
12457718be8SEnji Cooper static void
fixroot(struct puffs_kargs * kargs)12557718be8SEnji Cooper fixroot(struct puffs_kargs *kargs)
12657718be8SEnji Cooper {
12757718be8SEnji Cooper
12857718be8SEnji Cooper kargs->pa_root_vtype %= VBAD;
12957718be8SEnji Cooper }
13057718be8SEnji Cooper
13157718be8SEnji Cooper static void
unfixputter(struct puffs_kargs * kargs)13257718be8SEnji Cooper unfixputter(struct puffs_kargs *kargs)
13357718be8SEnji Cooper {
13457718be8SEnji Cooper
13557718be8SEnji Cooper rump_sys_close(kargs->pa_fd);
13657718be8SEnji Cooper }
13757718be8SEnji Cooper
13857718be8SEnji Cooper typedef void (*fixfn)(struct puffs_kargs *);
13957718be8SEnji Cooper static fixfn fixstack[] = {
14057718be8SEnji Cooper fixversion,
14157718be8SEnji Cooper fixkflag,
14257718be8SEnji Cooper fixfhflag,
14357718be8SEnji Cooper fixspare,
14457718be8SEnji Cooper fixhandsize,
14557718be8SEnji Cooper fixhandsize2,
14657718be8SEnji Cooper fixputter,
14757718be8SEnji Cooper fixroot,
14857718be8SEnji Cooper };
14957718be8SEnji Cooper
15057718be8SEnji Cooper static void
fixup(int nfix,struct puffs_kargs * kargs)15157718be8SEnji Cooper fixup(int nfix, struct puffs_kargs *kargs)
15257718be8SEnji Cooper {
15357718be8SEnji Cooper int i;
15457718be8SEnji Cooper
15557718be8SEnji Cooper assert(nfix <= __arraycount(fixstack));
15657718be8SEnji Cooper for (i = 0; i < nfix; i++)
15757718be8SEnji Cooper fixstack[i](kargs);
15857718be8SEnji Cooper }
15957718be8SEnji Cooper
16057718be8SEnji Cooper static void
unfixup(int nfix,struct puffs_kargs * kargs)16157718be8SEnji Cooper unfixup(int nfix, struct puffs_kargs *kargs)
16257718be8SEnji Cooper {
16357718be8SEnji Cooper
16457718be8SEnji Cooper if (nfix >= 7)
16557718be8SEnji Cooper unfixputter(kargs);
16657718be8SEnji Cooper }
16757718be8SEnji Cooper
16857718be8SEnji Cooper static pthread_mutex_t damtx;
16957718be8SEnji Cooper static pthread_cond_t dacv;
17057718be8SEnji Cooper static int dafd = -1;
17157718be8SEnji Cooper
17257718be8SEnji Cooper static void *
respondthread(void * arg)17357718be8SEnji Cooper respondthread(void *arg)
17457718be8SEnji Cooper {
17557718be8SEnji Cooper char buf[PUFFS_MSG_MAXSIZE];
17657718be8SEnji Cooper struct puffs_req *preq = (void *)buf;
17757718be8SEnji Cooper struct pollfd pfd;
17857718be8SEnji Cooper ssize_t n;
17957718be8SEnji Cooper
18057718be8SEnji Cooper pthread_mutex_lock(&damtx);
18157718be8SEnji Cooper for (;;) {
18257718be8SEnji Cooper while (dafd == -1)
18357718be8SEnji Cooper pthread_cond_wait(&dacv, &damtx);
18457718be8SEnji Cooper
18557718be8SEnji Cooper while (dafd != -1) {
18657718be8SEnji Cooper pthread_mutex_unlock(&damtx);
18757718be8SEnji Cooper pfd.fd = dafd;
18857718be8SEnji Cooper pfd.events = POLLIN;
18957718be8SEnji Cooper pfd.revents = 0;
19057718be8SEnji Cooper if (rump_sys_poll(&pfd, 1, 10) == 0) {
19157718be8SEnji Cooper pthread_mutex_lock(&damtx);
19257718be8SEnji Cooper continue;
19357718be8SEnji Cooper }
19457718be8SEnji Cooper n = rump_sys_read(dafd, buf, sizeof(buf));
19557718be8SEnji Cooper if (n <= 0) {
19657718be8SEnji Cooper pthread_mutex_lock(&damtx);
19757718be8SEnji Cooper break;
19857718be8SEnji Cooper }
19957718be8SEnji Cooper
20057718be8SEnji Cooper /* just say it was succesful */
20157718be8SEnji Cooper preq->preq_rv = 0;
20257718be8SEnji Cooper rump_sys_write(dafd, buf, n);
20357718be8SEnji Cooper pthread_mutex_lock(&damtx);
20457718be8SEnji Cooper }
20557718be8SEnji Cooper }
20657718be8SEnji Cooper
20757718be8SEnji Cooper return NULL;
20857718be8SEnji Cooper }
20957718be8SEnji Cooper
21057718be8SEnji Cooper static void
testbody(int nfix)21157718be8SEnji Cooper testbody(int nfix)
21257718be8SEnji Cooper {
21357718be8SEnji Cooper pthread_t pt;
21457718be8SEnji Cooper struct puffs_kargs kargs;
21557718be8SEnji Cooper unsigned long seed;
21657718be8SEnji Cooper int i;
21757718be8SEnji Cooper
21857718be8SEnji Cooper seed = time(NULL);
21957718be8SEnji Cooper srandom(seed);
22057718be8SEnji Cooper printf("test seeded RNG with %lu\n", seed);
22157718be8SEnji Cooper
22257718be8SEnji Cooper rump_init();
22357718be8SEnji Cooper
22457718be8SEnji Cooper pthread_mutex_init(&damtx, NULL);
22557718be8SEnji Cooper pthread_cond_init(&dacv, NULL);
22657718be8SEnji Cooper pthread_create(&pt, NULL, respondthread, NULL);
22757718be8SEnji Cooper
22857718be8SEnji Cooper ATF_REQUIRE(rump_sys_mkdir("/mnt", 0777) == 0);
22957718be8SEnji Cooper
23057718be8SEnji Cooper for (i = 0; i < ITERATIONS; i++) {
23157718be8SEnji Cooper tests_makegarbage(&kargs, sizeof(kargs));
23257718be8SEnji Cooper fixup(nfix, &kargs);
23357718be8SEnji Cooper if (rump_sys_mount(MOUNT_PUFFS, "/mnt", 0,
23457718be8SEnji Cooper &kargs, sizeof(kargs)) == 0) {
23557718be8SEnji Cooper struct stat sb;
23657718be8SEnji Cooper
23757718be8SEnji Cooper pthread_mutex_lock(&damtx);
23857718be8SEnji Cooper dafd = kargs.pa_fd;
23957718be8SEnji Cooper pthread_cond_signal(&dacv);
24057718be8SEnji Cooper pthread_mutex_unlock(&damtx);
24157718be8SEnji Cooper
24257718be8SEnji Cooper rump_sys_stat("/mnt", &sb);
24357718be8SEnji Cooper rump_sys_unmount("/mnt", MNT_FORCE);
24457718be8SEnji Cooper }
24557718be8SEnji Cooper unfixup(nfix, &kargs);
24657718be8SEnji Cooper
24757718be8SEnji Cooper pthread_mutex_lock(&damtx);
24857718be8SEnji Cooper dafd = -1;
24957718be8SEnji Cooper pthread_mutex_unlock(&damtx);
25057718be8SEnji Cooper }
25157718be8SEnji Cooper }
25257718be8SEnji Cooper
25357718be8SEnji Cooper #define MAKETEST(_n_) \
25457718be8SEnji Cooper ATF_TC(mountfuzz##_n_); \
25557718be8SEnji Cooper ATF_TC_HEAD(mountfuzz##_n_, tc) \
25657718be8SEnji Cooper {atf_tc_set_md_var(tc, "descr", "garbage kargs, " # _n_ " fix(es)");} \
25757718be8SEnji Cooper ATF_TC_BODY(mountfuzz##_n_, tc) {testbody(_n_);}
25857718be8SEnji Cooper
25957718be8SEnji Cooper MAKETEST(0);
26057718be8SEnji Cooper MAKETEST(1);
26157718be8SEnji Cooper MAKETEST(2);
26257718be8SEnji Cooper MAKETEST(3);
26357718be8SEnji Cooper MAKETEST(4);
26457718be8SEnji Cooper MAKETEST(5);
26557718be8SEnji Cooper MAKETEST(6);
26657718be8SEnji Cooper MAKETEST(7);
26757718be8SEnji Cooper MAKETEST(8);
26857718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)26957718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
27057718be8SEnji Cooper {
27157718be8SEnji Cooper
27257718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz0);
27357718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz1);
27457718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz2);
27557718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz3);
27657718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz4);
27757718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz5);
27857718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz6);
27957718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz7);
28057718be8SEnji Cooper ATF_TP_ADD_TC(tp, mountfuzz8);
28157718be8SEnji Cooper
28257718be8SEnji Cooper return atf_no_error();
28357718be8SEnji Cooper }
284