Petr Menšík aeea22
From 25ff8ab2b0772262d358272a3ed70a24fc6e4887 Mon Sep 17 00:00:00 2001
Petr Menšík aeea22
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org>
Petr Menšík aeea22
Date: Wed, 25 Apr 2018 14:04:31 +0200
Petr Menšík aeea22
Subject: [PATCH] Replace isc_safe routines with their OpenSSL counter parts
Petr Menšík aeea22
Petr Menšík aeea22
(cherry picked from commit 66ba2fdad583d962a1f4971c85d58381f0849e4d)
Petr Menšík aeea22
Petr Menšík aeea22
Remove isc_safe_memcompare, it's not needed anywhere and can't be replaced with CRYPTO_memcmp()
Petr Menšík aeea22
Petr Menšík aeea22
(cherry picked from commit b105ccee68ccc3c18e6ea530063b3c8e5a42571c)
Petr Menšík aeea22
Petr Menšík aeea22
Fix the isc_safe_memwipe() usage with (NULL, >0)
Petr Menšík aeea22
Petr Menšík aeea22
(cherry picked from commit 083461d3329ff6f2410745848a926090586a9846)
Petr Menšík aeea22
---
Petr Menšík aeea22
 bin/dnssec/dnssec-signzone.c |  2 +-
Petr Menšík aeea22
 lib/dns/nsec3.c              |  4 +--
Petr Menšík aeea22
 lib/dns/spnego.c             |  4 +--
Petr Menšík aeea22
 lib/isc/Makefile.in          |  8 ++---
Petr Menšík aeea22
 lib/isc/include/isc/safe.h   | 18 ++++------
Petr Menšík aeea22
 lib/isc/safe.c               | 81 --------------------------------------------
Petr Menšík aeea22
 lib/isc/tests/safe_test.c    | 20 -----------
Petr Menšík aeea22
 7 files changed, 13 insertions(+), 124 deletions(-)
Petr Menšík aeea22
 delete mode 100644 lib/isc/safe.c
