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
ifconfig: $(NET_LIB) ifconfig.o
$(CC) $(LDFLAGS) -o ifconfig ifconfig.o $(NLIB) $(RESLIB)
nameif: nameif.o
$(CC) $(LDFLAGS) -o nameif nameif.o
nameif: $(NET_LIB) nameif.o
$(CC) $(LDFLAGS) -o nameif nameif.o $(NLIB) $(RESLIB)
hostname: hostname.o
$(CC) $(LDFLAGS) -o hostname hostname.o $(DNLIB)
......@@ -185,8 +185,8 @@ iptunnel: $(NET_LIB) iptunnel.o
ipmaddr: $(NET_LIB) ipmaddr.o
$(CC) $(LDFLAGS) -o ipmaddr ipmaddr.o $(NLIB) $(RESLIB)
mii-tool: mii-tool.o
$(CC) $(LDFLAGS) -o mii-tool mii-tool.o
mii-tool: $(NET_LIB) mii-tool.o
$(CC) $(LDFLAGS) -o mii-tool mii-tool.o $(NLIB) $(RESLIB)
installbin:
@echo
......
......@@ -178,7 +178,7 @@ static int arp_del(char **args)
if (*++args == NULL)
usage();
if (strcmp(*args, "255.255.255.255") != 0) {
strcpy(host, *args);
safe_strncpy(host, *args, (sizeof host));
if (ap->input(0, host, sa) < 0) {
ap->herror(host);
return (-1);
......@@ -243,7 +243,7 @@ static int arp_getdevhw(char *ifname, struct sockaddr *sa, struct hwtype *hw)
struct ifreq ifr;
struct hwtype *xhw;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
safe_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
fprintf(stderr, _("arp: cant get HW-Address for `%s': %s.\n"), ifname, strerror(errno));
return (-1);
......@@ -355,7 +355,7 @@ static int arp_set(char **args)
if (*++args == NULL)
usage();
if (strcmp(*args, "255.255.255.255") != 0) {
strcpy(host, *args);
safe_strncpy(host, *args, (sizeof host));
if (ap->input(0, host, sa) < 0) {
ap->herror(host);
return (-1);
......
......@@ -99,7 +99,7 @@ static int do_ioctl_get_ifindex(char *dev)
int fd;
int err;
strcpy(ifr.ifr_name, dev);
safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGIFINDEX, &ifr);
if (err) {
......@@ -117,7 +117,7 @@ static int do_ioctl_get_iftype(char *dev)
int fd;
int err;
strcpy(ifr.ifr_name, dev);
safe_strncpy(ifr.ifr_name, dev, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (err) {
......@@ -156,7 +156,7 @@ static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
int fd;
int err;
strcpy(ifr.ifr_name, basedev);
safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGETTUNNEL, &ifr);
......@@ -172,7 +172,7 @@ static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
int fd;
int err;
strcpy(ifr.ifr_name, basedev);
safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, cmd, &ifr);
......@@ -188,7 +188,7 @@ static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
int fd;
int err;
strcpy(ifr.ifr_name, basedev);
safe_strncpy(ifr.ifr_name, basedev, IFNAMSIZ);
ifr.ifr_ifru.ifru_data = (void*)p;
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCDELTUNNEL, &ifr);
......
......@@ -38,6 +38,7 @@
#include "net-locale.h"
#endif
#include "intl.h"
#include "util.h"
static char X25_errmsg[128];
......@@ -88,7 +89,9 @@ X25_input(int type, char *bufp, struct sockaddr *sap)
/* Address the correct length ? */
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
fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
#endif
......@@ -107,7 +110,7 @@ X25_input(int type, char *bufp, struct sockaddr *sap)
if (strlen(bufp) < 1 || strlen(bufp) > 15 || sigdigits > strlen(bufp)) {
if (p != NULL)
*p = '/';
strcpy(X25_errmsg, _("Invalid address"));
safe_strncpy(X25_errmsg, _("Invalid address"), sizeof(X25_errmsg));
#ifdef DEBUG
fprintf(stderr, "x25_input(%s): %s !\n", X25_errmsg, orig);
#endif
......
......@@ -38,6 +38,7 @@
#include "net-locale.h"
#endif
#include "intl.h"
#include "util.h"
#include "net-features.h"
......@@ -64,7 +65,7 @@ static int X25_setroute(int action, int options, char **args)
if (*args == NULL)
return(usage());
strcpy(target, *args++);
safe_strncpy(target, *args++, sizeof(target));
/* Clean out the x25_route_struct structure. */
memset((char *) &rt, 0, sizeof(rt));
......@@ -89,7 +90,7 @@ static int X25_setroute(int action, int options, char **args)
return(usage());
if (rt.device[0])
return(usage());
strcpy(rt.device, *args);
safe_strncpy(rt.device, *args, sizeof(rt.device));
args++;
}
if (rt.device[0]=='\0')
......
......@@ -54,6 +54,7 @@
#include <linux/sockios.h>
#include "version.h"
#include "net-support.h"
#include "util.h"
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)
struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
/* 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 (!maybe || (errno != ENODEV))
fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
......@@ -434,7 +435,7 @@ static void watch_one_xcvr(int skfd, char *ifname, int index)
int now;
/* 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 (errno != ENODEV)
fprintf(stderr, "SIOCGMIIPHY on '%s' failed: %s\n",
......
......@@ -23,6 +23,7 @@
#include <errno.h>
#include "intl.h"
#include "net-support.h"
#include "util.h"
const char default_conf[] = "/etc/mactab";
const char *fname = default_conf;
......@@ -78,13 +79,6 @@ int parsemac(char *str, unsigned char *mac)
return 0;
}
void *xmalloc(unsigned sz)
{
void *p = calloc(sz,1);
if (!p) errno=ENOMEM, err("xmalloc");
return p;
}
void opensock(void)
{
if (ctl_sk < 0)
......@@ -100,8 +94,8 @@ int setname(char *oldname, char *newname)
struct ifreq ifr;
opensock();
memset(&ifr,0,sizeof(struct ifreq));
strncpy(ifr.ifr_name, oldname, IFNAMSIZ);
strncpy(ifr.ifr_newname, newname, IFNAMSIZ);
safe_strncpy(ifr.ifr_name, oldname, IFNAMSIZ);
safe_strncpy(ifr.ifr_newname, newname, IFNAMSIZ);
return ioctl(ctl_sk, SIOCSIFNAME, &ifr);
}
......@@ -111,7 +105,7 @@ int getmac(char *name, unsigned char *mac)
struct ifreq ifr;
opensock();
memset(&ifr,0,sizeof(struct ifreq));
strcpy(ifr.ifr_name, name);
safe_strncpy(ifr.ifr_name, name, IFNAMSIZ);
r = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
return r;
......
......@@ -42,6 +42,7 @@
#include "intl.h"
#include "net-support.h"
#include "version.h"
#include "util.h"
int skfd = -1;
......@@ -100,7 +101,7 @@ int main(int argc, char **argv)
usage();
spp = argv;
strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
plip=(struct plipconf *)&ifr.ifr_data;
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