182a0dd
From 59b280ebe22eceaf4250cb3b776674619a4d4ece Mon Sep 17 00:00:00 2001
046a421
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
046a421
Date: Mon, 5 Feb 2018 11:07:41 +0100
182a0dd
Subject: [PATCH] warnquota: Fix comparing user name to non-null-terminated
046a421
 utmp.ut_user
046a421
MIME-Version: 1.0
046a421
Content-Type: text/plain; charset=UTF-8
046a421
Content-Transfer-Encoding: 8bit
046a421
046a421
GCC 8 with GNU libc 2.27 warns:
046a421
046a421
gcc -DHAVE_CONFIG_H -I.    -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include  -I/usr/include/libnl3  -g -O2 -Wall -fPIC -I/usr/include/tirpc  -c -o quota_nld-quota_nld.o `test -f 'quota_nld.c' || echo './'`quota_nld.c
046a421
quota_nld.c: In function ‘write_console_warning’:
046a421
quota_nld.c:273:7: warning: ‘strcmp’ argument 2 declared attribute ‘nonstring’ [-Wstringop-overflow=]
046a421
   if (strcmp(user, uent->ut_user))
046a421
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~
046a421
In file included from /usr/include/utmp.h:29,
046a421
                 from quota_nld.c:23:
046a421
/usr/include/bits/utmp.h:65:8: note: argument ‘ut_user’ declared here
046a421
   char ut_user[UT_NAMESIZE]
046a421
        ^~~~~~~
046a421
046a421
This is because ut_user value misses the terminating null byte if it
046a421
fits exactly into ut_user array, as document in utmp(5):
046a421
046a421
    String fields are terminated by a null byte ('\0') if they are
046a421
    shorter than  the size of the field.
046a421
046a421
Recent GCC and glibc recevied compile-time checks and annotations
046a421
(__attribute_nonstring_) that catches these mistakes.
046a421
046a421
This patch fixes it by using strncmp(3) and by ignoring user names
046a421
that does not fit into utmp log format. It's better not to warn than
046a421
spamming unrelated user.
046a421
046a421
Signed-off-by: Petr Písař <ppisar@redhat.com>
182a0dd
Signed-off-by: Jan Kara <jack@suse.cz>
046a421
---
182a0dd
 quota_nld.c | 5 ++++-
182a0dd
 1 file changed, 4 insertions(+), 1 deletion(-)
046a421
046a421
diff --git a/quota_nld.c b/quota_nld.c
182a0dd
index ea541e0..8559f25 100644
046a421
--- a/quota_nld.c
046a421
+++ b/quota_nld.c
182a0dd
@@ -262,6 +262,8 @@ static void write_console_warning(struct quota_warning *warn)
182a0dd
 	    warn->warntype == QUOTA_NL_BSOFTBELOW) && !(flags & FL_PRINTBELOW))
182a0dd
 		return;
182a0dd
 	uid2user(warn->caused_id, user);
182a0dd
+	if (strlen(user) > UT_NAMESIZE)
182a0dd
+		goto skip_utmp;
182a0dd
 	strcpy(dev, "/dev/");
182a0dd
 
182a0dd
 	setutent();
182a0dd
@@ -270,7 +272,7 @@ static void write_console_warning(struct quota_warning *warn)
046a421
 		if (uent->ut_type != USER_PROCESS)
046a421
 			continue;
046a421
 		/* Entry for a different user? */
046a421
-		if (strcmp(user, uent->ut_user))
182a0dd
+		if (strncmp(user, uent->ut_user, UT_NAMESIZE))
046a421
 			continue;
046a421
 		sstrncpy(dev+5, uent->ut_line, PATH_MAX-5);
046a421
 		if (stat(dev, &st) < 0)
182a0dd
@@ -281,6 +283,7 @@ static void write_console_warning(struct quota_warning *warn)
182a0dd
 		}
182a0dd
 	}
182a0dd
 	if (!max_atime) {
182a0dd
+skip_utmp:
182a0dd
 		/*
182a0dd
 		 * This can happen quite easily so don't spam syslog with
182a0dd
 		 * the error
046a421
-- 
046a421
2.13.6
046a421