Repository: secdev/scapy

Issue:
socket.error: [Errno 12] Cannot allocate memory
### Brief description

Description The socket could not allocate memory because it was too large to listen for all network adapter filtering rules


### Scapy version

2.4.5

### Python version

2.7

### Operating system

Linux pve 5.4.106-1-pve #1 SMP PVE 5.4.106-1 (Fri, 19 Mar 2021 11:08:47 +0100) x86_64 x86_64 x86_64 GNU/Linux

### Additional environment information

_No response_

### How to reproduce

    packet_filter_rule = "net 192.168.192.10/31 or net 192.168.192.12/30 or net 192.168.192.16/28 or net 192.168.192.32/27 or net 192.168.192.64/26 or net 192.168.192.128/25 or net 192.168.193.0/24 or net 192.168.194.0/23 or net 192.168.196.0/22 or net 192.168.200.0/21 or net 192.168.208.0/20 or net 192.168.224.0/22 or net 192.168.228.0/23 or net 192.168.230.0/26 or net 192.168.230.64/27 or net 192.168.230.96/30"
    
    sniff(store=False, prn=self._callback, iface=None, filter=packet_filter_rule)
  

### Actual result
      Traceback (most recent call last):
        File "vnet.py", line 10, in <module>
          main()
        File "/usr/src/app/service/vnet/worker.py", line 218, in main
          Worker().run()
        File "/usr/src/app/service/vnet/worker.py", line 36, in run
          self.handle_task()
        File "/usr/src/app/service/vnet/worker.py", line 90, in handle_task
          filter=self.packet_filter_rule)
        File "/usr/local/lib/python2.7/dist-packages/scapy/sendrecv.py", line 1036, in sniff
          sniffer._run(*args, **kwargs)
        File "/usr/local/lib/python2.7/dist-packages/scapy/sendrecv.py", line 907, in _run
          *arg, **karg)] = iface
        File "/usr/local/lib/python2.7/dist-packages/scapy/arch/linux.py", line 407, in __init__
          attach_filter(self.ins, filter, iface)
        File "/usr/local/lib/python2.7/dist-packages/scapy/arch/linux.py", line 143, in attach_filter
          sock.setsockopt(socket.SOL_SOCKET, SO_ATTACH_FILTER, bp)
        File "/usr/lib/python2.7/socket.py", line 228, in meth
          return getattr(self._sock,name)(*args)
      socket.error: [Errno 12] Cannot allocate memory



### Expected result

_No response_

### Related resources

_No response_


Discussion:

Resolving patch:
diff --git a/scapy/arch/common.py b/scapy/arch/common.py
--- a/scapy/arch/common.py
+++ b/scapy/arch/common.py
@@ -137,7 +137,7 @@ def compile_filter(filter_exp,  # type: str
             linktype = ARPHDR_ETHER
     if linktype is not None:
         ret = pcap_compile_nopcap(
-            MTU, linktype, ctypes.byref(bpf), bpf_filter, 0, -1
+            MTU, linktype, ctypes.byref(bpf), bpf_filter, 1, -1
         )
     elif iface:
         err = create_string_buffer(PCAP_ERRBUF_SIZE)
@@ -149,7 +149,7 @@ def compile_filter(filter_exp,  # type: str
         if error:
             raise OSError(error)
         ret = pcap_compile(
-            pcap, ctypes.byref(bpf), bpf_filter, 0, -1
+            pcap, ctypes.byref(bpf), bpf_filter, 1, -1
         )
         pcap_close(pcap)
     if ret == -1:
diff --git a/scapy/arch/libpcap.py b/scapy/arch/libpcap.py
--- a/scapy/arch/libpcap.py
+++ b/scapy/arch/libpcap.py
@@ -293,7 +293,7 @@ def fileno(self):
 
         def setfilter(self, f):
             filter_exp = create_string_buffer(f.encode("utf8"))
-            if pcap_compile(self.pcap, byref(self.bpf_program), filter_exp, 0, -1) == -1:  # noqa: E501
+            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:


Test patch: