00db7f4
From 76594cba9a1e910bb36160d96fc3872349341799 Mon Sep 17 00:00:00 2001
aeea22a
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org>
aeea22a
Date: Wed, 25 Apr 2018 14:04:31 +0200
aeea22a
Subject: [PATCH] Replace isc_safe routines with their OpenSSL counter parts
aeea22a
aeea22a
(cherry picked from commit 66ba2fdad583d962a1f4971c85d58381f0849e4d)
aeea22a
aeea22a
Remove isc_safe_memcompare, it's not needed anywhere and can't be replaced with CRYPTO_memcmp()
aeea22a
aeea22a
(cherry picked from commit b105ccee68ccc3c18e6ea530063b3c8e5a42571c)
aeea22a
aeea22a
Fix the isc_safe_memwipe() usage with (NULL, >0)
aeea22a
aeea22a
(cherry picked from commit 083461d3329ff6f2410745848a926090586a9846)
aeea22a
---
aeea22a
 bin/dnssec/dnssec-signzone.c |  2 +-
1e41691
 lib/dns/nsec3.c              |  4 +-
1e41691
 lib/dns/spnego.c             |  4 +-
1e41691
 lib/isc/Makefile.in          |  8 +---
1e41691
 lib/isc/include/isc/safe.h   | 18 ++------
1e41691
 lib/isc/safe.c               | 83 ------------------------------------
1e41691
 lib/isc/tests/safe_test.c    | 18 --------
1e41691
 7 files changed, 11 insertions(+), 126 deletions(-)
aeea22a
 delete mode 100644 lib/isc/safe.c
