644833
From f435f80b9ae88caf9fe8af2e9b705dc8296ad6f3 Mon Sep 17 00:00:00 2001
644833
From: Karel Zak <kzak@redhat.com>
644833
Date: Mon, 13 Feb 2023 16:22:23 +0100
644833
Subject: last: use full size of the username
644833
644833
utmp uses 32 bytes for username, last(1) truncates it to 31 when calls getpwnam().
644833
644833
Reported-by: Radka Skvarilova <rskvaril@redhat.com>
644833
Signed-off-by: Karel Zak <kzak@redhat.com>
644833
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2160321
644833
Upstream: http://github.com/util-linux/util-linux/commit/4b646f01600a5efcf16e8e8991010b49b250bdfe
644833
---
644833
 login-utils/last.1 | 4 ++++
644833
 login-utils/last.c | 6 ++++--
644833
 2 files changed, 8 insertions(+), 2 deletions(-)
644833
644833
diff --git a/login-utils/last.1 b/login-utils/last.1
644833
index 94b4ed012..9169abd35 100644
644833
--- a/login-utils/last.1
644833
+++ b/login-utils/last.1
644833
@@ -178,6 +178,10 @@ files to be used, they can be created with a simple
644833
 .BR touch (1)
644833
 command (for example,
644833
 .IR "touch /var/log/wtmp" ).
644833
+
644833
+The utmp file format uses fixed sizes of strings, which means that very long strings are
644833
+impossible to store in the file and impossible to display by last. The usual limits are 32
644833
+bytes for a user and line name and 256 bytes for a hostname.
644833
 .SH FILES
644833
 /var/log/wtmp
644833
 .br
644833
diff --git a/login-utils/last.c b/login-utils/last.c
644833
index 8f7c36984..41ce03894 100644
644833
--- a/login-utils/last.c
644833
+++ b/login-utils/last.c
644833
@@ -602,12 +602,14 @@ static int is_phantom(const struct last_control *ctl, struct utmpx *ut)
644833
 {
644833
 	struct passwd *pw;
644833
 	char path[sizeof(ut->ut_line) + 16];
644833
+	char user[sizeof(ut->ut_user) + 1];
644833
 	int ret = 0;
644833
 
644833
 	if (ut->ut_tv.tv_sec < ctl->boot_time.tv_sec)
644833
 		return 1;
644833
-	ut->ut_user[sizeof(ut->ut_user) - 1] = '\0';
644833
-	pw = getpwnam(ut->ut_user);
644833
+
644833
+	mem2strcpy(user, ut->ut_user, sizeof(ut->ut_user), sizeof(user));
644833
+	pw = getpwnam(user);
644833
 	if (!pw)
644833
 		return 1;
644833
 	snprintf(path, sizeof(path), "/proc/%u/loginuid", ut->ut_pid);
644833
-- 
644833
2.39.1
644833