Petr Menšík aeea22
Petr Menšík aeea22
diff --git a/bin/dnssec/dnssec-signzone.c b/bin/dnssec/dnssec-signzone.c
Petr Menšík aeea22
index 53be1f5c60..351296a356 100644
Petr Menšík aeea22
--- a/bin/dnssec/dnssec-signzone.c
Petr Menšík aeea22
+++ b/bin/dnssec/dnssec-signzone.c
Petr Menšík aeea22
@@ -786,7 +786,7 @@ hashlist_add_dns_name(hashlist_t *l, /*const*/ dns_name_t *name,
Petr Menšík aeea22
 
Petr Menšík aeea22
 static int
Petr Menšík aeea22
 hashlist_comp(const void *a, const void *b) {
Petr Menšík aeea22
-	return (isc_safe_memcompare(a, b, hash_length + 1));
Petr Menšík aeea22
+	return (memcmp(a, b, hash_length + 1));
Petr Menšík aeea22
 }
Petr Menšík aeea22
 
Petr Menšík aeea22
 static void
Petr Menšík aeea22
diff --git a/lib/dns/nsec3.c b/lib/dns/nsec3.c
Petr Menšík aeea22
index d364308aaf..37b6a8a7fe 100644
Petr Menšík aeea22
--- a/lib/dns/nsec3.c
Petr Menšík aeea22
+++ b/lib/dns/nsec3.c
Petr Menšík aeea22
@@ -1950,7 +1950,7 @@ dns_nsec3_noexistnodata(dns_rdatatype_t type, dns_name_t* name,
Petr Menšík aeea22
 	 * Work out what this NSEC3 covers.
Petr Menšík aeea22
 	 * Inside (<0) or outside (>=0).
Petr Menšík aeea22
 	 */
Petr Menšík aeea22
-	scope = isc_safe_memcompare(owner, nsec3.next, nsec3.next_length);
Petr Menšík aeea22
+	scope = memcmp(owner, nsec3.next, nsec3.next_length);
Petr Menšík aeea22
 
Petr Menšík aeea22
 	/*
Petr Menšík aeea22
 	 * Prepare to compute all the hashes.
Petr Menšík aeea22
@@ -1974,7 +1974,7 @@ dns_nsec3_noexistnodata(dns_rdatatype_t type, dns_name_t* name,
Petr Menšík aeea22
 			return (ISC_R_IGNORE);
Petr Menšík aeea22
 		}
Petr Menšík aeea22
 
Petr Menšík aeea22
-		order = isc_safe_memcompare(hash, owner, length);
Petr Menšík aeea22
+		order = memcmp(hash, owner, length);
Petr Menšík aeea22
 		if (first && order == 0) {
Petr Menšík aeea22
 			/*
Petr Menšík aeea22
 			 * The hashes are the same.
Petr Menšík aeea22
diff --git a/lib/dns/spnego.c b/lib/dns/spnego.c
Petr Menšík aeea22
index ce3e42d650..079d4c1b4a 100644
Petr Menšík aeea22
--- a/lib/dns/spnego.c
Petr Menšík aeea22
+++ b/lib/dns/spnego.c
Petr Menšík aeea22
@@ -369,7 +369,7 @@ gssapi_spnego_decapsulate(OM_uint32 *,
Petr Menšík aeea22
 
Petr Menšík aeea22
 /* mod_auth_kerb.c */
Petr Menšík aeea22
 
Petr Menšík aeea22
-static int
Petr Menšík aeea22
+static isc_boolean_t
Petr Menšík aeea22
 cmp_gss_type(gss_buffer_t token, gss_OID gssoid)
Petr Menšík aeea22
 {
Petr Menšík aeea22
 	unsigned char *p;
Petr Menšík aeea22
@@ -393,7 +393,7 @@ cmp_gss_type(gss_buffer_t token, gss_OID gssoid)
Petr Menšík aeea22
 	if (((OM_uint32) *p++) != gssoid->length)
Petr Menšík aeea22
 		return (GSS_S_DEFECTIVE_TOKEN);
Petr Menšík aeea22
 
Petr Menšík aeea22
-	return (isc_safe_memcompare(p, gssoid->elements, gssoid->length));
Petr Menšík aeea22
+	return (!isc_safe_memequal(p, gssoid->elements, gssoid->length));
Petr Menšík aeea22
 }
Petr Menšík aeea22
 
Petr Menšík aeea22
 /* accept_sec_context.c */
Petr Menšík aeea22
diff --git a/lib/isc/Makefile.in b/lib/isc/Makefile.in
Petr Menšík aeea22
index ba53ef1091..98acffffc9 100644
Petr Menšík aeea22
--- a/lib/isc/Makefile.in
Petr Menšík aeea22
+++ b/lib/isc/Makefile.in
Petr Menšík aeea22
@@ -60,7 +60,7 @@ OBJS =		@ISC_EXTRA_OBJS@ @ISC_PK11_O@ @ISC_PK11_RESULT_O@ \
Petr Menšík aeea22
 		parseint.@O@ portset.@O@ quota.@O@ radix.@O@ random.@O@ \
Petr Menšík aeea22
 		ratelimiter.@O@ refcount.@O@ region.@O@ regex.@O@ result.@O@ \
Petr Menšík aeea22
 		rwlock.@O@ \
Petr Menšík aeea22
-		safe.@O@ serial.@O@ sha1.@O@ sha2.@O@ sockaddr.@O@ stats.@O@ \
Petr Menšík aeea22
+		serial.@O@ sha1.@O@ sha2.@O@ sockaddr.@O@ stats.@O@ \
Petr Menšík aeea22
 		string.@O@ strtoul.@O@ symtab.@O@ task.@O@ taskpool.@O@ \
Petr Menšík aeea22
 		tm.@O@ timer.@O@ version.@O@ \
Petr Menšík aeea22
 		${UNIXOBJS} ${NLSOBJS} ${THREADOBJS}
Petr Menšík aeea22
@@ -79,7 +79,7 @@ SRCS =		@ISC_EXTRA_SRCS@ @ISC_PK11_C@ @ISC_PK11_RESULT_C@ \
Petr Menšík aeea22
 		netaddr.c netscope.c pool.c ondestroy.c \
Petr Menšík aeea22
 		parseint.c portset.c quota.c radix.c random.c ${CHACHASRCS} \
Petr Menšík aeea22
 		ratelimiter.c refcount.c region.c regex.c result.c rwlock.c \
Petr Menšík aeea22
-		safe.c serial.c sha1.c sha2.c sockaddr.c stats.c string.c \
Petr Menšík aeea22
+		serial.c sha1.c sha2.c sockaddr.c stats.c string.c \
Petr Menšík aeea22
 		strtoul.c symtab.c task.c taskpool.c timer.c \
Petr Menšík aeea22
 		tm.c version.c
Petr Menšík aeea22
 
Petr Menšík aeea22
@@ -95,10 +95,6 @@ TESTDIRS =	@UNITTESTS@
Petr Menšík aeea22
 
Petr Menšík aeea22
 @BIND9_MAKE_RULES@
Petr Menšík aeea22
 
Petr Menšík aeea22
-safe.@O@: safe.c
Petr Menšík aeea22
-	${LIBTOOL_MODE_COMPILE} ${CC} ${ALL_CFLAGS} @CCNOOPT@ \
Petr Menšík aeea22
-		-c ${srcdir}/safe.c
Petr Menšík aeea22
-
Petr Menšík aeea22
 version.@O@: version.c
Petr Menšík aeea22
 	${LIBTOOL_MODE_COMPILE} ${CC} ${ALL_CFLAGS} \
Petr Menšík aeea22
 		-DVERSION=\"${VERSION}\" \
Petr Menšík aeea22
diff --git a/lib/isc/include/isc/safe.h b/lib/isc/include/isc/safe.h
Petr Menšík aeea22
index f29f00bac6..b8a0b2290c 100644
Petr Menšík aeea22
--- a/lib/isc/include/isc/safe.h
Petr Menšík aeea22
+++ b/lib/isc/include/isc/safe.h
Petr Menšík aeea22
@@ -15,27 +15,21 @@
Petr Menšík aeea22
 
Petr Menšík aeea22
 /*! \file isc/safe.h */
Petr Menšík aeea22
 
Petr Menšík aeea22
-#include <isc/types.h>
Petr Menšík aeea22
-#include <stdlib.h>
Petr Menšík aeea22
+#include <isc/boolean.h>
Petr Menšík aeea22
+#include <isc/lang.h>
Petr Menšík aeea22
+
Petr Menšík aeea22
+#include <openssl/crypto.h>
Petr Menšík aeea22
 
Petr Menšík aeea22
 ISC_LANG_BEGINDECLS
Petr Menšík aeea22
 
Petr Menšík aeea22
-isc_boolean_t
Petr Menšík aeea22
-isc_safe_memequal(const void *s1, const void *s2, size_t n);
Petr Menšík aeea22
+#define isc_safe_memequal(s1, s2, n) ISC_TF(!CRYPTO_memcmp(s1, s2, n))
Petr Menšík aeea22
 /*%<
Petr Menšík aeea22
  * Returns ISC_TRUE iff. two blocks of memory are equal, otherwise
Petr Menšík aeea22
  * ISC_FALSE.
Petr Menšík aeea22
  *
Petr Menšík aeea22
  */
Petr Menšík aeea22
 
Petr Menšík aeea22
-int
Petr Menšík aeea22
-isc_safe_memcompare(const void *b1, const void *b2, size_t len);
Petr Menšík aeea22
-/*%<
Petr Menšík aeea22
- * Clone of libc memcmp() which is safe to differential timing attacks.
Petr Menšík aeea22
- */
Petr Menšík aeea22
-
Petr Menšík aeea22
-void
Petr Menšík aeea22
-isc_safe_memwipe(void *ptr, size_t len);
Petr Menšík aeea22
+#define isc_safe_memwipe(ptr, len) OPENSSL_cleanse(ptr, len)
Petr Menšík aeea22
 /*%<
Petr Menšík aeea22
  * Clear the memory of length `len` pointed to by `ptr`.
Petr Menšík aeea22
  *
Petr Menšík aeea22
diff --git a/lib/isc/safe.c b/lib/isc/safe.c
Petr Menšík aeea22
deleted file mode 100644
Petr Menšík aeea22
index 5c9e1e2d13..0000000000
Petr Menšík aeea22
--- a/lib/isc/safe.c
Petr Menšík aeea22
+++ /dev/null
Petr Menšík aeea22
@@ -1,81 +0,0 @@
Petr Menšík aeea22
-/*
Petr Menšík aeea22
- * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Petr Menšík aeea22
- *
Petr Menšík aeea22
- * This Source Code Form is subject to the terms of the Mozilla Public
Petr Menšík aeea22
- * License, v. 2.0. If a copy of the MPL was not distributed with this
Petr Menšík aeea22
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Petr Menšík aeea22
- *
Petr Menšík aeea22
- * See the COPYRIGHT file distributed with this work for additional
Petr Menšík aeea22
- * information regarding copyright ownership.
Petr Menšík aeea22
- */
Petr Menšík aeea22
-
Petr Menšík aeea22
-/*! \file */
Petr Menšík aeea22
-
Petr Menšík aeea22
-#include <config.h>
Petr Menšík aeea22
-
Petr Menšík aeea22
-#include <isc/safe.h>
Petr Menšík aeea22
-#include <isc/string.h>
Petr Menšík aeea22
-#include <isc/util.h>
Petr Menšík aeea22
-
Petr Menšík aeea22
-#ifdef WIN32
Petr Menšík aeea22
-#include <windows.h>
Petr Menšík aeea22
-#endif
Petr Menšík aeea22
-
Petr Menšík aeea22
-#ifdef _MSC_VER
Petr Menšík aeea22
-#pragma optimize("", off)
Petr Menšík aeea22
-#endif
Petr Menšík aeea22
-
Petr Menšík aeea22
-isc_boolean_t
Petr Menšík aeea22
-isc_safe_memequal(const void *s1, const void *s2, size_t n) {
Petr Menšík aeea22
-	isc_uint8_t acc = 0;
Petr Menšík aeea22
-
Petr Menšík aeea22
-	if (n != 0U) {
Petr Menšík aeea22
-		const isc_uint8_t *p1 = s1, *p2 = s2;
Petr Menšík aeea22
-
Petr Menšík aeea22
-		do {
Petr Menšík aeea22
-			acc |= *p1++ ^ *p2++;
Petr Menšík aeea22
-		} while (--n != 0U);
Petr Menšík aeea22
-	}
Petr Menšík aeea22
-	return (ISC_TF(acc == 0));
Petr Menšík aeea22
-}
Petr Menšík aeea22
-
Petr Menšík aeea22
-
Petr Menšík aeea22
-int
Petr Menšík aeea22
-isc_safe_memcompare(const void *b1, const void *b2, size_t len) {
Petr Menšík aeea22
-	const unsigned char *p1 = b1, *p2 = b2;
Petr Menšík aeea22
-	size_t i;
Petr Menšík aeea22
-	int res = 0, done = 0;
Petr Menšík aeea22
-
Petr Menšík aeea22
-	for (i = 0; i < len; i++) {
Petr Menšík aeea22
-		/* lt is -1 if p1[i] < p2[i]; else 0. */
Petr Menšík aeea22
-		int lt = (p1[i] - p2[i]) >> CHAR_BIT;
Petr Menšík aeea22
-
Petr Menšík aeea22
-		/* gt is -1 if p1[i] > p2[i]; else 0. */
Petr Menšík aeea22
-		int gt = (p2[i] - p1[i]) >> CHAR_BIT;
Petr Menšík aeea22
-
Petr Menšík aeea22
-		/* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
Petr Menšík aeea22
-		int cmp = lt - gt;
Petr Menšík aeea22
-
Petr Menšík aeea22
-		/* set res = cmp if !done. */
Petr Menšík aeea22
-		res |= cmp & ~done;
Petr Menšík aeea22
-
Petr Menšík aeea22
-		/* set done if p1[i] != p2[i]. */
Petr Menšík aeea22
-		done |= lt | gt;
Petr Menšík aeea22
-	}
Petr Menšík aeea22
-
Petr Menšík aeea22
-	return (res);
Petr Menšík aeea22
-}
Petr Menšík aeea22
-
Petr Menšík aeea22
-void
Petr Menšík aeea22
-isc_safe_memwipe(void *ptr, size_t len) {
Petr Menšík aeea22
-	if (ISC_UNLIKELY(ptr == NULL || len == 0))
Petr Menšík aeea22
-		return;
Petr Menšík aeea22
-
Petr Menšík aeea22
-#ifdef WIN32
Petr Menšík aeea22
-	SecureZeroMemory(ptr, len);
Petr Menšík aeea22
-#elif HAVE_EXPLICIT_BZERO
Petr Menšík aeea22
-	explicit_bzero(ptr, len);
Petr Menšík aeea22
-#else
Petr Menšík aeea22
-	memset(ptr, 0, len);
Petr Menšík aeea22
-#endif
Petr Menšík aeea22
-}
Petr Menšík aeea22
diff --git a/lib/isc/tests/safe_test.c b/lib/isc/tests/safe_test.c
Petr Menšík aeea22
index f721cd1096..ea3e61f98d 100644
Petr Menšík aeea22
--- a/lib/isc/tests/safe_test.c
Petr Menšík aeea22
+++ b/lib/isc/tests/safe_test.c
Petr Menšík aeea22
@@ -39,24 +39,6 @@ ATF_TC_BODY(isc_safe_memequal, tc) {
Petr Menšík aeea22
 				     "\x00\x00\x00\x00", 4));
Petr Menšík aeea22
 }
