Skip to content

ENT-14276: Refactor Addr lib - #6485

Open
victormlg wants to merge 21 commits into
cfengine:masterfrom
victormlg:addr_lib
Open

victormlg wants to merge 21 commits into
cfengine:masterfrom
victormlg:addr_lib

Conversation

@victormlg

Copy link
Copy Markdown
Contributor

No description provided.

@victormlg victormlg changed the title ENT-14276_ Refactor Addr lib ENT-14276: Refactor Addr lib Nov 9, 2026
Comment thread cf-serverd/server_access.c Fixed
Comment thread cf-serverd/server_access.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
Comment thread libcfnet/addr_lib.c Fixed
@victormlg
victormlg marked this pull request as ready for review November 9, 2026 16:17

@larsewi larsewi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate each bug fix into their own commits with a changelog.

Comment thread libcfnet/addr_lib.c Resolved
Comment thread libcfnet/addr_lib.c Resolved
Comment thread libcfnet/addr_lib.c Resolved
Comment thread libcfnet/addr_lib.c Resolved
Comment thread cf-serverd/server_access.c Resolved
victormlg and others added 21 commits November 16, 2026 10:32
Covers the current behaviour, before refactoring them.

Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It was never defined.

Ticket: ENT-14276
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
"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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
FuzzyMatchParse() always returned true. It now uses the same parsers as
FuzzySetMatch(), including for CIDR patterns.

Changelog: Title
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ParseHostPort() classified e.g. "1password.com" as an IPv4 address. It
now uses inet_pton().

Changelog: Title
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There is nothing fuzzy about them. IPRangeMatch() and HostRangeMatch()
now return true on match, like StringMatch(), instead of 0.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The acl argument is always dereferenced.

Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Victor Moene <victor.moene@northern.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread libcfnet/addr_lib.c Outdated
(address[len] == '\0' || address[len] == '.');
}

static bool MatchIPv4CIDR(const char *pattern, const char *address)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left off reviewing here in commit 47d645e

@craigcomstock craigcomstock left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments, but looks good to me!

Comment thread libcfnet/addr_lib.c
*
* @return false if the input is empty, has invalid digits or exceeds max
*/
static bool ParseNumber(const char *start, const char *end, int base,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little confused at first by [start, end). It feels like a more common argument pair would be const char *string, size_t string_length. I don't see any check for start or end being NULL with an assert. That might be a good idea to sanity check as well as end < start though that will fall through the for loop pretty well.

Comment thread libcfnet/addr_lib.c

while (*count < max)
{
const char *sep = memchr(start, ':', end - start);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe an assert that end is greater than start would be wise?

Comment thread libcfnet/addr_lib.c

if (strstr(gap + 1, "::") != NULL)
{
return false; // More than one "::" (or ":::")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe log this as an error instead of a comment?

Comment thread libcfnet/addr_lib.c
if (!ParseIPv6Groups(pattern, gap, groups, IPV6_GROUPS - 1, &head_count) ||
!ParseIPv6Groups(gap + 2, end, tail, IPV6_GROUPS - 1 - head_count, &tail_count))
{
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe log an error here saying what is wrong and what the input was.

Comment thread libcfnet/addr_lib.c Outdated
if (!ParseNumber(start, end, 10, 255, &value) ||
value < octets[i].from || value > octets[i].to)
{
/* address has fewer octets than the pattern: no match */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be nice to include this comment as an error log message?

Comment thread libcfnet/addr_lib.c
Log(LOG_LEVEL_DEBUG, "Unequal");
return false;
}
return (i + 1 == count); // Address must have at least count octets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again here, it seems like logging an error might be a good idea.

Comment thread libcfnet/addr_lib.c
const char *slash = strchr(pattern, '/');
if (slash == NULL)
{
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it would be useful to log errors about why the parse function fails to aid the user in fixing their pattern or address?

Comment thread libcfnet/addr_lib.c
unsigned char addr[BINARY_ADDR_SIZE];
if (inet_pton(family, address, addr) != 1)
{
Log(LOG_LEVEL_ERR, "Invalid reference %s: %s", family_name, address);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the word "reference" here and not "address"?

Comment thread libcfnet/addr_lib.c Outdated
if (base_len == 0 || base_len == len)
{
return 1;
return 1; // No base name, or no number

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for these failure cases, should we log why they are failing? maybe error, or maybe verbose level if errors are logged elsewhere. If a general error is logged due to returning 1 maybe that message should say something about "see verbose logs for details".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants