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

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