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>
12 # Wireshark - Network traffic analyzer
13 # By Gerald Combs <gerald@wireshark.org>
14 # Copyright 1998 Gerald Combs
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 . `dirname $0`/test-common.sh || exit 1
33 # Sanity check to make sure we can find our plugins. Zero or less disables.
36 # Did we catch a signal?
39 # Perform a two pass analysis on the capture file?
42 # Specific config profile ?
45 # Run under valgrind ?
48 # Run under AddressSanitizer ?
51 # Don't skip any byte from being changed
54 # The maximum permitted amount of memory leaked. Eventually this should be
55 # worked down to zero, but right now that would fail on every single capture.
56 # Only has effect when running under valgrind.
57 MAX_LEAK=`expr 1024 \* 100`
59 # To do: add options for file names and limits
60 while getopts "2b:C:d:e:agp:P:o:" OPTCHAR ; do
64 b) WIRESHARK_BIN_DIR=$OPTARG ;;
65 C) CONFIG_PROFILE="-C $OPTARG " ;;
67 e) ERR_PROB=$OPTARG ;;
69 p) MAX_PASSES=$OPTARG ;;
70 P) MIN_PLUGINS=$OPTARG ;;
71 o) CHANGE_OFFSET=$OPTARG ;;
74 shift $(($OPTIND - 1))
76 ### usually you won't have to change anything below this line ###
79 ws_check_exec "$TSHARK" "$EDITCAP" "$CAPINFOS" "$DATE" "$TMP_DIR"
81 COMMON_ARGS="${CONFIG_PROFILE}${TWO_PASS}"
82 if [ $VALGRIND -eq 1 ]; then
83 RUNNER="`dirname $0`/valgrind-wireshark.sh"
84 COMMON_ARGS="-b $WIRESHARK_BIN_DIR $COMMON_ARGS"
85 declare -a RUNNER_ARGS=("" "-T")
86 # Valgrind requires more resources, so permit 1.5x memory and 3x time
87 # (1.5x time is too small for a few large captures in the menagerie)
88 MAX_CPU_TIME=`expr 3 \* $MAX_CPU_TIME`
89 MAX_VMEM=`expr 3 \* $MAX_VMEM / 2`
91 # Not using valgrind, use regular tshark.
92 # TShark arguments (you won't have to change these)
93 # n Disable network object name resolution
94 # V Print a view of the details of the packet rather than a one-line summary of the packet
95 # x Cause TShark to print a hex and ASCII dump of the packet data after printing the summary or details
96 # r Read packet data from the following infile
98 declare -a RUNNER_ARGS=("-nVxr" "-nr")
99 # Running with a read filter but without generating the tree exposes some
100 # "More than 100000 items in tree" bugs.
101 # Not sure if we want to add even more cycles to the fuzz bot's work load...
102 #declare -a RUNNER_ARGS=("${CONFIG_PROFILE}${TWO_PASS}-nVxr" "${CONFIG_PROFILE}${TWO_PASS}-nr" "-Yframe ${CONFIG_PROFILE}${TWO_PASS}-nr")
106 # Make sure we have a valid test set
109 if [ "$OSTYPE" == "cygwin" ] ; then
110 CF=`cygpath --windows "$CF"`
112 "$CAPINFOS" "$CF" > /dev/null 2>&1 && FOUND=1
113 if [ $FOUND -eq 1 ] ; then break ; fi
116 if [ $FOUND -eq 0 ] ; then
118 Error: No valid capture files found.
120 Usage: `basename $0` [-2] [-b bin_dir] [-C config_profile] [-d work_dir] [-e error probability] [-o changes offset] [-g] [-a] [-p passes] capture file 1 [capture file 2]...
125 PLUGIN_COUNT=`$TSHARK -G plugins | grep dissector | wc -l`
126 if [ $MIN_PLUGINS -gt 0 -a $PLUGIN_COUNT -lt $MIN_PLUGINS ] ; then
127 echo "Warning: Found fewer plugins than expected ($PLUGIN_COUNT vs $MIN_PLUGINS)."
131 if [ $ASAN -ne 0 ]; then
132 echo -n "ASan enabled. Virtual memory limit is "
135 echo "ASan disabled. Virtual memory limit is $MAX_VMEM"
139 if [ $MAX_PASSES -gt 0 ]; then
140 HOWMANY="$MAX_PASSES passes"
142 echo -n "Running $RUNNER $COMMON_ARGS with args: "
143 printf "\"%s\" " "${RUNNER_ARGS[@]}"
147 # Clean up on <ctrl>C, etc
148 trap "DONE=1; echo 'Caught signal'" HUP INT TERM
151 # Iterate over our capture files.
153 while [ \( $PASS -lt $MAX_PASSES -o $MAX_PASSES -lt 1 \) -a $DONE -ne 1 ] ; do
155 echo "Starting pass $PASS:"
159 if [ $DONE -eq 1 ]; then
160 break # We caught a signal
163 if [ $(( $RUN % 50 )) -eq 0 ] ; then
166 if [ "$OSTYPE" == "cygwin" ] ; then
167 CF=`cygpath --windows "$CF"`
171 if [ $VALGRIND -eq 1 -a `ls -s $CF | cut -d' ' -f1` -gt 8000 ]; then
172 echo "Too big for valgrind"
176 "$CAPINFOS" "$CF" > /dev/null 2> $TMP_DIR/$ERR_FILE
178 if [ $RETVAL -eq 1 ] ; then
179 echo "Not a valid capture file"
180 rm -f $TMP_DIR/$ERR_FILE
182 elif [ $RETVAL -ne 0 -a $DONE -ne 1 ] ; then
190 "$EDITCAP" -E $ERR_PROB -o $CHANGE_OFFSET "$CF" $TMP_DIR/$TMP_FILE > /dev/null 2>&1
191 if [ $? -ne 0 ] ; then
192 "$EDITCAP" -E $ERR_PROB -o $CHANGE_OFFSET -T ether "$CF" $TMP_DIR/$TMP_FILE \
194 if [ $? -ne 0 ] ; then
195 echo "Invalid format for editcap"
202 for ARGS in "${RUNNER_ARGS[@]}" ; do
203 if [ $DONE -eq 1 ]; then
204 break # We caught a signal
207 echo -e "Command and args: $RUNNER $ARGS\n" > $TMP_DIR/$ERR_FILE
209 # Run in a child process with limits, e.g. stop it if it's running
210 # longer then MAX_CPU_TIME seconds. (ulimit may not be supported
211 # well on some platforms, particularly cygwin.)
213 ulimit -S -t $MAX_CPU_TIME -s $MAX_STACK
215 SUBSHELL_PID=$($SHELL -c 'echo $PPID')
217 # Don't enable ulimit -v when using ASAN. See
218 # https://github.com/google/sanitizers/wiki/AddressSanitizer#ulimit--v
219 if [ $ASAN -eq 0 ]; then
223 "$RUNNER" $COMMON_ARGS $ARGS $TMP_DIR/$TMP_FILE \
224 > /dev/null 2>> $TMP_DIR/$ERR_FILE.$SUBSHELL_PID
227 RUNNER_PIDS="$RUNNER_PIDS $RUNNER_PID"
228 RUNNER_ERR_FILES="$RUNNER_ERR_FILES $TMP_DIR/$ERR_FILE.$RUNNER_PID"
231 for RUNNER_PID in $RUNNER_PIDS ; do
234 mv $TMP_DIR/$ERR_FILE.$RUNNER_PID $TMP_DIR/$ERR_FILE
236 # Uncomment the next two lines to enable dissector bug
238 #grep -i "dissector bug" $TMP_DIR/$ERR_FILE \
239 # > /dev/null 2>&1 && DISSECTOR_BUG=1
241 if [ $VALGRIND -eq 1 -a $DONE -ne 1 ]; then
242 VG_ERR_CNT=`grep "ERROR SUMMARY:" $TMP_DIR/$ERR_FILE | cut -f4 -d' '`
243 VG_DEF_LEAKED=`grep "definitely lost:" $TMP_DIR/$ERR_FILE | cut -f7 -d' ' | tr -d ,`
244 VG_IND_LEAKED=`grep "indirectly lost:" $TMP_DIR/$ERR_FILE | cut -f7 -d' ' | tr -d ,`
245 VG_TOTAL_LEAKED=`expr $VG_DEF_LEAKED + $VG_IND_LEAKED`
246 if [ $? -ne 0 ] ; then
248 elif [ "$VG_TOTAL_LEAKED" -gt "$MAX_LEAK" ] ; then
251 if grep -q "Valgrind cannot continue" $TMP_DIR/$ERR_FILE; then
256 if [ $DONE -ne 1 -a \( $RETVAL -ne 0 -o $DISSECTOR_BUG -ne 0 -o $VG_ERR_CNT -ne 0 \) ] ; then
257 rm -f $RUNNER_ERR_FILES
263 rm -f $TMP_DIR/$TMP_FILE $TMP_DIR/$ERR_FILE