Blame SOURCES/quota-4.04-warnquota-Check-snprintf-for-overflows.patch

ecf77e
From eeef53917864600e0f5ac42ce5c3d884967012a1 Mon Sep 17 00:00:00 2001
ecf77e
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
ecf77e
Date: Mon, 5 Feb 2018 10:31:47 +0100
ecf77e
Subject: [PATCH 1/2] warnquota: Check snprintf() for overflows
ecf77e
MIME-Version: 1.0
ecf77e
Content-Type: text/plain; charset=UTF-8
ecf77e
Content-Transfer-Encoding: 8bit
ecf77e
ecf77e
GCC 8 with GNU libc 2.27 prerelease warns:
ecf77e
ecf77e
gcc -DHAVE_CONFIG_H -I.     -g -O2 -Wall -fPIC -I/usr/include/tirpc  -c -o warnquota.o warnquota.c
ecf77e
warnquota.c: In function ‘lookup_user’:
ecf77e
warnquota.c:415:29: warning: ‘%s’ directive output may be truncated writing up to 2047 bytes into a region of size 255 [-Wformat-truncation=]
ecf77e
  snprintf(searchbuf, 256, "(%s=%s)", config->ldap_search_attr, user);
ecf77e
                             ^~
ecf77e
warnquota.c:415:2: note: ‘snprintf’ output 4 or more bytes (assuming 2051) into a destination of size 256
ecf77e
  snprintf(searchbuf, 256, "(%s=%s)", config->ldap_search_attr, user);
ecf77e
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ecf77e
warnquota.c: In function ‘warn_quota’:
ecf77e
warnquota.c:896:51: warning: ‘%s’ directive output may be truncated writing up to 2047 bytes into a region of size 2041 [-Wformat-truncation=]
ecf77e
    snprintf(config->ldap_uri, CNF_BUFFER, "ldap://%s:%d", config->ldap_host, config->ldap_port);
ecf77e
                                                   ^~      ~~~~~~~~~~~~~~~~~
ecf77e
warnquota.c:896:4: note: ‘snprintf’ output between 10 and 2067 bytes into a destination of size 2048
ecf77e
    snprintf(config->ldap_uri, CNF_BUFFER, "ldap://%s:%d", config->ldap_host, config->ldap_port);
ecf77e
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ecf77e
ecf77e
This is patch fixes it by catching the cases when snprintf() truncates and
ecf77e
reporting an error.
ecf77e
ecf77e
Perfect fix would fall back into dynamically allocated buffers but
ecf77e
I think that would make these corner case too complicated provided
ecf77e
nobody had yet complained about them.
ecf77e
ecf77e
Signed-off-by: Petr Písař <ppisar@redhat.com>
ecf77e
---
ecf77e
 warnquota.c | 17 +++++++++++++++--
ecf77e
 1 file changed, 15 insertions(+), 2 deletions(-)
ecf77e
ecf77e
diff --git a/warnquota.c b/warnquota.c
ecf77e
index 073c45e..bc11055 100644
ecf77e
--- a/warnquota.c
ecf77e
+++ b/warnquota.c
ecf77e
@@ -412,7 +412,13 @@ static char *lookup_user(struct configparams *config, char *user)
ecf77e
 	}
ecf77e
 
ecf77e
 	/* search for the offender_name in ldap */
ecf77e
-	snprintf(searchbuf, 256, "(%s=%s)", config->ldap_search_attr, user);
ecf77e
+	if (256 <= snprintf(searchbuf, 256, "(%s=%s)", config->ldap_search_attr,
ecf77e
+		    user)) {
ecf77e
+		errstr(_("Could not format LDAP search filter for %s user and "
ecf77e
+			"%s search attribute due to excessive length.\n"),
ecf77e
+			user, config->ldap_search_attr);
ecf77e
+		return NULL;
ecf77e
+	}
ecf77e
 	ret = ldap_search_ext_s(ldapconn,
ecf77e
 		config->ldap_basedn, LDAP_SCOPE_SUBTREE,
ecf77e
 		searchbuf, NULL, 0, NULL, NULL, NULL,
ecf77e
@@ -893,7 +899,14 @@ cc_parse_err:
ecf77e
 	if (config->use_ldap_mail)
ecf77e
 	{
ecf77e
 		if (!config->ldap_uri[0]) {
ecf77e
-			snprintf(config->ldap_uri, CNF_BUFFER, "ldap://%s:%d", config->ldap_host, config->ldap_port);
ecf77e
+			if (CNF_BUFFER <= snprintf(config->ldap_uri, CNF_BUFFER,
ecf77e
+				    "ldap://%s:%d", config->ldap_host,
ecf77e
+				    config->ldap_port)) {
ecf77e
+				errstr(_("Could not format LDAP URI because "
ecf77e
+					    "it's longer than %d bytes.\n"),
ecf77e
+					    CNF_BUFFER);
ecf77e
+				return -1;
ecf77e
+			}
ecf77e
 			errstr(_("LDAP library version >= 2.3 detected. Please use LDAP_URI instead of hostname and port.\nGenerated URI %s\n"), config->ldap_uri);
ecf77e
 		}
ecf77e
 	}
ecf77e
-- 
ecf77e
2.13.6
ecf77e