Blame SOURCES/0065-Ticket-bz1525628-invalid-password-migration-causes-u.patch

96373c
From 40fcaabfaa2c865471cc5fb1fab04106bc3ec611 Mon Sep 17 00:00:00 2001
96373c
From: William Brown <firstyear@redhat.com>
96373c
Date: Thu, 18 Jan 2018 11:27:58 +1000
96373c
Subject: [PATCH] Ticket bz1525628 - invalid password migration causes unauth
96373c
 bind
96373c
96373c
Bug Description:  Slapi_ct_memcmp expects both inputs to be
96373c
at LEAST size n. If they are not, we only compared UP to n.
96373c
96373c
Invalid migrations of passwords (IE {CRYPT}XX) would create
96373c
a pw which is just salt and no hash. ct_memcmp would then
96373c
only verify the salt bits and would allow the authentication.
96373c
96373c
This relies on an administrative mistake both of allowing
96373c
password migration (nsslapd-allow-hashed-passwords) and then
96373c
subsequently migrating an INVALID password to the server.
96373c
96373c
Fix Description:  slapi_ct_memcmp now access n1, n2 size
96373c
and will FAIL if they are not the same, but will still compare
96373c
n bytes, where n is the "longest" memory, to the first byte
96373c
of the other to prevent length disclosure of the shorter
96373c
value (generally the mis-migrated password)
96373c
96373c
https://bugzilla.redhat.com/show_bug.cgi?id=1525628
96373c
96373c
Author: wibrown
96373c
96373c
Review by: ???
96373c
96373c
Signed-off-by: Mark Reynolds <mreynolds@redhat.com>
96373c
---
96373c
 .../bz1525628_ct_memcmp_invalid_hash_test.py       | 56 ++++++++++++++++++++++
96373c
 ldap/servers/plugins/pwdstorage/clear_pwd.c        |  4 +-
96373c
 ldap/servers/plugins/pwdstorage/crypt_pwd.c        |  4 +-
96373c
 ldap/servers/plugins/pwdstorage/md5_pwd.c          |  4 +-
96373c
 ldap/servers/plugins/pwdstorage/sha_pwd.c          | 16 +++++--
96373c
 ldap/servers/plugins/pwdstorage/smd5_pwd.c         |  2 +-
96373c
 ldap/servers/slapd/ch_malloc.c                     | 36 ++++++++++++--
96373c
 ldap/servers/slapd/slapi-plugin.h                  |  2 +-
96373c
 8 files changed, 108 insertions(+), 16 deletions(-)
96373c
 create mode 100644 dirsrvtests/tests/suites/password/bz1525628_ct_memcmp_invalid_hash_test.py
