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

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