changeset 4505:b64cc1ef18bb draft

sun: More validation around route messages
author Roy Marples <roy@marples.name>
date Sat, 04 May 2019 08:17:01 +0000
parents d2b939b8b5bc
children 18ac581d4185
files src/if-sun.c
diffstat 1 files changed, 20 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/if-sun.c	Fri May 03 15:44:13 2019 +0000
+++ b/src/if-sun.c	Sat May 04 08:17:01 2019 +0000
@@ -637,7 +637,7 @@
 {
 	const struct sockaddr *rti_info[RTAX_MAX];
 
-	if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY))
+	if (~rtm->rtm_addrs & RTA_DST)
 		return -1;
 
 	/* We have already checked that at least one address must be
@@ -654,7 +654,7 @@
 	if (rtm->rtm_addrs & RTA_NETMASK)
 		COPYSA(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
 	/* dhcpcd likes an unspecified gateway to indicate via the link. */
-	if (rt->rt_flags & RTF_GATEWAY &&
+	if (rtm->rtm_addrs & RTA_GATEWAY &&
 	    rti_info[RTAX_GATEWAY]->sa_family != AF_LINK)
 		COPYSA(&rt->rt_gateway, rti_info[RTAX_GATEWAY]);
 	if (rtm->rtm_addrs & RTA_SRC)
@@ -710,7 +710,7 @@
 	return rt;
 }
 
-static void
+static int
 if_finishrt(struct dhcpcd_ctx *ctx, struct rt *rt)
 {
 	int mtu;
@@ -754,10 +754,8 @@
 	if (rt->rt_ifp == NULL) {
 		if (if_route_get(ctx, rt) == NULL) {
 			rt->rt_ifp = if_loopback(ctx);
-			if (rt->rt_ifp == NULL) {
-				logerr(__func__);
-				return;
-			}
+			if (rt->rt_ifp == NULL)
+				return - 1;
 		}
 	}
 
@@ -766,8 +764,12 @@
 	 * This confuses dhcpcd as it expects MTU to be 0
 	 * when no explicit MTU has been set. */
 	mtu = if_getmtu(rt->rt_ifp);
+	if (mtu == -1)
+		return -1;
 	if (rt->rt_mtu == (unsigned int)mtu)
 		rt->rt_mtu = 0;
+
+	return 0;
 }
 
 static uint64_t
@@ -832,10 +834,10 @@
 	}
 #endif
 
-	if (if_copyrt(ctx, &rt, rtm) == -1)
+	if (if_copyrt(ctx, &rt, rtm) == -1 && errno != ESRCH)
 		return -1;
-
-	if_finishrt(ctx, &rt);
+	if (if_finishrt(ctx, &rt) == -1)
+		return -1;
 	rt_recvrt(rtm->rtm_type, &rt, rtm->rtm_pid);
 	return 0;
 }
@@ -1366,8 +1368,10 @@
 		rt.rt_mtu = re->ipRouteInfo.re_max_frag;
 		if_octetstr(ifname, &re->ipRouteIfIndex, sizeof(ifname));
 		rt.rt_ifp = if_find(ctx->ifaces, ifname);
-		if_finishrt(ctx, &rt);
-		rt_recvrt(RTM_ADD, &rt, 0);
+		if (if_finishrt(ctx, &rt) == -1)
+			logerr(__func__);
+		else
+			rt_recvrt(RTM_ADD, &rt, 0);
 	} while (++re < e);
 	return 0;
 }
@@ -1413,8 +1417,10 @@
 		rt.rt_mtu = re->ipv6RouteInfo.re_max_frag;
 		if_octetstr(ifname, &re->ipv6RouteIfIndex, sizeof(ifname));
 		rt.rt_ifp = if_find(ctx->ifaces, ifname);
-		if_finishrt(ctx, &rt);
-		rt_recvrt(RTM_ADD, &rt, 0);
+		if (if_finishrt(ctx, &rt) == -1)
+			logerr(__func__);
+		else
+			rt_recvrt(RTM_ADD, &rt, 0);
 	} while (++re < e);
 	return 0;
 }