96373c
96373c
diff --git a/dirsrvtests/tests/suites/password/bz1525628_ct_memcmp_invalid_hash_test.py b/dirsrvtests/tests/suites/password/bz1525628_ct_memcmp_invalid_hash_test.py
96373c
new file mode 100644
96373c
index 000000000..2f38384a1
96373c
--- /dev/null
96373c
+++ b/dirsrvtests/tests/suites/password/bz1525628_ct_memcmp_invalid_hash_test.py
96373c
@@ -0,0 +1,56 @@
96373c
+# --- BEGIN COPYRIGHT BLOCK ---
96373c
+# Copyright (C) 2018 Red Hat, Inc.
96373c
+# All rights reserved.
96373c
+#
96373c
+# License: GPL (version 3 or any later version).
96373c
+# See LICENSE for details.
96373c
+# --- END COPYRIGHT BLOCK ---
96373c
+#
96373c
+
96373c
+import ldap
96373c
+import pytest
96373c
+import logging
96373c
+from lib389.topologies import topology_st
96373c
+from lib389._constants import PASSWORD, DEFAULT_SUFFIX
96373c
+
96373c
+from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
96373c
+
96373c
+logging.getLogger(__name__).setLevel(logging.DEBUG)
96373c
+log = logging.getLogger(__name__)
96373c
+
96373c
+def test_invalid_hash_fails(topology_st):
96373c
+    """When given a malformed hash from userpassword migration
96373c
+    slapi_ct_memcmp would check only to the length of the shorter
96373c
+    field. This affects some values where it would ONLY verify
96373c
+    the salt is valid, and thus would allow any password to bind.
96373c
+
96373c
+    :id: 8131c029-7147-47db-8d03-ec5db2a01cfb
96373c
+    :setup: Standalone Instance
96373c
+    :steps:
96373c
+        1. Create a user
96373c
+        2. Add an invalid password hash (truncated)
96373c
+        3. Attempt to bind
96373c
+    :expectedresults:
96373c
+        1. User is added
96373c
+        2. Invalid pw hash is added
96373c
+        3. Bind fails
96373c
+    """
96373c
+    log.info("Running invalid hash test")
96373c
+
96373c
+    # Allow setting raw password hashes for migration.
96373c
+    topology_st.standalone.config.set('nsslapd-allow-hashed-passwords', 'on')
96373c
+
96373c
+    users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
96373c
+    user = users.create(properties=TEST_USER_PROPERTIES)
96373c
+    user.set('userPassword', '{CRYPT}XX')
96373c
+
96373c
+    # Attempt to bind. This should fail.
96373c
+    with pytest.raises(ldap.INVALID_CREDENTIALS):
96373c
+        user.bind(PASSWORD)
96373c
+    with pytest.raises(ldap.INVALID_CREDENTIALS):
96373c
+        user.bind('XX')
96373c
+    with pytest.raises(ldap.INVALID_CREDENTIALS):
96373c
+        user.bind('{CRYPT}XX')
96373c
+
96373c
+    log.info("PASSED")
96373c
+
96373c
diff --git a/ldap/servers/plugins/pwdstorage/clear_pwd.c b/ldap/servers/plugins/pwdstorage/clear_pwd.c
96373c
index f5e6f9d4c..3d340752d 100644
96373c
--- a/ldap/servers/plugins/pwdstorage/clear_pwd.c
96373c
+++ b/ldap/servers/plugins/pwdstorage/clear_pwd.c
96373c
@@ -39,7 +39,7 @@ clear_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
          * However, even if the first part of userpw matches dbpwd, but len !=, we
96373c
          * have already failed anyawy. This prevents substring matching.
96373c
          */
