blob: 8eb299319905b0528a62c0ec92de883ea228566f (
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
|
#!/bin/sh
# $OpenBSD: t1.sh,v 1.1 2008/08/20 09:29:51 mpf Exp $
# Test if gzip(1) detects truncated or corrupted files
# Copyright (c) 2008 Marco Pfatschbacher <mpf@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# truncate at 2k
gzip < /etc/rc | dd bs=1k count=2 of=t1.gz 2>/dev/null
if gzip -vt t1.gz; then
echo "=> ERROR: truncation not detected!"
exit 1
else
echo "=> OK"
fi
# truncate at 1k
gzip < /etc/rc | dd bs=1k count=1 of=t1.gz 2>/dev/null
if gzip -vt t1.gz; then
echo "=> ERROR: truncation not detected!"
exit 1
else
echo "=> OK"
fi
# skip some data in the middle
gzip < /etc/rc | dd bs=1k seek=1 >> t1.gz 2>/dev/null
if gzip -vt t1.gz; then
echo "=> ERROR: corruption not detected!"
exit 1
else
echo "=> OK"
fi
# simple fuzzer that modifies one random byte at a random offset
gzip < /etc/rc > t1.gz
for i in `jot 100`; do
where=$((RANDOM % 2048 + 256)) # random offset (but skip the header)
orig=`dd if=t1.gz skip=$where bs=1 count=1 2>/dev/null |\
hexdump -e '"%d"'`
fuzz=$((((orig + RANDOM) % 256) + 1))
if [ $fuzz = $orig ]; then
fuzz=$(((fuzz + 1) % 256))
fi
echo "$i/100: fuzzing byte @$where: $orig -> $fuzz"
if (dd if=t1.gz bs=1 count=$where 2>/dev/null; \
echo -n \\0`printf "%o" $fuzz`; \
dd if=t1.gz bs=1 skip=$((where+1)) 2>/dev/null) | gzip -tv; then
echo "=> ERROR: corruption not detected!"
exit 1
else
echo "=> OK"
fi
done
exit 0
|