]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/utmp.c: Fix use of last utmp entry instead of patrial-match entry master
authorEvgeny Grin (Karlson2k) <k2k@drgrin.dev>
Sun, 10 Aug 2025 12:08:18 +0000 (14:08 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Sun, 10 Aug 2025 13:58:21 +0000 (15:58 +0200)
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) <k2k@drgrin.dev>
lib/utmp.c

index 7159e0a3bc536052c7ec8d9a5186b996b3efdf4d..90ac9d18c962204a6064e42f45e1c30a2962f6b2 100644 (file)
@@ -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;