From 5958f656cc82cefc02f2d78ebd68f4baeb329bf8 Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Sun, 10 Aug 2025 14:08:18 +0200 Subject: [PATCH] lib/utmp.c: Fix use of last utmp entry instead of patrial-match entry The pointer returned by getutxent() function may always point to the same shared and reused buffer. Instead of copying the utmp entry pointer value the content of utmp entry must be copied otherwise the next call of getutxent() will overwrite previously found entry. This commit has no optimisations to highlight what is really fixed. Fixes: 841776561f56bae7382c6bd47e428201a155d39c (09-08-2025; "lib/utmp.c: Fix umtp entry search") Signed-off-by: Evgeny Grin (Karlson2k) --- lib/utmp.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/utmp.c b/lib/utmp.c index 7159e0a3b..90ac9d18c 100644 --- a/lib/utmp.c +++ b/lib/utmp.c @@ -165,13 +165,16 @@ get_current_utmp(pid_t main_pid) if (is_my_tty(ut->ut_line)) break; /* Perfect match, stop the search */ - if (NULL == ut_by_pid) - ut_by_pid = ut; + if (NULL == ut_by_pid) { + ut_by_pid = XMALLOC(1, struct utmpx); + *ut_by_pid = *ut; + } } else if ( (NULL == ut_by_line) && (LOGIN_PROCESS == ut->ut_type) /* Be more picky when matching by 'ut_line' only */ && (is_my_tty(ut->ut_line))) { - ut_by_line = ut; + ut_by_line = XMALLOC(1, struct utmpx); + *ut_by_line = *ut; } } @@ -186,6 +189,8 @@ get_current_utmp(pid_t main_pid) ut = ut_copy; } + free(ut_by_line); + free(ut_by_pid); endutxent(); return ut; -- 2.47.3