Blame SOURCES/0091-last-sync-utmp-strings-use-with-upstream-code.patch

f46785
From 25f7136d326753cb0bb7612a98db6542c9fdc3bd Mon Sep 17 00:00:00 2001
f46785
From: Karel Zak <kzak@redhat.com>
f46785
Date: Thu, 2 Feb 2023 15:22:52 +0100
f46785
Subject: last: sync utmp strings use with upstream code
f46785
f46785
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2160321
f46785
Signed-off-by: Karel Zak <kzak@redhat.com>
f46785
---
f46785
 include/strutils.h  | 30 +++++++++++++++++++
f46785
 include/timeutils.h |  1 +
f46785
 login-utils/last.c  | 73 +++++++++++++++++++++++----------------------
f46785
 3 files changed, 69 insertions(+), 35 deletions(-)
f46785
f46785
diff --git a/include/strutils.h b/include/strutils.h
f46785
index 5d07fcc7c..193f1acbc 100644
f46785
--- a/include/strutils.h
f46785
+++ b/include/strutils.h
f46785
@@ -65,6 +65,36 @@ static inline void xstrncpy(char *dest, const char *src, size_t n)
f46785
 	dest[n-1] = 0;
f46785
 }
f46785
 
f46785
+/* This is like strncpy(), but based on memcpy(), so compilers and static
f46785
+ * analyzers do not complain when sizeof(destination) is the same as 'n' and
f46785
+ * result is not terminated by zero.
f46785
+ *
f46785
+ * Use this function to copy string to logs with fixed sizes (wtmp/utmp. ...)
f46785
+ * where string terminator is optional.
f46785
+ */
f46785
+static inline void * __attribute__((nonnull (1)))
f46785
+str2memcpy(void *dest, const char *src, size_t n)
f46785
+{
f46785
+	size_t bytes = strlen(src) + 1;
f46785
+
f46785
+	if (bytes > n)
f46785
+		bytes = n;
f46785
+
f46785
+	memcpy(dest, src, bytes);
f46785
+	return dest;
f46785
+}
f46785
+
f46785
+static inline char * __attribute__((nonnull (1)))
f46785
+mem2strcpy(char *dest, const void *src, size_t n, size_t nmax)
f46785
+{
f46785
+	if (n + 1 > nmax)
f46785
+		n = nmax - 1;
f46785
+
f46785
+	memset(dest, '\0', nmax);
f46785
+	memcpy(dest, src, n);
f46785
+	return dest;
f46785
+}
f46785
+
f46785
 static inline int strdup_to_offset(void *stru, size_t offset, const char *str)
f46785
 {
f46785
 	char *n = NULL;
f46785
diff --git a/include/timeutils.h b/include/timeutils.h
f46785
index 230e6db5f..f1540a183 100644
f46785
--- a/include/timeutils.h
f46785
+++ b/include/timeutils.h
f46785
@@ -74,6 +74,7 @@ enum {
f46785
 	ISO_TIMESTAMP_COMMA_GT  = ISO_TIMESTAMP_COMMA_G | ISO_T
f46785
 };
f46785
 
f46785
+#define CTIME_BUFSIZ    26
f46785
 #define ISO_BUFSIZ	42
f46785
 
f46785
 int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz);
f46785
diff --git a/login-utils/last.c b/login-utils/last.c
f46785
index 80d77d20b..8f7c36984 100644
f46785
--- a/login-utils/last.c
f46785
+++ b/login-utils/last.c
f46785
@@ -339,15 +339,22 @@ static int time_formatter(int fmt, char *dst, size_t dlen, time_t *when)
f46785
 		break;
f46785
 	case LAST_TIMEFTM_HHMM:
