summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorMark Johnston <markj@FreeBSD.org>2020-12-02 16:46:45 +0000
committerMark Johnston <markj@FreeBSD.org>2020-12-02 16:46:45 +0000
commitecce515d54bcea54fea03f731aad646c87761d22 (patch)
tree87bc786222a86084554aa5534db17473992c2927 /usr.sbin
parente997614fd283b2750cf087f4628323190a8262e1 (diff)
downloadsrc-test2-ecce515d54bcea54fea03f731aad646c87761d22.tar.gz
src-test2-ecce515d54bcea54fea03f731aad646c87761d22.zip
rtsold: Fix bugs reported by Coverity
- Avoid leaking a socket if llflags_get() fails. - Avoid leaking a file handle if rtsold_init_dumpfile() fails. - Tighten the check in if_nametosdl() which determines whether we failed to find the specified interface. - Fix errno handling in an error path in rtsock_open(). MFC after: 1 week
Notes
Notes: svn path=/head/; revision=368278
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/rtsold/cap_llflags.c31
-rw-r--r--usr.sbin/rtsold/dump.c1
-rw-r--r--usr.sbin/rtsold/if.c2
-rw-r--r--usr.sbin/rtsold/rtsock.c2
4 files changed, 21 insertions, 15 deletions
diff --git a/usr.sbin/rtsold/cap_llflags.c b/usr.sbin/rtsold/cap_llflags.c
index b2c07df7379d..195f83893b4d 100644
--- a/usr.sbin/rtsold/cap_llflags.c
+++ b/usr.sbin/rtsold/cap_llflags.c
@@ -72,9 +72,12 @@ llflags_get(const char *ifname, int *flagsp)
if (s < 0)
return (-1);
- if (getifaddrs(&ifap) != 0)
- return (-1);
- error = -1;
+ ifap = NULL;
+ if (getifaddrs(&ifap) != 0) {
+ error = errno;
+ goto out;
+ }
+ error = ENOENT;
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifa->ifa_name, ifname) != 0)
continue;
@@ -88,27 +91,29 @@ llflags_get(const char *ifname, int *flagsp)
memset(&ifr6, 0, sizeof(ifr6));
if (strlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name)) >=
sizeof(ifr6.ifr_name)) {
- freeifaddrs(ifap);
- errno = EINVAL;
- return (-1);
+ error = errno;
+ goto out;
}
memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
error = errno;
- freeifaddrs(ifap);
- errno = error;
- return (-1);
+ goto out;
}
*flagsp = ifr6.ifr_ifru.ifru_flags6;
error = 0;
break;
}
+out:
(void)close(s);
- freeifaddrs(ifap);
- if (error == -1)
- errno = ENOENT;
- return (error);
+ if (ifap != NULL)
+ freeifaddrs(ifap);
+ if (error != 0) {
+ errno = error;
+ return (-1);
+ } else {
+ return (0);
+ }
}
int
diff --git a/usr.sbin/rtsold/dump.c b/usr.sbin/rtsold/dump.c
index 65b4f979bd43..803a6c0d95d3 100644
--- a/usr.sbin/rtsold/dump.c
+++ b/usr.sbin/rtsold/dump.c
@@ -148,6 +148,7 @@ rtsold_init_dumpfile(const char *dumpfile)
if (caph_rights_limit(fileno(fp), &rights) != 0) {
warnmsg(LOG_WARNING, __func__, "caph_rights_limit(%s): %s",
dumpfile, strerror(errno));
+ (void)fclose(fp);
return (NULL);
}
return (fp);
diff --git a/usr.sbin/rtsold/if.c b/usr.sbin/rtsold/if.c
index 2cc5fa41c29b..b2f2c640b175 100644
--- a/usr.sbin/rtsold/if.c
+++ b/usr.sbin/rtsold/if.c
@@ -327,7 +327,7 @@ if_nametosdl(char *name)
}
}
}
- if (next == lim) {
+ if (next >= lim) {
/* search failed */
free(buf);
return (NULL);
diff --git a/usr.sbin/rtsold/rtsock.c b/usr.sbin/rtsold/rtsock.c
index 37f7df29d0b7..4dfbf61f47db 100644
--- a/usr.sbin/rtsold/rtsock.c
+++ b/usr.sbin/rtsold/rtsock.c
@@ -84,7 +84,7 @@ rtsock_open(void)
if (caph_rights_limit(s, &rights) != 0) {
error = errno;
(void)close(s);
- errno = errno;
+ errno = error;
return (-1);
}
return (s);