Commit 3d31cea2 authored by Jiri Popelka's avatar Jiri Popelka

Use safe_strncpy wherever possible

to avoid string overflow and to make sure the string
is terminated.

- link mii-tool and nameif against lib/
- remove xmalloc() from nameif because it's defined also in util.c
parent 60674bcb
...@@ -155,8 +155,8 @@ subdirs: libdir ...@@ -155,8 +155,8 @@ subdirs: libdir
ifconfig: $(NET_LIB) ifconfig.o ifconfig: $(NET_LIB) ifconfig.o
$(CC) $(LDFLAGS) -o ifconfig ifconfig.o $(NLIB) $(RESLIB) $(CC) $(LDFLAGS) -o ifconfig ifconfig.o $(NLIB) $(RESLIB)
nameif: nameif.o nameif: $(NET_LIB) nameif.o
$(CC) $(LDFLAGS) -o nameif nameif.o $(CC) $(LDFLAGS) -o nameif nameif.o $(NLIB) $(RESLIB)
hostname: hostname.o hostname: hostname.o
$(CC) $(LDFLAGS) -o hostname hostname.o $(DNLIB) $(CC) $(LDFLAGS) -o hostname hostname.o $(DNLIB)
...@@ -185,8 +185,8 @@ iptunnel: $(NET_LIB) iptunnel.o ...@@ -185,8 +185,8 @@ iptunnel: $(NET_LIB) iptunnel.o
ipmaddr: $(NET_LIB) ipmaddr.o ipmaddr: $(NET_LIB) ipmaddr.o
$(CC) $(LDFLAGS) -o ipmaddr ipmaddr.o $(NLIB) $(RESLIB) $(CC) $(LDFLAGS) -o ipmaddr ipmaddr.o $(NLIB) $(RESLIB)
mii-tool: mii-tool.o mii-tool: $(NET_LIB) mii-tool.o
$(CC) $(LDFLAGS) -o mii-tool mii-tool.o $(CC) $(LDFLAGS) -o mii-tool mii-tool.o $(NLIB) $(RESLIB)
installbin: installbin:
@echo @echo
......
...@@ -178,7 +178,7 @@ static int arp_del(char **args) ...@@ -178,7 +178,7 @@ static int arp_del(char **args)
if (*++args == NULL) if (*++args == NULL)
usage(); usage();
if (strcmp(*args, "255.255.255.255") != 0) { if (strcmp(*args, "255.255.255.255") != 0) {
strcpy(host, *args); safe_strncpy(host, *args, (sizeof host));
if (ap->input(0, host, sa) < 0) { if (ap->input(0, host, sa) < 0) {
ap->herror(host); ap->herror(host);
return (-1); return (-1);
...@@ -243,7 +243,7 @@ static int arp_getdevhw(char *ifname, struct sockaddr *sa, struct hwtype *hw) ...@@ -243,7 +243,7 @@ static int arp_getdevhw(char *ifname, struct sockaddr *sa, struct hwtype *hw)
struct ifreq ifr; struct ifreq ifr;
struct hwtype *xhw; struct hwtype *xhw;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) { if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
fprintf(stderr, _("arp: cant get HW-Address for `%s': %s.\n"), ifname, strerror(errno)); fprintf(stderr, _("arp: cant get HW-Address for `%s': %s.\n"), ifname, strerror(errno));
return (-1); return (-1);
...@@ -355,7 +355,7 @@ static int arp_set(char **args) ...@@ -355,7 +355,7 @@ static int arp_set(char **args)
if (*++args == NULL) if (*++args == NULL)
usage(); usage();
if (strcmp(*args, "255.255.255.255") != 0) { if (strcmp(*args, "255.255.255.255") != 0) {
strcpy(host, *args); safe_strncpy(host, *args, (sizeof host));
if (ap->input(0, host, sa) < 0) { if (ap->input(0, host, sa) < 0) {
ap->herror(host); ap->herror(host);
return (-1); return (-1);
......
...@@ -99,7 +99,7 @@ static int do_ioctl_get_ifindex(char *dev) ...@@ -99,7 +99,7 @@ static int do_ioctl_get_ifindex(char *dev)
int fd; int fd;
int err; int err;
strcpy(ifr.ifr_name, dev); safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGIFINDEX, &ifr); err = ioctl(fd, SIOCGIFINDEX, &ifr);
if (err) { if (err) {
...@@ -117,7 +117,7 @@ static int do_ioctl_get_iftype(char *dev) ...@@ -117,7 +117,7 @@ static int do_ioctl_get_iftype(char *dev)
int fd; int fd;
int err; int err;
strcpy(ifr.ifr_name, dev); safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGIFHWADDR, &ifr); err = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (err) { if (err) {
...@@ -156,7 +156,7 @@ static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p) ...@@ -156,7 +156,7 @@ static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
int fd; int fd;
int err; int err;
strcpy(ifr.ifr_name, basedev); safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p; ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGETTUNNEL, &ifr); err = ioctl(fd, SIOCGETTUNNEL, &ifr);
...@@ -172,7 +172,7 @@ static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p) ...@@ -172,7 +172,7 @@ static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
int fd; int fd;
int err; int err;
strcpy(ifr.ifr_name, basedev); safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p; ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, cmd, &ifr); err = ioctl(fd, cmd, &ifr);
...@@ -188,7 +188,7 @@ static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p) ...@@ -188,7 +188,7 @@ static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
int fd; int fd;
int err; int err;
strcpy(ifr.ifr_name, basedev); safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p; ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0); fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCDELTUNNEL, &ifr); err = ioctl(fd, SIOCDELTUNNEL, &ifr);
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "net-locale.h" #include "net-locale.h"
#endif #endif
#include "intl.h" #include "intl.h"
#include "util.h"
static char X25_errmsg[128]; static char X25_errmsg[128];
...@@ -88,7 +89,9 @@ X25_input(int type, char *bufp, struct sockaddr *sap) ...@@ -88,7 +89,9 @@ X25_input(int type, char *bufp, struct sockaddr *sap)
/* Address the correct length ? */ /* Address the correct length ? */
if (strlen(bufp)>18) { if (strlen(bufp)>18) {
strcpy(X25_errmsg, _("Address can't exceed eighteen digits with sigdigits")); safe_strncpy(X25_errmsg,
_("Address can't exceed eighteen digits with sigdigits"),
sizeof(X25_errmsg));
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig); fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
#endif #endif
...@@ -107,7 +110,7 @@ X25_input(int type, char *bufp, struct sockaddr *sap) ...@@ -107,7 +110,7 @@ X25_input(int type, char *bufp, struct sockaddr *sap)
if (strlen(bufp) < 1 || strlen(bufp) > 15 || sigdigits > strlen(bufp)) { if (strlen(bufp) < 1 || strlen(bufp) > 15 || sigdigits > strlen(bufp)) {
if (p != NULL) if (p != NULL)
*p = '/'; *p = '/';
strcpy(X25_errmsg, _("Invalid address")); safe_strncpy(X25_errmsg, _("Invalid address"), sizeof(X25_errmsg));
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig); fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
#endif #endif
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "net-locale.h" #include "net-locale.h"
#endif #endif
#include "intl.h" #include "intl.h"
#include "util.h"
#include "net-features.h" #include "net-features.h"
...@@ -64,7 +65,7 @@ static int X25_setroute(int action, int options, char **args) ...@@ -64,7 +65,7 @@ static int X25_setroute(int action, int options, char **args)
if (*args == NULL) if (*args == NULL)
return(usage()); return(usage());
strcpy(target, *args++); safe_strncpy(target, *args++, sizeof(target));
/* Clean out the x25_route_struct structure. */ /* Clean out the x25_route_struct structure. */
memset((char *) &rt, 0, sizeof(rt)); memset((char *) &rt, 0, sizeof(rt));
...@@ -89,7 +90,7 @@ static int X25_setroute(int action, int options, char **args) ...@@ -89,7 +90,7 @@ static int X25_setroute(int action, int options, char **args)
return(usage()); return(usage());
if (rt.device[0]) if (rt.device[0])
return(usage()); return(usage());
strcpy(rt.device, *args); safe_strncpy(rt.device, *args, sizeof(rt.device));
args++; args++;
} }
if (rt.device[0]=='\0') if (rt.device[0]=='\0')
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include <linux/sockios.h> #include <linux/sockios.h>
#include "version.h" #include "version.h"
#include "net-support.h" #include "net-support.h"
#include "util.h"
static char *Release = RELEASE, *Signature = "David Hinds based on Donald Becker's mii-diag"; static char *Release = RELEASE, *Signature = "David Hinds based on Donald Becker's mii-diag";
...@@ -384,7 +385,7 @@ static int do_one_xcvr(int skfd, char *ifname, int maybe) ...@@ -384,7 +385,7 @@ static int do_one_xcvr(int skfd, char *ifname, int maybe)
struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data; struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
/* Get the vitals from the interface. */ /* Get the vitals from the interface. */
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) { if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
if (!maybe || (errno != ENODEV)) if (!maybe || (errno != ENODEV))
fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n", fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
...@@ -434,7 +435,7 @@ static void watch_one_xcvr(int skfd, char *ifname, int index) ...@@ -434,7 +435,7 @@ static void watch_one_xcvr(int skfd, char *ifname, int index)
int now; int now;
/* Get the vitals from the interface. */ /* Get the vitals from the interface. */
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) { if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0) {
if (errno != ENODEV) if (errno != ENODEV)
fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n", fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <errno.h> #include <errno.h>
#include "intl.h" #include "intl.h"
#include "net-support.h" #include "net-support.h"
#include "util.h"
const char default_conf[] = "/etc/mactab"; const char default_conf[] = "/etc/mactab";
const char *fname = default_conf; const char *fname = default_conf;
...@@ -78,13 +79,6 @@ int parsemac(char *str, unsigned char *mac) ...@@ -78,13 +79,6 @@ int parsemac(char *str, unsigned char *mac)
return 0; return 0;
} }
void *xmalloc(unsigned sz)
{
void *p = calloc(sz,1);
if (!p) errno=ENOMEM, err("xmalloc");
return p;
}
void opensock(void) void opensock(void)
{ {
if (ctl_sk < 0) if (ctl_sk < 0)
...@@ -100,8 +94,8 @@ int setname(char *oldname, char *newname) ...@@ -100,8 +94,8 @@ int setname(char *oldname, char *newname)
struct ifreq ifr; struct ifreq ifr;
opensock(); opensock();
memset(&ifr,0,sizeof(struct ifreq)); memset(&ifr,0,sizeof(struct ifreq));
strncpy(ifr.ifr_name, oldname, IFNAMSIZ); safe_strncpy(ifr.ifr_name, oldname, IFNAMSIZ);
strncpy(ifr.ifr_newname, newname, IFNAMSIZ); safe_strncpy(ifr.ifr_newname, newname, IFNAMSIZ);
return ioctl(ctl_sk, SIOCSIFNAME, &ifr); return ioctl(ctl_sk, SIOCSIFNAME, &ifr);
} }
...@@ -111,7 +105,7 @@ int getmac(char *name, unsigned char *mac) ...@@ -111,7 +105,7 @@ int getmac(char *name, unsigned char *mac)
struct ifreq ifr; struct ifreq ifr;
opensock(); opensock();
memset(&ifr,0,sizeof(struct ifreq)); memset(&ifr,0,sizeof(struct ifreq));
strcpy(ifr.ifr_name, name); safe_strncpy(ifr.ifr_name, name, IFNAMSIZ);
r = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr); r = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6); memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
return r; return r;
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include "intl.h" #include "intl.h"
#include "net-support.h" #include "net-support.h"
#include "version.h" #include "version.h"
#include "util.h"
int skfd = -1; int skfd = -1;
...@@ -100,7 +101,7 @@ int main(int argc, char **argv) ...@@ -100,7 +101,7 @@ int main(int argc, char **argv)
usage(); usage();
spp = argv; spp = argv;
strncpy(ifr.ifr_name, *spp++, IFNAMSIZ); safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
plip=(struct plipconf *)&ifr.ifr_data; plip=(struct plipconf *)&ifr.ifr_data;
plip->pcmd = PLIP_GET_TIMEOUT; /* get current settings for device */ plip->pcmd = PLIP_GET_TIMEOUT; /* get current settings for device */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment