Make opening a non-existent interface on Linux return 'no such interface' - #1560
Make opening a non-existent interface on Linux return 'no such interface'#1560afipay wants to merge 1 commit into
Conversation
2848591 to
b6e51a7
Compare
|
Thank you for preparing this change. The comments that clarify the problem and the solution should be in the commit message. |
|
On Linux the only proposed change is to It looks like the two proposed changes are not related and should be two different commits, each stating and solving a separate problem. Also, since This requires a bit more work before it is ready. |
d5797aa to
8b097c7
Compare
02956d2 to
ffb13dc
Compare
0fa0fea to
7129d48
Compare
7129d48 to
ea3d11d
Compare
|
Rebased. |
ea3d11d to
4f0bbfa
Compare
In pcap_activate_linux(), check whether the device exists, using an unprivileged ioctl socket from get_if_ioctl_socket(), before attempting any operation that requires privileges, so that attempting to open a non-existent device consistently reports PCAP_ERROR_NO_SUCH_DEVICE rather than PCAP_ERROR_PERM_DENIED from the attempt to create a PF_PACKET socket without CAP_NET_RAW. Fixes the-tcpdump-group#1538. Also addresses the-tcpdump-group/tcpdump#1334, where a zero-padded numeric interface argument behaved differently depending on whether its length reached IFNAMSIZ.
4f0bbfa to
a703d58
Compare
|
Should the check whether a device exists be done before trying to bind it, or after the attempt to bind the device fails with a permission error? There's a time-of-check/time-of-use issue for both of those, but, if interfaces are appearing or disappearing, there's a risk of an error being reported for a condition that's not true by the time the user sees the error no matter what the code does, so I'm not bothered by that. The one advantage of doing it afterwards is that fewer system calls are made in the case where everything succeeds, i.e. if the program does have permission to capture. I'm not sure that's a big enough difference to worry about. |
|
Make it work, make it right, make it fast? |
Fix interface existence check on Linux before privileged operations
When attempting to open a non-existent network interface on Linux,
libpcap would return PCAP_ERROR_PERM_DENIED instead of the correct
PCAP_ERROR_NO_SUCH_DEVICE. This occurred because the privileged
socket(PF_PACKET, SOCK_RAW, 0) call in setup_socket() would fail
with permission errors before interface validation could occur.
This fix adds an interface existence check using an unprivileged
AF_INET socket and SIOCGIFINDEX ioctl before attempting to create
the privileged packet socket. Non-existent interfaces now correctly
return PCAP_ERROR_NO_SUCH_DEVICE.
This resolves the issue where applications like tcpdump would report
"Permission denied" instead of "No such device exists" for non-existent
interfaces, breaking their fallback logic from interface names to indices.
Note: BSD/macOS platforms have the same underlying issue where BPF device
access fails with permission errors before interface validation. This
should be addressed in a separate commit.
Fixes: libpcap issue #1538
Related: tcpdump issue #1334