From 82c097df2f838515cf18ecb0f83551a5b5361760 Mon Sep 17 00:00:00 2001 From: asomers Date: Wed, 3 May 2017 17:21:01 +0000 Subject: [PATCH] Various Coverity fixes in ifconfig(8) * Exit early if kldload(2) fails (1011259). This is the only change that affects ifconfig's behavior. * Close memory and resource leaks (1305624, 1305205, 1007100) * Mark usage() as _Noreturn (1305806, 1305750) * Fix some dereference after null checks (1011474, 270774) Reported by: Coverity CID: 1305624, 1305205, 1007100, 1305806, 1305750, 1011474, CID: 270774, 1011259 Reviewed by: cem MFC after: 3 weeks Sponsored by: Spectra Logic Corp Differential Revision: https://reviews.freebsd.org/D10587 --- sbin/ifconfig/af_inet6.c | 12 ++++++----- sbin/ifconfig/ifclone.c | 1 + sbin/ifconfig/ifconfig.c | 43 ++++++++++++++++++++-------------------- sbin/ifconfig/iflagg.c | 13 ++---------- sbin/ifconfig/ifpfsync.c | 1 + 5 files changed, 32 insertions(+), 38 deletions(-) diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c index d5a9246c4d2e..e93d94355eb8 100644 --- a/sbin/ifconfig/af_inet6.c +++ b/sbin/ifconfig/af_inet6.c @@ -349,12 +349,14 @@ in6_getaddr(const char *s, int which) bzero(&hints, sizeof(struct addrinfo)); hints.ai_family = AF_INET6; error = getaddrinfo(s, NULL, &hints, &res); + if (error != 0) { + if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) + errx(1, "%s: bad value", s); + } else { + bcopy(res->ai_addr, sin, res->ai_addrlen); + freeaddrinfo(res); + } } - if (error != 0) { - if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) - errx(1, "%s: bad value", s); - } else - bcopy(res->ai_addr, sin, res->ai_addrlen); } static int diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c index e25fa9b01232..b81e8d26af8b 100644 --- a/sbin/ifconfig/ifclone.c +++ b/sbin/ifconfig/ifclone.c @@ -87,6 +87,7 @@ list_cloners(void) putchar('\n'); free(buf); + close(s); } struct clone_defcb { diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 8f42c5a04de9..94ebfaf1ffb0 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -106,7 +106,7 @@ static int ifconfig(int argc, char *const *argv, int iscreate, static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl, struct ifaddrs *ifa); static void tunnel_status(int s); -static void usage(void); +static void usage(void) _Noreturn; static struct afswtch *af_getbyname(const char *name); static struct afswtch *af_getbyfamily(int af); @@ -802,26 +802,24 @@ ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) */ p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); } - if (p->c_u.c_func || p->c_u.c_func2) { - if (p->c_parameter == NEXTARG) { - if (argv[1] == NULL) - errx(1, "'%s' requires argument", - p->c_name); - p->c_u.c_func(argv[1], 0, s, afp); + if (p->c_parameter == NEXTARG && p->c_u.c_func) { + if (argv[1] == NULL) + errx(1, "'%s' requires argument", + p->c_name); + p->c_u.c_func(argv[1], 0, s, afp); + argc--, argv++; + } else if (p->c_parameter == OPTARG && p->c_u.c_func) { + p->c_u.c_func(argv[1], 0, s, afp); + if (argv[1] != NULL) argc--, argv++; - } else if (p->c_parameter == OPTARG) { - p->c_u.c_func(argv[1], 0, s, afp); - if (argv[1] != NULL) - argc--, argv++; - } else if (p->c_parameter == NEXTARG2) { - if (argc < 3) - errx(1, "'%s' requires 2 arguments", - p->c_name); - p->c_u.c_func2(argv[1], argv[2], s, afp); - argc -= 2, argv += 2; - } else - p->c_u.c_func(*argv, p->c_parameter, s, afp); - } + } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) { + if (argc < 3) + errx(1, "'%s' requires 2 arguments", + p->c_name); + p->c_u.c_func2(argv[1], argv[2], s, afp); + argc -= 2, argv += 2; + } else if (p->c_u.c_func) + p->c_u.c_func(*argv, p->c_parameter, s, afp); argc--, argv++; } @@ -1297,8 +1295,8 @@ printb(const char *s, unsigned v, const char *bits) printf("%s=%o", s, v); else printf("%s=%x", s, v); - bits++; if (bits) { + bits++; putchar('<'); while ((i = *bits++) != '\0') { if (v & (1 << (i-1))) { @@ -1377,7 +1375,8 @@ ifmaybeload(const char *name) } /* not present, we should try to load it */ - kldload(ifkind); + if (kldload(ifkind) < 0) + err(1, "kldload(%s)", ifkind); } static struct cmd basic_cmds[] = { diff --git a/sbin/ifconfig/iflagg.c b/sbin/ifconfig/iflagg.c index e1b50cfb9fbf..825acf69d339 100644 --- a/sbin/ifconfig/iflagg.c +++ b/sbin/ifconfig/iflagg.c @@ -200,24 +200,17 @@ static void lagg_status(int s) { struct lagg_protos lpr[] = LAGG_PROTOS; - struct lagg_reqport rp, rpbuf[LAGG_MAX_PORTS]; + struct lagg_reqport rpbuf[LAGG_MAX_PORTS]; struct lagg_reqall ra; struct lagg_reqopts ro; struct lagg_reqflags rf; struct lacp_opreq *lp; const char *proto = ""; - int i, isport = 0; + int i; - bzero(&rp, sizeof(rp)); bzero(&ra, sizeof(ra)); bzero(&ro, sizeof(ro)); - strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname)); - strlcpy(rp.rp_portname, name, sizeof(rp.rp_portname)); - - if (ioctl(s, SIOCGLAGGPORT, &rp) == 0) - isport = 1; - strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname)); ra.ra_size = sizeof(rpbuf); ra.ra_port = rpbuf; @@ -257,8 +250,6 @@ lagg_status(int s) sep = ","; } } - if (isport) - printf(" laggdev %s", rp.rp_ifname); putchar('\n'); if (verbose) { printf("\tlagg options:\n"); diff --git a/sbin/ifconfig/ifpfsync.c b/sbin/ifconfig/ifpfsync.c index 9dbe1d67a56b..82199ee5acee 100644 --- a/sbin/ifconfig/ifpfsync.c +++ b/sbin/ifconfig/ifpfsync.c @@ -120,6 +120,7 @@ setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp) if (ioctl(s, SIOCSETPFSYNC, (caddr_t)&ifr) == -1) err(1, "SIOCSETPFSYNC"); + freeaddrinfo(peerres); } /* ARGSUSED */