Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: use MachineGUID correctly. #8724

Merged
merged 8 commits into from
Aug 27, 2024
13 changes: 11 additions & 2 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography"),
0,
KEY_QUERY_VALUE,
KEY_QUERY_VALUE|KEY_WOW64_64KEY,
&hKey);

if (status != ERROR_SUCCESS) {
Expand All @@ -1420,9 +1420,16 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
return -1;
}

*out_size = dwBufSize;
memcpy(*out_id, buf, dwBufSize);

/* RegQueryValueEx sets dwBufSize to strlen()+1 for the NULL
* terminator, but we only need strlen(). */
*out_size = dwBufSize-1;
return 0;
}
else {
flb_error("unable to retrieve MachineGUID, error code: %d", status);
}
#elif defined (FLB_SYSTEM_MACOS)
bool bret;
CFStringRef serialNumber;
Expand Down Expand Up @@ -1464,6 +1471,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
}
#endif

flb_warn("falling back on random machine UUID");

/* generate a random uuid */
uuid = flb_malloc(38);
if (!uuid) {
Expand Down
24 changes: 24 additions & 0 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,38 @@ void test_flb_utils_split_quoted_errors()
void test_flb_utils_get_machine_id()
{
int ret;
int idx;
char *id = NULL;
size_t size;
char *id2 = NULL;
size_t size2;

ret = flb_utils_get_machine_id(&id, &size);
TEST_CHECK(ret == 0);
TEST_CHECK(size != 0);
TEST_CHECK(id != NULL);

for (idx = 0; idx < size; idx++) {
if (!TEST_CHECK(id[idx] != 0)) {
fprintf(stderr, "zero in ID: id[%d] = 0x%02x\n", idx, id[idx]);
}
}

ret = flb_utils_get_machine_id(&id2, &size2);
TEST_CHECK(ret == 0);
TEST_CHECK(size2 != 0);
TEST_CHECK(id2 != NULL);
TEST_CHECK(size2 == size);

for (idx = 0; idx < size; idx++) {
if (!TEST_CHECK(id[idx] == id2[idx])) {
fprintf(stderr, "bad byte in id v2 id2: id[%d] = 0x%02x, id2[%d] = 0x%02x\n",
idx, id[idx], idx, id2[idx]);
}
}

flb_free(id);
flb_free(id2);
}

struct size_to_bytes_check {
Expand Down
Loading