From 236705679263b5ea40673f44ba627caae78994b7 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:32:45 +0100 Subject: [PATCH 01/22] Added unit tests for FuzzySetMatch() and ParseHostPort() Covers the current behaviour, before refactoring them. Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- tests/unit/addr_lib_test.c | 89 +++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 9600eecdeab..1434c222394 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -122,6 +122,11 @@ static void test_ParseHostPort() /***** CORNER CASES *****/ + ParseHostPort(strcpy(test_string, "[1.2.3.4"), &hostname, &port); + assert_string_equal(hostname, "1.2.3.4"); + assert_int_equal(port, NULL); + hostname = NULL; port = NULL; + ParseHostPort(strcpy(test_string, ""), &hostname, &port); assert_int_equal(hostname, NULL); assert_int_equal(port, NULL); @@ -143,6 +148,85 @@ static void test_ParseHostPort() hostname = NULL; port = NULL; } +static void test_ParseHostPort_address_type() +{ + char *hostname, *port; + char test_string[64]; + +#define ASSERT_ADDRESS_TYPE(input, type) \ + assert_int_equal(ParseHostPort(strcpy(test_string, input), &hostname, &port), type) + + ASSERT_ADDRESS_TYPE("1.2.3.4", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("1.2.3.4:80", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("[1.2.3.4]:80", ADDRESS_TYPE_IPV4); + ASSERT_ADDRESS_TYPE("ffff::dd:12:34", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("[ffff::dd:12:34]:80", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("::ffff:192.0.2.128", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("fe80::1%eth0", ADDRESS_TYPE_IPV6); + ASSERT_ADDRESS_TYPE("www.cfengine.com:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("localhost", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("[1.2.3.4", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("", ADDRESS_TYPE_OTHER); + +#undef ASSERT_ADDRESS_TYPE +} + +#define ASSERT_MATCH(pattern, address) \ + assert_int_equal(FuzzySetMatch(pattern, address), 0) +#define ASSERT_NO_MATCH(pattern, address) \ + assert_int_not_equal(FuzzySetMatch(pattern, address), 0) + +static void test_FuzzySetMatch_ipv4() +{ + /* Prefix */ + ASSERT_MATCH("1.2.3", "1.2.3.4"); + ASSERT_NO_MATCH("1.2.3", "1.2.34.5"); + ASSERT_NO_MATCH("1.2.3.4", "1.2.3.45"); + + /* Range */ + ASSERT_MATCH("10.0.0.1-50", "10.0.0.1"); + ASSERT_MATCH("10.0.0.1-50", "10.0.0.25"); + ASSERT_MATCH("10.0.0.1-50", "10.0.0.50"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.0"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.51"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.1.25"); + ASSERT_MATCH("10.1-5", "10.3.7.8"); + ASSERT_NO_MATCH("10.1-5", "10.6.7.8"); + ASSERT_NO_MATCH("10.0.0.50-1", "10.0.0.25"); + + /* CIDR */ + ASSERT_MATCH("10.0.0.0/8", "10.200.3.4"); + ASSERT_NO_MATCH("10.0.0.0/8", "11.0.0.1"); + ASSERT_MATCH("8.8.8.8/31", "8.8.8.9"); + ASSERT_NO_MATCH("8.8.8.8/31", "8.8.8.10"); + ASSERT_MATCH("1.2.3.4/32", "1.2.3.4"); + ASSERT_MATCH("0.0.0.0/0", "1.2.3.4"); + ASSERT_NO_MATCH("0.0.0.0/0", "not-an-ip-address"); + ASSERT_NO_MATCH("1.2.3.4/33", "1.2.3.4"); + + /* Mixed notations */ + ASSERT_NO_MATCH("10.0.0.1-5/8", "10.0.0.1"); + ASSERT_NO_MATCH("10.0.0.0/8", "::1"); +} + +static void test_FuzzySetMatch_ipv6() +{ + /* CIDR */ + ASSERT_MATCH("2001:db8::/32", "2001:db8:ffff::1"); + ASSERT_NO_MATCH("2001:db8::/32", "2001:db9::1"); + ASSERT_MATCH("2001:db8::/31", "2001:db9::1"); + ASSERT_MATCH("::/0", "fe80::1"); + ASSERT_NO_MATCH("2001:db8::/129", "2001:db8::1"); + + /* Range */ + ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:ffff:0:0:0:0:0:1"); + ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:20:0:0:0:0:0:1"); + ASSERT_NO_MATCH("2001:10-20:0:0:0:0:0:1", "2001:21:0:0:0:0:0:1"); +} + +#undef ASSERT_MATCH +#undef ASSERT_NO_MATCH + static void test_FuzzySetMatch() { /* Sanity: exact and subnet matches still work. */ @@ -186,8 +270,11 @@ int main() const UnitTest tests[] = { unit_test(test_ParseHostPort), + unit_test(test_ParseHostPort_address_type), unit_test(test_FuzzySetMatch), - unit_test(test_FuzzyMatchParse) + unit_test(test_FuzzySetMatch_ipv4), + unit_test(test_FuzzySetMatch_ipv6), + unit_test(test_FuzzyMatchParse), }; return run_tests(tests); From 93ecf302b53f626e400ad37891e5e227694b466a Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:32:51 +0100 Subject: [PATCH 02/22] Extracted MatchPrefix() from FuzzySetMatch() FuzzySetMatch() now returns -1 instead of the strncmp() result when a prefix does not match. All callers only compare the result to 0. Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index dcfd8621fbc..1ba129b2e74 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -30,6 +30,24 @@ #define CF_ADDRSIZE 128 +/******************************************************************************/ +/* Matching */ +/******************************************************************************/ + +/** + * Plain string prefix match, at octet boundaries, i.e. "10.1.2" matches + * "10.1.2.3", but "10.1.2.1" does not match "10.1.2.12". + */ +static bool MatchPrefix(const char *pattern, const char *address) +{ + const size_t len = strlen(pattern); + return strncmp(pattern, address, len) == 0 && + (address[len] == '\0' || address[len] == '.'); +} + +/******************************************************************************/ +/* Public API */ +/******************************************************************************/ /* Match two IP strings - with : or . in hex or decimal s1 is the test string, and s2 is the reference e.g. @@ -98,15 +116,7 @@ int FuzzySetMatch(const char *s1, const char *s2) if (!(isrange || isCIDR)) { - if (strlen(s2) > strlen(s1)) - { - if (*(s2 + strlen(s1)) != '.') - { - return -1; // Because xxx.1 should not match xxx.12 in the same octet - } - } - - return strncmp(s1, s2, strlen(s1)); /* do partial string match */ + return MatchPrefix(s1, s2) ? 0 : -1; } if (isv4) From 47d645ef409bd95ad55664da82fbe7130b5f9c32 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:32:54 +0100 Subject: [PATCH 03/22] Extracted MatchIPv4CIDR() from FuzzySetMatch() Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 79 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 1ba129b2e74..c35758228f6 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -45,6 +45,43 @@ static bool MatchPrefix(const char *pattern, const char *address) (address[len] == '\0' || address[len] == '.'); } +static bool MatchIPv4CIDR(const char *pattern, const char *address) +{ + struct sockaddr_in addr1, addr2; + char network[CF_ADDRSIZE]; + unsigned long mask; + + // Checks that the addr to match is a valid address + // else bogus addresses will always match /0 range + if (inet_pton(AF_INET, address, &addr2.sin_addr) != 1) + { + Log(LOG_LEVEL_ERR, "Invalid reference IPv4: %s", address); + return false; + } + + network[0] = '\0'; + int ret = sscanf(pattern, "%16[^/]/%lu", network, &mask); + if (ret != 2 || mask > 32) + { + Log(LOG_LEVEL_ERR, "Invalid IPv4 CIDR: %s", pattern); + return false; + } + else if (mask == 0) + { + return true; /* /0 CIDR matches everything */ + } + + inet_pton(AF_INET, network, &addr1.sin_addr); + + unsigned long a1 = htonl(addr1.sin_addr.s_addr); + unsigned long a2 = htonl(addr2.sin_addr.s_addr); + + unsigned long shift = 32 - mask; + assert(shift < 32); /* Undefined behaviour if 32 */ + + return (a1 >> shift) == (a2 >> shift); +} + /******************************************************************************/ /* Public API */ /******************************************************************************/ @@ -123,47 +160,7 @@ int FuzzySetMatch(const char *s1, const char *s2) { if (isCIDR) { - struct sockaddr_in addr1, addr2; - unsigned long mask; - - // Checks that the addr to match is a valid address - // else bogus addresses will always match /0 range - if (inet_pton(AF_INET, s2, &addr2.sin_addr) != 1) { - Log(LOG_LEVEL_ERR, "Invalid reference IPv4: %s", s2); - return -1; - } - - address[0] = '\0'; - int ret = sscanf(s1, "%16[^/]/%lu", address, &mask); - if (ret != 2 || mask > 32) - { - Log(LOG_LEVEL_ERR, "Invalid IPv4 CIDR: %s", s1); - return -1; - } - else if (mask == 0) - { - return 0; /* /0 CIDR matches everything */ - } - - inet_pton(AF_INET, address, &addr1.sin_addr); - - unsigned long a1 = htonl(addr1.sin_addr.s_addr); - unsigned long a2 = htonl(addr2.sin_addr.s_addr); - - unsigned long shift = 32 - mask; - assert(shift < 32); /* Undefined behaviour if 32 */ - - a1 = a1 >> shift; - a2 = a2 >> shift; - - if (a1 == a2) - { - return 0; - } - else - { - return -1; - } + return MatchIPv4CIDR(s1, s2) ? 0 : -1; } else { From 9bb2f43d1644a8a085847ef527b643de5bbf7736 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:32:56 +0100 Subject: [PATCH 04/22] Extracted MatchIPv4Range() from FuzzySetMatch() Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 151 +++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 73 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index c35758228f6..f256bae6e68 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -82,6 +82,83 @@ static bool MatchIPv4CIDR(const char *pattern, const char *address) return (a1 >> shift) == (a2 >> shift); } +static bool MatchIPv4Range(const char *pattern, const char *address) +{ + long from = -1, to = -1, cmp = -1; + char buffer1[64], buffer2[64]; + + const char *sp1 = pattern; + const char *sp2 = address; + + for (int i = 0; i < 4; i++) + { + buffer1[0] = '\0'; + sscanf(sp1, "%63[^.]", buffer1); + buffer1[63] = '\0'; + + if (strlen(buffer1) == 0) + { + break; + } + + sp1 += strlen(buffer1); + if (*sp1 == '.') + { + sp1++; + } + + buffer2[0] = '\0'; + sscanf(sp2, "%63[^.]", buffer2); + buffer2[63] = '\0'; + + if (strlen(buffer2) == 0) + { + /* address has fewer octets than the pattern: no match */ + return false; + } + + sp2 += strlen(buffer2); + if (*sp2 == '.') + { + sp2++; + } + + if (strstr(buffer1, "-")) + { + sscanf(buffer1, "%ld-%ld", &from, &to); + sscanf(buffer2, "%ld", &cmp); + + if ((from < 0) || (to < 0)) + { + Log(LOG_LEVEL_DEBUG, "Couldn't read range"); + return false; + } + + if ((from > cmp) || (cmp > to)) + { + Log(LOG_LEVEL_DEBUG, "Out of range %ld > %ld > %ld, range '%s'", from, cmp, to, buffer2); + return false; + } + } + else + { + sscanf(buffer1, "%ld", &from); + sscanf(buffer2, "%ld", &cmp); + + if (from != cmp) + { + Log(LOG_LEVEL_DEBUG, "Unequal"); + return false; + } + } + + Log(LOG_LEVEL_DEBUG, "Matched octet '%s' with '%s'", buffer1, buffer2); + } + + Log(LOG_LEVEL_DEBUG, "Matched IP range"); + return true; +} + /******************************************************************************/ /* Public API */ /******************************************************************************/ @@ -164,79 +241,7 @@ int FuzzySetMatch(const char *s1, const char *s2) } else { - long i, from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; - - const char *sp1 = s1; - const char *sp2 = s2; - - for (i = 0; i < 4; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^.]", buffer1); - buffer1[63] = '\0'; - - if (strlen(buffer1) == 0) - { - break; - } - - sp1 += strlen(buffer1); - if (*sp1 == '.') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^.]", buffer2); - buffer2[63] = '\0'; - - if (strlen(buffer2) == 0) - { - /* s2 has fewer octets than the pattern: no match */ - return -1; - } - - sp2 += strlen(buffer2); - if (*sp2 == '.') - { - sp2++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%ld-%ld", &from, &to); - sscanf(buffer2, "%ld", &cmp); - - if ((from < 0) || (to < 0)) - { - Log(LOG_LEVEL_DEBUG, "Couldn't read range"); - return -1; - } - - if ((from > cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "Out of range %ld > %ld > %ld, range '%s'", from, cmp, to, buffer2); - return -1; - } - } - else - { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); - - if (from != cmp) - { - Log(LOG_LEVEL_DEBUG, "Unequal"); - return -1; - } - } - - Log(LOG_LEVEL_DEBUG, "Matched octet '%s' with '%s'", buffer1, buffer2); - } - - Log(LOG_LEVEL_DEBUG, "Matched IP range"); - return 0; + return MatchIPv4Range(s1, s2) ? 0 : -1; } } From 32085e2216956452253ab9f1adf5a279839b4b52 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:32:59 +0100 Subject: [PATCH 05/22] Extracted MatchIPv6CIDR() from FuzzySetMatch() Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 97 +++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index f256bae6e68..9d77803701a 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -82,6 +82,55 @@ static bool MatchIPv4CIDR(const char *pattern, const char *address) return (a1 >> shift) == (a2 >> shift); } +static bool MatchIPv6CIDR(const char *pattern, const char *address) +{ + struct sockaddr_in6 addr1 = {0}; + struct sockaddr_in6 addr2 = {0}; + char network[CF_ADDRSIZE]; + unsigned long mask; + + network[0] = '\0'; + int ret = sscanf(pattern, "%40[^/]/%lu", network, &mask); + if (ret != 2 || mask > 128) + { + Log(LOG_LEVEL_ERR, "Invalid IPv6 CIDR: %s", pattern); + return false; + } + int blocks = mask / 8; + int remaining_bits = mask % 8; + + addr1.sin6_family = AF_INET6; + if (inet_pton(AF_INET6, network, &addr1.sin6_addr) != 1) + { + return false; + } + addr2.sin6_family = AF_INET6; + if (inet_pton(AF_INET6, address, &addr2.sin6_addr) != 1) + { + return false; + } + + for (int i = 0; i < blocks; i++) + { + if (addr1.sin6_addr.s6_addr[i] != addr2.sin6_addr.s6_addr[i]) + { + return false; + } + } + + if (remaining_bits > 0) + { + uint8_t bitmask = (uint8_t)(0xFF << (8 - remaining_bits)); + + if ((addr1.sin6_addr.s6_addr[blocks] & bitmask) != + (addr2.sin6_addr.s6_addr[blocks] & bitmask)) + { + return false; + } + } + return true; +} + static bool MatchIPv4Range(const char *pattern, const char *address) { long from = -1, to = -1, cmp = -1; @@ -173,7 +222,6 @@ static bool MatchIPv4Range(const char *pattern, const char *address) int FuzzySetMatch(const char *s1, const char *s2) { short isCIDR = false, isrange = false, isv6 = false, isv4 = false; - char address[CF_ADDRSIZE]; if (strcmp(s1, s2) == 0) { @@ -247,54 +295,9 @@ int FuzzySetMatch(const char *s1, const char *s2) if (isv6) { - int i; - if (isCIDR) { - struct sockaddr_in6 addr1 = {0}; - struct sockaddr_in6 addr2 = {0}; - unsigned long mask; - - address[0] = '\0'; - int ret = sscanf(s1, "%40[^/]/%lu", address, &mask); - if (ret != 2 || mask > 128) - { - Log(LOG_LEVEL_ERR, "Invalid IPv6 CIDR: %s", s1); - return -1; - } - int blocks = mask / 8; - int remaining_bits = mask % 8; - - addr1.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, address, &addr1.sin6_addr) != 1) - { - return -1; - } - addr2.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, s2, &addr2.sin6_addr) != 1) - { - return -1; - } - - for (i = 0; i < blocks; i++) - { - if (addr1.sin6_addr.s6_addr[i] != addr2.sin6_addr.s6_addr[i]) - { - return -1; - } - } - - if (remaining_bits > 0) - { - uint8_t bitmask = (uint8_t)(0xFF << (8 - remaining_bits)); - - if ((addr1.sin6_addr.s6_addr[blocks] & bitmask) != - (addr2.sin6_addr.s6_addr[blocks] & bitmask)) - { - return -1; - } - } - return 0; + return MatchIPv6CIDR(s1, s2) ? 0 : -1; } else { From faefd1467e30ab13ed821a7bc846de099c8c8843 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:02 +0100 Subject: [PATCH 06/22] Extracted MatchIPv6Range() from FuzzySetMatch() Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 119 +++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 57 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 9d77803701a..a28ef63bc3f 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -208,6 +208,67 @@ static bool MatchIPv4Range(const char *pattern, const char *address) return true; } +static bool MatchIPv6Range(const char *pattern, const char *address) +{ + long from = -1, to = -1, cmp = -1; + char buffer1[64], buffer2[64]; + + const char *sp1 = pattern; + const char *sp2 = address; + + for (int i = 0; i < 8; i++) + { + buffer1[0] = '\0'; + sscanf(sp1, "%63[^:]", buffer1); + buffer1[63] = '\0'; + + sp1 += strlen(buffer1); + if (*sp1 == ':') + { + sp1++; + } + + buffer2[0] = '\0'; + sscanf(sp2, "%63[^:]", buffer2); + buffer2[63] = '\0'; + + sp2 += strlen(buffer2); + if (*sp2 == ':') + { + sp2++; + } + + if (strstr(buffer1, "-")) + { + sscanf(buffer1, "%lx-%lx", &from, &to); + sscanf(buffer2, "%lx", &cmp); + + if (from < 0 || to < 0) + { + return false; + } + + if ((from >= cmp) || (cmp > to)) + { + Log(LOG_LEVEL_DEBUG, "%lx < %lx < %lx", from, cmp, to); + return false; + } + } + else + { + sscanf(buffer1, "%ld", &from); + sscanf(buffer2, "%ld", &cmp); + + if (from != cmp) + { + return false; + } + } + } + + return true; +} + /******************************************************************************/ /* Public API */ /******************************************************************************/ @@ -301,63 +362,7 @@ int FuzzySetMatch(const char *s1, const char *s2) } else { - long i, from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; - - const char *sp1 = s1; - const char *sp2 = s2; - - for (i = 0; i < 8; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^:]", buffer1); - buffer1[63] = '\0'; - - sp1 += strlen(buffer1); - if (*sp1 == ':') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^:]", buffer2); - buffer2[63] = '\0'; - - sp2 += strlen(buffer2); - if (*sp2 == ':') - { - sp2++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%lx-%lx", &from, &to); - sscanf(buffer2, "%lx", &cmp); - - if (from < 0 || to < 0) - { - return -1; - } - - if ((from >= cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "%lx < %lx < %lx", from, cmp, to); - return -1; - } - } - else - { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); - - if (from != cmp) - { - return -1; - } - } - } - - return 0; + return MatchIPv6Range(s1, s2) ? 0 : -1; } } From a35d55520233253e993e83430ec18eb746e62a26 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:05 +0100 Subject: [PATCH 07/22] Extracted GetPatternNotation() from FuzzySetMatch() FuzzySetMatch() now determines the notation of the pattern (prefix, CIDR or range) and calls the matching helper. Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 130 ++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index a28ef63bc3f..f0a994c7ff3 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -30,6 +30,50 @@ #define CF_ADDRSIZE 128 +/* Notation used by an address pattern, see FuzzySetMatch() */ +typedef enum +{ + PATTERN_PREFIX, // "10.1.2.3", "10.1.2" + PATTERN_CIDR, // "10.1.2.0/24", "2001:db8::/32" + PATTERN_RANGE, // "10.1.2.1-50", "2001:db8:0-ff::1" +} PatternNotation; + +/** + * Determines the notation of an address pattern: CIDR if it contains a '/', + * range if it contains a '-', otherwise prefix. + * + * @param pattern the address pattern, e.g. "10.1.2.0/24" + * @param notation receives the notation of the pattern + * @return true on success, false (and logs an error) if the pattern mixes + * CIDR and range notation, in which case notation is not set + */ +static bool GetPatternNotation(const char *pattern, PatternNotation *notation) +{ + const bool is_cidr = (strchr(pattern, '/') != NULL); + const bool is_range = (strchr(pattern, '-') != NULL); + + if (is_cidr && is_range) + { + Log(LOG_LEVEL_ERR, + "Cannot mix CIDR notation with xxx-yyy range notation '%s'", pattern); + return false; + } + + if (is_cidr) + { + *notation = PATTERN_CIDR; + } + else if (is_range) + { + *notation = PATTERN_RANGE; + } + else + { + *notation = PATTERN_PREFIX; + } + return true; +} + /******************************************************************************/ /* Matching */ /******************************************************************************/ @@ -277,96 +321,54 @@ static bool MatchIPv6Range(const char *pattern, const char *address) s1 is the test string, and s2 is the reference e.g. FuzzySetMatch("128.39.74.10/23","128.39.75.56") == 0 + s1 may be a CIDR ("10.0.0.0/8", "2001:db8::/32"), a range + ("10.0.0.1-50", "2001:db8:0-ff::1") or an address prefix ("10.1.2"). + Returns 0 on match. */ /* TODO rename to AddrSubnetMatch() */ int FuzzySetMatch(const char *s1, const char *s2) { - short isCIDR = false, isrange = false, isv6 = false, isv4 = false; - if (strcmp(s1, s2) == 0) { return 0; } - if (strstr(s1, "/") != 0) - { - isCIDR = true; - } - - if (strstr(s1, "-") != 0) - { - isrange = true; - } + const bool is_v4 = (strchr(s1, '.') != NULL || strchr(s2, '.') != NULL); + const bool is_v6 = (strchr(s1, ':') != NULL || strchr(s2, ':') != NULL); - if (strstr(s1, ".") != 0) + if (is_v4 && is_v6) { - isv4 = true; + return -1; // Mixed IPv4 and IPv6 } - if (strstr(s1, ":") != 0) + PatternNotation notation; + if (!GetPatternNotation(s1, ¬ation)) { - isv6 = true; - } - - if (strstr(s2, ".") != 0) - { - isv4 = true; - } - - if (strstr(s2, ":") != 0) - { - isv6 = true; - } - - if (isv4 && isv6) - { - /* This is just wrong */ - return -1; - } - - if (isCIDR && isrange) - { - Log(LOG_LEVEL_ERR, "Cannot mix CIDR notation with xxx-yyy range notation '%s'", s1); return -1; } - if (!(isv6 || isv4)) + if (!is_v4 && !is_v6) { Log(LOG_LEVEL_ERR, "Not a valid address range - or not a fully qualified name '%s'", s1); return -1; } - if (!(isrange || isCIDR)) + bool match = false; + switch (notation) { - return MatchPrefix(s1, s2) ? 0 : -1; - } - - if (isv4) - { - if (isCIDR) - { - return MatchIPv4CIDR(s1, s2) ? 0 : -1; - } - else - { - return MatchIPv4Range(s1, s2) ? 0 : -1; - } - } - - if (isv6) - { - if (isCIDR) - { - return MatchIPv6CIDR(s1, s2) ? 0 : -1; - } - else - { - return MatchIPv6Range(s1, s2) ? 0 : -1; - } + case PATTERN_PREFIX: + match = MatchPrefix(s1, s2); + break; + case PATTERN_CIDR: + match = is_v4 ? MatchIPv4CIDR(s1, s2) : MatchIPv6CIDR(s1, s2); + break; + case PATTERN_RANGE: + match = is_v4 ? MatchIPv4Range(s1, s2) : MatchIPv6Range(s1, s2); + break; } - return -1; + return match ? 0 : -1; } bool FuzzyHostParse(const char *arg2) From c6e2947ec549c69407099611fa4f1ba3443129e6 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:07 +0100 Subject: [PATCH 08/22] Restructured ParseHostPort() It now splits host and port first and determines the address type afterwards, instead of doing both in each branch. Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 124 +++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 71 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index f0a994c7ff3..ad8376d26c1 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -610,108 +610,90 @@ bool FuzzyMatchParse(const char *s) } -/** - * Simple check to avoid writing to illegal memory addresses. - * NOT a proper test for valid IP. - */ -static AddressType AddressTypeCheckValidity(char *s, AddressType address_type) -{ - if(NULL_OR_EMPTY(s)) - { - return ADDRESS_TYPE_OTHER; - } - if(strlen(s) >= CF_MAX_IP_LEN) - { - return ADDRESS_TYPE_OTHER; - } - return address_type; -} - /** * Parses "hostname:port" or "[hostname]:port", where hostname may also be * IPv4 or IPv6 address string. * * @param hostname will point to the hostname, or NULL if no or empty hostname * @param port will point to the port, or NULL if no or empty port + * @return the type of hostname: ADDRESS_TYPE_IPV4 if it starts with a digit, + * ADDRESS_TYPE_IPV6 if it contains a ':' (not validated), otherwise + * (hostname, invalid or too long) ADDRESS_TYPE_OTHER * @WARNING modifies #s to '\0' terminate hostname if followed by port. */ AddressType ParseHostPort(char *s, char **hostname, char **port) { s = TrimWhitespace(s); - if ( NULL_OR_EMPTY(s) ) + if (NULL_OR_EMPTY(s)) { *hostname = NULL; *port = NULL; return ADDRESS_TYPE_OTHER; } - AddressType address_type = ADDRESS_TYPE_OTHER; - char *h, *p; // hostname, port temporaries - - h = s; - p = NULL; - - char *first_colon = strchr(s, ':'); - char *first_dot = strchr(s, '.'); + char *host = s; + char *port_str = NULL; + bool is_ipv6 = false; + bool classify = true; - if (s[0] == '[') // [host or ip]:port + if (s[0] == '[') // [host or ip]:port { - h = s + 1; - p = strchr(h, ']'); - if (p != NULL) + host = s + 1; + char *closing = strchr(host, ']'); + if (closing != NULL) { - if (first_colon != NULL && first_colon < p) - { - address_type = ADDRESS_TYPE_IPV6; - } - else if (isdigit(h[0])) + *closing = '\0'; + is_ipv6 = (strchr(host, ':') != NULL); + if (closing[1] == ':') { - address_type = ADDRESS_TYPE_IPV4; - } // (else it's other by default) - - *p = '\0'; // '\0' terminate host name - if (p[1] == ':') // move port* forward - { - p += 2; + port_str = closing + 2; } } - } - else if (first_colon == NULL) // localhost, 192.168.0.1 - { - if (isdigit(h[0])) + else { - address_type = ADDRESS_TYPE_IPV4; + classify = false; // Unterminated bracket } } - else if (first_dot == NULL || first_colon < first_dot) + else { - // If only one colon: (cfengine.com:222 or localhost:) - if (strchr(first_colon + 1, ':') == NULL) - { - *first_colon = '\0'; - p = first_colon + 1; - } - else // Multiple colons: + char *first_colon = strchr(s, ':'); + char *first_dot = strchr(s, '.'); + + if (first_colon != NULL) { - address_type = ADDRESS_TYPE_IPV6; + if (first_dot != NULL && first_dot < first_colon) + { + // "1.2.3.4:80" or "cfengine.com:80" + *first_colon = '\0'; + port_str = first_colon + 1; + } + else if (strchr(first_colon + 1, ':') == NULL) + { + // "localhost:80" or "localhost:" + *first_colon = '\0'; + port_str = first_colon + 1; + classify = false; + } + else // Multiple colons: IPv6 address, no port + { + is_ipv6 = true; + } } } - else // (first_dot < first_colon) : IPv4 or hostname + + *hostname = (host[0] != '\0') ? host : NULL; + *port = (port_str != NULL && port_str[0] != '\0') ? port_str : NULL; + + if (!classify || *hostname == NULL || strlen(*hostname) >= CF_MAX_IP_LEN) { - p = strchr(h, ':'); - if (p != NULL) - { - *p = '\0'; // '\0'-terminate hostname - p++; - } - if (isdigit(h[0])) - { - address_type = ADDRESS_TYPE_IPV4; - } + return ADDRESS_TYPE_OTHER; + } + if (is_ipv6) + { + return ADDRESS_TYPE_IPV6; } - *hostname = (h[0] != '\0') ? h : NULL; - *port = (p != NULL && p[0] != '\0') ? p : NULL; - - return AddressTypeCheckValidity(*hostname, address_type); + return isdigit((unsigned char) (*hostname)[0]) + ? ADDRESS_TYPE_IPV4 + : ADDRESS_TYPE_OTHER; } From 078426a65661d60644b82c582b2a11c1fa38decb Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:10 +0100 Subject: [PATCH 09/22] Removed declaration of IsLoopbackAddress() It was never defined. Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.h | 1 - 1 file changed, 1 deletion(-) diff --git a/libcfnet/addr_lib.h b/libcfnet/addr_lib.h index aba84c91ba0..faffc1fce56 100644 --- a/libcfnet/addr_lib.h +++ b/libcfnet/addr_lib.h @@ -27,7 +27,6 @@ #include -bool IsLoopbackAddress(const char *address); int FuzzySetMatch(const char *s1, const char *s2); bool FuzzyHostParse(const char *arg2); int FuzzyHostMatch(const char *arg0, const char *arg1, const char *basename); From ec2ff053637907b81f455721ae73235093e422cd Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:14 +0100 Subject: [PATCH 10/22] Fixed IPv6 range patterns comparing groups as decimal numbers Groups without a range, e.g. "fe80", were parsed as decimal, so "fe80:1-3:0:0:0:0:0:1" matched "fe81:2:0:0:0:0:0:1". Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 4 ++-- tests/unit/addr_lib_test.c | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index ad8376d26c1..f9952089da8 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -300,8 +300,8 @@ static bool MatchIPv6Range(const char *pattern, const char *address) } else { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); + sscanf(buffer1, "%lx", &from); + sscanf(buffer2, "%lx", &cmp); if (from != cmp) { diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 1434c222394..6ba052b13a4 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -222,6 +222,12 @@ static void test_FuzzySetMatch_ipv6() ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:ffff:0:0:0:0:0:1"); ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:20:0:0:0:0:0:1"); ASSERT_NO_MATCH("2001:10-20:0:0:0:0:0:1", "2001:21:0:0:0:0:0:1"); + + /* Groups are hexadecimal */ + ASSERT_MATCH("2001:a-f:0:0:0:0:0:1", "2001:c:0:0:0:0:0:1"); + ASSERT_NO_MATCH("fe80:1-3:0:0:0:0:0:1", "fe81:2:0:0:0:0:0:1"); + ASSERT_NO_MATCH("2001:1-3:0:0:0:0:0:a", "2001:2:0:0:0:0:0:b"); + ASSERT_MATCH("2001:1-3:0:0:0:0:0:A", "2001:2:0:0:0:0:0:a"); } #undef ASSERT_MATCH From 473ba5269b1462bcc9b3c9532956c7e38aa07eb2 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:19 +0100 Subject: [PATCH 11/22] Fixed IPv6 range patterns excluding the lower bound of a range "2001:10-20:0:0:0:0:0:1" did not match "2001:10:0:0:0:0:0:1". Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 2 +- tests/unit/addr_lib_test.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index f9952089da8..3e14451ea89 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -292,7 +292,7 @@ static bool MatchIPv6Range(const char *pattern, const char *address) return false; } - if ((from >= cmp) || (cmp > to)) + if ((from > cmp) || (cmp > to)) { Log(LOG_LEVEL_DEBUG, "%lx < %lx < %lx", from, cmp, to); return false; diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 6ba052b13a4..32b399b82e0 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -218,8 +218,10 @@ static void test_FuzzySetMatch_ipv6() ASSERT_MATCH("::/0", "fe80::1"); ASSERT_NO_MATCH("2001:db8::/129", "2001:db8::1"); - /* Range */ + /* Range: both bounds are inclusive */ + ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:0:0:0:0:0:0:1"); ASSERT_MATCH("2001:0-ffff:0:0:0:0:0:1", "2001:ffff:0:0:0:0:0:1"); + ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:10:0:0:0:0:0:1"); ASSERT_MATCH("2001:10-20:0:0:0:0:0:1", "2001:20:0:0:0:0:0:1"); ASSERT_NO_MATCH("2001:10-20:0:0:0:0:0:1", "2001:21:0:0:0:0:0:1"); From a55c1cc0779fc433fb59e1839ef1804cea26c094 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:24 +0100 Subject: [PATCH 12/22] IPv6 range patterns now support `::` Neither in the pattern nor in the address, so "2001:db8:1-5::1" never matched "2001:db8:3::1". Invalid groups are now rejected too. Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 237 +++++++++++++++++++++++++++++-------- tests/unit/addr_lib_test.c | 13 ++ 2 files changed, 201 insertions(+), 49 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 3e14451ea89..b6b7e5578ff 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -29,6 +29,15 @@ #include #define CF_ADDRSIZE 128 +#define IPV6_GROUPS 8 + +/* Inclusive interval matched by one octet/group of a range pattern, + * e.g. "10-20", or "7" which is the interval 7-7. */ +typedef struct +{ + unsigned long from; + unsigned long to; +} Interval; /* Notation used by an address pattern, see FuzzySetMatch() */ typedef enum @@ -38,6 +47,170 @@ typedef enum PATTERN_RANGE, // "10.1.2.1-50", "2001:db8:0-ff::1" } PatternNotation; +/******************************************************************************/ +/* Parsing helpers */ +/******************************************************************************/ + +/** + * Parses the unsigned number in [start, end), written in the given base + * (10 or 16). Unlike strtoul(), no sign, whitespace or "0x" is accepted. + * + * @return false if the input is empty, has invalid digits or exceeds max + */ +static bool ParseNumber(const char *start, const char *end, int base, + unsigned long max, unsigned long *value) +{ + assert(start != NULL); + assert(end != NULL); + assert(start <= end); + assert(base == 10 || base == 16); + + if (start == end) + { + return false; + } + + unsigned long result = 0; + for (const char *c = start; c < end; c++) + { + const unsigned char ch = *c; + unsigned long digit; + if (isdigit(ch)) + { + digit = ch - '0'; + } + else if (base == 16 && isxdigit(ch)) + { + digit = tolower(ch) - 'a' + 10; + } + else + { + return false; + } + + if (result > (max - digit) / base) + { + return false; + } + result = result * base + digit; + } + + *value = result; + return true; +} + +/** + * Parses "N" or "FROM-TO" in [start, end) into an interval. + * + * @return false if a number is invalid, exceeds max, or FROM > TO + */ +static bool ParseInterval(const char *start, const char *end, int base, + unsigned long max, Interval *interval) +{ + assert(interval != NULL); + + const char *dash = memchr(start, '-', end - start); + if (dash == NULL) + { + if (!ParseNumber(start, end, base, max, &interval->from)) + { + return false; + } + interval->to = interval->from; + return true; + } + + return ParseNumber(start, dash, base, max, &interval->from) && + ParseNumber(dash + 1, end, base, max, &interval->to) && + interval->from <= interval->to; +} + +/** + * Parses up to max ':'-separated hex groups (numbers or ranges) in + * [start, end). An empty input yields 0 groups. + */ +static bool ParseIPv6Groups(const char *start, const char *end, + Interval *groups, size_t max, size_t *count) +{ + assert(start <= end); + + *count = 0; + if (start == end) + { + return true; + } + + while (*count < max) + { + const char *sep = memchr(start, ':', end - start); + const char *group_end = (sep != NULL) ? sep : end; + if (!ParseInterval(start, group_end, 16, 0xFFFF, &groups[*count])) + { + return false; + } + (*count)++; + + if (sep == NULL) + { + return true; + } + start = sep + 1; + } + + return false; // Too many groups +} + +/** + * Parses an IPv6 range pattern into exactly 8 groups, expanding "::", + * e.g. "2001:db8:1-5::1". + */ +static bool ParseIPv6RangePattern(const char *pattern, Interval groups[IPV6_GROUPS]) +{ + const char *end = pattern + strlen(pattern); + const char *gap = strstr(pattern, "::"); + size_t count; + + if (gap == NULL) + { + if (!ParseIPv6Groups(pattern, end, groups, IPV6_GROUPS, &count) || + count != IPV6_GROUPS) + { + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': expected 8 valid groups, or '::'", + pattern); + return false; + } + return true; + } + + if (strstr(gap + 1, "::") != NULL) + { + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': '::' may only appear once", pattern); + return false; + } + + // "::" stands for at least one zero group + size_t head_count, tail_count; + Interval tail[IPV6_GROUPS]; + if (!ParseIPv6Groups(pattern, gap, groups, IPV6_GROUPS - 1, &head_count) || + !ParseIPv6Groups(gap + 2, end, tail, IPV6_GROUPS - 1 - head_count, &tail_count)) + { + Log(LOG_LEVEL_VERBOSE, + "Invalid IPv6 range '%s': invalid group, or more than 7 groups besides '::'", + pattern); + return false; + } + + const size_t zero_count = IPV6_GROUPS - head_count - tail_count; + for (size_t i = head_count; i < head_count + zero_count; i++) + { + groups[i] = (Interval) { .from = 0, .to = 0 }; + } + memcpy(groups + head_count + zero_count, tail, tail_count * sizeof(*tail)); + return true; +} + /** * Determines the notation of an address pattern: CIDR if it contains a '/', * range if it contains a '-', otherwise prefix. @@ -254,59 +427,25 @@ static bool MatchIPv4Range(const char *pattern, const char *address) static bool MatchIPv6Range(const char *pattern, const char *address) { - long from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; - - const char *sp1 = pattern; - const char *sp2 = address; - - for (int i = 0; i < 8; i++) + Interval groups[IPV6_GROUPS]; + if (!ParseIPv6RangePattern(pattern, groups)) { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^:]", buffer1); - buffer1[63] = '\0'; - - sp1 += strlen(buffer1); - if (*sp1 == ':') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^:]", buffer2); - buffer2[63] = '\0'; - - sp2 += strlen(buffer2); - if (*sp2 == ':') - { - sp2++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%lx-%lx", &from, &to); - sscanf(buffer2, "%lx", &cmp); + Log(LOG_LEVEL_DEBUG, "Invalid IPv6 range '%s'", pattern); + return false; + } - if (from < 0 || to < 0) - { - return false; - } + struct in6_addr addr; + if (inet_pton(AF_INET6, address, &addr) != 1) + { + return false; + } - if ((from > cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "%lx < %lx < %lx", from, cmp, to); - return false; - } - } - else + for (size_t i = 0; i < IPV6_GROUPS; i++) + { + const unsigned long value = (addr.s6_addr[2 * i] << 8) | addr.s6_addr[2 * i + 1]; + if (value < groups[i].from || value > groups[i].to) { - sscanf(buffer1, "%lx", &from); - sscanf(buffer2, "%lx", &cmp); - - if (from != cmp) - { - return false; - } + return false; } } diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 32b399b82e0..9aa7791214c 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -230,6 +230,19 @@ static void test_FuzzySetMatch_ipv6() ASSERT_NO_MATCH("fe80:1-3:0:0:0:0:0:1", "fe81:2:0:0:0:0:0:1"); ASSERT_NO_MATCH("2001:1-3:0:0:0:0:0:a", "2001:2:0:0:0:0:0:b"); ASSERT_MATCH("2001:1-3:0:0:0:0:0:A", "2001:2:0:0:0:0:0:a"); + + /* "::" compression in pattern and address */ + ASSERT_MATCH("2001:db8:1-5::1", "2001:db8:3::1"); + ASSERT_MATCH("2001:db8:1-5::1", "2001:db8:3:0:0:0:0:1"); + ASSERT_MATCH("2001:db8:1-5:0:0:0:0:1", "2001:db8:3::1"); + ASSERT_NO_MATCH("2001:db8:1-5::1", "2001:db8:3::2"); + ASSERT_MATCH("::1-5", "::3"); + ASSERT_MATCH("fe80::0-ff:1", "fe80::12:1"); + + /* Invalid range patterns never match */ + ASSERT_NO_MATCH("2001:db8:1-5", "2001:db8:3::1"); + ASSERT_NO_MATCH("2001::db8::1-5", "2001::db8:0:0:3"); + ASSERT_NO_MATCH("2001:db8:1-10000::1", "2001:db8:3::1"); } #undef ASSERT_MATCH From bc97da1e97249010bf87dcb553caec8f0b48a32e Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:28 +0100 Subject: [PATCH 13/22] Fixed bug where IPv4 range patterns reused values from the previous octet When an octet failed to parse, the value of the previous octet was compared instead, so "10.x.0.1-50" matched "10.10.0.25". Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 111 ++++++++++++++++++------------------- tests/unit/addr_lib_test.c | 5 ++ 2 files changed, 59 insertions(+), 57 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index b6b7e5578ff..efa21576169 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -29,6 +29,7 @@ #include #define CF_ADDRSIZE 128 +#define IPV4_OCTETS 4 #define IPV6_GROUPS 8 /* Inclusive interval matched by one octet/group of a range pattern, @@ -125,6 +126,33 @@ static bool ParseInterval(const char *start, const char *end, int base, interval->from <= interval->to; } +/** + * Parses an IPv4 pattern of 1 to 4 '.'-separated octets, each of which is a + * decimal number or range, e.g. "10.0.0.1-50", "10.1-5" or "10.1.2.3". + * + * @return the number of octets parsed, or 0 if the pattern is invalid + */ +static size_t ParseIPv4Pattern(const char *pattern, Interval octets[IPV4_OCTETS]) +{ + const char *start = pattern; + for (size_t count = 0; count < IPV4_OCTETS; count++) + { + const char *end = start + strcspn(start, "."); + if (!ParseInterval(start, end, 10, 255, &octets[count])) + { + return 0; + } + + if (*end == '\0') + { + return count + 1; + } + start = end + 1; + } + + return 0; // More than 4 octets +} + /** * Parses up to max ':'-separated hex groups (numbers or ranges) in * [start, end). An empty input yields 0 groups. @@ -348,80 +376,49 @@ static bool MatchIPv6CIDR(const char *pattern, const char *address) return true; } +/** + * Matches an IPv4 range pattern. A pattern with fewer than 4 octets matches + * on the leading octets only, e.g. "10.1-5" matches "10.3.7.8". + */ static bool MatchIPv4Range(const char *pattern, const char *address) { - long from = -1, to = -1, cmp = -1; - char buffer1[64], buffer2[64]; - - const char *sp1 = pattern; - const char *sp2 = address; - - for (int i = 0; i < 4; i++) + Interval octets[IPV4_OCTETS]; + const size_t count = ParseIPv4Pattern(pattern, octets); + if (count == 0) { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^.]", buffer1); - buffer1[63] = '\0'; - - if (strlen(buffer1) == 0) - { - break; - } - - sp1 += strlen(buffer1); - if (*sp1 == '.') - { - sp1++; - } - - buffer2[0] = '\0'; - sscanf(sp2, "%63[^.]", buffer2); - buffer2[63] = '\0'; + Log(LOG_LEVEL_DEBUG, "Invalid IPv4 range '%s'", pattern); + return false; + } - if (strlen(buffer2) == 0) + const char *start = address; + for (size_t i = 0; i < count; i++) + { + const char *end = start + strcspn(start, "."); + unsigned long value; + if (!ParseNumber(start, end, 10, 255, &value)) { - /* address has fewer octets than the pattern: no match */ + Log(LOG_LEVEL_VERBOSE, "Invalid IPv4 address '%s'", address); return false; } - - sp2 += strlen(buffer2); - if (*sp2 == '.') + if (value < octets[i].from || value > octets[i].to) { - sp2++; + return false; } - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%ld-%ld", &from, &to); - sscanf(buffer2, "%ld", &cmp); - - if ((from < 0) || (to < 0)) - { - Log(LOG_LEVEL_DEBUG, "Couldn't read range"); - return false; - } - - if ((from > cmp) || (cmp > to)) - { - Log(LOG_LEVEL_DEBUG, "Out of range %ld > %ld > %ld, range '%s'", from, cmp, to, buffer2); - return false; - } - } - else + if (*end == '\0') { - sscanf(buffer1, "%ld", &from); - sscanf(buffer2, "%ld", &cmp); - - if (from != cmp) + if (i + 1 < count) { - Log(LOG_LEVEL_DEBUG, "Unequal"); + Log(LOG_LEVEL_VERBOSE, + "IPv4 address '%s' has fewer octets than range '%s'", + address, pattern); return false; } + return true; } - - Log(LOG_LEVEL_DEBUG, "Matched octet '%s' with '%s'", buffer1, buffer2); + start = end + 1; } - Log(LOG_LEVEL_DEBUG, "Matched IP range"); return true; } diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 9aa7791214c..9488c3edbd3 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -192,7 +192,12 @@ static void test_FuzzySetMatch_ipv4() ASSERT_NO_MATCH("10.0.0.1-50", "10.0.1.25"); ASSERT_MATCH("10.1-5", "10.3.7.8"); ASSERT_NO_MATCH("10.1-5", "10.6.7.8"); + ASSERT_NO_MATCH("10.0.0.1-50", "10.0.0.abc"); ASSERT_NO_MATCH("10.0.0.50-1", "10.0.0.25"); + ASSERT_NO_MATCH("10.x.0.1-50", "10.0.0.25"); + /* Octets that failed to parse used to reuse the previous octet's value */ + ASSERT_NO_MATCH("10.x.0.1-50", "10.10.0.25"); + ASSERT_NO_MATCH("10.0.5.1-50", "10.0.5.abc"); /* CIDR */ ASSERT_MATCH("10.0.0.0/8", "10.200.3.4"); From f02295c82057c26fe131212ba4a4a57525abecf3 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:33 +0100 Subject: [PATCH 14/22] IPv4 CIDR patterns with an invalid network address, e.g. `bogus/0`, are now rejected The address was never validated, so an uninitialized address was compared, and e.g. "bogus/0" matched every address. Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 5 ++--- tests/unit/addr_lib_test.c | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index efa21576169..d520d3bc851 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -306,7 +306,8 @@ static bool MatchIPv4CIDR(const char *pattern, const char *address) network[0] = '\0'; int ret = sscanf(pattern, "%16[^/]/%lu", network, &mask); - if (ret != 2 || mask > 32) + if (ret != 2 || mask > 32 || + inet_pton(AF_INET, network, &addr1.sin_addr) != 1) { Log(LOG_LEVEL_ERR, "Invalid IPv4 CIDR: %s", pattern); return false; @@ -316,8 +317,6 @@ static bool MatchIPv4CIDR(const char *pattern, const char *address) return true; /* /0 CIDR matches everything */ } - inet_pton(AF_INET, network, &addr1.sin_addr); - unsigned long a1 = htonl(addr1.sin_addr.s_addr); unsigned long a2 = htonl(addr2.sin_addr.s_addr); diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 9488c3edbd3..08858c5bcc9 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -208,6 +208,9 @@ static void test_FuzzySetMatch_ipv4() ASSERT_MATCH("0.0.0.0/0", "1.2.3.4"); ASSERT_NO_MATCH("0.0.0.0/0", "not-an-ip-address"); ASSERT_NO_MATCH("1.2.3.4/33", "1.2.3.4"); + /* The pattern's address used to be read uninitialized when invalid */ + ASSERT_NO_MATCH("bogus.address/0", "1.2.3.4"); + ASSERT_NO_MATCH("1.2.3.999/24", "1.2.3.4"); /* Mixed notations */ ASSERT_NO_MATCH("10.0.0.1-5/8", "10.0.0.1"); From a9c77d4b9941b77874ca3989e3c8783a335d59ab Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:37 +0100 Subject: [PATCH 15/22] Policy functions `iprange()` and `isipinsubnet()` now detect invalid ranges FuzzyMatchParse() always returned true. It now uses the same parsers as FuzzySetMatch(), including for CIDR patterns. Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 320 +++++++++++++------------------------ tests/unit/addr_lib_test.c | 29 ++++ 2 files changed, 137 insertions(+), 212 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index d520d3bc851..0f4e4bc31fd 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -28,10 +28,12 @@ #include #include -#define CF_ADDRSIZE 128 #define IPV4_OCTETS 4 #define IPV6_GROUPS 8 +/* Large enough for the binary form of both IPv4 and IPv6 addresses */ +#define BINARY_ADDR_SIZE sizeof(struct in6_addr) + /* Inclusive interval matched by one octet/group of a range pattern, * e.g. "10-20", or "7" which is the interval 7-7. */ typedef struct @@ -239,6 +241,53 @@ static bool ParseIPv6RangePattern(const char *pattern, Interval groups[IPV6_GROU return true; } +/** + * Parses a CIDR pattern "ADDRESS/BITS" of the given family. + * + * @param address receives the binary network address (BINARY_ADDR_SIZE bytes) + */ +static bool ParseCIDR(const char *pattern, int family, + unsigned char *address, unsigned long *bits) +{ + assert(family == AF_INET || family == AF_INET6); + const char *family_name = (family == AF_INET) ? "IPv4" : "IPv6"; + + const char *slash = strchr(pattern, '/'); + if (slash == NULL) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': missing '/'", + family_name, pattern); + return false; + } + + char address_str[CF_MAX_IP_LEN]; + const size_t address_len = slash - pattern; + if (address_len >= sizeof(address_str)) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': address is too long", + family_name, pattern); + return false; + } + memcpy(address_str, pattern, address_len); + address_str[address_len] = '\0'; + + if (inet_pton(family, address_str, address) != 1) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': '%s' is not a valid %s address", + family_name, pattern, address_str, family_name); + return false; + } + + const unsigned long max_bits = (family == AF_INET) ? 32 : 128; + if (!ParseNumber(slash + 1, slash + strlen(slash), 10, max_bits, bits)) + { + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR '%s': mask must be a number from 0 to %lu", + family_name, pattern, max_bits); + return false; + } + return true; +} + /** * Determines the notation of an address pattern: CIDR if it contains a '/', * range if it contains a '-', otherwise prefix. @@ -290,89 +339,48 @@ static bool MatchPrefix(const char *pattern, const char *address) (address[len] == '\0' || address[len] == '.'); } -static bool MatchIPv4CIDR(const char *pattern, const char *address) +/* True if the first bits of a and b are equal */ +static bool LeadingBitsEqual(const unsigned char *a, const unsigned char *b, + unsigned long bits) { - struct sockaddr_in addr1, addr2; - char network[CF_ADDRSIZE]; - unsigned long mask; + const size_t whole_bytes = bits / 8; + const unsigned int remaining_bits = bits % 8; - // Checks that the addr to match is a valid address - // else bogus addresses will always match /0 range - if (inet_pton(AF_INET, address, &addr2.sin_addr) != 1) + if (memcmp(a, b, whole_bytes) != 0) { - Log(LOG_LEVEL_ERR, "Invalid reference IPv4: %s", address); return false; } - - network[0] = '\0'; - int ret = sscanf(pattern, "%16[^/]/%lu", network, &mask); - if (ret != 2 || mask > 32 || - inet_pton(AF_INET, network, &addr1.sin_addr) != 1) - { - Log(LOG_LEVEL_ERR, "Invalid IPv4 CIDR: %s", pattern); - return false; - } - else if (mask == 0) + if (remaining_bits == 0) { - return true; /* /0 CIDR matches everything */ + return true; } - unsigned long a1 = htonl(addr1.sin_addr.s_addr); - unsigned long a2 = htonl(addr2.sin_addr.s_addr); - - unsigned long shift = 32 - mask; - assert(shift < 32); /* Undefined behaviour if 32 */ - - return (a1 >> shift) == (a2 >> shift); + const unsigned char mask = (unsigned char) (0xFF << (8 - remaining_bits)); + return (a[whole_bytes] & mask) == (b[whole_bytes] & mask); } -static bool MatchIPv6CIDR(const char *pattern, const char *address) +static bool MatchCIDR(const char *pattern, int family, const char *address) { - struct sockaddr_in6 addr1 = {0}; - struct sockaddr_in6 addr2 = {0}; - char network[CF_ADDRSIZE]; - unsigned long mask; + const char *family_name = (family == AF_INET) ? "IPv4" : "IPv6"; - network[0] = '\0'; - int ret = sscanf(pattern, "%40[^/]/%lu", network, &mask); - if (ret != 2 || mask > 128) + unsigned char network[BINARY_ADDR_SIZE]; + unsigned long bits; + if (!ParseCIDR(pattern, family, network, &bits)) { - Log(LOG_LEVEL_ERR, "Invalid IPv6 CIDR: %s", pattern); + Log(LOG_LEVEL_ERR, "Invalid %s CIDR: %s", family_name, pattern); return false; } - int blocks = mask / 8; - int remaining_bits = mask % 8; - addr1.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, network, &addr1.sin6_addr) != 1) + // Checks that the address to match is valid, otherwise bogus addresses + // would always match a /0 range + unsigned char addr[BINARY_ADDR_SIZE]; + if (inet_pton(family, address, addr) != 1) { + Log(LOG_LEVEL_ERR, "Invalid %s address: %s", family_name, address); return false; } - addr2.sin6_family = AF_INET6; - if (inet_pton(AF_INET6, address, &addr2.sin6_addr) != 1) - { - return false; - } - - for (int i = 0; i < blocks; i++) - { - if (addr1.sin6_addr.s6_addr[i] != addr2.sin6_addr.s6_addr[i]) - { - return false; - } - } - if (remaining_bits > 0) - { - uint8_t bitmask = (uint8_t)(0xFF << (8 - remaining_bits)); - - if ((addr1.sin6_addr.s6_addr[blocks] & bitmask) != - (addr2.sin6_addr.s6_addr[blocks] & bitmask)) - { - return false; - } - } - return true; + return LeadingBitsEqual(network, addr, bits); } /** @@ -496,7 +504,7 @@ int FuzzySetMatch(const char *s1, const char *s2) match = MatchPrefix(s1, s2); break; case PATTERN_CIDR: - match = is_v4 ? MatchIPv4CIDR(s1, s2) : MatchIPv6CIDR(s1, s2); + match = MatchCIDR(s1, is_v4 ? AF_INET : AF_INET6, s2); break; case PATTERN_RANGE: match = is_v4 ? MatchIPv4Range(s1, s2) : MatchIPv6Range(s1, s2); @@ -572,176 +580,64 @@ int FuzzyHostMatch(const char *arg0, const char *arg1, const char *refhost) return 0; } +/** + * Checks that s is an address pattern understood by FuzzySetMatch(). + */ bool FuzzyMatchParse(const char *s) { - short isCIDR = false, isrange = false, isv6 = false, isv4 = false, isADDR = false; - char address[CF_ADDRSIZE]; - int mask, count = 0; - - for (const char *sp = s; *sp != '\0'; sp++) /* Is this an address or hostname */ - { - if (!isxdigit((int) *sp)) - { - isADDR = false; - break; - } - - if (*sp == ':') /* Catches any ipv6 address */ - { - isADDR = true; - break; - } - - if (isdigit((int) *sp)) /* catch non-ipv4 address - no more than 3 digits */ - { - count++; - if (count > 3) - { - isADDR = false; - break; - } - } - else - { - count = 0; - } - } + const bool is_v4 = (strchr(s, '.') != NULL); + const bool is_v6 = (strchr(s, ':') != NULL); - if (!isADDR) + if (is_v4 && is_v6) { - return true; + Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses in '%s'", s); + return false; } - if (strstr(s, "/") != 0) + if (!is_v4 && !is_v6) { - isCIDR = true; + Log(LOG_LEVEL_VERBOSE, "'%s' is not an IP address, range or subnet", s); + return false; } - if (strstr(s, "-") != 0) + PatternNotation notation; + if (!GetPatternNotation(s, ¬ation)) { - isrange = true; + return false; } - if (strstr(s, ".") != 0) + bool valid = false; + if (notation == PATTERN_CIDR) { - isv4 = true; + unsigned char network[BINARY_ADDR_SIZE]; + unsigned long bits; + valid = ParseCIDR(s, is_v4 ? AF_INET : AF_INET6, network, &bits); } - - if (strstr(s, ":") != 0) + else if (is_v4) // Range or prefix { - isv6 = true; + Interval octets[IPV4_OCTETS]; + valid = (ParseIPv4Pattern(s, octets) > 0); } - - if (isv4 && isv6) + else if (notation == PATTERN_RANGE) // IPv6 { - Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses"); - return false; + Interval groups[IPV6_GROUPS]; + valid = ParseIPv6RangePattern(s, groups); } - - if (isCIDR && isrange) + else // IPv6 full address, or prefix of 1-7 groups { - Log(LOG_LEVEL_ERR, "Cannot mix CIDR notation with xx-yy range notation"); - return false; + struct in6_addr addr; + Interval groups[IPV6_GROUPS - 1]; + size_t count; + valid = inet_pton(AF_INET6, s, &addr) == 1 || + (ParseIPv6Groups(s, s + strlen(s), groups, IPV6_GROUPS - 1, &count) && + count > 0); } - if (isv4 && isCIDR) + if (!valid) { - if (strlen(s) > 4 + 3 * 4 + 1 + 2) /* xxx.yyy.zzz.mmm/cc */ - { - Log(LOG_LEVEL_ERR, "IPv4 address looks too long"); - return false; - } - - address[0] = '\0'; - mask = 0; - sscanf(s, "%16[^/]/%d", address, &mask); - - if (mask < 8) - { - Log(LOG_LEVEL_ERR, "Mask value %d in '%s' is less than 8", mask, s); - return false; - } - - if (mask > 30) - { - Log(LOG_LEVEL_ERR, "Mask value %d in '%s' is silly (> 30)", mask, s); - return false; - } + Log(LOG_LEVEL_VERBOSE, "Invalid IP address, range or subnet '%s'", s); } - - if (isv4 && isrange) - { - long i, from = -1, to = -1; - char buffer1[64]; - - const char *sp1 = s; - - for (i = 0; i < 4; i++) - { - buffer1[0] = '\0'; - sscanf(sp1, "%63[^.]", buffer1); - buffer1[63] = '\0'; - - if (strlen(buffer1) == 0) - { - break; - } - - sp1 += strlen(buffer1); - if (*sp1 == '.') - { - sp1++; - } - - if (strstr(buffer1, "-")) - { - sscanf(buffer1, "%ld-%ld", &from, &to); - - if ((from < 0) || (to < 0)) - { - Log(LOG_LEVEL_ERR, "Error in IP range - looks like address, or bad hostname"); - return false; - } - - if (to < from) - { - Log(LOG_LEVEL_ERR, "Bad IP range"); - return false; - } - - } - } - } - - if (isv6 && isCIDR) - { - char address[CF_ADDRSIZE]; - int mask; - - if (strlen(s) < 20) - { - Log(LOG_LEVEL_ERR, "IPv6 address looks too short"); - return false; - } - - if (strlen(s) > 42) - { - Log(LOG_LEVEL_ERR, "IPv6 address looks too long"); - return false; - } - - address[0] = '\0'; - mask = 0; - sscanf(s, "%40[^/]/%d", address, &mask); - - if (mask > 15) - { - Log(LOG_LEVEL_ERR, "IPv6 CIDR mask is too large"); - return false; - } - } - - return true; + return valid; } diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 08858c5bcc9..0861bc458d4 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -285,6 +285,35 @@ static void test_FuzzyMatchParse() { assert_true(FuzzyMatchParse("1.2.3.4")); assert_true(FuzzyMatchParse("1-10.2.3.4")); + assert_true(FuzzyMatchParse("1.2.3")); + assert_true(FuzzyMatchParse("192.168.1.50-140")); + assert_true(FuzzyMatchParse("0.0.0.0/0")); + assert_true(FuzzyMatchParse("8.8.8.8/31")); + assert_true(FuzzyMatchParse("1.2.3.4/32")); + assert_true(FuzzyMatchParse("2001:db8::1")); + assert_true(FuzzyMatchParse("2001:db8")); + assert_true(FuzzyMatchParse("2001:db8::/64")); + assert_true(FuzzyMatchParse("::/0")); + assert_true(FuzzyMatchParse("2001:db8:1-5::1")); + assert_true(FuzzyMatchParse("2001:0-ffff:0:0:0:0:0:1")); + + assert_false(FuzzyMatchParse("")); + assert_false(FuzzyMatchParse("localhost")); + assert_false(FuzzyMatchParse("www.cfengine.com")); + assert_false(FuzzyMatchParse("1.2.3.4.5")); + assert_false(FuzzyMatchParse("1.2.3.")); + assert_false(FuzzyMatchParse("1.2.3.256")); + assert_false(FuzzyMatchParse("10.5-1.0.0")); + assert_false(FuzzyMatchParse("10.0.0.0/33")); + assert_false(FuzzyMatchParse("10.0.0.0/")); + assert_false(FuzzyMatchParse("10.0.0/8")); + assert_false(FuzzyMatchParse("1.2.3.4/8-9")); + assert_false(FuzzyMatchParse("1.2.3.4:5::")); + assert_false(FuzzyMatchParse("2001:db8::/129")); + assert_false(FuzzyMatchParse("2001:db8:1-5")); + assert_false(FuzzyMatchParse("2001::1::1-2")); + assert_false(FuzzyMatchParse("2001:db8:5-1::1")); + assert_false(FuzzyMatchParse("2001:db8:g::1")); /* A range with fewer octets used to read past the end of the string. */ char *s = xstrdup("1-2.3"); From 2af3afa4486865bcd35492b2103a1eb2ae318f10 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:42 +0100 Subject: [PATCH 16/22] Fixed out-of-bounds read in `hostrange()` on all-digit host names FuzzyHostMatch() read before the start of its buffer when the host name was empty or only digits. Only visible with ASan/valgrind. Changelog: Title Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 51 +++++++++++++++++--------------------- tests/unit/addr_lib_test.c | 24 ++++++++++++++++++ 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 0f4e4bc31fd..67236213ece 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -531,48 +531,43 @@ bool FuzzyHostParse(const char *arg2) return true; } -int FuzzyHostMatch(const char *arg0, const char *arg1, const char *refhost) +/** + * Checks whether refhost is prefix followed by a number within range, + * e.g. FuzzyHostMatch("host", "1-10", "HOST5") == 0. + * The comparison of the prefix is case-insensitive. + * + * Returns 0 on match. + */ +int FuzzyHostMatch(const char *prefix, const char *range, const char *refhost) { - char *sp, refbase[1024]; - long cmp = -1, start = -1, end = -1; - char buf1[CF_BUFSIZE], buf2[CF_BUFSIZE]; - - strlcpy(refbase, refhost, sizeof(refbase)); - sp = refbase + strlen(refbase) - 1; - - while (isdigit((int) *sp)) + // Split refhost into base name and trailing number, e.g. "host12" + const size_t len = strlen(refhost); + size_t base_len = len; + while (base_len > 0 && isdigit((unsigned char) refhost[base_len - 1])) { - sp--; + base_len--; } - sp++; - sscanf(sp, "%ld", &cmp); - *sp = '\0'; - - if (cmp < 0) + if (base_len == 0 || base_len == len) { - return 1; + return 1; // No base name, or no number } - if (strlen(refbase) == 0) + unsigned long number; + if (!ParseNumber(refhost + base_len, refhost + len, 10, LONG_MAX, &number)) { - return 1; + return 1; // Number too large } - sscanf(arg1, "%ld-%ld", &start, &end); - - if ((cmp < start) || (cmp > end)) + long start = -1, end = -1; + sscanf(range, "%ld-%ld", &start, &end); + if ((long) number < start || (long) number > end) { return 1; } - strlcpy(buf1, refbase, CF_BUFSIZE); - strlcpy(buf2, arg0, CF_BUFSIZE); - - ToLowerStrInplace(buf1); - ToLowerStrInplace(buf2); - - if (strcmp(buf1, buf2) != 0) + if (strlen(prefix) != base_len || + !StringEqualN_IgnoreCase(prefix, refhost, base_len)) { return 1; } diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index 0861bc458d4..f46379dd7c5 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -321,6 +321,29 @@ static void test_FuzzyMatchParse() free(s); } +static void test_FuzzyHostMatch() +{ + assert_int_equal(FuzzyHostMatch("host", "1-10", "host1"), 0); + assert_int_equal(FuzzyHostMatch("host", "1-10", "host10"), 0); + assert_int_equal(FuzzyHostMatch("host", "1-10", "HOST5"), 0); + assert_int_equal(FuzzyHostMatch("Host", "1-10", "host5"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host0"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host11"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "hostx5"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "myhost5"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host"), 0); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host99999999999999999999999"), 0); + + /* Host names that are empty or all digits used to be scanned backwards + * past the start of the buffer. */ + char *s = xstrdup(""); + assert_int_not_equal(FuzzyHostMatch("host", "1-10", s), 0); + free(s); + + s = xstrdup("123"); + assert_int_not_equal(FuzzyHostMatch("", "1-200", s), 0); + free(s); +} int main() { @@ -333,6 +356,7 @@ int main() unit_test(test_FuzzySetMatch_ipv4), unit_test(test_FuzzySetMatch_ipv6), unit_test(test_FuzzyMatchParse), + unit_test(test_FuzzyHostMatch), }; return run_tests(tests); From 97a0e4e97e804ab5fe78e0b2a2de89e106e3194f Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:46 +0100 Subject: [PATCH 17/22] Fixed sys.policy_hub when bootstrapped to a host name starting with a digit It contained the host name, e.g. "1password.com", instead of its IP address. ParseHostPort() now uses inet_pton() to detect IPv4. Changelog: Title Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 21 ++++++++------------- tests/unit/addr_lib_test.c | 6 ++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 67236213ece..739083b3b14 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -642,9 +642,9 @@ bool FuzzyMatchParse(const char *s) * * @param hostname will point to the hostname, or NULL if no or empty hostname * @param port will point to the port, or NULL if no or empty port - * @return the type of hostname: ADDRESS_TYPE_IPV4 if it starts with a digit, - * ADDRESS_TYPE_IPV6 if it contains a ':' (not validated), otherwise - * (hostname, invalid or too long) ADDRESS_TYPE_OTHER + * @return the type of hostname: ADDRESS_TYPE_IPV4 if it is a valid IPv4 + * address, ADDRESS_TYPE_IPV6 if it contains a ':' (not validated), + * otherwise (hostname, invalid or too long) ADDRESS_TYPE_OTHER * @WARNING modifies #s to '\0' terminate hostname if followed by port. */ AddressType ParseHostPort(char *s, char **hostname, char **port) @@ -687,19 +687,13 @@ AddressType ParseHostPort(char *s, char **hostname, char **port) if (first_colon != NULL) { - if (first_dot != NULL && first_dot < first_colon) + // "1.2.3.4:80", "cfengine.com:80", "localhost:80" or "localhost:" + if ((first_dot != NULL && first_dot < first_colon) || + strchr(first_colon + 1, ':') == NULL) { - // "1.2.3.4:80" or "cfengine.com:80" *first_colon = '\0'; port_str = first_colon + 1; } - else if (strchr(first_colon + 1, ':') == NULL) - { - // "localhost:80" or "localhost:" - *first_colon = '\0'; - port_str = first_colon + 1; - classify = false; - } else // Multiple colons: IPv6 address, no port { is_ipv6 = true; @@ -719,7 +713,8 @@ AddressType ParseHostPort(char *s, char **hostname, char **port) return ADDRESS_TYPE_IPV6; } - return isdigit((unsigned char) (*hostname)[0]) + struct in_addr addr; + return (inet_pton(AF_INET, *hostname, &addr) == 1) ? ADDRESS_TYPE_IPV4 : ADDRESS_TYPE_OTHER; } diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index f46379dd7c5..ce3cd9c8268 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -168,6 +168,12 @@ static void test_ParseHostPort_address_type() ASSERT_ADDRESS_TYPE("[1.2.3.4", ADDRESS_TYPE_OTHER); ASSERT_ADDRESS_TYPE("", ADDRESS_TYPE_OTHER); + /* Hostnames starting with a digit are not IPv4 addresses */ + ASSERT_ADDRESS_TYPE("1password.com", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("1password.com:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("[1password.com]:80", ADDRESS_TYPE_OTHER); + ASSERT_ADDRESS_TYPE("1.2.3", ADDRESS_TYPE_OTHER); + #undef ASSERT_ADDRESS_TYPE } From acda8ce8e762c2689dde4c7ba7e53a37a2edd702 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:50 +0100 Subject: [PATCH 18/22] Renamed address matching functions after their policy functions There is nothing fuzzy about them. IPRangeMatch() and HostRangeMatch() now return true on match, like StringMatch(), instead of 0. Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- cf-serverd/server_access.c | 6 +- libcfnet/addr_lib.c | 109 ++++++++++++++++---------------- libcfnet/addr_lib.h | 8 +-- libpromises/evalfunction.c | 12 ++-- libpromises/item_lib.c | 2 +- tests/unit/addr_lib_test.c | 126 ++++++++++++++++++------------------- 6 files changed, 131 insertions(+), 132 deletions(-) diff --git a/cf-serverd/server_access.c b/cf-serverd/server_access.c index 8ef0aa5c892..0f642db45a2 100644 --- a/cf-serverd/server_access.c +++ b/cf-serverd/server_access.c @@ -29,7 +29,7 @@ #include "strlist.h" #include "server.h" -#include /* FuzzySetMatch */ +#include /* IPRangeMatch */ #include /* StringMatchFull TODO REMOVE */ #include #include @@ -90,7 +90,7 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *rule = NULL; for (size_t i = 0; i < StrList_Len(acl->admit.ips); i++) { - if (FuzzySetMatch(StrList_At(acl->admit.ips, i), ipaddr) == 0 || + if (IPRangeMatch(StrList_At(acl->admit.ips, i), ipaddr) || /* Legacy regex matching, TODO DEPRECATE */ StringMatchFull(StrList_At(acl->admit.ips, i), ipaddr)) { @@ -196,7 +196,7 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *rule = NULL; for (size_t i = 0; i < StrList_Len(acl->deny.ips); i++) { - if (FuzzySetMatch(StrList_At(acl->deny.ips, i), ipaddr) == 0 || + if (IPRangeMatch(StrList_At(acl->deny.ips, i), ipaddr) || /* Legacy regex matching, TODO DEPRECATE */ StringMatchFull(StrList_At(acl->deny.ips, i), ipaddr)) { diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 739083b3b14..c0db3895ffd 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -42,7 +42,7 @@ typedef struct unsigned long to; } Interval; -/* Notation used by an address pattern, see FuzzySetMatch() */ +/* Notation used by an address pattern, see IPRangeMatch() */ typedef enum { PATTERN_PREFIX, // "10.1.2.3", "10.1.2" @@ -460,66 +460,67 @@ static bool MatchIPv6Range(const char *pattern, const char *address) /* Public API */ /******************************************************************************/ -/* Match two IP strings - with : or . in hex or decimal - s1 is the test string, and s2 is the reference e.g. - FuzzySetMatch("128.39.74.10/23","128.39.75.56") == 0 - - s1 may be a CIDR ("10.0.0.0/8", "2001:db8::/32"), a range - ("10.0.0.1-50", "2001:db8:0-ff::1") or an address prefix ("10.1.2"). - - Returns 0 on match. */ - -/* TODO rename to AddrSubnetMatch() */ -int FuzzySetMatch(const char *s1, const char *s2) +/** + * Checks whether an IPv4 or IPv6 address is matched by a pattern, e.g. + * IPRangeMatch("128.39.74.10/23", "128.39.75.56") is true. + * + * The pattern may be a CIDR ("10.0.0.0/8", "2001:db8::/32"), a range + * ("10.0.0.1-50", "2001:db8:0-ff::1") or an address prefix ("10.1.2"). + */ +bool IPRangeMatch(const char *pattern, const char *address) { - if (strcmp(s1, s2) == 0) + if (strcmp(pattern, address) == 0) { - return 0; + return true; } - const bool is_v4 = (strchr(s1, '.') != NULL || strchr(s2, '.') != NULL); - const bool is_v6 = (strchr(s1, ':') != NULL || strchr(s2, ':') != NULL); + const bool is_v4 = (strchr(pattern, '.') != NULL || strchr(address, '.') != NULL); + const bool is_v6 = (strchr(pattern, ':') != NULL || strchr(address, ':') != NULL); if (is_v4 && is_v6) { - return -1; // Mixed IPv4 and IPv6 + return false; // Mixed IPv4 and IPv6 } PatternNotation notation; - if (!GetPatternNotation(s1, ¬ation)) + if (!GetPatternNotation(pattern, ¬ation)) { - return -1; + return false; } if (!is_v4 && !is_v6) { - Log(LOG_LEVEL_ERR, "Not a valid address range - or not a fully qualified name '%s'", s1); - return -1; + Log(LOG_LEVEL_ERR, "Not a valid address range - or not a fully qualified name '%s'", pattern); + return false; } bool match = false; switch (notation) { case PATTERN_PREFIX: - match = MatchPrefix(s1, s2); + match = MatchPrefix(pattern, address); break; case PATTERN_CIDR: - match = MatchCIDR(s1, is_v4 ? AF_INET : AF_INET6, s2); + match = MatchCIDR(pattern, is_v4 ? AF_INET : AF_INET6, address); break; case PATTERN_RANGE: - match = is_v4 ? MatchIPv4Range(s1, s2) : MatchIPv6Range(s1, s2); + match = is_v4 ? MatchIPv4Range(pattern, address) : MatchIPv6Range(pattern, address); break; } - return match ? 0 : -1; + return match; } -bool FuzzyHostParse(const char *arg2) +/** + * Checks that range has the form "X-Y", where X and Y are decimal numbers, + * as used by HostRangeMatch(). + */ +bool HostRangeIsValid(const char *range) { long start = -1, end = -1; int n; - n = sscanf(arg2, "%ld-%ld", &start, &end); + n = sscanf(range, "%ld-%ld", &start, &end); if (n != 2) { @@ -532,71 +533,69 @@ bool FuzzyHostParse(const char *arg2) } /** - * Checks whether refhost is prefix followed by a number within range, - * e.g. FuzzyHostMatch("host", "1-10", "HOST5") == 0. + * Checks whether hostname is prefix followed by a number within range, + * e.g. HostRangeMatch("host", "1-10", "HOST5") is true. * The comparison of the prefix is case-insensitive. - * - * Returns 0 on match. */ -int FuzzyHostMatch(const char *prefix, const char *range, const char *refhost) +bool HostRangeMatch(const char *prefix, const char *range, const char *hostname) { - // Split refhost into base name and trailing number, e.g. "host12" - const size_t len = strlen(refhost); + // Split hostname into base name and trailing number, e.g. "host12" + const size_t len = strlen(hostname); size_t base_len = len; - while (base_len > 0 && isdigit((unsigned char) refhost[base_len - 1])) + while (base_len > 0 && isdigit((unsigned char) hostname[base_len - 1])) { base_len--; } if (base_len == 0 || base_len == len) { - return 1; // No base name, or no number + return false; // No base name, or no number } unsigned long number; - if (!ParseNumber(refhost + base_len, refhost + len, 10, LONG_MAX, &number)) + if (!ParseNumber(hostname + base_len, hostname + len, 10, LONG_MAX, &number)) { - return 1; // Number too large + return false; // Number too large } long start = -1, end = -1; sscanf(range, "%ld-%ld", &start, &end); if ((long) number < start || (long) number > end) { - return 1; + return false; } if (strlen(prefix) != base_len || - !StringEqualN_IgnoreCase(prefix, refhost, base_len)) + !StringEqualN_IgnoreCase(prefix, hostname, base_len)) { - return 1; + return false; } - return 0; + return true; } /** - * Checks that s is an address pattern understood by FuzzySetMatch(). + * Checks that pattern is an address pattern understood by IPRangeMatch(). */ -bool FuzzyMatchParse(const char *s) +bool IPRangeIsValid(const char *pattern) { - const bool is_v4 = (strchr(s, '.') != NULL); - const bool is_v6 = (strchr(s, ':') != NULL); + const bool is_v4 = (strchr(pattern, '.') != NULL); + const bool is_v6 = (strchr(pattern, ':') != NULL); if (is_v4 && is_v6) { - Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses in '%s'", s); + Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses in '%s'", pattern); return false; } if (!is_v4 && !is_v6) { - Log(LOG_LEVEL_VERBOSE, "'%s' is not an IP address, range or subnet", s); + Log(LOG_LEVEL_VERBOSE, "'%s' is not an IP address, range or subnet", pattern); return false; } PatternNotation notation; - if (!GetPatternNotation(s, ¬ation)) + if (!GetPatternNotation(pattern, ¬ation)) { return false; } @@ -606,31 +605,31 @@ bool FuzzyMatchParse(const char *s) { unsigned char network[BINARY_ADDR_SIZE]; unsigned long bits; - valid = ParseCIDR(s, is_v4 ? AF_INET : AF_INET6, network, &bits); + valid = ParseCIDR(pattern, is_v4 ? AF_INET : AF_INET6, network, &bits); } else if (is_v4) // Range or prefix { Interval octets[IPV4_OCTETS]; - valid = (ParseIPv4Pattern(s, octets) > 0); + valid = (ParseIPv4Pattern(pattern, octets) > 0); } else if (notation == PATTERN_RANGE) // IPv6 { Interval groups[IPV6_GROUPS]; - valid = ParseIPv6RangePattern(s, groups); + valid = ParseIPv6RangePattern(pattern, groups); } else // IPv6 full address, or prefix of 1-7 groups { struct in6_addr addr; Interval groups[IPV6_GROUPS - 1]; size_t count; - valid = inet_pton(AF_INET6, s, &addr) == 1 || - (ParseIPv6Groups(s, s + strlen(s), groups, IPV6_GROUPS - 1, &count) && + valid = inet_pton(AF_INET6, pattern, &addr) == 1 || + (ParseIPv6Groups(pattern, pattern + strlen(pattern), groups, IPV6_GROUPS - 1, &count) && count > 0); } if (!valid) { - Log(LOG_LEVEL_VERBOSE, "Invalid IP address, range or subnet '%s'", s); + Log(LOG_LEVEL_VERBOSE, "Invalid IP address, range or subnet '%s'", pattern); } return valid; } diff --git a/libcfnet/addr_lib.h b/libcfnet/addr_lib.h index faffc1fce56..6cf0fd7c8fa 100644 --- a/libcfnet/addr_lib.h +++ b/libcfnet/addr_lib.h @@ -27,10 +27,10 @@ #include -int FuzzySetMatch(const char *s1, const char *s2); -bool FuzzyHostParse(const char *arg2); -int FuzzyHostMatch(const char *arg0, const char *arg1, const char *basename); -bool FuzzyMatchParse(const char *item); +bool IPRangeMatch(const char *pattern, const char *address); +bool IPRangeIsValid(const char *pattern); +bool HostRangeMatch(const char *prefix, const char *range, const char *hostname); +bool HostRangeIsValid(const char *range); typedef enum { ADDRESS_TYPE_OTHER, // Hostname or invalid diff --git a/libpromises/evalfunction.c b/libpromises/evalfunction.c index d42df8c6fca..c5f35be7757 100644 --- a/libpromises/evalfunction.c +++ b/libpromises/evalfunction.c @@ -6476,7 +6476,7 @@ static FnCallResult FnCallIPRange(EvalContext *ctx, ARG_UNUSED const Policy *pol const char *range = RlistScalarValue(finalargs); const Rlist *ifaces = finalargs->next; - if (!FuzzyMatchParse(range)) + if (!IPRangeIsValid(range)) { Log(LOG_LEVEL_VERBOSE, "%s(%s): argument is not a valid address range", @@ -6488,7 +6488,7 @@ static FnCallResult FnCallIPRange(EvalContext *ctx, ARG_UNUSED const Policy *pol ip != NULL; ip = ip->next) { - if (FuzzySetMatch(range, ip->name) == 0) + if (IPRangeMatch(range, ip->name)) { /* * MODE1: iprange(range) @@ -6548,7 +6548,7 @@ static FnCallResult FnCallIsIpInSubnet(ARG_UNUSED EvalContext *ctx, const char *range = RlistScalarValue(finalargs); const Rlist *ips = finalargs->next; - if (!FuzzyMatchParse(range)) + if (!IPRangeIsValid(range)) { Log(LOG_LEVEL_VERBOSE, "%s(%s): argument is not a valid address range", @@ -6560,7 +6560,7 @@ static FnCallResult FnCallIsIpInSubnet(ARG_UNUSED EvalContext *ctx, { const char *ip_s = RlistScalarValue(ip); - if (FuzzySetMatch(range, ip_s) == 0) + if (IPRangeMatch(range, ip_s)) { Log(LOG_LEVEL_DEBUG, "%s(%s): Match on IP '%s'", fp->name, range, ip_s); @@ -6578,12 +6578,12 @@ static FnCallResult FnCallHostRange(ARG_UNUSED EvalContext *ctx, ARG_UNUSED cons char *prefix = RlistScalarValue(finalargs); char *range = RlistScalarValue(finalargs->next); - if (!FuzzyHostParse(range)) + if (!HostRangeIsValid(range)) { return FnFailure(); } - return FnReturnContext(FuzzyHostMatch(prefix, range, VUQNAME) == 0); + return FnReturnContext(HostRangeMatch(prefix, range, VUQNAME)); } /*********************************************************************/ diff --git a/libpromises/item_lib.c b/libpromises/item_lib.c index fd697f328ff..8d36f3febcd 100644 --- a/libpromises/item_lib.c +++ b/libpromises/item_lib.c @@ -789,7 +789,7 @@ bool IsMatchItemIn(const Item *list, const char *item) CYCLE_DECLARE(ptr, slow, toggle); while (ptr != NULL) { - if (FuzzySetMatch(ptr->name, item) == 0 || + if (IPRangeMatch(ptr->name, item) || (IsRegex(ptr->name) && StringMatchFull(ptr->name, item))) { diff --git a/tests/unit/addr_lib_test.c b/tests/unit/addr_lib_test.c index ce3cd9c8268..3a9d7cf8378 100644 --- a/tests/unit/addr_lib_test.c +++ b/tests/unit/addr_lib_test.c @@ -178,11 +178,11 @@ static void test_ParseHostPort_address_type() } #define ASSERT_MATCH(pattern, address) \ - assert_int_equal(FuzzySetMatch(pattern, address), 0) + assert_true(IPRangeMatch(pattern, address)) #define ASSERT_NO_MATCH(pattern, address) \ - assert_int_not_equal(FuzzySetMatch(pattern, address), 0) + assert_false(IPRangeMatch(pattern, address)) -static void test_FuzzySetMatch_ipv4() +static void test_IPRangeMatch_ipv4() { /* Prefix */ ASSERT_MATCH("1.2.3", "1.2.3.4"); @@ -223,7 +223,7 @@ static void test_FuzzySetMatch_ipv4() ASSERT_NO_MATCH("10.0.0.0/8", "::1"); } -static void test_FuzzySetMatch_ipv6() +static void test_IPRangeMatch_ipv6() { /* CIDR */ ASSERT_MATCH("2001:db8::/32", "2001:db8:ffff::1"); @@ -262,92 +262,92 @@ static void test_FuzzySetMatch_ipv6() #undef ASSERT_MATCH #undef ASSERT_NO_MATCH -static void test_FuzzySetMatch() +static void test_IPRangeMatch() { /* Sanity: exact and subnet matches still work. */ - assert_int_equal(FuzzySetMatch("128.39.74.10/23", "128.39.75.56"), 0); - assert_int_not_equal(FuzzySetMatch("128.39.74.10/24", "128.39.75.56"), 0); - assert_int_equal(FuzzySetMatch("1.2.3.4", "1.2.3.4"), 0); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", "1.2.3.5"), 0); + assert_true(IPRangeMatch("128.39.74.10/23", "128.39.75.56")); + assert_false(IPRangeMatch("128.39.74.10/24", "128.39.75.56")); + assert_true(IPRangeMatch("1.2.3.4", "1.2.3.4")); + assert_false(IPRangeMatch("1.2.3.4", "1.2.3.5")); /* An address with fewer octets than the pattern used to walk the scan * pointer past the end of the string. Heap-allocate the address so that * the over-read is caught under ASan. It must simply not match. */ char *s = xstrdup("1"); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", s), 0); + assert_false(IPRangeMatch("1.2.3.4", s)); free(s); s = xstrdup("1.2"); - assert_int_not_equal(FuzzySetMatch("1.2.3.4", s), 0); + assert_false(IPRangeMatch("1.2.3.4", s)); free(s); /* Same for an IPv6 range pattern against a short address. */ s = xstrdup("2001"); - assert_int_not_equal(FuzzySetMatch("2001:0-ffff:0:0:0:0:0:1", s), 0); + assert_false(IPRangeMatch("2001:0-ffff:0:0:0:0:0:1", s)); free(s); } -static void test_FuzzyMatchParse() +static void test_IPRangeIsValid() { - assert_true(FuzzyMatchParse("1.2.3.4")); - assert_true(FuzzyMatchParse("1-10.2.3.4")); - assert_true(FuzzyMatchParse("1.2.3")); - assert_true(FuzzyMatchParse("192.168.1.50-140")); - assert_true(FuzzyMatchParse("0.0.0.0/0")); - assert_true(FuzzyMatchParse("8.8.8.8/31")); - assert_true(FuzzyMatchParse("1.2.3.4/32")); - assert_true(FuzzyMatchParse("2001:db8::1")); - assert_true(FuzzyMatchParse("2001:db8")); - assert_true(FuzzyMatchParse("2001:db8::/64")); - assert_true(FuzzyMatchParse("::/0")); - assert_true(FuzzyMatchParse("2001:db8:1-5::1")); - assert_true(FuzzyMatchParse("2001:0-ffff:0:0:0:0:0:1")); - - assert_false(FuzzyMatchParse("")); - assert_false(FuzzyMatchParse("localhost")); - assert_false(FuzzyMatchParse("www.cfengine.com")); - assert_false(FuzzyMatchParse("1.2.3.4.5")); - assert_false(FuzzyMatchParse("1.2.3.")); - assert_false(FuzzyMatchParse("1.2.3.256")); - assert_false(FuzzyMatchParse("10.5-1.0.0")); - assert_false(FuzzyMatchParse("10.0.0.0/33")); - assert_false(FuzzyMatchParse("10.0.0.0/")); - assert_false(FuzzyMatchParse("10.0.0/8")); - assert_false(FuzzyMatchParse("1.2.3.4/8-9")); - assert_false(FuzzyMatchParse("1.2.3.4:5::")); - assert_false(FuzzyMatchParse("2001:db8::/129")); - assert_false(FuzzyMatchParse("2001:db8:1-5")); - assert_false(FuzzyMatchParse("2001::1::1-2")); - assert_false(FuzzyMatchParse("2001:db8:5-1::1")); - assert_false(FuzzyMatchParse("2001:db8:g::1")); + assert_true(IPRangeIsValid("1.2.3.4")); + assert_true(IPRangeIsValid("1-10.2.3.4")); + assert_true(IPRangeIsValid("1.2.3")); + assert_true(IPRangeIsValid("192.168.1.50-140")); + assert_true(IPRangeIsValid("0.0.0.0/0")); + assert_true(IPRangeIsValid("8.8.8.8/31")); + assert_true(IPRangeIsValid("1.2.3.4/32")); + assert_true(IPRangeIsValid("2001:db8::1")); + assert_true(IPRangeIsValid("2001:db8")); + assert_true(IPRangeIsValid("2001:db8::/64")); + assert_true(IPRangeIsValid("::/0")); + assert_true(IPRangeIsValid("2001:db8:1-5::1")); + assert_true(IPRangeIsValid("2001:0-ffff:0:0:0:0:0:1")); + + assert_false(IPRangeIsValid("")); + assert_false(IPRangeIsValid("localhost")); + assert_false(IPRangeIsValid("www.cfengine.com")); + assert_false(IPRangeIsValid("1.2.3.4.5")); + assert_false(IPRangeIsValid("1.2.3.")); + assert_false(IPRangeIsValid("1.2.3.256")); + assert_false(IPRangeIsValid("10.5-1.0.0")); + assert_false(IPRangeIsValid("10.0.0.0/33")); + assert_false(IPRangeIsValid("10.0.0.0/")); + assert_false(IPRangeIsValid("10.0.0/8")); + assert_false(IPRangeIsValid("1.2.3.4/8-9")); + assert_false(IPRangeIsValid("1.2.3.4:5::")); + assert_false(IPRangeIsValid("2001:db8::/129")); + assert_false(IPRangeIsValid("2001:db8:1-5")); + assert_false(IPRangeIsValid("2001::1::1-2")); + assert_false(IPRangeIsValid("2001:db8:5-1::1")); + assert_false(IPRangeIsValid("2001:db8:g::1")); /* A range with fewer octets used to read past the end of the string. */ char *s = xstrdup("1-2.3"); - FuzzyMatchParse(s); + IPRangeIsValid(s); free(s); } -static void test_FuzzyHostMatch() +static void test_HostRangeMatch() { - assert_int_equal(FuzzyHostMatch("host", "1-10", "host1"), 0); - assert_int_equal(FuzzyHostMatch("host", "1-10", "host10"), 0); - assert_int_equal(FuzzyHostMatch("host", "1-10", "HOST5"), 0); - assert_int_equal(FuzzyHostMatch("Host", "1-10", "host5"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host0"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host11"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "hostx5"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "myhost5"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host"), 0); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", "host99999999999999999999999"), 0); + assert_true(HostRangeMatch("host", "1-10", "host1")); + assert_true(HostRangeMatch("host", "1-10", "host10")); + assert_true(HostRangeMatch("host", "1-10", "HOST5")); + assert_true(HostRangeMatch("Host", "1-10", "host5")); + assert_false(HostRangeMatch("host", "1-10", "host0")); + assert_false(HostRangeMatch("host", "1-10", "host11")); + assert_false(HostRangeMatch("host", "1-10", "hostx5")); + assert_false(HostRangeMatch("host", "1-10", "myhost5")); + assert_false(HostRangeMatch("host", "1-10", "host")); + assert_false(HostRangeMatch("host", "1-10", "host99999999999999999999999")); /* Host names that are empty or all digits used to be scanned backwards * past the start of the buffer. */ char *s = xstrdup(""); - assert_int_not_equal(FuzzyHostMatch("host", "1-10", s), 0); + assert_false(HostRangeMatch("host", "1-10", s)); free(s); s = xstrdup("123"); - assert_int_not_equal(FuzzyHostMatch("", "1-200", s), 0); + assert_false(HostRangeMatch("", "1-200", s)); free(s); } @@ -358,11 +358,11 @@ int main() { unit_test(test_ParseHostPort), unit_test(test_ParseHostPort_address_type), - unit_test(test_FuzzySetMatch), - unit_test(test_FuzzySetMatch_ipv4), - unit_test(test_FuzzySetMatch_ipv6), - unit_test(test_FuzzyMatchParse), - unit_test(test_FuzzyHostMatch), + unit_test(test_IPRangeMatch), + unit_test(test_IPRangeMatch_ipv4), + unit_test(test_IPRangeMatch_ipv6), + unit_test(test_IPRangeIsValid), + unit_test(test_HostRangeMatch), }; return run_tests(tests); From e3d1a36497a88677c5868189dcbed3135bd48e5c Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:33:55 +0100 Subject: [PATCH 19/22] Added assert to access_CheckResource() The acl argument is always dereferenced. Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- cf-serverd/server_access.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cf-serverd/server_access.c b/cf-serverd/server_access.c index 0f642db45a2..52972136d1c 100644 --- a/cf-serverd/server_access.c +++ b/cf-serverd/server_access.c @@ -73,6 +73,8 @@ static bool access_CheckResource(const struct resource_acl *acl, const char *ipaddr, const char *hostname, const char *key, const char *username) { + assert(acl != NULL); + bool access = false; /* DENY by default */ bool have_match = false; /* No matching rule found yet */ From bc62b2d86e8f7f5613182e9156bd3656716e2517 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:34:00 +0100 Subject: [PATCH 20/22] Replaced strchr() with StringContainsChar() in addr_lib.c Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index c0db3895ffd..8185a666a03 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -299,8 +299,8 @@ static bool ParseCIDR(const char *pattern, int family, */ static bool GetPatternNotation(const char *pattern, PatternNotation *notation) { - const bool is_cidr = (strchr(pattern, '/') != NULL); - const bool is_range = (strchr(pattern, '-') != NULL); + const bool is_cidr = StringContainsChar(pattern, '/'); + const bool is_range = StringContainsChar(pattern, '-'); if (is_cidr && is_range) { @@ -474,8 +474,8 @@ bool IPRangeMatch(const char *pattern, const char *address) return true; } - const bool is_v4 = (strchr(pattern, '.') != NULL || strchr(address, '.') != NULL); - const bool is_v6 = (strchr(pattern, ':') != NULL || strchr(address, ':') != NULL); + const bool is_v4 = (StringContainsChar(pattern, '.') || StringContainsChar(address, '.')); + const bool is_v6 = (StringContainsChar(pattern, ':') || StringContainsChar(address, ':')); if (is_v4 && is_v6) { @@ -579,8 +579,8 @@ bool HostRangeMatch(const char *prefix, const char *range, const char *hostname) */ bool IPRangeIsValid(const char *pattern) { - const bool is_v4 = (strchr(pattern, '.') != NULL); - const bool is_v6 = (strchr(pattern, ':') != NULL); + const bool is_v4 = StringContainsChar(pattern, '.'); + const bool is_v6 = StringContainsChar(pattern, ':'); if (is_v4 && is_v6) { @@ -668,7 +668,7 @@ AddressType ParseHostPort(char *s, char **hostname, char **port) if (closing != NULL) { *closing = '\0'; - is_ipv6 = (strchr(host, ':') != NULL); + is_ipv6 = StringContainsChar(host, ':'); if (closing[1] == ':') { port_str = closing + 2; @@ -688,7 +688,7 @@ AddressType ParseHostPort(char *s, char **hostname, char **port) { // "1.2.3.4:80", "cfengine.com:80", "localhost:80" or "localhost:" if ((first_dot != NULL && first_dot < first_colon) || - strchr(first_colon + 1, ':') == NULL) + !StringContainsChar(first_colon + 1, ':')) { *first_colon = '\0'; port_str = first_colon + 1; From 2003c090580b2ee77c25cff0313e112005125f62 Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Mon, 16 Nov 2026 10:34:04 +0100 Subject: [PATCH 21/22] Replaced strstr() and strcmp() with String*() helpers in addr_lib.c Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 8185a666a03..40dd5ffa95e 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -213,7 +213,7 @@ static bool ParseIPv6RangePattern(const char *pattern, Interval groups[IPV6_GROU return true; } - if (strstr(gap + 1, "::") != NULL) + if (StringContains(gap + 1, "::")) { Log(LOG_LEVEL_VERBOSE, "Invalid IPv6 range '%s': '::' may only appear once", pattern); @@ -335,7 +335,7 @@ static bool GetPatternNotation(const char *pattern, PatternNotation *notation) static bool MatchPrefix(const char *pattern, const char *address) { const size_t len = strlen(pattern); - return strncmp(pattern, address, len) == 0 && + return StringEqualN(pattern, address, len) && (address[len] == '\0' || address[len] == '.'); } @@ -469,7 +469,7 @@ static bool MatchIPv6Range(const char *pattern, const char *address) */ bool IPRangeMatch(const char *pattern, const char *address) { - if (strcmp(pattern, address) == 0) + if (StringEqual(pattern, address)) { return true; } From 2e725a082384f3a2da55936199fd02e4e600ba0d Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Tue, 17 Nov 2026 15:01:29 +0100 Subject: [PATCH 22/22] `iprange()` and `isipinsubnet()` now log invalid arguments at verbose level E.g. isipinsubnet("10.0.0.0/8", "not-an-ip-address") logged an error. The same applies to IP lists in cf-serverd, e.g. admit_ips and allowconnects. Changelog: Commit Ticket: ENT-14276 Signed-off-by: Victor Moene Co-Authored-By: Claude Opus 5.5 --- libcfnet/addr_lib.c | 10 +++++----- ...valid-ip.error.pass.cf => isipinsubnet-valid-ip.cf} | 3 --- 2 files changed, 5 insertions(+), 8 deletions(-) rename tests/acceptance/02_classes/02_functions/{isipinsubnet-valid-ip.error.pass.cf => isipinsubnet-valid-ip.cf} (92%) diff --git a/libcfnet/addr_lib.c b/libcfnet/addr_lib.c index 40dd5ffa95e..b4d218b7e58 100644 --- a/libcfnet/addr_lib.c +++ b/libcfnet/addr_lib.c @@ -304,7 +304,7 @@ static bool GetPatternNotation(const char *pattern, PatternNotation *notation) if (is_cidr && is_range) { - Log(LOG_LEVEL_ERR, + Log(LOG_LEVEL_VERBOSE, "Cannot mix CIDR notation with xxx-yyy range notation '%s'", pattern); return false; } @@ -367,7 +367,7 @@ static bool MatchCIDR(const char *pattern, int family, const char *address) unsigned long bits; if (!ParseCIDR(pattern, family, network, &bits)) { - Log(LOG_LEVEL_ERR, "Invalid %s CIDR: %s", family_name, pattern); + Log(LOG_LEVEL_VERBOSE, "Invalid %s CIDR: %s", family_name, pattern); return false; } @@ -376,7 +376,7 @@ static bool MatchCIDR(const char *pattern, int family, const char *address) unsigned char addr[BINARY_ADDR_SIZE]; if (inet_pton(family, address, addr) != 1) { - Log(LOG_LEVEL_ERR, "Invalid %s address: %s", family_name, address); + Log(LOG_LEVEL_VERBOSE, "Invalid %s address: %s", family_name, address); return false; } @@ -490,7 +490,7 @@ bool IPRangeMatch(const char *pattern, const char *address) if (!is_v4 && !is_v6) { - Log(LOG_LEVEL_ERR, "Not a valid address range - or not a fully qualified name '%s'", pattern); + Log(LOG_LEVEL_VERBOSE, "Not a valid address range - or not a fully qualified name '%s'", pattern); return false; } @@ -584,7 +584,7 @@ bool IPRangeIsValid(const char *pattern) if (is_v4 && is_v6) { - Log(LOG_LEVEL_ERR, "Mixture of IPv6 and IPv4 addresses in '%s'", pattern); + Log(LOG_LEVEL_VERBOSE, "Mixture of IPv6 and IPv4 addresses in '%s'", pattern); return false; } diff --git a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf similarity index 92% rename from tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf rename to tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf index d1929a2a910..c23da722aea 100644 --- a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf +++ b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf @@ -2,9 +2,6 @@ # # Test isipinsubnet() # -# This is an .error.pass.cf test, because isipinsubnet() is given -# 'not-an-ip-address' on purpose and logs an error. -# ####################################################### body common control {