Blame SOURCES/0088-Ticket-bz1525628-1.3.6-backport-invalid-password-mig.patch

fb1149
From 4219c54d9706f5597e8186d4f983b30587c2762e Mon Sep 17 00:00:00 2001
fb1149
From: Mark Reynolds <mreynolds@redhat.com>
fb1149
Date: Tue, 13 Feb 2018 10:32:06 -0500
fb1149
Subject: [PATCH] Ticket bz1525628 1.3.6 backport - invalid password migration 
fb1149
 causes unauth bind
fb1149
fb1149
Bug Description:  Slapi_ct_memcmp expects both inputs to be
fb1149
at LEAST size n. If they are not, we only compared UP to n.
fb1149
fb1149
Invalid migrations of passwords (IE {CRYPT}XX) would create
fb1149
a pw which is just salt and no hash. ct_memcmp would then
fb1149
only verify the salt bits and would allow the authentication.
fb1149
fb1149
This relies on an administrative mistake both of allowing
fb1149
password migration (nsslapd-allow-hashed-passwords) and then
fb1149
subsequently migrating an INVALID password to the server.
fb1149
fb1149
Fix Description:  slapi_ct_memcmp now access n1, n2 size
fb1149
and will FAIL if they are not the same, but will still compare
fb1149
n bytes, where n is the "longest" memory, to the first byte
fb1149
of the other to prevent length disclosure of the shorter
fb1149
value (generally the mis-migrated password)
fb1149
fb1149
https://bugzilla.redhat.com/show_bug.cgi?id=1525628
fb1149
fb1149
Author: wibrown
fb1149
---
fb1149
 ldap/servers/plugins/pwdstorage/clear_pwd.c |  4 +-
fb1149
 ldap/servers/plugins/pwdstorage/crypt_pwd.c |  4 +-
fb1149
 ldap/servers/plugins/pwdstorage/md5_pwd.c   | 14 +++----
fb1149
 ldap/servers/plugins/pwdstorage/sha_pwd.c   | 18 +++++++--
fb1149
 ldap/servers/plugins/pwdstorage/smd5_pwd.c  | 60 +++++++++++++++--------------
fb1149
 ldap/servers/slapd/ch_malloc.c              | 37 +++++++++++++++---
fb1149
 ldap/servers/slapd/slapi-plugin.h           |  2 +-
fb1149
 7 files changed, 88 insertions(+), 51 deletions(-)
fb1149
fb1149
diff --git a/ldap/servers/plugins/pwdstorage/clear_pwd.c b/ldap/servers/plugins/pwdstorage/clear_pwd.c
fb1149
index b9b362d34..050e60dd7 100644
fb1149
--- a/ldap/servers/plugins/pwdstorage/clear_pwd.c
fb1149
+++ b/ldap/servers/plugins/pwdstorage/clear_pwd.c
fb1149
@@ -39,7 +39,7 @@ clear_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
          * However, even if the first part of userpw matches dbpwd, but len !=, we
fb1149
          * have already failed anyawy. This prevents substring matching.
fb1149
          */
