From d74520768b32c35eae422f2b9dda9a1bf494122d Mon Sep 17 00:00:00 2001 From: Derrick Lyndon Pallas Date: Thu, 11 Apr 2019 18:10:56 +0000 Subject: [PATCH 1/3] OfflinePacketFilter: avoid leaks on error in init --- src/offline_packet_filter.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/offline_packet_filter.cpp b/src/offline_packet_filter.cpp index a0fb462..ad8878a 100644 --- a/src/offline_packet_filter.cpp +++ b/src/offline_packet_filter.cpp @@ -58,8 +58,14 @@ void OfflinePacketFilter::init(const string& pcap_filter, link_type, snap_len ); + if (!handle_) { + throw pcap_open_failed(); + } if (pcap_compile(handle_, &filter_, pcap_filter.c_str(), 1, 0xffffffff) == -1) { - throw invalid_pcap_filter(pcap_geterr(handle_)); + string error(pcap_geterr(handle_)); + pcap_freecode(&filter_); + pcap_close(handle_); + throw invalid_pcap_filter(error.c_str()); } } From 0c40a0714b51952cf5b0ec8fcf56ae15de9e24b0 Mon Sep 17 00:00:00 2001 From: Derrick Lyndon Pallas Date: Thu, 11 Apr 2019 18:11:25 +0000 Subject: [PATCH 2/3] PacketWriter: avoid use-after-free on error in init --- src/packet_writer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp index 7e79a10..f0072d8 100644 --- a/src/packet_writer.cpp +++ b/src/packet_writer.cpp @@ -86,8 +86,9 @@ void PacketWriter::init(const string& file_name, int link_type) { } dumper_ = pcap_dump_open(handle_, file_name.c_str()); if (!dumper_) { + string error(pcap_geterr(handle_)); pcap_close(handle_); - throw pcap_error(pcap_geterr(handle_)); + throw pcap_error(error); } } From 064439236c39057b5ac97914087fc97a3dd0822c Mon Sep 17 00:00:00 2001 From: Derrick Lyndon Pallas Date: Thu, 11 Apr 2019 18:12:26 +0000 Subject: [PATCH 3/3] OfflinePacketFilter: avoid leak during copy-construction or assignment --- src/offline_packet_filter.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/offline_packet_filter.cpp b/src/offline_packet_filter.cpp index ad8878a..c57b827 100644 --- a/src/offline_packet_filter.cpp +++ b/src/offline_packet_filter.cpp @@ -37,11 +37,14 @@ using std::string; namespace Tins { OfflinePacketFilter::OfflinePacketFilter(const OfflinePacketFilter& other) { - *this = other; + string_filter_ = other.string_filter_; + init(string_filter_, pcap_datalink(other.handle_), pcap_snapshot(other.handle_)); } OfflinePacketFilter& OfflinePacketFilter::operator=(const OfflinePacketFilter& other) { string_filter_ = other.string_filter_; + pcap_freecode(&filter_); + pcap_close(handle_); init(string_filter_, pcap_datalink(other.handle_), pcap_snapshot(other.handle_)); return* this; }