]> The Tcpdump Group git mirrors - libpcap/commitdiff
Use pcap_strlcpy() when copying a string to a fixed-length buffer.
authorGuy Harris <[email protected]>
Thu, 3 Oct 2019 18:13:37 +0000 (11:13 -0700)
committerGuy Harris <[email protected]>
Thu, 3 Oct 2019 18:13:37 +0000 (11:13 -0700)
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.

rpcapd/fileconf.c
rpcapd/rpcapd.c

index 76d59334dbbb5dd026a2e7990051619da566ca10..b79dda186e68c46da10901df42dd7a6e8c278e55 100644 (file)
@@ -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 = <name or address>\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)
index 5b6c14cd1404ebe320ebc1d2df70c086b9488f34..ecb88955d3c68a6ba0585805eda175babf5c6273 100644 (file)
@@ -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':