Systemd-Journal: fix crash when the uid or gid do not have names (#16015)
Costa Tsaousis committed
Sep 19, 2023 at 22:12 UTC
541facfa3e5021ebc5c7fb8944987f3a3a02d48a
1 file changed
+16
-14
collectors/systemd-journal.plugin/systemd-journal.c
+16
-14
@@ -511,23 +511,25 @@ static FACET_ROW_SEVERITY syslog_priority_to_facet_severity(int priority) {
511
512
static char *uid_to_username(uid_t uid, char *buffer, size_t buffer_size) {
513
static __thread char tmp[1024 + 1];
514
- struct passwd pw, *result;
514
+ struct passwd pw, *result = NULL;
515
516
- if (getpwuid_r(uid, &pw, tmp, 1024, &result) != 0 || result == NULL)
517
- return NULL;
516
+ if (getpwuid_r(uid, &pw, tmp, sizeof(tmp), &result) != 0 || !result || !pw.pw_name || !(*pw.pw_name))
517
+ snprintfz(buffer, buffer_size - 1, "%u", uid);
518
+ else
519
+ snprintfz(buffer, buffer_size - 1, "%u (%s)", uid, pw.pw_name);
520
519
- snprintfz(buffer, buffer_size - 1, "%u (%s)", uid, pw.pw_name);
521
return buffer;
522
}
523
524
static char *gid_to_groupname(gid_t gid, char* buffer, size_t buffer_size) {
524
- static __thread char tmp[1024 + 1];
525
- struct group grp, *result;
525
+ static __thread char tmp[1024];
526
+ struct group grp, *result = NULL;
527
527
- if (getgrgid_r(gid, &grp, tmp, 1024, &result) != 0 || result == NULL)
528
- return NULL;
528
+ if (getgrgid_r(gid, &grp, tmp, sizeof(tmp), &result) != 0 || !result || !grp.gr_name || !(*grp.gr_name))
529
+ snprintfz(buffer, buffer_size - 1, "%u", gid);
530
+ else
531
+ snprintfz(buffer, buffer_size - 1, "%u (%s)", gid, grp.gr_name);
532
530
- snprintfz(buffer, buffer_size - 1, "%u (%s)", gid, grp.gr_name);
533
return buffer;
534
}
535
@@ -560,7 +562,7 @@ static void netdata_systemd_journal_transform_priority(FACETS *facets __maybe_un
562
// ----------------------------------------------------------------------------
563
// UID and GID transformation
564
563
-#define UID_GID_HASHTABLE_SIZE 1000
565
+#define UID_GID_HASHTABLE_SIZE 10000
566
567
struct word_t2str_hashtable_entry {
568
struct word_t2str_hashtable_entry *next;
@@ -598,8 +600,8 @@ const char *uid_to_username_cached(uid_t uid, size_t *length) {
600
601
struct word_t2str_hashtable_entry **e = word_t2str_hashtable_slot(&uid_hashtable, uid);
602
if(!(*e)) {
601
- static __thread char buf[1024 + 1];
602
- const char *name = uid_to_username(uid, buf, 1024);
603
+ static __thread char buf[1024];
604
+ const char *name = uid_to_username(uid, buf, sizeof(buf));
605
size_t size = strlen(name) + 1;
606
607
*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) {
621
622
struct word_t2str_hashtable_entry **e = word_t2str_hashtable_slot(&gid_hashtable, gid);
623
if(!(*e)) {
622
- static __thread char buf[1024 + 1];
623
- const char *name = gid_to_groupname(gid, buf, 1024);
624
+ static __thread char buf[1024];
625
+ const char *name = gid_to_groupname(gid, buf, sizeof(buf));
626
size_t size = strlen(name) + 1;
627
628
*e = callocz(1, sizeof(struct word_t2str_hashtable_entry) + size);