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

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