1#!/bin/sh 2 3# Test scenario by: kib@ 4# Test program obtained from Kyle Evans <kevans@FreeBSD.org> 5 6# Demonstrate UFS SU file corruption: 7# ffs: on write into a buffer without content 8 9[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 10 11. ../default.cfg 12 13set -u 14prog=$(basename "$0" .sh) 15s=0 16cat > /tmp/$prog.c <<EOF 17#include <sys/mman.h> 18#include <sys/stat.h> 19 20#include <assert.h> 21#include <err.h> 22#include <fcntl.h> 23#include <stdio.h> 24#include <unistd.h> 25 26#define FILE "file" 27 28int 29main(void) 30{ 31 struct stat sb; 32 ssize_t wsz; 33 size_t bufsz; 34 void *buf, *obuf; 35 int mfd, fd; 36 int done = 0; 37 38 mfd = open(FILE, O_RDONLY); 39 assert(mfd >= 0); 40 41 assert(fstat(mfd, &sb) == 0); 42 bufsz = sb.st_size; 43 buf = obuf = mmap(NULL, bufsz, PROT_READ, MAP_SHARED, mfd, 0); 44 assert(buf != MAP_FAILED); 45 46 /* O_RDWR */ 47 fd = open(FILE, O_RDWR); 48 if (fd < 0) 49 err(1, "open"); 50 assert(fd >= 0); 51 52again: 53 while (bufsz > 0) { 54 wsz = write(fd, buf, bufsz); 55 if (wsz < 0) 56 err(1, "write"); 57 else if (wsz == 0) 58 fprintf(stderr, "Huh?\n"); 59 bufsz -= wsz; 60 buf += wsz; 61 } 62 63 bufsz = sb.st_size; 64 buf = obuf; 65 66 if (++done < 2) 67 goto again; 68 69 close(fd); 70 munmap(obuf, sb.st_size); 71 close(mfd); 72 return (0); 73} 74EOF 75mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c || exit 1 76 77mount | grep -q "on $mntpoint " && umount -f $mntpoint 78mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart 79mdconfig -s 32m -u $mdstart 80 81pagesize=$(sysctl -n hw.pagesize) 82newfs -Un -b $pagesize /dev/md$mdstart > /dev/null 83mount /dev/md$mdstart $mntpoint 84dd if=/dev/random of=/mnt/file.orig bs=${pagesize} count=1 status=none 85cp $mntpoint/file.orig $mntpoint/file 86cat $mntpoint/file $mntpoint/file > $mntpoint/file.post 87umount $mntpoint 88 89mount /dev/md$mdstart $mntpoint 90(cd $mntpoint; /tmp/$prog) 91 92if ! cmp $mntpoint/file $mntpoint/file.post; then 93 echo "Files differ" 94 ls -l $mntpoint/file $mntpoint/file.post 95 s=1 96fi 97 98umount $mntpoint 99mdconfig -d -u $mdstart 100rm /tmp/$prog /tmp/$prog.c 101exit $s 102