f46785
 	{
f46785
-		struct tm *tm = localtime(when);
f46785
-		if (!snprintf(dst, dlen, "%02d:%02d", tm->tm_hour, tm->tm_min))
f46785
+		struct tm tm;
f46785
+
f46785
+		localtime_r(when, &tm;;
f46785
+		if (!snprintf(dst, dlen, "%02d:%02d", tm.tm_hour, tm.tm_min))
f46785
 			ret = -1;
f46785
 		break;
f46785
 	}
f46785
 	case LAST_TIMEFTM_CTIME:
f46785
-		snprintf(dst, dlen, "%s", ctime(when));
f46785
+	{
f46785
+		char buf[CTIME_BUFSIZ];
f46785
+
f46785
+		ctime_r(when, buf);
f46785
+		snprintf(dst, dlen, "%s", buf);
f46785
 		ret = rtrim_whitespace((unsigned char *) dst);
f46785
 		break;
f46785
+	}
f46785
 	case LAST_TIMEFTM_ISO8601:
f46785
 		ret = strtime_iso(when, ISO_TIMESTAMP_T, dst, dlen);
f46785
 		break;
f46785
@@ -394,8 +401,7 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
f46785
 	/*
f46785
 	 *	uucp and ftp have special-type entries
f46785
 	 */
f46785
-	utline[0] = 0;
f46785
-	strncat(utline, p->ut_line, sizeof(p->ut_line));
f46785
+	mem2strcpy(utline, p->ut_line, sizeof(p->ut_line), sizeof(utline));
f46785
 	if (strncmp(utline, "ftp", 3) == 0 && isdigit(utline[3]))
f46785
 		utline[3] = 0;
f46785
 	if (strncmp(utline, "uucp", 4) == 0 && isdigit(utline[4]))
f46785
@@ -447,48 +453,48 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
f46785
 
f46785
 	if (logout_time == currentdate) {
f46785
 		if (ctl->time_fmt > LAST_TIMEFTM_SHORT) {
f46785
-			sprintf(logouttime, "  still running");
f46785
+			snprintf(logouttime, sizeof(logouttime), "  still running");
f46785
 			length[0] = 0;
f46785
 		} else {
f46785
-			sprintf(logouttime, "  still");
f46785
-			sprintf(length, "running");
f46785
+			snprintf(logouttime, sizeof(logouttime), "  still");
f46785
+			snprintf(length, sizeof(length), "running");
f46785
 		}
f46785
 	} else if (days) {
f46785
-		sprintf(length, "(%d+%02d:%02d)", days, abs(hours), abs(mins)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
+		snprintf(length, sizeof(length), "(%d+%02d:%02d)", days, abs(hours), abs(mins)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
 	} else if (hours) {
f46785
-		sprintf(length, " (%02d:%02d)", hours, abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
+		snprintf(length, sizeof(length), " (%02d:%02d)", hours, abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
 	} else if (secs >= 0) {
f46785
-		sprintf(length, " (%02d:%02d)", hours, mins); 
f46785
+		snprintf(length, sizeof(length), " (%02d:%02d)", hours, mins);
f46785
 	} else {
f46785
-		sprintf(length, " (-00:%02d)", abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
+		snprintf(length, sizeof(length), " (-00:%02d)", abs(mins));  /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
f46785
 	}
f46785
 
f46785
 	switch(what) {
f46785
 		case R_CRASH:
f46785
-			sprintf(logouttime, "- crash");
f46785
+			snprintf(logouttime, sizeof(logouttime), "- crash");
f46785
 			break;
f46785
 		case R_DOWN:
f46785
-			sprintf(logouttime, "- down ");
f46785
+			snprintf(logouttime, sizeof(logouttime), "- down ");
f46785
 			break;
f46785
 		case R_NOW:
f46785
 			if (ctl->time_fmt > LAST_TIMEFTM_SHORT) {
f46785
-				sprintf(logouttime, "  still logged in");
f46785
+				snprintf(logouttime, sizeof(logouttime), "  still logged in");
f46785
 				length[0] = 0;
f46785
 			} else {
f46785
-				sprintf(logouttime, "  still");
f46785
-				sprintf(length, "logged in");
f46785
+				snprintf(logouttime, sizeof(logouttime), "  still");
f46785
+				snprintf(length, sizeof(length), "logged in");
f46785
 			}
f46785
 			break;
f46785
 		case R_PHANTOM:
f46785
 			if (ctl->time_fmt > LAST_TIMEFTM_SHORT) {
f46785
-				sprintf(logouttime, "  gone - no logout");
f46785
+				snprintf(logouttime, sizeof(logouttime), "  gone - no logout");
f46785
 				length[0] = 0;
f46785
 			} else if (ctl->time_fmt == LAST_TIMEFTM_SHORT) {
f46785
-				sprintf(logouttime, "   gone");
f46785
-				sprintf(length, "- no logout");
f46785
+				snprintf(logouttime, sizeof(logouttime), "   gone");
f46785
+				snprintf(length, sizeof(length), "- no logout");
f46785
 			} else {
f46785
 				logouttime[0] = 0;
f46785
-				sprintf(length, "no logout");
f46785
+				snprintf(length, sizeof(length), "no logout");
f46785
 			}
f46785
 			break;
f46785
 		case R_TIMECHANGE:
f46785
@@ -508,15 +514,8 @@ static int list(const struct last_control *ctl, struct utmpx *p, time_t logout_t
f46785
 	r = -1;
f46785
 	if (ctl->usedns || ctl->useip)
f46785
 		r = dns_lookup(domain, sizeof(domain), ctl->useip, (int32_t*)p->ut_addr_v6);
f46785
-	if (r < 0) {
f46785
-		size_t sz = sizeof(p->ut_host);
f46785
-
f46785
-		if (sz > sizeof(domain))
f46785
-			sz = sizeof(domain);
f46785
-
f46785
-		xstrncpy(domain, p->ut_host, sz);
f46785
-	}
f46785
-
f46785
+	if (r < 0)
f46785
+		mem2strcpy(domain, p->ut_host, sizeof(p->ut_host), sizeof(domain));
f46785
 
f46785
 	if (ctl->showhost) {
f46785
 		if (!ctl->altlist) {
f46785
@@ -607,10 +606,11 @@ static int is_phantom(const struct last_control *ctl, struct utmpx *ut)
f46785
 
f46785
 	if (ut->ut_tv.tv_sec < ctl->boot_time.tv_sec)
f46785
 		return 1;
f46785
+	ut->ut_user[sizeof(ut->ut_user) - 1] = '\0';
f46785
 	pw = getpwnam(ut->ut_user);
f46785
 	if (!pw)
f46785
 		return 1;
f46785
-	sprintf(path, "/proc/%u/loginuid", ut->ut_pid);
f46785
+	snprintf(path, sizeof(path), "/proc/%u/loginuid", ut->ut_pid);
f46785
 	if (access(path, R_OK) == 0) {
f46785
 		unsigned int loginuid;
f46785
 		FILE *f = NULL;
f46785
@@ -624,8 +624,11 @@ static int is_phantom(const struct last_control *ctl, struct utmpx *ut)
f46785
 			return 1;
f46785
 	} else {
f46785
 		struct stat st;
f46785
+		char utline[sizeof(ut->ut_line) + 1];
f46785
+
f46785
+		mem2strcpy(utline, ut->ut_line, sizeof(ut->ut_line), sizeof(utline));
f46785
 
f46785
-		sprintf(path, "/dev/%s", ut->ut_line);
f46785
+		snprintf(path, sizeof(path), "/dev/%s", utline);
f46785
 		if (stat(path, &st))
f46785
 			return 1;
f46785
 		if (pw->pw_uid != st.st_uid)
f46785
@@ -736,7 +739,7 @@ static void process_wtmp_file(const struct last_control *ctl,
f46785
 		else {
f46785
 			if (ut.ut_type != DEAD_PROCESS &&
f46785
 			    ut.ut_user[0] && ut.ut_line[0] &&
f46785
-			    strcmp(ut.ut_user, "LOGIN") != 0)
f46785
+			    strncmp(ut.ut_user, "LOGIN", 5) != 0)
f46785
 				ut.ut_type = USER_PROCESS;
f46785
 			/*
f46785
 			 * Even worse, applications that write ghost
f46785
@@ -749,7 +752,7 @@ static void process_wtmp_file(const struct last_control *ctl,
f46785
 			/*
f46785
 			 * Clock changes.
f46785
 			 */
f46785
-			if (strcmp(ut.ut_user, "date") == 0) {
f46785
+			if (strncmp(ut.ut_user, "date", 4) == 0) {
f46785
 				if (ut.ut_line[0] == '|')
f46785
 					ut.ut_type = OLD_TIME;
f46785
 				if (ut.ut_line[0] == '{')
f46785
@@ -784,7 +787,7 @@ static void process_wtmp_file(const struct last_control *ctl,
f46785
 		case RUN_LVL:
f46785
 			x = ut.ut_pid & 255;
f46785
 			if (ctl->extended) {
f46785
-				sprintf(ut.ut_line, "(to lvl %c)", x);
f46785
+				snprintf(ut.ut_line, sizeof(ut.ut_line), "(to lvl %c)", x);
f46785
 				quit = list(ctl, &ut, lastrch, R_NORMAL);
f46785
 			}
f46785
 			if (x == '0' || x == '6') {
f46785
-- 
f46785
2.39.1
f46785