96373c
-        if (slapi_ct_memcmp(userpwd, dbpwd, len_dbp) != 0) {
96373c
+        if (slapi_ct_memcmp(userpwd, dbpwd, len_user, len_dbp) != 0) {
96373c
             result = 1;
96373c
         }
96373c
     } else {
96373c
@@ -51,7 +51,7 @@ clear_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
          * dbpwd to itself. We have already got result == 1 if we are here, so we are
96373c
          * just trying to take up time!
96373c
          */
96373c
-        if (slapi_ct_memcmp(dbpwd, dbpwd, len_dbp)) {
96373c
+        if (slapi_ct_memcmp(dbpwd, dbpwd, len_dbp, len_dbp)) {
96373c
             /* Do nothing, we have the if to fix a coverity check. */
96373c
         }
96373c
     }
96373c
diff --git a/ldap/servers/plugins/pwdstorage/crypt_pwd.c b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
96373c
index 3bd226581..0dccd1b51 100644
96373c
--- a/ldap/servers/plugins/pwdstorage/crypt_pwd.c
96373c
+++ b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
96373c
@@ -65,13 +65,13 @@ crypt_close(Slapi_PBlock *pb __attribute__((unused)))
96373c
 int
96373c
 crypt_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
 {
96373c
-    int rc;
96373c
+    int32_t rc;
96373c
     char *cp;
96373c
     PR_Lock(cryptlock);
96373c
     /* we use salt (first 2 chars) of encoded password in call to crypt() */
96373c
     cp = crypt(userpwd, dbpwd);
96373c
     if (cp) {
96373c
-        rc = slapi_ct_memcmp(dbpwd, cp, strlen(dbpwd));
96373c
+        rc = slapi_ct_memcmp(dbpwd, cp, strlen(dbpwd), strlen(cp));
96373c
     } else {
96373c
         rc = -1;
96373c
     }
96373c
diff --git a/ldap/servers/plugins/pwdstorage/md5_pwd.c b/ldap/servers/plugins/pwdstorage/md5_pwd.c
96373c
index 1e2cf58e7..2c2aacaa6 100644
96373c
--- a/ldap/servers/plugins/pwdstorage/md5_pwd.c
96373c
+++ b/ldap/servers/plugins/pwdstorage/md5_pwd.c
96373c
@@ -30,7 +30,7 @@
96373c
 int
96373c
 md5_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
 {
96373c
-    int rc = -1;
96373c
+    int32_t rc = -1;
96373c
     char *bver;
96373c
     PK11Context *ctx = NULL;
96373c
     unsigned int outLen;
96373c
@@ -57,7 +57,7 @@ md5_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
     bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
96373c
     /* bver points to b2a_out upon success */
96373c
     if (bver) {
96373c
-        rc = slapi_ct_memcmp(bver, dbpwd, strlen(dbpwd));
96373c
+        rc = slapi_ct_memcmp(bver, dbpwd, strlen(dbpwd), strlen(bver));
96373c
     } else {
96373c
         slapi_log_err(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
96373c
                       "Could not base64 encode hashed value for password compare");
96373c
diff --git a/ldap/servers/plugins/pwdstorage/sha_pwd.c b/ldap/servers/plugins/pwdstorage/sha_pwd.c
96373c
index 1fbe0bc82..381b31d7c 100644
96373c
--- a/ldap/servers/plugins/pwdstorage/sha_pwd.c
96373c
+++ b/ldap/servers/plugins/pwdstorage/sha_pwd.c
96373c
@@ -49,7 +49,7 @@ sha_pw_cmp(const char *userpwd, const char *dbpwd, unsigned int shaLen)
96373c
     char userhash[MAX_SHA_HASH_SIZE];
96373c
     char quick_dbhash[MAX_SHA_HASH_SIZE + SHA_SALT_LENGTH + 3];
96373c
     char *dbhash = quick_dbhash;
96373c
-    struct berval salt;
96373c
+    struct berval salt = {0};
96373c
     PRUint32 hash_len;
96373c
     unsigned int secOID;
96373c
     char *schemeName;
96373c
@@ -122,9 +122,19 @@ sha_pw_cmp(const char *userpwd, const char *dbpwd, unsigned int shaLen)
96373c
 
96373c
     /* the proof is in the comparison... */
96373c
     if (hash_len >= shaLen) {
96373c
-        result = slapi_ct_memcmp(userhash, dbhash, shaLen);
96373c
+        /*
96373c
+         * This say "if the hash has a salt IE >, OR if they are equal, check the hash component ONLY.
96373c
+         * This is why we repeat shaLen twice, even though it seems odd. If you have a dbhast of ssha
96373c
+         * it's len is 28, and the userpw is 20, but 0 - 20 is the sha, and 21-28 is the salt, which
96373c
+         * has already been processed into userhash.
96373c
+         * The case where dbpwd is truncated is handled above in "invalid base64" arm.
96373c
+         */
96373c
+        result = slapi_ct_memcmp(userhash, dbhash, shaLen, shaLen);
96373c
     } else {
96373c
-        result = slapi_ct_memcmp(userhash, dbhash + OLD_SALT_LENGTH, hash_len - OLD_SALT_LENGTH);
96373c
+        /* This case is for if the salt is at the START, which only applies to DS40B1 case.
96373c
+         * May never be a valid check...
96373c
+         */
96373c
+        result = slapi_ct_memcmp(userhash, dbhash + OLD_SALT_LENGTH, shaLen, hash_len - OLD_SALT_LENGTH);
96373c
     }
96373c
 
96373c
 loser:
96373c
diff --git a/ldap/servers/plugins/pwdstorage/smd5_pwd.c b/ldap/servers/plugins/pwdstorage/smd5_pwd.c
96373c
index a83ac6fa4..cbfc74ff3 100644
96373c
--- a/ldap/servers/plugins/pwdstorage/smd5_pwd.c
96373c
+++ b/ldap/servers/plugins/pwdstorage/smd5_pwd.c
96373c
@@ -82,7 +82,7 @@ smd5_pw_cmp(const char *userpwd, const char *dbpwd)
96373c
     PK11_DestroyContext(ctx, 1);
96373c
 
96373c
     /* Compare everything up to the salt. */
96373c
-    rc = slapi_ct_memcmp(userhash, dbhash, MD5_LENGTH);
96373c
+    rc = slapi_ct_memcmp(userhash, dbhash, MD5_LENGTH, MD5_LENGTH);
96373c
 
96373c
 loser:
96373c
     if (dbhash && dbhash != quick_dbhash)
96373c
diff --git a/ldap/servers/slapd/ch_malloc.c b/ldap/servers/slapd/ch_malloc.c
96373c
index ef436b3e8..90a2b2c1a 100644
96373c
--- a/ldap/servers/slapd/ch_malloc.c
96373c
+++ b/ldap/servers/slapd/ch_malloc.c
96373c
@@ -336,8 +336,8 @@ slapi_ch_smprintf(const char *fmt, ...)
96373c
 
96373c
 /* Constant time memcmp. Does not shortcircuit on failure! */
96373c
 /* This relies on p1 and p2 both being size at least n! */
96373c
-int
96373c
-slapi_ct_memcmp(const void *p1, const void *p2, size_t n)
96373c
+int32_t
96373c
+slapi_ct_memcmp(const void *p1, const void *p2, size_t n1, size_t n2)
96373c
 {
96373c
     int result = 0;
96373c
     const unsigned char *_p1 = (const unsigned char *)p1;
96373c
@@ -347,9 +347,35 @@ slapi_ct_memcmp(const void *p1, const void *p2, size_t n)
96373c
         return 2;
96373c
     }
96373c
 
96373c
-    for (size_t i = 0; i < n; i++) {
96373c
-        if (_p1[i] ^ _p2[i]) {
96373c
-            result = 1;
96373c
+    if (n1 == n2) {
96373c
+        for (size_t i = 0; i < n1; i++) {
96373c
+            if (_p1[i] ^ _p2[i]) {
96373c
+                result = 1;
96373c
+            }
96373c
+        }
96373c
+    } else {
96373c
+        const unsigned char *_pa;
96373c
+        const unsigned char *_pb;
96373c
+        size_t nl;
96373c
+        if (n2 > n1) {
96373c
+            _pa = _p2;
96373c
+            _pb = _p2;
96373c
+            nl = n2;
96373c
+        } else {
96373c
+            _pa = _p1;
96373c
+            _pb = _p1;
96373c
+            nl = n1;
96373c
+        }
96373c
+        /* We already fail as n1 != n2 */
96373c
+        result = 3;
96373c
+        for (size_t i = 0; i < nl; i++) {
96373c
+            if (_pa[i] ^ _pb[i]) {
96373c
+                /*
96373c
+                 * If we don't mutate result here, dead code elimination
96373c
+                 * we remove for loop.
96373c
+                 */
96373c
+                result = 4;
96373c
+            }
96373c
         }
96373c
     }
96373c
     return result;
96373c
diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h
96373c
index 4566202d3..95cdcc0da 100644
96373c
--- a/ldap/servers/slapd/slapi-plugin.h
96373c
+++ b/ldap/servers/slapd/slapi-plugin.h
96373c
@@ -5862,7 +5862,7 @@ char *slapi_ch_smprintf(const char *fmt, ...)
96373c
  * \param n length in bytes of the content of p1 AND p2.
96373c
  * \return 0 on match. 1 on non-match. 2 on presence of NULL pointer in p1 or p2.
96373c
  */
96373c
-int slapi_ct_memcmp(const void *p1, const void *p2, size_t n);
96373c
+int32_t slapi_ct_memcmp(const void *p1, const void *p2, size_t n1, size_t n2);
96373c
 
96373c
 /*
96373c
  * syntax plugin routines
96373c
-- 
96373c
2.13.6
96373c