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