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

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