Petr Menšík aeea22
 
Petr Menšík aeea22
-ATF_TC(isc_safe_memcompare);
Petr Menšík aeea22
-ATF_TC_HEAD(isc_safe_memcompare, tc) {
Petr Menšík aeea22
-	atf_tc_set_md_var(tc, "descr", "safe memcompare()");
Petr Menšík aeea22
-}
Petr Menšík aeea22
-ATF_TC_BODY(isc_safe_memcompare, tc) {
Petr Menšík aeea22
-	UNUSED(tc);
Petr Menšík aeea22
-
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("test", "test", 4) == 0);
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("test", "tesc", 4) > 0);
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("test", "tesy", 4) < 0);
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("\x00\x00\x00\x00",
Petr Menšík aeea22
-				      "\x00\x00\x00\x00", 4) == 0);
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("\x00\x00\x00\x00",
Petr Menšík aeea22
-				      "\x00\x00\x00\x01", 4) < 0);
Petr Menšík aeea22
-	ATF_CHECK(isc_safe_memcompare("\x00\x00\x00\x02",
Petr Menšík aeea22
-				      "\x00\x00\x00\x00", 4) > 0);
Petr Menšík aeea22
-}
Petr Menšík aeea22
-
Petr Menšík aeea22
 ATF_TC(isc_safe_memwipe);
