From: Evgeny Grin (Karlson2k) Date: Sun, 10 Aug 2025 12:08:18 +0000 (+0200) Subject: lib/utmp.c: Fix use of last utmp entry instead of patrial-match entry X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=5958f656cc82cefc02f2d78ebd68f4baeb329bf8;p=thirdparty%2Fshadow.git 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) --- 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;