Skip to content

Commit

Permalink
utils: Detect machine_id corruption and fill out a dummy value
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Dec 5, 2024
1 parent 9acc096 commit f4db2d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
char *id;
size_t bytes;
char *uuid;
int fallback = FLB_FALSE;

#ifdef __linux__
char *dbus_var = "/var/lib/dbus/machine-id";
Expand All @@ -1488,6 +1489,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
if (access(dbus_var, F_OK) == 0) { /* check if the file exists first */
ret = machine_id_read_and_sanitize(dbus_var, &id, &bytes);
if (ret == 0) {
if (bytes == 0) {
/* guid is somewhat corrupted */
fallback = FLB_TRUE;
goto fallback;
}
*out_id = id;
*out_size = bytes;
return 0;
Expand All @@ -1498,6 +1504,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
if (access(dbus_etc, F_OK) == 0) { /* check if the file exists first */
ret = machine_id_read_and_sanitize(dbus_etc, &id, &bytes);
if (ret == 0) {
if (bytes == 0) {
/* guid is somewhat corrupted */
fallback = FLB_TRUE;
goto fallback;
}
*out_id = id;
*out_size = bytes;
return 0;
Expand Down Expand Up @@ -1593,6 +1604,8 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
}
#endif

fallback:

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

/* generate a random uuid */
Expand All @@ -1605,6 +1618,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size)
if (ret == 0) {
*out_id = uuid;
*out_size = strlen(uuid);
if (fallback == FLB_TRUE) {
return 2;
}
return 0;
}

Expand Down
20 changes: 12 additions & 8 deletions tests/internal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ void test_flb_utils_get_machine_id()
size_t size2;

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

Expand All @@ -626,15 +626,19 @@ void test_flb_utils_get_machine_id()
}

ret = flb_utils_get_machine_id(&id2, &size2);
TEST_CHECK(ret == 0);
TEST_CHECK(ret == 0 || ret == 2);
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]);
if (ret == 2) {
TEST_CHECK(size2 == size);
}
else {
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]);
}
}
}

Expand Down

0 comments on commit f4db2d3

Please sign in to comment.