fb1149
-        if (slapi_ct_memcmp(userpwd, dbpwd, len_dbp) != 0) {
fb1149
+        if (slapi_ct_memcmp(userpwd, dbpwd, len_user, len_dbp) != 0) {
fb1149
             result = 1;
fb1149
         }
fb1149
     } else {
fb1149
@@ -51,7 +51,7 @@ clear_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
          * dbpwd to itself. We have already got result == 1 if we are here, so we are
fb1149
          * just trying to take up time!
fb1149
          */
fb1149
-        if (slapi_ct_memcmp(dbpwd, dbpwd, len_dbp)) {
fb1149
+        if (slapi_ct_memcmp(dbpwd, dbpwd, len_dbp, len_dbp)) {
fb1149
             /* Do nothing, we have the if to fix a coverity check. */
fb1149
         }
fb1149
     }
fb1149
diff --git a/ldap/servers/plugins/pwdstorage/crypt_pwd.c b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
fb1149
index dfd5af94b..ff8eabb07 100644
fb1149
--- a/ldap/servers/plugins/pwdstorage/crypt_pwd.c
fb1149
+++ b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
fb1149
@@ -56,13 +56,13 @@ crypt_close(Slapi_PBlock *pb __attribute__((unused)))
fb1149
 int
fb1149
 crypt_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
 {
fb1149
-    int rc;
fb1149
+    int32_t rc;
fb1149
     char *cp;
fb1149
     PR_Lock(cryptlock);
fb1149
     /* we use salt (first 2 chars) of encoded password in call to crypt() */
fb1149
     cp = crypt( userpwd, dbpwd );
fb1149
     if (cp) {
fb1149
-       rc= slapi_ct_memcmp( dbpwd, cp, strlen(dbpwd));
fb1149
+       rc= slapi_ct_memcmp(dbpwd, cp, strlen(dbpwd), strlen(cp));
fb1149
     } else {
fb1149
        rc = -1;
fb1149
     }
fb1149
diff --git a/ldap/servers/plugins/pwdstorage/md5_pwd.c b/ldap/servers/plugins/pwdstorage/md5_pwd.c
fb1149
index b27994667..88c11688b 100644
fb1149
--- a/ldap/servers/plugins/pwdstorage/md5_pwd.c
fb1149
+++ b/ldap/servers/plugins/pwdstorage/md5_pwd.c
fb1149
@@ -30,12 +30,12 @@
fb1149
 int
fb1149
 md5_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
 {
fb1149
-   int rc=-1;
fb1149
-   char * bver;
fb1149
-   PK11Context *ctx=NULL;
fb1149
+   int32_t rc=-1;
fb1149
+   char *bver;
fb1149
+   PK11Context *ctx = NULL;
fb1149
    unsigned int outLen;
fb1149
    unsigned char hash_out[MD5_HASH_LEN];
fb1149
-   unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
fb1149
+   unsigned char b2a_out[MD5_HASH_LEN * 2]; /* conservative */
fb1149
    SECItem binary_item;
fb1149
 
fb1149
    ctx = PK11_CreateDigestContext(SEC_OID_MD5);
fb1149
@@ -57,10 +57,10 @@ md5_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
    bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
fb1149
    /* bver points to b2a_out upon success */
fb1149
    if (bver) {
fb1149
-	   rc = slapi_ct_memcmp(bver,dbpwd, strlen(dbpwd));
fb1149
+       rc = slapi_ct_memcmp(bver,dbpwd, strlen(dbpwd), strlen(bver));
fb1149
    } else {
fb1149
-	   slapi_log_err(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
fb1149
-					   "Could not base64 encode hashed value for password compare");
fb1149
+       slapi_log_err(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
fb1149
+                     "Could not base64 encode hashed value for password compare");
fb1149
    }
fb1149
 loser:
fb1149
    return rc;
fb1149
diff --git a/ldap/servers/plugins/pwdstorage/sha_pwd.c b/ldap/servers/plugins/pwdstorage/sha_pwd.c
fb1149
index 5f41c5b93..c9db8964a 100644
fb1149
--- a/ldap/servers/plugins/pwdstorage/sha_pwd.c
fb1149
+++ b/ldap/servers/plugins/pwdstorage/sha_pwd.c
fb1149
@@ -49,7 +49,7 @@ sha_pw_cmp (const char *userpwd, const char *dbpwd, unsigned int shaLen )
fb1149
     char userhash[MAX_SHA_HASH_SIZE];
fb1149
     char quick_dbhash[MAX_SHA_HASH_SIZE + SHA_SALT_LENGTH + 3];
fb1149
     char *dbhash = quick_dbhash;
fb1149
-    struct berval salt;
fb1149
+    struct berval salt = {0};
fb1149
     PRUint32 hash_len;
fb1149
     unsigned int secOID;
fb1149
     char *schemeName;
fb1149
@@ -120,10 +120,20 @@ sha_pw_cmp (const char *userpwd, const char *dbpwd, unsigned int shaLen )
fb1149
     }
fb1149
 
fb1149
     /* the proof is in the comparison... */
fb1149
-    if ( hash_len >= shaLen ) {
fb1149
-        result = slapi_ct_memcmp( userhash, dbhash, shaLen );
fb1149
+    if (hash_len >= shaLen) {
fb1149
+        /*
fb1149
+         * This say "if the hash has a salt IE >, OR if they are equal, check the hash component ONLY.
fb1149
+         * This is why we repeat shaLen twice, even though it seems odd. If you have a dbhast of ssha
fb1149
+         * it's len is 28, and the userpw is 20, but 0 - 20 is the sha, and 21-28 is the salt, which
fb1149
+         * has already been processed into userhash.
fb1149
+         * The case where dbpwd is truncated is handled above in "invalid base64" arm.
fb1149
+         */
fb1149
+        result = slapi_ct_memcmp(userhash, dbhash, shaLen, shaLen);
fb1149
     } else {
fb1149
-        result = slapi_ct_memcmp( userhash, dbhash + OLD_SALT_LENGTH, hash_len - OLD_SALT_LENGTH );
fb1149
+        /* This case is for if the salt is at the START, which only applies to DS40B1 case.
fb1149
+         * May never be a valid check...
fb1149
+         */
fb1149
+        result = slapi_ct_memcmp(userhash, dbhash + OLD_SALT_LENGTH, shaLen, hash_len - OLD_SALT_LENGTH);
fb1149
     }
fb1149
 
fb1149
 loser:
fb1149
diff --git a/ldap/servers/plugins/pwdstorage/smd5_pwd.c b/ldap/servers/plugins/pwdstorage/smd5_pwd.c
fb1149
index 2e9d195ea..f6b4bb4a0 100644
fb1149
--- a/ldap/servers/plugins/pwdstorage/smd5_pwd.c
fb1149
+++ b/ldap/servers/plugins/pwdstorage/smd5_pwd.c
fb1149
@@ -52,35 +52,37 @@ smd5_pw_cmp( const char *userpwd, const char *dbpwd )
fb1149
    /*
fb1149
     * Decode hash stored in database.
fb1149
     */
fb1149
-   hash_len = pwdstorage_base64_decode_len(dbpwd, 0);
fb1149
-   if ( hash_len >= sizeof(quick_dbhash) ) { /* get more space: */
fb1149
-      dbhash = (char*) slapi_ch_calloc( hash_len + 1, sizeof(char) );
fb1149
-      if ( dbhash == NULL ) goto loser;
fb1149
-   } else {
fb1149
-      memset( quick_dbhash, 0, sizeof(quick_dbhash) );
fb1149
-   }
fb1149
-
fb1149
-   hashresult = PL_Base64Decode( dbpwd, 0, dbhash );
fb1149
-   if (NULL == hashresult) {
fb1149
-      slapi_log_err(SLAPI_LOG_PLUGIN, SALTED_MD5_SUBSYSTEM_NAME,
fb1149
-            "smd5_pw_cmp: userPassword \"%s\" is the wrong length "
fb1149
-            "or is not properly encoded BASE64\n", dbpwd );
fb1149
-      goto loser;
fb1149
-   }
fb1149
-
fb1149
-   salt.bv_val = (void*)(dbhash + MD5_LENGTH); /* salt starts after hash value */
fb1149
-   salt.bv_len = hash_len - MD5_LENGTH; /* remaining bytes must be salt */
fb1149
-
fb1149
-   /* create the hash */
fb1149
-   memset( userhash, 0, sizeof(userhash) );
fb1149
-   PK11_DigestBegin(ctx);
fb1149
-   PK11_DigestOp(ctx, (const unsigned char *)userpwd, strlen(userpwd));
fb1149
-   PK11_DigestOp(ctx, (unsigned char*)(salt.bv_val), salt.bv_len);
fb1149
-   PK11_DigestFinal(ctx, userhash, &outLen, sizeof userhash);
fb1149
-   PK11_DestroyContext(ctx, 1);
fb1149
-
fb1149
-   /* Compare everything up to the salt. */
fb1149
-   rc = slapi_ct_memcmp( userhash, dbhash, MD5_LENGTH );
fb1149
+    hash_len = pwdstorage_base64_decode_len(dbpwd, 0);
fb1149
+    if (hash_len >= sizeof(quick_dbhash)) { /* get more space: */
fb1149
+        dbhash = (char *)slapi_ch_calloc(hash_len + 1, sizeof(char));
fb1149
+        if (dbhash == NULL)
fb1149
+            goto loser;
fb1149
+    } else {
fb1149
+        memset(quick_dbhash, 0, sizeof(quick_dbhash));
fb1149
+    }
fb1149
+
fb1149
+    hashresult = PL_Base64Decode(dbpwd, 0, dbhash);
fb1149
+    if (NULL == hashresult) {
fb1149
+        slapi_log_err(SLAPI_LOG_PLUGIN, SALTED_MD5_SUBSYSTEM_NAME,
fb1149
+                      "smd5_pw_cmp: userPassword \"%s\" is the wrong length "
fb1149
+                      "or is not properly encoded BASE64\n",
fb1149
+                      dbpwd);
fb1149
+        goto loser;
fb1149
+    }
fb1149
+
fb1149
+    salt.bv_val = (void *)(dbhash + MD5_LENGTH); /* salt starts after hash value */
fb1149
+    salt.bv_len = hash_len - MD5_LENGTH;         /* remaining bytes must be salt */
fb1149
+
fb1149
+    /* create the hash */
fb1149
+    memset(userhash, 0, sizeof(userhash));
fb1149
+    PK11_DigestBegin(ctx);
fb1149
+    PK11_DigestOp(ctx, (const unsigned char *)userpwd, strlen(userpwd));
fb1149
+    PK11_DigestOp(ctx, (unsigned char *)(salt.bv_val), salt.bv_len);
fb1149
+    PK11_DigestFinal(ctx, userhash, &outLen, sizeof userhash);
fb1149
+    PK11_DestroyContext(ctx, 1);
fb1149
+
fb1149
+    /* Compare everything up to the salt. */
fb1149
+    rc = slapi_ct_memcmp(userhash, dbhash, MD5_LENGTH, MD5_LENGTH);
fb1149
 
fb1149
 loser:
fb1149
    if ( dbhash && dbhash != quick_dbhash ) slapi_ch_free_string( (char **)&dbhash );
fb1149
diff --git a/ldap/servers/slapd/ch_malloc.c b/ldap/servers/slapd/ch_malloc.c
fb1149
index 52ccb64e8..da0b5f6d8 100644
fb1149
--- a/ldap/servers/slapd/ch_malloc.c
fb1149
+++ b/ldap/servers/slapd/ch_malloc.c
fb1149
@@ -343,8 +343,8 @@ slapi_ch_smprintf(const char *fmt, ...)
fb1149
 
fb1149
 /* Constant time memcmp. Does not shortcircuit on failure! */
fb1149
 /* This relies on p1 and p2 both being size at least n! */
fb1149
-int
fb1149
-slapi_ct_memcmp( const void *p1, const void *p2, size_t n)
fb1149
+int32_t
fb1149
+slapi_ct_memcmp( const void *p1, const void *p2, size_t n1, size_t n2)
fb1149
 {
fb1149
     int result = 0;
fb1149
     const unsigned char *_p1 = (const unsigned char *)p1;
fb1149
@@ -353,10 +353,35 @@ slapi_ct_memcmp( const void *p1, const void *p2, size_t n)
fb1149
     if (_p1 == NULL || _p2 == NULL) {
fb1149
         return 2;
fb1149
     }
fb1149
-
fb1149
-    for (size_t i = 0; i < n; i++) {
fb1149
-        if (_p1[i] ^ _p2[i]) {
fb1149
-            result = 1;
fb1149
+    if (n1 == n2) {
fb1149
+        for (size_t i = 0; i < n1; i++) {
fb1149
+            if (_p1[i] ^ _p2[i]) {
fb1149
+                result = 1;
fb1149
+            }
fb1149
+        }
fb1149
+    } else {
fb1149
+        const unsigned char *_pa;
fb1149
+        const unsigned char *_pb;
fb1149
+        size_t nl;
fb1149
+        if (n2 > n1) {
fb1149
+            _pa = _p2;
fb1149
+            _pb = _p2;
fb1149
+            nl = n2;
fb1149
+        } else {
fb1149
+            _pa = _p1;
fb1149
+            _pb = _p1;
fb1149
+            nl = n1;
fb1149
+        }
fb1149
+        /* We already fail as n1 != n2 */
fb1149
+        result = 3;
fb1149
+        for (size_t i = 0; i < nl; i++) {
fb1149
+            if (_pa[i] ^ _pb[i]) {
fb1149
+                /*
fb1149
+                 * If we don't mutate result here, dead code elimination
fb1149
+                 * we remove for loop.
fb1149
+                 */
fb1149
+                result = 4;
fb1149
+            }
fb1149
         }
fb1149
     }
fb1149
     return result;
fb1149
diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h
fb1149
index 16aa1b711..8555be939 100644
fb1149
--- a/ldap/servers/slapd/slapi-plugin.h
fb1149
+++ b/ldap/servers/slapd/slapi-plugin.h
fb1149
@@ -5856,7 +5856,7 @@ char * slapi_ch_smprintf(const char *fmt, ...)
fb1149
  * \param n length in bytes of the content of p1 AND p2.
fb1149
  * \return 0 on match. 1 on non-match. 2 on presence of NULL pointer in p1 or p2.
fb1149
  */
fb1149
-int slapi_ct_memcmp( const void *p1, const void *p2, size_t n);
fb1149
+int32_t slapi_ct_memcmp( const void *p1, const void *p2, size_t n1, size_t n2);
fb1149
 
fb1149
 /*
fb1149
  * syntax plugin routines
fb1149
-- 
fb1149
2.13.6
fb1149