]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
BSD: Fix an error parsing the routing table
authorRoy Marples <roy@marples.name>
Tue, 23 Aug 2022 12:57:17 +0000 (13:57 +0100)
committerRoy Marples <roy@marples.name>
Tue, 23 Aug 2022 12:57:17 +0000 (13:57 +0100)
Also report any errors reading the routing table.

src/if-bsd.c
src/route.c

index 67b65785695415afc19ffa83a32e520911fd0339..b54715a0c462f666a9b9d26f6ed0aa0c15d136c7 100644 (file)
@@ -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;
 }
index ef9c412580001a761c7ecad54e825a4e58f80c17..5e61730148f61df2de5194bc66b666001c4e9f84 100644 (file)
@@ -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;