Repository: secdev/scapy

Issue:
The error raised by wrong BPF filter syntax is uncaught when using AsyncSniffer.
### Brief description

The error raised by wrong BPF filter syntax in AsyncSniffer is not caught.

### Scapy version

2.4.5

### Python version

3.9.7

### Operating system

Linux 5.13.0-37-generic

### Additional environment information

This behavior is only reproducible on Linux, it couldn't be reproduced on Windows.
I didn't try on MAC.

### How to reproduce

**Step 1:**
Open scapy

**Step 2:**
Run the following lines of code:
`e = AsyncSniffer(filter='adawdadadsadaaw')`
`e.start()`

**Step 3:**
Observe that:
- the incorrect filter error is uncaught
- the AsyncSniffer doesn't capture packets
- e.running is True, even though there was an error raised and the AsyncSniffer dosen't catch anything.

### Actual result

```
Exception in thread AsyncSniffer:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/dist-packages/scapy/sendrecv.py", line 1127, in _run
    sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,
  File "/usr/local/lib/python3.9/dist-packages/scapy/arch/linux.py", line 497, in __init__
    attach_filter(self.ins, filter, self.iface)
  File "/usr/local/lib/python3.9/dist-packages/scapy/arch/linux.py", line 166, in attach_filter
    bp = compile_filter(bpf_filter, iface)
  File "/usr/local/lib/python3.9/dist-packages/scapy/arch/common.py", line 156, in compile_filter
    raise Scapy_Exception(
scapy.error.Scapy_Exception: Failed to compile filter expression adawdadadsadaaw (-1)
```

### Expected result

The error should be caught.

**For lack of a better section, I will add more info here:**
After a brief investigation, this behavior might be caused by the fact that:
File scapy/scapy/arch/linux.py line 498, there is a try-except, whose `except `catches only `ImportErrors`:
```
if filter is not None:
    try:
        attach_filter(self.ins, filter, self.iface)
    except ImportError as ex:
        log_runtime.error("Cannot set filter: %s", ex)
```

If the `except` is edited to catch all exceptions, the error is caught and the problematic behavior is eliminated.
```
if filter is not None:
    try:
        attach_filter(self.ins, filter, self.iface)
    except Exception as ex:
        log_runtime.error("Cannot set filter: %s", ex)
```

Example after modification:

```
>>> e = AsyncSniffer(filter='adawdadadsadaaw')
>>> e.start()
>>> ERROR: Cannot set filter: Failed to compile filter expression adawdadadsadaaw (-1)
>>>
>>> e.stop()
<Sniffed: TCP:35 UDP:0 ICMP:0 Other:0>
>>>
```

### Related resources

_No response_


Discussion:
Thanks for reporting this issue? Can you make a PR with a fix?  I will suggest to catch both `ImportError` and `Scapy_Exception`.

Resolving patch:
diff --git a/scapy/arch/linux.py b/scapy/arch/linux.py
--- a/scapy/arch/linux.py
+++ b/scapy/arch/linux.py
@@ -494,7 +494,7 @@ def __init__(self,
             if filter is not None:
                 try:
                     attach_filter(self.ins, filter, self.iface)
-                except ImportError as ex:
+                except (ImportError, Scapy_Exception) as ex:
                     log_runtime.error("Cannot set filter: %s", ex)
         if self.promisc:
             set_promisc(self.ins, self.iface)


Test patch: