summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCosta Tsaousis <costa@netdata.cloud>2023-09-19 22:12:06 +0300
committerGitHub <noreply@github.com>2023-09-19 22:12:06 +0300
commit541facfa3e5021ebc5c7fb8944987f3a3a02d48a (patch)
tree4bb6a70a73a942f0a36c4547c10395c87c2a2c2d
parent83c48807de20f299c58c9873894aad9889af3b24 (diff)
Systemd-Journal: fix crash when the uid or gid do not have names (#16015)
-rw-r--r--collectors/systemd-journal.plugin/systemd-journal.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/collectors/systemd-journal.plugin/systemd-journal.c b/collectors/systemd-journal.plugin/systemd-journal.c
index 6efbb57550..522c127bde 100644
--- a/collectors/systemd-journal.plugin/systemd-journal.c
+++ b/collectors/systemd-journal.plugin/systemd-journal.c
@@ -511,23 +511,25 @@ static FACET_ROW_SEVERITY syslog_priority_to_facet_severity(int priority) {
static char *uid_to_username(uid_t uid, char *buffer, size_t buffer_size) {
static __thread char tmp[1024 + 1];
- struct passwd pw, *result;
+ struct passwd pw, *result = NULL;
- if (getpwuid_r(uid, &pw, tmp, 1024, &result) != 0 || result == NULL)
- return NULL;
+ if (getpwuid_r(uid, &pw, tmp, sizeof(tmp), &result) != 0 || !result || !pw.pw_name || !(*pw.pw_name))
+ snprintfz(buffer, buffer_size - 1, "%u", uid);
+ else
+ snprintfz(buffer, buffer_size - 1, "%u (%s)", uid, pw.pw_name);
- snprintfz(buffer, buffer_size - 1, "%u (%s)", uid, pw.pw_name);
return buffer;
}
static char *gid_to_groupname(gid_t gid, char* buffer, size_t buffer_size) {
- static __thread char tmp[1024 + 1];
- struct group grp, *result;
+ static __thread char tmp[1024];
+ struct group grp, *result = NULL;
- if (getgrgid_r(gid, &grp, tmp, 1024, &result) != 0 || result == NULL)
- return NULL;
+ if (getgrgid_r(gid, &grp, tmp, sizeof(tmp), &result) != 0 || !result || !grp.gr_name || !(*grp.gr_name))
+ snprintfz(buffer, buffer_size - 1, "%u", gid);
+ else
+ snprintfz(buffer, buffer_size - 1, "%u (%s)", gid, grp.gr_name);
- snprintfz(buffer, buffer_size - 1, "%u (%s)", gid, grp.gr_name);
return buffer;
}
@@ -560,7 +562,7 @@ static void netdata_systemd_journal_transform_priority(FACETS *facets __maybe_un
// ----------------------------------------------------------------------------
// UID and GID transformation
-#define UID_GID_HASHTABLE_SIZE 1000
+#define UID_GID_HASHTABLE_SIZE 10000
struct word_t2str_hashtable_entry {
struct word_t2str_hashtable_entry *next;
@@ -598,8 +600,8 @@ const char *uid_to_username_cached(uid_t uid, size_t *length) {
struct word_t2str_hashtable_entry **e = word_t2str_hashtable_slot(&uid_hashtable, uid);
if(!(*e)) {
- static __thread char buf[1024 + 1];
- const char *name = uid_to_username(uid, buf, 1024);
+ static __thread char buf[1024];
+ const char *name = uid_to_username(uid, buf, sizeof(buf));
size_t size = strlen(name) + 1;
*e = callocz(1, sizeof(struct word_t2str_hashtable_entry) + size);
@@ -619,8 +621,8 @@ const char *gid_to_groupname_cached(gid_t gid, size_t *length) {
struct word_t2str_hashtable_entry **e = word_t2str_hashtable_slot(&gid_hashtable, gid);
if(!(*e)) {
- static __thread char buf[1024 + 1];
- const char *name = gid_to_groupname(gid, buf, 1024);
+ static __thread char buf[1024];
+ const char *name = gid_to_groupname(gid, buf, sizeof(buf));
size_t size = strlen(name) + 1;
*e = callocz(1, sizeof(struct word_t2str_hashtable_entry) + size);