From 6183c0f2af55f1d2dade4d74cca0915aedce4a19 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 2 Jan 2018 17:17:57 +0000 Subject: [PATCH] Validate the prefix to be within range Signed-off-by: Michael Tremer --- src/network.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/network.c b/src/network.c index dc1775a..866b377 100644 --- a/src/network.c +++ b/src/network.c @@ -37,6 +37,18 @@ struct loc_network { uint32_t asn; }; +static int valid_prefix(struct in6_addr* address, unsigned int prefix) { + // The prefix cannot be larger than 128 bits + if (prefix > 128) + return 1; + + // And the prefix cannot be zero + if (prefix == 0) + return 1; + + return 0; +} + LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network, struct in6_addr start_address, unsigned int prefix) { // Address cannot be unspecified @@ -63,6 +75,12 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network return -EINVAL; } + // Validate the prefix + if (valid_prefix(&start_address, prefix) != 0) { + DEBUG(ctx, "Invalid prefix: %u\n", prefix); + return -EINVAL; + } + struct loc_network* n = calloc(1, sizeof(*n)); if (!n) return -ENOMEM; -- 2.39.2