From: Roy Marples Date: Tue, 23 Aug 2022 12:57:17 +0000 (+0100) Subject: BSD: Fix an error parsing the routing table X-Git-Tag: v10.0.0~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b11859ac4bf584e49bf3a21700b44c90c8a6f89;p=thirdparty%2Fdhcpcd.git BSD: Fix an error parsing the routing table Also report any errors reading the routing table. --- diff --git a/src/if-bsd.c b/src/if-bsd.c index 67b65785..b54715a0 100644 --- a/src/if-bsd.c +++ b/src/if-bsd.c @@ -919,25 +919,18 @@ int if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) { struct rt_msghdr *rtm; - int mib[6]; + int mib[6] = { CTL_NET, PF_ROUTE, 0, af, NET_RT_DUMP, 0 }; size_t needed; char *buf, *p, *end; struct rt rt, *rtn; - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = af; - mib[4] = NET_RT_DUMP; - mib[5] = 0; - - if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) + if (sysctl(mib, __arraycount(mib), NULL, &needed, NULL, 0) == -1) return -1; if (needed == 0) return 0; if ((buf = malloc(needed)) == NULL) return -1; - if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { + if (sysctl(mib, __arraycount(mib), buf, &needed, NULL, 0) == -1) { free(buf); return -1; } @@ -945,7 +938,7 @@ if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) end = buf + needed; for (p = buf; p < end; p += rtm->rtm_msglen) { rtm = (void *)p; - if (p + rtm->rtm_msglen >= end) { + if (p + sizeof(*rtm) > end || p + rtm->rtm_msglen > end) { errno = EINVAL; break; } @@ -1650,12 +1643,11 @@ inet6_sysctl(int code, int val, int action) mib[3] = code; size = sizeof(val); if (action) { - if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), - NULL, 0, &val, size) == -1) + if (sysctl(mib, __arraycount(mib), NULL, 0, &val, size) == -1) return -1; return 0; } - if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1) + if (sysctl(mib, __arraycount(mib), &val, &size, NULL, 0) == -1) return -1; return val; } diff --git a/src/route.c b/src/route.c index ef9c4125..5e617301 100644 --- a/src/route.c +++ b/src/route.c @@ -711,7 +711,8 @@ rt_build(struct dhcpcd_ctx *ctx, int af) rb_tree_init(&routes, &rt_compare_proto_ops); rb_tree_init(&added, &rt_compare_os_ops); rb_tree_init(&kroutes, &rt_compare_os_ops); - if_initrt(ctx, &kroutes, af); + if (if_initrt(ctx, &kroutes, af) != 0) + logerr("%s: if_initrt", __func__); ctx->rt_order = 0; ctx->options |= DHCPCD_RTBUILD;