Added fuzzying of dvparser.

pull/22/head
weetmuts 2019-03-15 21:49:18 +01:00
rodzic d60493ee45
commit 7394aa7175
4 zmienionych plików z 98 dodań i 0 usunięć

Wyświetl plik

@ -111,6 +111,9 @@ $(BUILD)/wmbusmeters: $(METER_OBJS) $(BUILD)/main.o
$(BUILD)/testinternals: $(METER_OBJS) $(BUILD)/testinternals.o
$(CXX) -o $(BUILD)/testinternals $(METER_OBJS) $(BUILD)/testinternals.o $(DEBUG_LDFLAGS) -lpthread
$(BUILD)/fuzz: $(METER_OBJS) $(BUILD)/fuzz.o
$(CXX) -o $(BUILD)/fuzz $(METER_OBJS) $(BUILD)/fuzz.o $(DEBUG_LDFLAGS) -lpthread
clean:
rm -rf build/* build_arm/* build_debug/* build_arm_debug/* *~
@ -160,6 +163,13 @@ update_manufacturers:
rm tmpfile
mv m.h src/manufacturers.h
build_fuzz:
@if [ "${AFLHOME}" = "" ]; then echo 'You must supply aflhome "make build_fuzz AFLHOME=/home/afl"'; exit 1; fi
$(MAKE) AFL_HARDEN=1 CXX=$(AFLHOME)/afl-g++ $(BUILD)/fuzz
run_fuzz:
@if [ "${AFLHOME}" = "" ]; then echo 'You must supply aflhome "make run_fuzz AFLHOME=/home/afl"'; exit 1; fi
${AFLHOME}/afl-fuzz -i fuzz_testcases/ -o fuzz_findings/ build/fuzz
# Include dependency information generated by gcc in a previous compile.
include $(wildcard $(patsubst %.o,%.d,$(METER_OBJS)))

49
src/fuzz.cc 100644
Wyświetl plik

@ -0,0 +1,49 @@
/*
Copyright (C) 2019 Fredrik Öhrström
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"cmdline.h"
#include"meters.h"
#include"printer.h"
#include"serial.h"
#include"util.h"
#include"wmbus.h"
#include"dvparser.h"
#include<string.h>
#include<unistd.h>
using namespace std;
int main(int argc, char **argv)
{
// Fuzzying currently only tests the parsing of difvif wmbus data.
// The binary difvif data is sent on stdin.
char buf[1024];
vector<uchar> databytes;
for (;;) {
size_t len = read(0, buf, sizeof(buf));
if (len <= 0) break;
databytes.insert(databytes.end(), buf, buf+len);
}
map<string,pair<int,DVEntry>> values;
Telegram t;
vector<uchar>::iterator i = databytes.begin();
parseDV(&t, databytes, i, databytes.size(), &values);
}

Wyświetl plik

@ -17,3 +17,4 @@ tests/test_logfile.sh $PROG
tests/test_listen_to_all.sh $PROG
tests/test_multiple_ids.sh $PROG
tests/test_oneshot.sh $PROG
tests/test_wrongkeys.sh $PROG

Wyświetl plik

@ -0,0 +1,38 @@
#!/bin/bash
PROG="$1"
mkdir -p testoutput
TEST=testoutput
cat simulations/simulation_t1.txt | grep '^{' > $TEST/test_expected.txt
$PROG --format=json simulations/simulation_t1.txt \
MyWarmWater supercom587 12345678 11111111111111111111111111111111 \
MyColdWater supercom587 11111111 11111111111111111111111111111111 \
MoreWater iperl 12345699 11111111111111111111111111111111 \
WaterWater iperl 33225544 11111111111111111111111111111111 \
| grep warning > $TEST/test_output.txt
cat <<EOF > $TEST/test_expected.txt
(Mode5) warning: decryption received non-multiple of 16 bytes! Got 148 bytes shrinking message to 144 bytes.
(Mode5) warning: telegram payload does not start with 2F2F (did you use the correct encryption key?)
(Mode5) warning: decryption received non-multiple of 16 bytes! Got 148 bytes shrinking message to 144 bytes.
(Mode5) warning: telegram payload does not start with 2F2F (did you use the correct encryption key?)
(Mode5) warning: telegram payload does not start with 2F2F (did you use the correct encryption key?)
(Mode5) warning: decryption received non-multiple of 16 bytes! Got 10 bytes shrinking message to 0 bytes.
(Mode5) warning: telegram payload does not start with 2F2F (did you use the correct encryption key?)
EOF
# Check that the program does not crash!
if [ "$?" == "0" ]
then
diff $TEST/test_expected.txt $TEST/test_output.txt
if [ "$?" == "0" ]
then
echo Random keys OK
fi
else
echo Failure.
exit 1
fi