Petr Menšík aeea22
 ATF_TC_HEAD(isc_safe_memwipe, tc) {
Petr Menšík aeea22
 	atf_tc_set_md_var(tc, "descr", "isc_safe_memwipe()");
Petr Menšík aeea22
@@ -67,7 +49,6 @@ ATF_TC_BODY(isc_safe_memwipe, tc) {
Petr Menšík aeea22
 	/* These should pass. */
Petr Menšík aeea22
 	isc_safe_memwipe(NULL, 0);
Petr Menšík aeea22
 	isc_safe_memwipe((void *) -1, 0);
Petr Menšík aeea22
-	isc_safe_memwipe(NULL, 42);
Petr Menšík aeea22
 
Petr Menšík aeea22
 	/*
Petr Menšík aeea22
 	 * isc_safe_memwipe(ptr, size) should function same as
Petr Menšík aeea22
@@ -106,7 +87,6 @@ ATF_TC_BODY(isc_safe_memwipe, tc) {
Petr Menšík aeea22
  */
Petr Menšík aeea22
 ATF_TP_ADD_TCS(tp) {
Petr Menšík aeea22
 	ATF_TP_ADD_TC(tp, isc_safe_memequal);
Petr Menšík aeea22
-	ATF_TP_ADD_TC(tp, isc_safe_memcompare);
Petr Menšík aeea22
 	ATF_TP_ADD_TC(tp, isc_safe_memwipe);
Petr Menšík aeea22
 	return (atf_no_error());
Petr Menšík aeea22
 }
Petr Menšík aeea22
-- 
Petr Menšík aeea22
2.14.4
Petr Menšík aeea22