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

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