xref: /freebsd/tools/test/stress2/misc/pread.sh (revision 088cc7d221bb0743fc5ec12de983559b812366bd)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# pread(2) fuzzing inspired by the iknowthis test suite
308a272653SPeter Holm# by Tavis Ormandy <taviso  cmpxchg8b com>
318a272653SPeter Holm
328a272653SPeter Holm# Fixed in r227527.
338a272653SPeter Holm
348a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
358a272653SPeter Holm
368a272653SPeter Holm. ../default.cfg
378a272653SPeter Holm
388a272653SPeter Holmhere=`pwd`
398a272653SPeter Holmcd /tmp
408a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > pread.c
418a272653SPeter Holmmycc -o pread -Wall -Wextra pread.c
428a272653SPeter Holmrm -f pread.c
438a272653SPeter Holm
448a272653SPeter Holmmount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
458a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
468a272653SPeter Holm
478a272653SPeter Holmmount -t tmpfs tmpfs $mntpoint
488a272653SPeter Holmcp -a /usr/include $mntpoint
49*088cc7d2SAlexander Ziaeeecho "Testing tmpfs(4)"
508a272653SPeter Holm/tmp/pread $mntpoint
518a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
528a272653SPeter Holm	umount $mntpoint || sleep 1
538a272653SPeter Holmdone
548a272653SPeter Holm
55*088cc7d2SAlexander Ziaeeecho "Testing fdescfs(4)"
568a272653SPeter Holmmount -t fdescfs null /dev/fd
578a272653SPeter Holmfor i in `jot 100`; do
588a272653SPeter Holm	/tmp/pread /dev/fd
598a272653SPeter Holmdone
608a272653SPeter Holm
618a272653SPeter Holmwhile mount | grep -q "on /dev/fd "; do
628a272653SPeter Holm	umount /dev/fd || sleep 1
638a272653SPeter Holmdone
648a272653SPeter Holm
65*088cc7d2SAlexander Ziaeeecho "Testing procfs(4)"
668a272653SPeter Holmmount -t procfs procfs $mntpoint
678a272653SPeter Holm/tmp/pread $mntpoint
688a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
698a272653SPeter Holm	umount $mntpoint || sleep 1
708a272653SPeter Holmdone
718a272653SPeter Holm
728a272653SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart || exit 1
73608c97bfSPeter Holmnewfs $newfs_flags md$mdstart > /dev/null
74608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
758a272653SPeter Holmcp -a /usr/include $mntpoint
768a272653SPeter Holmecho "Testing FFS"
778a272653SPeter Holm/tmp/pread $mntpoint
788a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
798a272653SPeter Holm	umount $mntpoint || sleep 1
808a272653SPeter Holmdone
818a272653SPeter Holmmdconfig -d -u $mdstart
828a272653SPeter Holm
838a272653SPeter Holmmount -t nullfs /bin $mntpoint
84*088cc7d2SAlexander Ziaeeecho "Testing nullfs(4)"
858a272653SPeter Holm/tmp/pread $mntpoint
868a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
878a272653SPeter Holm	umount $mntpoint || sleep 1
888a272653SPeter Holmdone
898a272653SPeter Holm
90*088cc7d2SAlexander Ziaeeecho "Testing procfs(4)"
918a272653SPeter Holmmount -t procfs procfs $mntpoint
928a272653SPeter Holm/tmp/pread $mntpoint
938a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
948a272653SPeter Holm	umount $mntpoint || sleep 1
958a272653SPeter Holmdone
968a272653SPeter Holm
978a272653SPeter Holmecho "Testing devfs(8)"
988a272653SPeter Holmmount -t devfs devfs $mntpoint
998a272653SPeter Holm/tmp/pread $mntpoint
1008a272653SPeter Holmwhile mount | grep -q "on $mntpoint "; do
1018a272653SPeter Holm	umount $mntpoint || sleep 1
1028a272653SPeter Holmdone
1038a272653SPeter Holm
1048a272653SPeter Holmrm -f /tmp/pread
1058a272653SPeter Holmexit 0
1068a272653SPeter HolmEOF
1078a272653SPeter Holm#include <sys/types.h>
1088a272653SPeter Holm#include <strings.h>
1098a272653SPeter Holm#include <dirent.h>
1108a272653SPeter Holm#include <err.h>
1118a272653SPeter Holm#include <errno.h>
1128a272653SPeter Holm#include <fcntl.h>
1138a272653SPeter Holm#include <fts.h>
1148a272653SPeter Holm#include <pwd.h>
1158a272653SPeter Holm#include <signal.h>
1168a272653SPeter Holm#include <stdio.h>
1178a272653SPeter Holm#include <stdlib.h>
1188a272653SPeter Holm#include <sys/uio.h>
1198a272653SPeter Holm#include <unistd.h>
1208a272653SPeter Holm#include <sys/wait.h>
1218a272653SPeter Holm
1228a272653SPeter Holmstatic void
1238a272653SPeter Holmhand(int i __unused) {	/* handler */
1248a272653SPeter Holm	_exit(1);
1258a272653SPeter Holm}
1268a272653SPeter Holm
1278a272653SPeter Holmint
1288a272653SPeter Holmtest(char *path)
1298a272653SPeter Holm{
1308a272653SPeter Holm
1318a272653SPeter Holm	FTS		*fts;
1328a272653SPeter Holm	FTSENT		*p;
1338a272653SPeter Holm	int		ftsoptions;
1348a272653SPeter Holm	char		*args[2];
1358a272653SPeter Holm	int buf[64], fd;
1368a272653SPeter Holm
1378a272653SPeter Holm	signal(SIGSEGV, hand);
1388a272653SPeter Holm	signal(SIGABRT, hand);
1398a272653SPeter Holm	ftsoptions = FTS_PHYSICAL;
1408a272653SPeter Holm	args[0] = path;
1418a272653SPeter Holm	args[1] = 0;
1428a272653SPeter Holm
1438a272653SPeter Holm	if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
1448a272653SPeter Holm		err(1, "fts_open");
1458a272653SPeter Holm
1468a272653SPeter Holm	while ((p = fts_read(fts)) != NULL) {
1478a272653SPeter Holm		if ((fd = open(p->fts_path, O_RDONLY)) == -1) {
1488a272653SPeter Holm			if (errno != EACCES && errno != ENXIO)
1498a272653SPeter Holm				warn("open(%s)", p->fts_path);
1508a272653SPeter Holm			continue;
1518a272653SPeter Holm		}
1528a272653SPeter Holm		alarm(1);
1538a272653SPeter Holm		pread(fd, (void *)0xdeadc0de, 0x7ffffff, 0xffffffff);
1548a272653SPeter Holm		pread(fd, buf, 0x7ffffff, 0xffffffff);
1558a272653SPeter Holm		pread(fd, buf, sizeof(buf), 0xffffffff);
1568a272653SPeter Holm		pread(fd, buf, sizeof(buf), 0);
1578a272653SPeter Holm		close(fd);
1588a272653SPeter Holm	}
1598a272653SPeter Holm	fts_close(fts);
1608a272653SPeter Holm
1618a272653SPeter Holm	exit(0);
1628a272653SPeter Holm}
1638a272653SPeter Holm
1648a272653SPeter Holmint
1658a272653SPeter Holmmain(int argc __unused, char **argv)
1668a272653SPeter Holm{
1678a272653SPeter Holm	int i;
1688a272653SPeter Holm        struct passwd *pw;
1698a272653SPeter Holm
1708a272653SPeter Holm        if ((pw = getpwnam("nobody")) == NULL)
1718a272653SPeter Holm                err(1, "no such user: nobody");
1728a272653SPeter Holm
1738a272653SPeter Holm        if (setgroups(1, &pw->pw_gid) ||
1748a272653SPeter Holm            setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
1758a272653SPeter Holm            seteuid(pw->pw_uid) || setuid(pw->pw_uid))
1768a272653SPeter Holm                err(1, "Can't drop privileges to \"nobody\"");
1778a272653SPeter Holm        endpwent();
1788a272653SPeter Holm
1798a272653SPeter Holm	if (daemon(0, 0) == -1)
1808a272653SPeter Holm		err(1, "daemon()");
1818a272653SPeter Holm
1828a272653SPeter Holm	for (i = 0; i < 10; i++) {
1838a272653SPeter Holm		if (fork() == 0)
1848a272653SPeter Holm			test(argv[1]);
1858a272653SPeter Holm		wait(NULL);
1868a272653SPeter Holm	}
1878a272653SPeter Holm
1888a272653SPeter Holm	return (0);
1898a272653SPeter Holm}
190