Skip to content

Commit 78c07ee

Browse files
committed
Linux: Fix propagation of getprotobyname_r() errors.
pcap_nametoproto(), when Linux getprotobyname_r() fails, returns 0 to fail quickly similarly to pcap_nametonetaddr() and pcap_nametoport(). However, the correct way for pcap_nametoproto() to fail is to return PROTO_UNDEF. This is why on a Linux host with GNU libc and without the /etc/protocols file pcap_nametoproto() and subsequently lookup_proto() fail to fail, then pcap_compile() produces incorrect filter programs for "proto NAME", "protochain NAME" and variations thereof: checking whether getprotobyname_r is declared... yes checking for the Linux getprotobyname_r()... yes checking if features.h defines __GLIBC__... yes @@ -1,6 +1,6 @@ (000) ldh [12] (001) jeq #0x800 jt 2 jf 5 (002) ldb [23] -(003) jeq #0x6 jt 4 jf 5 +(003) jeq #0x0 jt 4 jf 5 (004) ret #262144 (005) ret #0 Return the correct error value to fix this and discuss the outputs of GNU getprotobyname_r() in a comment. This bug manifested in an s390x Debian 12 container provided by OSU OSL.
1 parent 3d05686 commit 78c07ee

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
104104
Use getprotobyname_r() correctly on OpenBSD.
105105
Linux:
106106
Do not use ether_hostton() from musl libc.
107+
Fix propagation of getprotobyname_r() errors.
107108
Haiku:
108109
Look for ethers(5) in /boot/system/settings/network/.
109110
DAG:

nametoaddr.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,26 @@ pcap_nametoproto(const char *str)
491491
int err;
492492

493493
err = getprotobyname_r(str, &result_buf, buf, sizeof buf, &p);
494+
/*
495+
* As far as GNU libc implementation goes, an "error" means the
496+
* protocol database could not be searched, which could mean err ==
497+
* ERANGE if the buffer is too small or ENOENT if the protocols(5)
498+
* file does not exist (the man page does not document the latter
499+
* eventuality). If the database has been searched normally and the
500+
* requested protocol name was not found, it is not an "error" and
501+
* err == 0.
502+
*
503+
* This notwithstanding, p == NULL iff a record was not found for any
504+
* reason (whether an "error" or not), which is the same semantics as
505+
* in every other HAVE_xxxxx branch of this block. The final check
506+
* after the block will catch that if necessary.
507+
*/
494508
if (err != 0) {
495509
/*
496510
* XXX - dynamically allocate the buffer, and make it
497511
* bigger if we get ERANGE back?
498512
*/
499-
return 0;
513+
return PROTO_UNDEF;
500514
}
501515
#elif defined(HAVE_SOLARIS_GETPROTOBYNAME_R)
502516
/*

0 commit comments

Comments
 (0)