Skip to content

Commit

Permalink
Correctly raise on bad filter (#4241)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Feb 5, 2024
1 parent a27b024 commit 1587028
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions scapy/arch/bpf/supersocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ def __init__(self,
try:
attach_filter(self.bpf_fd, filter, self.iface)
filter_attached = True
except ImportError as ex:
warning("Cannot set filter: %s" % ex)
except (ImportError, Scapy_Exception) as ex:
raise Scapy_Exception("Cannot set filter: %s" % ex)
if NETBSD and filter_attached is False:
# On NetBSD, a filter must be attached to an interface, otherwise
# no frame will be received by os.read(). When no filter has been
Expand Down
19 changes: 10 additions & 9 deletions scapy/arch/libpcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def close(self):
pcap_datalink,
pcap_findalldevs,
pcap_freealldevs,
pcap_geterr,
pcap_if_t,
pcap_lib_version,
pcap_next_ex,
Expand Down Expand Up @@ -386,16 +387,16 @@ def fileno(self):
return cast(int, pcap_get_selectable_fd(self.pcap))

def setfilter(self, f):
# type: (str) -> bool
# type: (str) -> None
filter_exp = create_string_buffer(f.encode("utf8"))
if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 1, -1) == -1: # noqa: E501
log_runtime.error("Could not compile filter expression %s", f)
return False
else:
if pcap_setfilter(self.pcap, byref(self.bpf_program)) == -1:
log_runtime.error("Could not set filter %s", f)
return False
return True
if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 1, -1) >= 0: # noqa: E501
if pcap_setfilter(self.pcap, byref(self.bpf_program)) >= 0:
# Success
return
errstr = decode_locale_str(
bytearray(pcap_geterr(self.pcap)).strip(b"\x00")
)
raise Scapy_Exception("Cannot set filter: %s" % errstr)

def setnonblock(self, i):
# type: (bool) -> None
Expand Down
2 changes: 1 addition & 1 deletion scapy/arch/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def __init__(self,
try:
attach_filter(self.ins, filter, self.iface)
except (ImportError, Scapy_Exception) as ex:
log_runtime.error("Cannot set filter: %s", ex)
raise Scapy_Exception("Cannot set filter: %s" % ex)
if self.promisc:
set_promisc(self.ins, self.iface)
self.ins.bind((self.iface, type))
Expand Down
9 changes: 9 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,15 @@ except Scapy_Exception:
else:
assert False

= Check online sniff() with a bad filter
~ needs_root tcpdump libpcap
try:
sniff(timeout=0, filter="bad filter")
except Scapy_Exception:
pass
else:
assert False

= Check offline sniff with lfilter
assert len(sniff(offline=[IP()/UDP(), IP()/TCP()], lfilter=lambda x: TCP in x)) == 1

Expand Down

0 comments on commit 1587028

Please sign in to comment.