Blame SOURCES/0024-Issue-4817-BUG-locked-crypt-accounts-on-import-may-a.patch

b8da0b
From 2f0218f91d35c83a2aaecb71849a54b2481390ab Mon Sep 17 00:00:00 2001
b8da0b
From: Firstyear <william@blackhats.net.au>
b8da0b
Date: Fri, 9 Jul 2021 11:53:35 +1000
b8da0b
Subject: [PATCH] Issue 4817 - BUG - locked crypt accounts on import may allow
b8da0b
 all passwords (#4819)
b8da0b
b8da0b
Bug Description: Due to mishanding of short dbpwd hashes, the
b8da0b
crypt_r algorithm was misused and was only comparing salts
b8da0b
in some cases, rather than checking the actual content
b8da0b
of the password.
b8da0b
b8da0b
Fix Description: Stricter checks on dbpwd lengths to ensure
b8da0b
that content passed to crypt_r has at least 2 salt bytes and
b8da0b
1 hash byte, as well as stricter checks on ct_memcmp to ensure
b8da0b
that compared values are the same length, rather than potentially
b8da0b
allowing overruns/short comparisons.
b8da0b
b8da0b
fixes: https://github.com/389ds/389-ds-base/issues/4817
b8da0b
b8da0b
Author: William Brown <william@blackhats.net.au>
b8da0b
b8da0b
Review by: @mreynolds389
b8da0b
---
b8da0b
 .../password/pwd_crypt_asterisk_test.py       | 50 +++++++++++++++++++
b8da0b
 ldap/servers/plugins/pwdstorage/crypt_pwd.c   | 20 +++++---
b8da0b
 2 files changed, 64 insertions(+), 6 deletions(-)
b8da0b
 create mode 100644 dirsrvtests/tests/suites/password/pwd_crypt_asterisk_test.py
b8da0b
b8da0b
diff --git a/dirsrvtests/tests/suites/password/pwd_crypt_asterisk_test.py b/dirsrvtests/tests/suites/password/pwd_crypt_asterisk_test.py
b8da0b
new file mode 100644
b8da0b
index 000000000..d76614db1
b8da0b
--- /dev/null
b8da0b
+++ b/dirsrvtests/tests/suites/password/pwd_crypt_asterisk_test.py
b8da0b
@@ -0,0 +1,50 @@
b8da0b
+# --- BEGIN COPYRIGHT BLOCK ---
b8da0b
+# Copyright (C) 2021 William Brown <william@blackhats.net.au>
b8da0b
+# All rights reserved.
b8da0b
+#
b8da0b
+# License: GPL (version 3 or any later version).
b8da0b
+# See LICENSE for details.
b8da0b
+# --- END COPYRIGHT BLOCK ---
b8da0b
+#
b8da0b
+import ldap
b8da0b
+import pytest
b8da0b
+from lib389.topologies import topology_st
b8da0b
+from lib389.idm.user import UserAccounts
b8da0b
+from lib389._constants import (DEFAULT_SUFFIX, PASSWORD)
b8da0b
+
b8da0b
+pytestmark = pytest.mark.tier1
b8da0b
+
b8da0b
+def test_password_crypt_asterisk_is_rejected(topology_st):
b8da0b
+    """It was reported that {CRYPT}* was allowing all passwords to be
b8da0b
+    valid in the bind process. This checks that we should be rejecting
b8da0b
+    these as they should represent locked accounts. Similar, {CRYPT}!
b8da0b
+
b8da0b
+    :id: 0b8f1a6a-f3eb-4443-985e-da14d0939dc3
b8da0b
+    :setup: Single instance
b8da0b
+    :steps: 1. Set a password hash in with CRYPT and the content *
b8da0b
+            2. Test a bind
b8da0b
+            3. Set a password hash in with CRYPT and the content !
b8da0b
+            4. Test a bind
b8da0b
+    :expectedresults:
b8da0b
+            1. Successfully set the values
b8da0b
+            2. The bind fails
b8da0b
+            3. Successfully set the values
b8da0b
+            4. The bind fails
b8da0b
+    """
b8da0b
+    topology_st.standalone.config.set('nsslapd-allow-hashed-passwords', 'on')
b8da0b
+    topology_st.standalone.config.set('nsslapd-enable-upgrade-hash', 'off')
b8da0b
+
b8da0b
+    users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
b8da0b
+    user = users.create_test_user()
b8da0b
+
b8da0b
+    user.set('userPassword', "{CRYPT}*")
b8da0b
+
b8da0b
+    # Attempt to bind with incorrect password.
b8da0b
+    with pytest.raises(ldap.INVALID_CREDENTIALS):
b8da0b
+        badconn = user.bind('badpassword')
b8da0b
+
b8da0b
+    user.set('userPassword', "{CRYPT}!")
b8da0b
+    # Attempt to bind with incorrect password.
b8da0b
+    with pytest.raises(ldap.INVALID_CREDENTIALS):
b8da0b
+        badconn = user.bind('badpassword')
b8da0b
+
b8da0b
diff --git a/ldap/servers/plugins/pwdstorage/crypt_pwd.c b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
b8da0b
index 9031b2199..1b37d41ed 100644
b8da0b
--- a/ldap/servers/plugins/pwdstorage/crypt_pwd.c
b8da0b
+++ b/ldap/servers/plugins/pwdstorage/crypt_pwd.c
b8da0b
@@ -48,15 +48,23 @@ static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
b8da0b
 int
b8da0b
 crypt_pw_cmp(const char *userpwd, const char *dbpwd)
b8da0b
 {
b8da0b
-    int rc;
b8da0b
-    char *cp;
b8da0b
+    int rc = -1;
b8da0b
+    char *cp = NULL;
b8da0b
+    size_t dbpwd_len = strlen(dbpwd);
b8da0b
     struct crypt_data data;
b8da0b
     data.initialized = 0;
b8da0b
 
b8da0b
-    /* we use salt (first 2 chars) of encoded password in call to crypt_r() */
b8da0b
-    cp = crypt_r(userpwd, dbpwd, &data);
b8da0b
-    if (cp) {
b8da0b
-        rc = slapi_ct_memcmp(dbpwd, cp, strlen(dbpwd));
b8da0b
+    /*
b8da0b
+     * there MUST be at least 2 chars of salt and some pw bytes, else this is INVALID and will
b8da0b
+     * allow any password to bind as we then only compare SALTS.
b8da0b
+     */
b8da0b
+    if (dbpwd_len >= 3) {
b8da0b
+        /* we use salt (first 2 chars) of encoded password in call to crypt_r() */
b8da0b
+        cp = crypt_r(userpwd, dbpwd, &data);
b8da0b
+    }
b8da0b
+    /* If these are not the same length, we can not proceed safely with memcmp. */
b8da0b
+    if (cp && dbpwd_len == strlen(cp)) {
b8da0b
+        rc = slapi_ct_memcmp(dbpwd, cp, dbpwd_len);
b8da0b
     } else {
b8da0b
         rc = -1;
b8da0b
     }
b8da0b
-- 
b8da0b
2.31.1
b8da0b