aeea22a
aeea22a
diff --git a/bin/dnssec/dnssec-signzone.c b/bin/dnssec/dnssec-signzone.c
ad7b3b8
index 6ddaebe..d921870 100644
aeea22a
--- a/bin/dnssec/dnssec-signzone.c
aeea22a
+++ b/bin/dnssec/dnssec-signzone.c
ad7b3b8
@@ -787,7 +787,7 @@ hashlist_add_dns_name(hashlist_t *l, /*const*/ dns_name_t *name,
aeea22a
 
aeea22a
 static int
aeea22a
 hashlist_comp(const void *a, const void *b) {
aeea22a
-	return (isc_safe_memcompare(a, b, hash_length + 1));
aeea22a
+	return (memcmp(a, b, hash_length + 1));
aeea22a
 }
aeea22a
 
aeea22a
 static void
aeea22a
diff --git a/lib/dns/nsec3.c b/lib/dns/nsec3.c
1e41691
index 6ae7ca8..01426d6 100644
aeea22a
--- a/lib/dns/nsec3.c
aeea22a
+++ b/lib/dns/nsec3.c
1e41691
@@ -1963,7 +1963,7 @@ dns_nsec3_noexistnodata(dns_rdatatype_t type, dns_name_t* name,
aeea22a
 	 * Work out what this NSEC3 covers.
aeea22a
 	 * Inside (<0) or outside (>=0).
aeea22a
 	 */
aeea22a
-	scope = isc_safe_memcompare(owner, nsec3.next, nsec3.next_length);
aeea22a
+	scope = memcmp(owner, nsec3.next, nsec3.next_length);
aeea22a
 
aeea22a
 	/*
aeea22a
 	 * Prepare to compute all the hashes.
1e41691
@@ -1987,7 +1987,7 @@ dns_nsec3_noexistnodata(dns_rdatatype_t type, dns_name_t* name,
aeea22a
 			return (ISC_R_IGNORE);
aeea22a
 		}
aeea22a
 
aeea22a
-		order = isc_safe_memcompare(hash, owner, length);
aeea22a
+		order = memcmp(hash, owner, length);
aeea22a
 		if (first && order == 0) {
aeea22a
 			/*
aeea22a
 			 * The hashes are the same.
aeea22a
diff --git a/lib/dns/spnego.c b/lib/dns/spnego.c
ad7b3b8
index ad77f24..670982a 100644
aeea22a
--- a/lib/dns/spnego.c
aeea22a
+++ b/lib/dns/spnego.c
ad7b3b8
@@ -371,7 +371,7 @@ gssapi_spnego_decapsulate(OM_uint32 *,
aeea22a
 
aeea22a
 /* mod_auth_kerb.c */
aeea22a
 
aeea22a
-static int
aeea22a
+static isc_boolean_t
aeea22a
 cmp_gss_type(gss_buffer_t token, gss_OID gssoid)
aeea22a
 {
aeea22a
 	unsigned char *p;
ad7b3b8
@@ -395,7 +395,7 @@ cmp_gss_type(gss_buffer_t token, gss_OID gssoid)
aeea22a
 	if (((OM_uint32) *p++) != gssoid->length)
aeea22a
 		return (GSS_S_DEFECTIVE_TOKEN);
aeea22a
 
aeea22a
-	return (isc_safe_memcompare(p, gssoid->elements, gssoid->length));
aeea22a
+	return (!isc_safe_memequal(p, gssoid->elements, gssoid->length));
aeea22a
 }
aeea22a
 
aeea22a
 /* accept_sec_context.c */
aeea22a
diff --git a/lib/isc/Makefile.in b/lib/isc/Makefile.in
00db7f4
index 0fd0837..8ad54bb 100644
aeea22a
--- a/lib/isc/Makefile.in
aeea22a
+++ b/lib/isc/Makefile.in
aeea22a
@@ -60,7 +60,7 @@ OBJS =		@ISC_EXTRA_OBJS@ @ISC_PK11_O@ @ISC_PK11_RESULT_O@ \
aeea22a
 		parseint.@O@ portset.@O@ quota.@O@ radix.@O@ random.@O@ \
aeea22a
 		ratelimiter.@O@ refcount.@O@ region.@O@ regex.@O@ result.@O@ \
aeea22a
 		rwlock.@O@ \
00db7f4
-		safe.@O@ serial.@O@ siphash.@O@ sha1.@O@ sha2.@O@ sockaddr.@O@ stats.@O@ \
00db7f4
+		serial.@O@ siphash.@O@ sha1.@O@ sha2.@O@ sockaddr.@O@ stats.@O@ \
aeea22a
 		string.@O@ strtoul.@O@ symtab.@O@ task.@O@ taskpool.@O@ \
aeea22a
 		tm.@O@ timer.@O@ version.@O@ \
aeea22a
 		${UNIXOBJS} ${NLSOBJS} ${THREADOBJS}
aeea22a
@@ -79,7 +79,7 @@ SRCS =		@ISC_EXTRA_SRCS@ @ISC_PK11_C@ @ISC_PK11_RESULT_C@ \
aeea22a
 		netaddr.c netscope.c pool.c ondestroy.c \
aeea22a
 		parseint.c portset.c quota.c radix.c random.c ${CHACHASRCS} \
aeea22a
 		ratelimiter.c refcount.c region.c regex.c result.c rwlock.c \
00db7f4
-		safe.c serial.c siphash.c sha1.c sha2.c sockaddr.c stats.c string.c \
00db7f4
+		serial.c siphash.c sha1.c sha2.c sockaddr.c stats.c string.c \
aeea22a
 		strtoul.c symtab.c task.c taskpool.c timer.c \
aeea22a
 		tm.c version.c
aeea22a
 
aeea22a
@@ -95,10 +95,6 @@ TESTDIRS =	@UNITTESTS@
aeea22a
 
aeea22a
 @BIND9_MAKE_RULES@
aeea22a
 
aeea22a
-safe.@O@: safe.c
aeea22a
-	${LIBTOOL_MODE_COMPILE} ${CC} ${ALL_CFLAGS} @CCNOOPT@ \
aeea22a
-		-c ${srcdir}/safe.c
aeea22a
-
aeea22a
 version.@O@: version.c
aeea22a
 	${LIBTOOL_MODE_COMPILE} ${CC} ${ALL_CFLAGS} \
aeea22a
 		-DVERSION=\"${VERSION}\" \
aeea22a
diff --git a/lib/isc/include/isc/safe.h b/lib/isc/include/isc/safe.h
ad7b3b8
index 66ed08b..88b8f47 100644
aeea22a
--- a/lib/isc/include/isc/safe.h
aeea22a
+++ b/lib/isc/include/isc/safe.h
ad7b3b8
@@ -15,29 +15,19 @@
aeea22a
 
aeea22a
 /*! \file isc/safe.h */
aeea22a
 
ad7b3b8
-#include <stdbool.h>
ad7b3b8
-
aeea22a
-#include <isc/types.h>
aeea22a
-#include <stdlib.h>
aeea22a
+#include <isc/lang.h>
aeea22a
+#include <openssl/crypto.h>
aeea22a
 
aeea22a
 ISC_LANG_BEGINDECLS
aeea22a
 
ad7b3b8
-bool
aeea22a
-isc_safe_memequal(const void *s1, const void *s2, size_t n);
ad7b3b8
+#define isc_safe_memequal(s1, s2, n) !CRYPTO_memcmp(s1, s2, n)
aeea22a
 /*%<
ad7b3b8
  * Returns true iff. two blocks of memory are equal, otherwise
ad7b3b8
  * false.
aeea22a
  *
aeea22a
  */
aeea22a
 
aeea22a
-int
aeea22a
-isc_safe_memcompare(const void *b1, const void *b2, size_t len);
aeea22a
-/*%<
aeea22a
- * Clone of libc memcmp() which is safe to differential timing attacks.
aeea22a
- */
aeea22a
-
aeea22a
-void
aeea22a
-isc_safe_memwipe(void *ptr, size_t len);
aeea22a
+#define isc_safe_memwipe(ptr, len) OPENSSL_cleanse(ptr, len)
aeea22a
 /*%<
aeea22a
  * Clear the memory of length `len` pointed to by `ptr`.
aeea22a
  *
aeea22a
diff --git a/lib/isc/safe.c b/lib/isc/safe.c
aeea22a
deleted file mode 100644
ad7b3b8
index 7a464b6..0000000
aeea22a
--- a/lib/isc/safe.c
aeea22a
+++ /dev/null
ad7b3b8
@@ -1,83 +0,0 @@
aeea22a
-/*
aeea22a
- * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
aeea22a
- *
aeea22a
- * This Source Code Form is subject to the terms of the Mozilla Public
aeea22a
- * License, v. 2.0. If a copy of the MPL was not distributed with this
aeea22a
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
aeea22a
- *
aeea22a
- * See the COPYRIGHT file distributed with this work for additional
aeea22a
- * information regarding copyright ownership.
aeea22a
- */
aeea22a
-
aeea22a
-/*! \file */
aeea22a
-
aeea22a
-#include <config.h>
aeea22a
-
ad7b3b8
-#include <stdbool.h>
ad7b3b8
-
aeea22a
-#include <isc/safe.h>
aeea22a
-#include <isc/string.h>
aeea22a
-#include <isc/util.h>
aeea22a
-
aeea22a
-#ifdef WIN32
aeea22a
-#include <windows.h>
aeea22a
-#endif
aeea22a
-
aeea22a
-#ifdef _MSC_VER
aeea22a
-#pragma optimize("", off)
aeea22a
-#endif
aeea22a
-
ad7b3b8
-bool
aeea22a
-isc_safe_memequal(const void *s1, const void *s2, size_t n) {
ad7b3b8
-	uint8_t acc = 0;
aeea22a
-
aeea22a
-	if (n != 0U) {
ad7b3b8
-		const uint8_t *p1 = s1, *p2 = s2;
aeea22a
-
aeea22a
-		do {
aeea22a
-			acc |= *p1++ ^ *p2++;
aeea22a
-		} while (--n != 0U);
aeea22a
-	}
ad7b3b8
-	return (acc == 0);
aeea22a
-}
aeea22a
-
aeea22a
-
aeea22a
-int
aeea22a
-isc_safe_memcompare(const void *b1, const void *b2, size_t len) {
aeea22a
-	const unsigned char *p1 = b1, *p2 = b2;
aeea22a
-	size_t i;
aeea22a
-	int res = 0, done = 0;
aeea22a
-
aeea22a
-	for (i = 0; i < len; i++) {
aeea22a
-		/* lt is -1 if p1[i] < p2[i]; else 0. */
aeea22a
-		int lt = (p1[i] - p2[i]) >> CHAR_BIT;
aeea22a
-
aeea22a
-		/* gt is -1 if p1[i] > p2[i]; else 0. */
aeea22a
-		int gt = (p2[i] - p1[i]) >> CHAR_BIT;
aeea22a
-
aeea22a
-		/* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
aeea22a
-		int cmp = lt - gt;
aeea22a
-
aeea22a
-		/* set res = cmp if !done. */
aeea22a
-		res |= cmp & ~done;
aeea22a
-
aeea22a
-		/* set done if p1[i] != p2[i]. */
aeea22a
-		done |= lt | gt;
aeea22a
-	}
aeea22a
-
aeea22a
-	return (res);
aeea22a
-}
aeea22a
-
aeea22a
-void
aeea22a
-isc_safe_memwipe(void *ptr, size_t len) {
aeea22a
-	if (ISC_UNLIKELY(ptr == NULL || len == 0))
aeea22a
-		return;
aeea22a
-
aeea22a
-#ifdef WIN32
aeea22a
-	SecureZeroMemory(ptr, len);
aeea22a
-#elif HAVE_EXPLICIT_BZERO
aeea22a
-	explicit_bzero(ptr, len);
aeea22a
-#else
aeea22a
-	memset(ptr, 0, len);
aeea22a
-#endif
aeea22a
-}
aeea22a
diff --git a/lib/isc/tests/safe_test.c b/lib/isc/tests/safe_test.c
00db7f4
index 266ac75..60e9181 100644
aeea22a
--- a/lib/isc/tests/safe_test.c
aeea22a
+++ b/lib/isc/tests/safe_test.c
00db7f4
@@ -45,22 +45,6 @@ isc_safe_memequal_test(void **state) {
1e41691
 				       "\x00\x00\x00\x00", 4));
aeea22a
 }
