From: Michael Tremer Date: Tue, 2 Jan 2018 17:17:57 +0000 (+0000) Subject: Validate the prefix to be within range X-Git-Tag: 0.9.0~133 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=6183c0f2af55f1d2dade4d74cca0915aedce4a19 Validate the prefix to be within range Signed-off-by: Michael Tremer --- 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;