From: Guy Harris Date: Thu, 3 Oct 2019 18:13:37 +0000 (-0700) Subject: Use pcap_strlcpy() when copying a string to a fixed-length buffer. X-Git-Tag: libpcap-1.10-bp~410 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/fb4f0f1c85560e65cee6c6f6ba0addb2fc468136 Use pcap_strlcpy() when copying a string to a fixed-length buffer. That makes sure we terminate with a '\0'. Reported by Charles Smith at Tangible Security. While we're at it, fix some existing pcap_strlcpy() calls to use the size of the buffer, using sizeof. --- diff --git a/rpcapd/fileconf.c b/rpcapd/fileconf.c index 76d59334..b79dda18 100644 --- a/rpcapd/fileconf.c +++ b/rpcapd/fileconf.c @@ -507,8 +507,7 @@ int fileconf_save(const char *savefile) fprintf(fp, "# Hosts which are allowed to connect to this server (passive mode)\n"); fprintf(fp, "# Format: PassiveClient = \n\n"); - strncpy(temphostlist, hostlist, MAX_HOST_LIST); - temphostlist[MAX_HOST_LIST] = 0; + pcap_strlcpy(temphostlist, hostlist, sizeof (temphostlist)); token = pcap_strtok_r(temphostlist, RPCAP_HOSTLIST_SEP, &lasts); while(token != NULL) diff --git a/rpcapd/rpcapd.c b/rpcapd/rpcapd.c index 5b6c14cd..ecb88955 100644 --- a/rpcapd/rpcapd.c +++ b/rpcapd/rpcapd.c @@ -194,8 +194,8 @@ int main(int argc, char *argv[]) // Initialize errbuf memset(errbuf, 0, sizeof(errbuf)); - strncpy(address, RPCAP_DEFAULT_NETADDR, MAX_LINE); - strncpy(port, RPCAP_DEFAULT_NETPORT, MAX_LINE); + pcap_strlcpy(address, RPCAP_DEFAULT_NETADDR, sizeof (address)); + pcap_strlcpy(port, RPCAP_DEFAULT_NETPORT, sizeof (port)); // Prepare to open a new server socket memset(&mainhints, 0, sizeof(struct addrinfo)); @@ -222,10 +222,10 @@ int main(int argc, char *argv[]) rpcapd_log_set(log_to_systemlog, log_debug_messages); break; case 'b': - strncpy(address, optarg, MAX_LINE); + pcap_strlcpy(address, optarg, sizeof (address)); break; case 'p': - strncpy(port, optarg, MAX_LINE); + pcap_strlcpy(port, optarg, sizeof (port)); break; case '4': mainhints.ai_family = PF_INET; // IPv4 server only @@ -253,7 +253,7 @@ int main(int argc, char *argv[]) break; case 'l': { - strncpy(hostlist, optarg, sizeof(hostlist)); + pcap_strlcpy(hostlist, optarg, sizeof(hostlist)); break; } case 'a': @@ -268,12 +268,12 @@ int main(int argc, char *argv[]) { tmpport = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts); - pcap_strlcpy(activelist[i].address, tmpaddress, MAX_LINE); + pcap_strlcpy(activelist[i].address, tmpaddress, sizeof (activelist[i].address)); if ((tmpport == NULL) || (strcmp(tmpport, "DEFAULT") == 0)) // the user choose a custom port - pcap_strlcpy(activelist[i].port, RPCAP_DEFAULT_NETPORT_ACTIVE, MAX_LINE); + pcap_strlcpy(activelist[i].port, RPCAP_DEFAULT_NETPORT_ACTIVE, sizeof (activelist[i].port)); else - pcap_strlcpy(activelist[i].port, tmpport, MAX_LINE); + pcap_strlcpy(activelist[i].port, tmpport, sizeof (activelist[i].port)); tmpaddress = pcap_strtok_r(NULL, RPCAP_HOSTLIST_SEP, &lasts); @@ -288,10 +288,10 @@ int main(int argc, char *argv[]) break; } case 'f': - pcap_strlcpy(loadfile, optarg, MAX_LINE); + pcap_strlcpy(loadfile, optarg, sizeof (loadfile)); break; case 's': - pcap_strlcpy(savefile, optarg, MAX_LINE); + pcap_strlcpy(savefile, optarg, sizeof (savefile)); break; #ifdef HAVE_OPENSSL case 'S':