From: Roy Marples Date: Thu, 5 Apr 2007 16:56:58 +0000 (+0000) Subject: Add safe_strncpy function to ensure everything is NULL terminated X-Git-Tag: v3.2.3~292 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=235ce0896deb6ce04326f8a1514f0b600b7c8dc1;p=thirdparty%2Fdhcpcd.git Add safe_strncpy function to ensure everything is NULL terminated --- diff --git a/common.c b/common.c index c7a50e02..8246b8e1 100644 --- a/common.c +++ b/common.c @@ -27,6 +27,16 @@ #include "common.h" #include "logger.h" +/* A way of safely handling strncpy */ +char *safe_strncpy (char *dst, const char *src, size_t size) +{ + if (! size) + return dst; + + dst[--size] = '\0'; + return strncpy (dst, src, size); +} + /* This requires us to link to rt on glibc, so we use sysinfo instead */ #ifdef __linux__ #include diff --git a/common.h b/common.h index 3ef15804..e788e0bb 100644 --- a/common.h +++ b/common.h @@ -22,7 +22,9 @@ #ifndef COMMON_H #define COMMON_H +char *safe_strncpy (char *dst, const char *src, size_t size); long uptime (void); void *xmalloc (size_t size); + #endif diff --git a/configure.c b/configure.c index 3ea93db7..a50b6880 100644 --- a/configure.c +++ b/configure.c @@ -689,7 +689,7 @@ int configure (const options_t *options, interface_t *iface, || strcmp (curhostname, "localhost") == 0) { if (dhcp->hostname) - strcpy (newhostname, dhcp->hostname); + safe_strncpy (newhostname, dhcp->hostname, sizeof (newhostname)); if (*newhostname) { diff --git a/dhcp.c b/dhcp.c index bd7289a1..31e385f2 100644 --- a/dhcp.c +++ b/dhcp.c @@ -513,7 +513,8 @@ int parse_dhcpmessage (dhcp_t *dhcp, const dhcpmessage_t *message) memset (first_route, 0, sizeof (route_t)); dhcp->address.s_addr = message->yiaddr; - strcpy (dhcp->servername, message->servername); + safe_strncpy (dhcp->servername, message->servername, + sizeof (dhcp->servername)); #define LEN_ERR \ { \ diff --git a/dhcpcd.c b/dhcpcd.c index 1d8fed25..ff74da03 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -206,7 +206,7 @@ int main(int argc, char **argv) exit (EXIT_FAILURE); } else - strcpy (options.hostname, optarg); + safe_strncpy (options.hostname, optarg, sizeof (options.hostname)); break; case 'i': if (strlen (optarg) > CLASS_ID_MAX_LEN) @@ -334,7 +334,8 @@ int main(int argc, char **argv) argv[optind], IF_NAMESIZE); exit (EXIT_FAILURE); } - strcpy (options.interface, argv[optind]); + safe_strncpy (options.interface, argv[optind], + sizeof (options.interface)); } else { diff --git a/interface.c b/interface.c index 7dce0500..3f4e2481 100644 --- a/interface.c +++ b/interface.c @@ -190,7 +190,7 @@ interface_t *read_interface (const char *ifname, int metric) #endif memset (&ifr, 0, sizeof (struct ifreq)); - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { logger (LOG_ERR, "socket: %s", strerror (errno)); @@ -200,7 +200,7 @@ interface_t *read_interface (const char *ifname, int metric) #ifdef __linux__ /* Do something with the metric parameter to satisfy the compiler warning */ metric = 0; - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl (s, SIOCGIFHWADDR, &ifr) <0) { logger (LOG_ERR, "ioctl SIOCGIFHWADDR: %s", strerror (errno)); @@ -227,7 +227,7 @@ interface_t *read_interface (const char *ifname, int metric) family = ifr.ifr_hwaddr.sa_family; #else ifr.ifr_metric = metric; - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl (s, SIOCSIFMETRIC, &ifr) < 0) { logger (LOG_ERR, "ioctl SIOCSIFMETRIC: %s", strerror (errno)); @@ -236,7 +236,7 @@ interface_t *read_interface (const char *ifname, int metric) } #endif - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(s, SIOCGIFMTU, &ifr) < 0) { logger (LOG_ERR, "ioctl SIOCGIFMTU: %s", strerror (errno)); @@ -247,7 +247,7 @@ interface_t *read_interface (const char *ifname, int metric) { logger (LOG_DEBUG, "MTU of %d is too low, setting to %d", ifr.ifr_mtu, MTU_MIN); ifr.ifr_mtu = MTU_MIN; - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(s, SIOCSIFMTU, &ifr) < 0) { logger (LOG_ERR, "ioctl SIOCSIFMTU,: %s", strerror (errno)); @@ -257,7 +257,7 @@ interface_t *read_interface (const char *ifname, int metric) } mtu = ifr.ifr_mtu; - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { logger (LOG_ERR, "ioctl SIOCGIFFLAGS: %s", strerror (errno)); @@ -266,7 +266,7 @@ interface_t *read_interface (const char *ifname, int metric) } ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) { logger (LOG_ERR, "ioctl SIOCSIFFLAGS: %s", strerror (errno)); @@ -278,7 +278,7 @@ interface_t *read_interface (const char *ifname, int metric) iface = xmalloc (sizeof (interface_t)); memset (iface, 0, sizeof (interface_t)); - strncpy (iface->name, ifname, IF_NAMESIZE); + safe_strncpy (iface->name, ifname, IF_NAMESIZE); snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname); memcpy (&iface->hwaddr, hwaddr, hwlen); iface->hwlen = hwlen; @@ -309,7 +309,7 @@ int get_mtu (const char *ifname) } memset (&ifr, 0, sizeof (struct ifreq)); - strcpy (ifr.ifr_name, ifname); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); r = ioctl (s, SIOCGIFMTU, &ifr); close (s); @@ -336,7 +336,7 @@ int set_mtu (const char *ifname, short int mtu) memset (&ifr, 0, sizeof (struct ifreq)); logger (LOG_DEBUG, "setting MTU to %d", mtu); - strcpy (ifr.ifr_name, ifname); + safe_strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name)); ifr.ifr_mtu = mtu; r = ioctl (s, SIOCSIFMTU, &ifr); close (s); @@ -365,7 +365,7 @@ static int do_address (const char *ifname, struct in_addr address, } memset (&ifa, 0, sizeof (ifa)); - strcpy (ifa.ifra_name, ifname); + safe_strncpy (ifa.ifra_name, ifname, sizeof (ifa.ifra_name)); #define ADDADDR(_var, _addr) \ { \ diff --git a/socket.c b/socket.c index 35ba6586..f2d8a25c 100644 --- a/socket.c +++ b/socket.c @@ -262,7 +262,7 @@ int open_socket (interface_t *iface, bool arp) } memset (&ifr, 0, sizeof (struct ifreq)); - strncpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); + safe_strncpy (ifr.ifr_name, iface->name, sizeof (ifr.ifr_name)); if (ioctl (fd, BIOCSETIF, &ifr) < 0) { logger (LOG_ERR, "cannot attach interface `%s' to bpf device `%s': %s",