From e81c47f8e3670e79b746eb50705b2999eb0665b4 Mon Sep 17 00:00:00 2001 From: dietWall Date: Wed, 19 Nov 2025 11:49:14 +0100 Subject: [PATCH 1/5] added an machine-id file handling for randomly generated uuids in a fallback scenario Signed-off-by: dietWall --- src/flb_utils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index e816651fb4d..5a6a1f84dc4 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -2043,6 +2043,31 @@ static int machine_id_read_and_sanitize(char *path, return 0; } +int write_uuid_to_file(char* filename, char* uuid) { + int fd; + size_t uuid_len; + + if (filename == NULL || uuid == NULL) { + return FLB_FALSE; + } + + /* write uuid to file */ + fd = flb_open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if (fd == -1) { + return FLB_FALSE; + } + + uuid_len = strlen(uuid); + + if (flb_write(fd, uuid, uuid_len) != uuid_len) { + flb_close(fd); + return FLB_FALSE; + } + + flb_close(fd); + return FLB_TRUE; +} + int flb_utils_get_machine_id(char **out_id, size_t *out_size) { int ret; @@ -2050,6 +2075,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) size_t bytes; char *uuid; int fallback = FLB_FALSE; + char *fallback_id = "machine-id"; //should reside in current working directory #ifdef __linux__ char *dbus_var = "/var/lib/dbus/machine-id"; @@ -2084,6 +2110,24 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) return 0; } } + + if (access(fallback_id, F_OK) == 0) + { + ret = machine_id_read_and_sanitize(fallback_id, &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; + } + #elif defined(__FreeBSD__) || defined(__NetBSD__) || \ defined(__OpenBSD__) || defined(__DragonFly__) @@ -2175,7 +2219,7 @@ 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 */ @@ -2185,7 +2229,15 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) return -1; } ret = flb_utils_uuid_v4_gen(uuid); + if (ret == 0) { + + int write_result = write_uuid_to_file(fallback_id, uuid); + if (write_result != 0) + { + //writing failed, next uuid generation + flb_warn("failed to write machine-id to file %s", fallback_id); + } *out_id = uuid; *out_size = strlen(uuid); if (fallback == FLB_TRUE) { @@ -2193,7 +2245,6 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) } return 0; } - return -1; } From 53141bf7deb4ef53b7e26d18e7efbaad2c76b9b6 Mon Sep 17 00:00:00 2001 From: dietWall Date: Wed, 19 Nov 2025 11:54:56 +0100 Subject: [PATCH 2/5] code formatting according contribution guidelines Signed-off-by: dietWall --- src/flb_utils.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index 5a6a1f84dc4..43cfdcbde12 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -2075,7 +2075,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) size_t bytes; char *uuid; int fallback = FLB_FALSE; - char *fallback_id = "machine-id"; //should reside in current working directory + char *fallback_id_file = "machine-id"; //should reside in current working directory #ifdef __linux__ char *dbus_var = "/var/lib/dbus/machine-id"; @@ -2111,9 +2111,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) } } - if (access(fallback_id, F_OK) == 0) + if (access(fallback_id_file, F_OK) == 0) { - ret = machine_id_read_and_sanitize(fallback_id, &id, &bytes); + ret = machine_id_read_and_sanitize(fallback_id_file, &id, &bytes); if(ret == 0) { if (bytes == 0) { @@ -2232,11 +2232,11 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (ret == 0) { - int write_result = write_uuid_to_file(fallback_id, uuid); - if (write_result != 0) - { - //writing failed, next uuid generation - flb_warn("failed to write machine-id to file %s", fallback_id); + ret = write_uuid_to_file(fallback_id_file, uuid); + if (ret != 0){ + //writing failed, next uuid generation will be random again + //write a message and return + flb_warn("failed to write machine-id to file %s", fallback_id_file); } *out_id = uuid; *out_size = strlen(uuid); From 3ef7444d215e8304ac932ed704f95bfb87666eb1 Mon Sep 17 00:00:00 2001 From: dietWall Date: Wed, 19 Nov 2025 14:44:29 +0100 Subject: [PATCH 3/5] more code formatting guidelines Signed-off-by: dietWall --- src/flb_utils.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index 43cfdcbde12..a240241dd3d 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -2043,7 +2043,8 @@ static int machine_id_read_and_sanitize(char *path, return 0; } -int write_uuid_to_file(char* filename, char* uuid) { +int write_uuid_to_file(char* filename, char* uuid) +{ int fd; size_t uuid_len; @@ -2075,7 +2076,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) size_t bytes; char *uuid; int fallback = FLB_FALSE; - char *fallback_id_file = "machine-id"; //should reside in current working directory + char *fallback_id_file = "machine-id"; /*should reside in current working directory*/ #ifdef __linux__ char *dbus_var = "/var/lib/dbus/machine-id"; @@ -2111,11 +2112,9 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) } } - if (access(fallback_id_file, F_OK) == 0) - { + if (access(fallback_id_file, F_OK) == 0){ ret = machine_id_read_and_sanitize(fallback_id_file, &id, &bytes); - if(ret == 0) - { + if(ret == 0){ if (bytes == 0) { /* guid is somewhat corrupted */ fallback = FLB_TRUE; @@ -2234,8 +2233,10 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) ret = write_uuid_to_file(fallback_id_file, uuid); if (ret != 0){ - //writing failed, next uuid generation will be random again - //write a message and return + /* + * writing failed, next uuid generation will be random again + * log a warning message and return + */ flb_warn("failed to write machine-id to file %s", fallback_id_file); } *out_id = uuid; From aed26a37b55f8f08b8341b840c508b92fa4a26fb Mon Sep 17 00:00:00 2001 From: dietWall Date: Wed, 19 Nov 2025 15:43:55 +0100 Subject: [PATCH 4/5] more suggested changes, code formatting and fixed some compiler warnigs Signed-off-by: dietWall --- src/flb_utils.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index a240241dd3d..05cf7d3563c 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -2053,19 +2053,19 @@ int write_uuid_to_file(char* filename, char* uuid) } /* write uuid to file */ - fd = flb_open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); + fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (fd == -1) { return FLB_FALSE; } uuid_len = strlen(uuid); - if (flb_write(fd, uuid, uuid_len) != uuid_len) { - flb_close(fd); + if (write(fd, uuid, uuid_len) != uuid_len) { + close(fd); return FLB_FALSE; } - flb_close(fd); + close(fd); return FLB_TRUE; } @@ -2076,7 +2076,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) size_t bytes; char *uuid; int fallback = FLB_FALSE; - char *fallback_id_file = "machine-id"; /*should reside in current working directory*/ + char *fallback_id_file = "/tmp/flb-machine-id"; /*should reside in current working directory*/ #ifdef __linux__ char *dbus_var = "/var/lib/dbus/machine-id"; @@ -2114,7 +2114,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (access(fallback_id_file, F_OK) == 0){ ret = machine_id_read_and_sanitize(fallback_id_file, &id, &bytes); - if(ret == 0){ + if (ret == 0){ if (bytes == 0) { /* guid is somewhat corrupted */ fallback = FLB_TRUE; @@ -2232,7 +2232,7 @@ int flb_utils_get_machine_id(char **out_id, size_t *out_size) if (ret == 0) { ret = write_uuid_to_file(fallback_id_file, uuid); - if (ret != 0){ + if (ret == FLB_FALSE){ /* * writing failed, next uuid generation will be random again * log a warning message and return From 71648550bcbafa9cc3906278d59aec66e02d4f23 Mon Sep 17 00:00:00 2001 From: dietWall Date: Wed, 19 Nov 2025 20:54:55 +0100 Subject: [PATCH 5/5] hardened file permissions Signed-off-by: dietWall --- src/flb_utils.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flb_utils.c b/src/flb_utils.c index 05cf7d3563c..963fc555128 100644 --- a/src/flb_utils.c +++ b/src/flb_utils.c @@ -2043,7 +2043,7 @@ static int machine_id_read_and_sanitize(char *path, return 0; } -int write_uuid_to_file(char* filename, char* uuid) +static int write_uuid_to_file(const char* filename, char* uuid) { int fd; size_t uuid_len; @@ -2053,7 +2053,7 @@ int write_uuid_to_file(char* filename, char* uuid) } /* write uuid to file */ - fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); + fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600); if (fd == -1) { return FLB_FALSE; }