Blame SOURCES/0005-Ticket-49890-ldapsearch-with-server-side-sort-crashe.patch

31affc
From a21ba4722268349b9c63000145e5d119e1fddd60 Mon Sep 17 00:00:00 2001
92192e
From: Mark Reynolds <mreynolds@redhat.com>
92192e
Date: Thu, 9 Aug 2018 15:27:59 -0400
92192e
Subject: [PATCH] Ticket 49890 : ldapsearch with server side sort crashes the 
92192e
 ldap server
92192e
92192e
Bug Description:
92192e
        Server side sort with a specified matching rule trigger a crash
92192e
92192e
Fix Description:
92192e
        Check if the we are able to index the provided value.
92192e
        If we are not then slapd_qsort returns an error (LDAP_OPERATION_ERROR)
92192e
92192e
https://pagure.io/389-ds-base/issue/49890
92192e
92192e
Reviewed by: mreynolds
92192e
92192e
Platforms tested: F27
92192e
92192e
Flag Day: no
92192e
92192e
Doc impact: no
92192e
92192e
(cherry picked from commit c989e18f7a3da060b16d39919b920b6b2a19a0ac)
92192e
---
92192e
 dirsrvtests/tests/suites/syntax/mr_test.py | 59 ++++++++++++++++++++++
92192e
 ldap/servers/slapd/back-ldbm/sort.c        | 14 +++++
92192e
 2 files changed, 73 insertions(+)
92192e
 create mode 100644 dirsrvtests/tests/suites/syntax/mr_test.py
92192e
92192e
diff --git a/dirsrvtests/tests/suites/syntax/mr_test.py b/dirsrvtests/tests/suites/syntax/mr_test.py
92192e
new file mode 100644
92192e
index 000000000..57061222a
92192e
--- /dev/null
92192e
+++ b/dirsrvtests/tests/suites/syntax/mr_test.py
92192e
@@ -0,0 +1,59 @@
92192e
+import logging
92192e
+import pytest
92192e
+import os
92192e
+import ldap
92192e
+from lib389.dbgen import dbgen
92192e
+from lib389._constants import *
92192e
+from lib389.topologies import topology_st as topo
92192e
+from lib389._controls import SSSRequestControl
92192e
+
92192e
+DEBUGGING = os.getenv("DEBUGGING", default=False)
92192e
+if DEBUGGING:
92192e
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
92192e
+else:
92192e
+    logging.getLogger(__name__).setLevel(logging.INFO)
92192e
+log = logging.getLogger(__name__)
92192e
+
92192e
+
92192e
+def test_sss_mr(topo):
92192e
+    """Test matching rule/server side sort does not crash DS
92192e
+
92192e
+    :id: 48c73d76-1694-420f-ab55-187135f2d260
92192e
+    :setup: Standalone Instance
92192e
+    :steps:
92192e
+        1. Add sample entries to the database
92192e
+        2. Perform search using server side control (uid:2.5.13.3)
92192e
+    :expectedresults:
92192e
+        1. Success
92192e
+        2. Success
92192e
+    """
92192e
+
92192e
+    log.info("Creating LDIF...")
92192e
+    ldif_dir = topo.standalone.get_ldif_dir()
92192e
+    ldif_file = os.path.join(ldif_dir, 'mr-crash.ldif')
92192e
+    dbgen(topo.standalone, 5, ldif_file, DEFAULT_SUFFIX)
92192e
+
92192e
+    log.info("Importing LDIF...")
92192e
+    topo.standalone.stop()
92192e
+    assert topo.standalone.ldif2db(DEFAULT_BENAME, None, None, None, ldif_file)
92192e
+    topo.standalone.start()
92192e
+
92192e
+    log.info('Search using server side sorting using undefined mr in the attr...')
92192e
+    sort_ctrl = SSSRequestControl(True, ['uid:2.5.13.3'])
92192e
+    controls = [sort_ctrl]
92192e
+    msg_id = topo.standalone.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE,
92192e
+                                        "objectclass=*", serverctrls=controls)
92192e
+    try:
92192e
+        rtype, rdata, rmsgid, response_ctrl = topo.standalone.result3(msg_id)
92192e
+    except ldap.OPERATIONS_ERROR:
92192e
+        pass
92192e
+
92192e
+    log.info("Test PASSED")
92192e
+
92192e
+
92192e
+if __name__ == '__main__':
92192e
+    # Run isolated
92192e
+    # -s for DEBUG mode
92192e
+    CURRENT_FILE = os.path.realpath(__file__)
92192e
+    pytest.main(["-s", CURRENT_FILE])
92192e
+
92192e
diff --git a/ldap/servers/slapd/back-ldbm/sort.c b/ldap/servers/slapd/back-ldbm/sort.c
92192e
index 5b84d87f3..70ac60803 100644
92192e
--- a/ldap/servers/slapd/back-ldbm/sort.c
92192e
+++ b/ldap/servers/slapd/back-ldbm/sort.c
92192e
@@ -546,6 +546,16 @@ compare_entries_sv(ID *id_a, ID *id_b, sort_spec *s, baggage_carrier *bc, int *e
92192e
             /* Now copy it, so the second call doesn't crap on it */
92192e
             value_a = slapi_ch_bvecdup(temp_value); /* Really, we'd prefer to not call the chXXX variant...*/
92192e
             matchrule_values_to_keys(this_one->mr_pb, actual_value_b, &value_b);
92192e
+
92192e
+            if ((actual_value_a && !value_a) ||
92192e
+                (actual_value_b && !value_b)) {
92192e
+                ber_bvecfree(actual_value_a);
92192e
+                ber_bvecfree(actual_value_b);
92192e
+                CACHE_RETURN(&inst->inst_cache, &a);
92192e
+                CACHE_RETURN(&inst->inst_cache, &b);
92192e
+                *error = 1;
92192e
+                return 0;
92192e
+            }
92192e
             if (actual_value_a)
92192e
                 ber_bvecfree(actual_value_a);
92192e
             if (actual_value_b)
92192e
@@ -717,6 +727,8 @@ recurse:
92192e
                A[i] >= A[lo] for higuy <= i <= hi */
92192e
 
92192e
             do {
92192e
+                if (error)
92192e
+                    return LDAP_OPERATIONS_ERROR;
92192e
                 loguy++;
92192e
             } while (loguy <= hi && compare_entries_sv(loguy, lo, s, bc, &error) <= 0);
92192e
 
92192e
@@ -724,6 +736,8 @@ recurse:
92192e
                either loguy > hi or A[loguy] > A[lo] */
92192e
 
92192e
             do {
92192e
+                if (error)
92192e
+                    return LDAP_OPERATIONS_ERROR;
92192e
                 higuy--;
92192e
             } while (higuy > lo && compare_entries_sv(higuy, lo, s, bc, &error) >= 0);
92192e
 
92192e
-- 
92192e
2.17.1
92192e