From 10a0a9cbc567cd4d70e43b5d15f3764b7d21dbc2 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 9 Jul 2020 10:10:58 +0200 Subject: [PATCH] last: fix use of non-terminated utmp->ut_line Addresses: https://github.com/karelzak/util-linux/pull/1097 Signed-off-by: Karel Zak --- login-utils/last.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/login-utils/last.c b/login-utils/last.c index 77c5e1b47f..9d71ba4435 100644 --- a/login-utils/last.c +++ b/login-utils/last.c @@ -612,7 +612,7 @@ static int is_phantom(const struct last_control *ctl, struct utmpx *ut) pw = getpwnam(ut->ut_user); if (!pw) return 1; - sprintf(path, "/proc/%u/loginuid", ut->ut_pid); + snprintf(path, sizeof(path), "/proc/%u/loginuid", ut->ut_pid); if (access(path, R_OK) == 0) { unsigned int loginuid; FILE *f = NULL; @@ -626,8 +626,11 @@ static int is_phantom(const struct last_control *ctl, struct utmpx *ut) return 1; } else { struct stat st; + char utline[sizeof(ut->ut_line) + 1]; - sprintf(path, "/dev/%s", ut->ut_line); + mem2strcpy(utline, ut->ut_line, sizeof(ut->ut_line), sizeof(utline)); + + snprintf(path, sizeof(path), "/dev/%s", utline); if (stat(path, &st)) return 1; if (pw->pw_uid != st.st_uid) -- 2.47.2