3 # Fuzz-testing script for TShark
5 # This script uses Editcap to add random errors ("fuzz") to a set of
6 # capture files specified on the command line. It runs TShark on
7 # each fuzzed file and checks for errors. The files are processed
8 # repeatedly until an error is found.
10 # Copyright 2013 Gerald Combs <gerald@wireshark.org>
14 # Wireshark - Network traffic analyzer
15 # By Gerald Combs <gerald@wireshark.org>
16 # Copyright 1998 Gerald Combs
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public License
20 # as published by the Free Software Foundation; either version 2
21 # of the License, or (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 .
`dirname $0`/test-common.sh ||
exit 1
35 # Directory containing binaries. Default current directory.
38 # Sanity check to make sure we can find our plugins. Zero or less disables.
41 # Did we catch a signal?
44 # Perform a two pass analysis on the capture file?
47 # Specific config profile ?
50 # Run under valgrind ?
53 # The maximum permitted amount of memory leaked. Eventually this should be
54 # worked down to zero, but right now that would fail on every single capture.
55 # Only has effect when running under valgrind.
56 MAX_LEAK
=`expr 1024 \* 500`
58 # To do: add options for file names and limits
59 while getopts ":2b:C:d:e:gp:P:" OPTCHAR
; do
63 C
) CONFIG_PROFILE
="-C $OPTARG " ;;
65 e
) ERR_PROB
=$OPTARG ;;
67 p
) MAX_PASSES
=$OPTARG ;;
68 P
) MIN_PLUGINS
=$OPTARG ;;
71 shift $
(($OPTIND - 1))
73 ### usually you won't have to change anything below this line ###
75 COMMON_ARGS
="${CONFIG_PROFILE}${TWO_PASS}"
76 if [ $VALGRIND -eq 1 ]; then
77 RUNNER
="`dirname $0`/valgrind-wireshark.sh"
78 COMMON_ARGS
="-b $BIN_DIR $COMMON_ARGS"
79 declare -a RUNNER_ARGS
=("" "-T")
80 # Valgrind requires more resources, so bump our limits to 1.5 times normal
81 MAX_CPU_TIME
=`expr 3 \* $MAX_CPU_TIME / 2`
82 MAX_VMEM
=`expr 3 \* $MAX_VMEM / 2`
84 # Not using valgrind, use regular tshark.
85 # TShark arguments (you won't have to change these)
86 # n Disable network object name resolution
87 # V Print a view of the details of the packet rather than a one-line summary of the packet
88 # x Cause TShark to print a hex and ASCII dump of the packet data after printing the summary or details
89 # r Read packet data from the following infile
91 declare -a RUNNER_ARGS
=("-nVxr" "-nr")
92 # Running with a read filter but without generating the tree exposes some
93 # "More than 100000 items in tree" bugs.
94 # Not sure if we want to add even more cycles to the fuzz bot's work load...
95 #declare -a RUNNER_ARGS=("${CONFIG_PROFILE}${TWO_PASS}-nVxr" "${CONFIG_PROFILE}${TWO_PASS}-nr" "-Yframe ${CONFIG_PROFILE}${TWO_PASS}-nr")
100 for i
in "$TSHARK" "$EDITCAP" "$CAPINFOS" "$DATE" "$TMP_DIR" ; do
102 echo "Couldn't find $i"
106 if [ $NOTFOUND -eq 1 ]; then
110 # Make sure we have a valid test set
113 if [ "$OSTYPE" == "cygwin" ] ; then
114 CF
=`cygpath --windows "$CF"`
116 "$CAPINFOS" "$CF" > /dev
/null
2>&1 && FOUND
=1
117 if [ $FOUND -eq 1 ] ; then break ; fi
120 if [ $FOUND -eq 0 ] ; then
122 Error: No valid capture files found.
124 Usage: `basename $0` [-2] [-b bin_dir] [-C config_profile] [-d work_dir] [-e error probability] [-g] [-p passes] capture file 1 [capture file 2]...
129 PLUGIN_COUNT
=`$TSHARK -G plugins | grep dissector | wc -l`
130 if [ $MIN_PLUGINS -gt 0 -a $PLUGIN_COUNT -lt $MIN_PLUGINS ] ; then
131 echo "Warning: Found fewer plugins than expected ($PLUGIN_COUNT vs $MIN_PLUGINS)."
136 if [ $MAX_PASSES -gt 0 ]; then
137 HOWMANY
="$MAX_PASSES passes"
139 echo -n "Running $RUNNER $COMMON_ARGS with args: "
140 printf "\"%s\" " "${RUNNER_ARGS[@]}"
144 # Clean up on <ctrl>C, etc
145 trap "DONE=1; echo 'Caught signal'" HUP INT TERM
148 # Iterate over our capture files.
150 while [ \
( $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 \
) -a $DONE -ne 1 ] ; do
152 echo "Starting pass $PASS:"
156 if [ $DONE -eq 1 ]; then
157 break # We caught a signal
160 if [ $
(( $RUN % 50 )) -eq 0 ] ; then
163 if [ "$OSTYPE" == "cygwin" ] ; then
164 CF
=`cygpath --windows "$CF"`
168 "$CAPINFOS" "$CF" > /dev
/null
2> $TMP_DIR/$ERR_FILE
170 if [ $RETVAL -eq 1 ] ; then
171 echo "Not a valid capture file"
172 rm -f $TMP_DIR/$ERR_FILE
174 elif [ $RETVAL -ne 0 -a $DONE -ne 1 ] ; then
182 "$EDITCAP" -E $ERR_PROB "$CF" $TMP_DIR/$TMP_FILE > /dev
/null
2>&1
183 if [ $?
-ne 0 ] ; then
184 "$EDITCAP" -E $ERR_PROB -T ether
"$CF" $TMP_DIR/$TMP_FILE \
186 if [ $?
-ne 0 ] ; then
187 echo "Invalid format for editcap"
192 for ARGS
in "${RUNNER_ARGS[@]}" ; do
193 if [ $DONE -eq 1 ]; then
194 break # We caught a signal
197 echo -e "Command and args: $RUNNER $ARGS\n" > $TMP_DIR/$ERR_FILE
199 # Run in a child process with limits, e.g. stop it if it's running
200 # longer then MAX_CPU_TIME seconds. (ulimit may not be supported
201 # well on some platforms, particularly cygwin.)
203 ulimit -S -t $MAX_CPU_TIME -v $MAX_VMEM -s $MAX_STACK
206 "$RUNNER" $COMMON_ARGS $ARGS $TMP_DIR/$TMP_FILE \
207 > /dev
/null
2>> $TMP_DIR/$ERR_FILE
211 # Uncomment the next two lines to enable dissector bug
213 #grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
214 # > /dev/null 2>&1 && DISSECTOR_BUG=1
216 if [ $VALGRIND -eq 1 -a $DONE -ne 1 ]; then
217 VG_ERR_CNT
=`grep "ERROR SUMMARY:" $TMP_DIR/$ERR_FILE | cut -f4 -d' '`
218 VG_DEF_LEAKED
=`grep "definitely lost:" $TMP_DIR/$ERR_FILE | cut -f7 -d' ' | tr -d ,`
219 VG_IND_LEAKED
=`grep "indirectly lost:" $TMP_DIR/$ERR_FILE | cut -f7 -d' ' | tr -d ,`
220 VG_TOTAL_LEAKED
=`expr $VG_DEF_LEAKED + $VG_IND_LEAKED`
221 if [ $?
-ne 0 ] ; then
223 elif [ "$VG_TOTAL_LEAKED" -gt "$MAX_LEAK" ] ; then
226 if grep -q "Valgrind cannot continue" $TMP_DIR/$ERR_FILE; then
231 if [ $DONE -ne 1 -a \
( $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 -o $VG_ERR_CNT -ne 0 \
) ] ; then
237 rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE