Blame SOURCES/0010-generate_hash-generate_pw_hash-don-t-use-strlen-for-.patch

670361
From 57f7c776dca0322fab107460cac71ac4b6e79b9a Mon Sep 17 00:00:00 2001
670361
From: Peter Jones <pjones@redhat.com>
670361
Date: Tue, 15 May 2018 11:20:15 -0400
670361
Subject: [PATCH 10/10] generate_hash() / generate_pw_hash(): don't use
670361
 strlen() for strncpy bounds
670361
670361
New gcc rightly comlplains when we do the following:
670361
670361
strncpy (dest, src, strlen(src));
670361
670361
For two reasons:
670361
a) it doesn't copy the NUL byte
670361
b) it's otherwise the same thing strcpy() would have done
670361
670361
This patch replaces that with stpncpy (just because it's slightly easier
670361
to use) and the real bounds for the destination.
670361
670361
Signed-off-by: Peter Jones <pjones@redhat.com>
670361
---
670361
 src/mokutil.c | 33 ++++++++++++++++++++++-----------
670361
 1 file changed, 22 insertions(+), 11 deletions(-)
670361
670361
diff --git a/src/mokutil.c b/src/mokutil.c
670361
index 0be9e8491fd..b5080107600 100644
670361
--- a/src/mokutil.c
670361
+++ b/src/mokutil.c
670361
@@ -764,9 +764,10 @@ generate_hash (pw_crypt_t *pw_crypt, char *password, unsigned int pw_len)
670361
 {
670361
 	pw_crypt_t new_crypt;
670361
 	char settings[SETTINGS_LEN];
670361
+	char *next;
670361
 	char *crypt_string;
670361
 	const char *prefix;
670361
-	int hash_len, prefix_len;
670361
+	int hash_len, settings_len = sizeof (settings) - 2;
670361
 
670361
 	if (!password || !pw_crypt || password[pw_len] != '\0')
670361
 		return -1;
670361
@@ -774,15 +775,19 @@ generate_hash (pw_crypt_t *pw_crypt, char *password, unsigned int pw_len)
670361
 	prefix = get_crypt_prefix (pw_crypt->method);
670361
 	if (!prefix)
670361
 		return -1;
670361
-	prefix_len = strlen(prefix);
670361
 
670361
 	pw_crypt->salt_size = get_salt_size (pw_crypt->method);
670361
 	generate_salt ((char *)pw_crypt->salt, pw_crypt->salt_size);
670361
 
670361
-	strncpy (settings, prefix, prefix_len);
670361
-	strncpy (settings + prefix_len, (const char *)pw_crypt->salt,
670361
-		 pw_crypt->salt_size);
670361
-	settings[pw_crypt->salt_size + prefix_len] = '\0';
670361
+	memset (settings, 0, sizeof (settings));
670361
+	next = stpncpy (settings, prefix, settings_len);
670361
+	if (pw_crypt->salt_size > settings_len - (next - settings)) {
670361
+		errno = EOVERFLOW;
670361
+		return -1;
670361
+	}
670361
+	next = stpncpy (next, (const char *)pw_crypt->salt,
670361
+			pw_crypt->salt_size);
670361
+	*next = '\0';
670361
 
670361
 	crypt_string = crypt (password, settings);
670361
 	if (!crypt_string)
670361
@@ -1929,10 +1934,11 @@ static int
670361
 generate_pw_hash (const char *input_pw)
670361
 {
670361
 	char settings[SETTINGS_LEN];
670361
+        char *next;
670361
 	char *password = NULL;
670361
 	char *crypt_string;
670361
 	const char *prefix;
670361
-	int prefix_len;
670361
+	int settings_len = sizeof (settings) - 2;
670361
 	unsigned int pw_len, salt_size;
670361
 
670361
 	if (input_pw) {
670361
@@ -1958,12 +1964,17 @@ generate_pw_hash (const char *input_pw)
670361
 	prefix = get_crypt_prefix (DEFAULT_CRYPT_METHOD);
670361
 	if (!prefix)
670361
 		return -1;
670361
-	prefix_len = strlen(prefix);
670361
 
670361
-	strncpy (settings, prefix, prefix_len);
670361
+	memset (settings, 0, sizeof (settings));
670361
+	next = stpncpy (settings, prefix, settings_len);
670361
 	salt_size = get_salt_size (DEFAULT_CRYPT_METHOD);
670361
-	generate_salt ((settings + prefix_len), salt_size);
670361
-	settings[DEFAULT_SALT_SIZE + prefix_len] = '\0';
670361
+	if (salt_size > settings_len - (next - settings)) {
670361
+		errno = EOVERFLOW;
670361
+		return -1;
670361
+	}
670361
+	generate_salt (next, salt_size);
670361
+	next += salt_size;
670361
+	*next = '\0';
670361
 
670361
 	crypt_string = crypt (password, settings);
670361
 	free (password);
670361
-- 
670361
2.17.1
670361