aeea22a
 
1e41691
-/* test isc_safe_memcompare() */
1e41691
-static void
1e41691
-isc_safe_memcompare_test(void **state) {
1e41691
-	UNUSED(state);
aeea22a
-
1e41691
-	assert_int_equal(isc_safe_memcompare("test", "test", 4), 0);
1e41691
-	assert_true(isc_safe_memcompare("test", "tesc", 4) > 0);
1e41691
-	assert_true(isc_safe_memcompare("test", "tesy", 4) < 0);
1e41691
-	assert_int_equal(isc_safe_memcompare("\x00\x00\x00\x00",
1e41691
-					     "\x00\x00\x00\x00", 4), 0);
1e41691
-	assert_true(isc_safe_memcompare("\x00\x00\x00\x00",
1e41691
-					"\x00\x00\x00\x01", 4) < 0);
1e41691
-	assert_true(isc_safe_memcompare("\x00\x00\x00\x02",
1e41691
-					"\x00\x00\x00\x00", 4) > 0);
aeea22a
-}
aeea22a
-
1e41691
 /* test isc_safe_memwipe() */
1e41691
 static void
1e41691
 isc_safe_memwipe_test(void **state) {
00db7f4
@@ -69,7 +53,6 @@ isc_safe_memwipe_test(void **state) {
aeea22a
 	/* These should pass. */
aeea22a
 	isc_safe_memwipe(NULL, 0);
aeea22a
 	isc_safe_memwipe((void *) -1, 0);
aeea22a
-	isc_safe_memwipe(NULL, 42);
aeea22a
 
aeea22a
 	/*
aeea22a
 	 * isc_safe_memwipe(ptr, size) should function same as
00db7f4
@@ -108,7 +91,6 @@ main(void) {
1e41691
 	const struct CMUnitTest tests[] = {
1e41691
 		cmocka_unit_test(isc_safe_memequal_test),
1e41691
 		cmocka_unit_test(isc_safe_memwipe_test),
1e41691
-		cmocka_unit_test(isc_safe_memcompare_test),
1e41691
 	};
1e41691
 
1e41691
 	return (cmocka_run_group_tests(tests, NULL, NULL));
aeea22a
-- 
1e41691
2.20.1
aeea22a