Blame SOURCES/0024-Ticket-49436-double-free-in-COS-in-some-conditions.patch

96373c
From dcf75750dff23e848cde2ae63a0778b123de6dd7 Mon Sep 17 00:00:00 2001
96373c
From: William Brown <firstyear@redhat.com>
96373c
Date: Thu, 2 Nov 2017 13:32:41 +1000
96373c
Subject: [PATCH] Ticket 49436 - double free in COS in some conditions
96373c
96373c
Bug Description:  virtualattrs and COS have some serious memory
96373c
ownership issues. What was happening is that COS with multiple
96373c
attributes using the same sp_handle would cause a structure
96373c
to be registered twice. During shutdown we would then trigger
96373c
a double free in the process.
96373c
96373c
Fix Description:  Change the behaviour of sp_handles to use a
96373c
handle *per* attribute we register to guarantee the assocation
96373c
between them.
96373c
96373c
https://pagure.io/389-ds-base/issue/49436
96373c
96373c
Author: wibrown
96373c
96373c
Review by: mreynolds, vashirov (Thanks!)
96373c
96373c
(cherry pick from commit ee4428a3f5d2d8e37a7107c7dce9d622fc17d41c)
96373c
---
96373c
 dirsrvtests/tests/suites/cos/indirect_cos_test.py | 43 +++++++----------------
96373c
 ldap/servers/plugins/cos/cos_cache.c              | 32 +++++++++--------
96373c
 ldap/servers/plugins/roles/roles_cache.c          |  8 ++---
96373c
 ldap/servers/slapd/vattr.c                        | 28 +++++++++------
96373c
 4 files changed, 51 insertions(+), 60 deletions(-)
96373c
96373c
diff --git a/dirsrvtests/tests/suites/cos/indirect_cos_test.py b/dirsrvtests/tests/suites/cos/indirect_cos_test.py
96373c
index 1aac6b8ed..452edcdf8 100644
96373c
--- a/dirsrvtests/tests/suites/cos/indirect_cos_test.py
96373c
+++ b/dirsrvtests/tests/suites/cos/indirect_cos_test.py
96373c
@@ -7,6 +7,7 @@ import subprocess
96373c
 
96373c
 from lib389 import Entry
96373c
 from lib389.idm.user import UserAccounts
96373c
+from lib389.idm.domain import Domain
96373c
 from lib389.topologies import topology_st as topo
96373c
 from lib389._constants import (DEFAULT_SUFFIX, DN_DM, PASSWORD, HOST_STANDALONE,
96373c
                                SERVERID_STANDALONE, PORT_STANDALONE)
96373c
@@ -48,14 +49,8 @@ def check_user(inst):
96373c
 def setup_subtree_policy(topo):
96373c
     """Set up subtree password policy
96373c
     """
96373c
-    try:
96373c
-        topo.standalone.modify_s("cn=config", [(ldap.MOD_REPLACE,
96373c
-                                                'nsslapd-pwpolicy-local',
96373c
-                                                'on')])
96373c
-    except ldap.LDAPError as e:
96373c
-        log.error('Failed to set fine-grained policy: error {}'.format(
96373c
-            e.message['desc']))
96373c
-        raise e
96373c
+
96373c
+    topo.standalone.config.set('nsslapd-pwpolicy-local', 'on')
96373c
 
96373c
     log.info('Create password policy for subtree {}'.format(OU_PEOPLE))
96373c
     try:
96373c
@@ -68,15 +63,9 @@ def setup_subtree_policy(topo):
96373c
             OU_PEOPLE, e.message['desc']))
96373c
         raise e
96373c
 
96373c
-    log.info('Add pwdpolicysubentry attribute to {}'.format(OU_PEOPLE))
96373c
-    try:
96373c
-        topo.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_REPLACE,
96373c
-                                                   'pwdpolicysubentry',
96373c
-                                                   PW_POLICY_CONT_PEOPLE2)])
96373c
-    except ldap.LDAPError as e:
96373c
-        log.error('Failed to pwdpolicysubentry pw policy '
96373c
-                  'policy for {}: error {}'.format(OU_PEOPLE, e.message['desc']))
96373c
-        raise e
96373c
+    domain = Domain(topo.standalone, DEFAULT_SUFFIX)
96373c
+    domain.replace('pwdpolicysubentry', PW_POLICY_CONT_PEOPLE2)
96373c
+
96373c
     time.sleep(1)
96373c
 
96373c
 
96373c
@@ -116,12 +105,9 @@ def setup(topo, request):
96373c
     """
96373c
     log.info('Add custom schema...')
96373c
     try:
96373c
-        ATTR_1 = ("( 1.3.6.1.4.1.409.389.2.189 NAME 'x-department' " +
96373c
-                  "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
96373c
-        ATTR_2 = ("( 1.3.6.1.4.1.409.389.2.187 NAME 'x-en-ou' " +
96373c
-                  "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
96373c
-        OC = ("( xPerson-oid NAME 'xPerson' DESC '' SUP person STRUCTURAL MAY " +
96373c
-              "( x-department $ x-en-ou ) X-ORIGIN 'user defined' )")
96373c
+        ATTR_1 = (b"( 1.3.6.1.4.1.409.389.2.189 NAME 'x-department' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
96373c
+        ATTR_2 = (b"( 1.3.6.1.4.1.409.389.2.187 NAME 'x-en-ou' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 X-ORIGIN 'user defined' )")
96373c
+        OC = (b"( xPerson-oid NAME 'xPerson' DESC '' SUP person STRUCTURAL MAY ( x-department $ x-en-ou ) X-ORIGIN 'user defined' )")
96373c
         topo.standalone.modify_s("cn=schema", [(ldap.MOD_ADD, 'attributeTypes', ATTR_1),
96373c
                                                (ldap.MOD_ADD, 'attributeTypes', ATTR_2),
96373c
                                                (ldap.MOD_ADD, 'objectClasses', OC)])
96373c
@@ -142,14 +128,9 @@ def setup(topo, request):
96373c
         'homeDirectory': '/home/test_user',
96373c
         'seeAlso': 'cn=cosTemplate,dc=example,dc=com'
96373c
     }
96373c
-    users.create(properties=user_properties)
96373c
-    try:
96373c
-        topo.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_ADD,
96373c
-                                                 'objectclass',
96373c
-                                                 'xPerson')])
96373c
-    except ldap.LDAPError as e:
96373c
-        log.fatal('Failed to add objectclass to user')
96373c
-        raise e
96373c
+    user = users.create(properties=user_properties)
96373c
+
96373c
+    user.add('objectClass', 'xPerson')
96373c
 
96373c
     # Setup COS
96373c
     log.info("Setup indirect COS...")
96373c
diff --git a/ldap/servers/plugins/cos/cos_cache.c b/ldap/servers/plugins/cos/cos_cache.c
96373c
index 9ae15db15..662dace35 100644
96373c
--- a/ldap/servers/plugins/cos/cos_cache.c
96373c
+++ b/ldap/servers/plugins/cos/cos_cache.c
96373c
@@ -109,9 +109,6 @@ void *cos_get_plugin_identity(void);
96373c
 #define COSTYPE_INDIRECT 3
96373c
 #define COS_DEF_ERROR_NO_TEMPLATES -2
96373c
 
96373c
-/* the global plugin handle */
96373c
-static volatile vattr_sp_handle *vattr_handle = NULL;
96373c
-
96373c
 /* both variables are protected by change_lock */
96373c
 static int cos_cache_notify_flag = 0;
96373c
 static PRBool cos_cache_at_work = PR_FALSE;
96373c
@@ -323,16 +320,6 @@ cos_cache_init(void)
96373c
         views_api = 0;
96373c
     }
96373c
 
96373c
-    if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
96373c
-                                cos_cache_vattr_get,
96373c
-                                cos_cache_vattr_compare,
96373c
-                                cos_cache_vattr_types) != 0) {
96373c
-        slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM,
96373c
-                      "cos_cache_init - Cannot register as service provider\n");
96373c
-        ret = -1;
96373c
-        goto out;
96373c
-    }
96373c
-
96373c
     if (PR_CreateThread(PR_USER_THREAD,
96373c
                         cos_cache_wait_on_change,
96373c
                         NULL,
96373c
@@ -860,8 +847,23 @@ cos_dn_defs_cb(Slapi_Entry *e, void *callback_data)
96373c
                                           dnVals[valIndex]->bv_val);
96373c
                 }
96373c
 
96373c
-                slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle,
96373c
-                                       dnVals[valIndex]->bv_val, NULL, NULL);
96373c
+                /*
96373c
+                 * Each SP_handle is associated to one and only one vattr.
96373c
+                 * We could consider making this a single function rather
96373c
+                 * than the double-call.
96373c
+                 */
96373c
+
96373c
+                vattr_sp_handle *vattr_handle = NULL;
96373c
+
96373c
+                if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
96373c
+                                            cos_cache_vattr_get,
96373c
+                                            cos_cache_vattr_compare,
96373c
+                                            cos_cache_vattr_types) != 0) {
96373c
+                    slapi_log_err(SLAPI_LOG_ERR, COS_PLUGIN_SUBSYSTEM, "cos_cache_init - Cannot register as service provider for %s\n", dnVals[valIndex]->bv_val);
96373c
+                } else {
96373c
+                    slapi_vattrspi_regattr((vattr_sp_handle *)vattr_handle, dnVals[valIndex]->bv_val, NULL, NULL);
96373c
+                }
96373c
+
96373c
             } /* if(attrType is cosAttribute) */
96373c
 
96373c
             /*
96373c
diff --git a/ldap/servers/plugins/roles/roles_cache.c b/ldap/servers/plugins/roles/roles_cache.c
96373c
index 59f5a6081..1e5865af8 100644
96373c
--- a/ldap/servers/plugins/roles/roles_cache.c
96373c
+++ b/ldap/servers/plugins/roles/roles_cache.c
96373c
@@ -47,9 +47,6 @@ static char *allUserAttributes[] = {
96373c
 /* views scoping */
96373c
 static void **views_api;
96373c
 
96373c
-/* Service provider handler */
96373c
-static vattr_sp_handle *vattr_handle = NULL;
96373c
-
96373c
 /* List of nested roles */
96373c
 typedef struct _role_object_nested
96373c
 {
96373c
@@ -224,6 +221,10 @@ roles_cache_init()
96373c
       so that we update the corresponding cache */
96373c
     slapi_register_backend_state_change(NULL, roles_cache_trigger_update_suffix);
96373c
 
96373c
+    /* Service provider handler - only used once! and freed by vattr! */
96373c
+    vattr_sp_handle *vattr_handle = NULL;
96373c
+
96373c
+
96373c
     if (slapi_vattrspi_register((vattr_sp_handle **)&vattr_handle,
96373c
                                 roles_sp_get_value,
96373c
                                 roles_sp_compare_value,
96373c
@@ -622,7 +623,6 @@ roles_cache_stop()
96373c
         current_role = next_role;
96373c
     }
96373c
     slapi_rwlock_unlock(global_lock);
96373c
-    slapi_ch_free((void **)&vattr_handle);
96373c
     roles_list = NULL;
96373c
 
96373c
     slapi_log_err(SLAPI_LOG_PLUGIN, ROLES_PLUGIN_SUBSYSTEM, "<-- roles_cache_stop\n");
96373c
diff --git a/ldap/servers/slapd/vattr.c b/ldap/servers/slapd/vattr.c
96373c
index 82deb41fe..432946c79 100644
96373c
--- a/ldap/servers/slapd/vattr.c
96373c
+++ b/ldap/servers/slapd/vattr.c
96373c
@@ -1864,7 +1864,12 @@ vattr_map_create(void)
96373c
 void
96373c
 vattr_map_entry_free(vattr_map_entry *vae)
96373c
 {
96373c
-    slapi_ch_free((void **)&(vae->sp_list));
96373c
+    vattr_sp_handle *list_entry = vae->sp_list;
96373c
+    while (list_entry != NULL) {
96373c
+        vattr_sp_handle *next_entry = list_entry->next;
96373c
+        slapi_ch_free((void **)&list_entry);
96373c
+        list_entry = next_entry;
96373c
+    }
96373c
     slapi_ch_free_string(&(vae->type_name));
96373c
     slapi_ch_free((void **)&vae;;
96373c
 }
96373c
@@ -2143,16 +2148,9 @@ slapi_vattr_schema_check_type(Slapi_Entry *e, char *type)
96373c
 vattr_map_entry *
96373c
 vattr_map_entry_new(char *type_name, vattr_sp_handle *sph, void *hint)
96373c
 {
96373c
-    vattr_map_entry *result = NULL;
96373c
-    vattr_sp_handle *sp_copy = NULL;
96373c
-
96373c
-    sp_copy = (vattr_sp_handle *)slapi_ch_calloc(1, sizeof(vattr_sp_handle));
96373c
-    sp_copy->sp = sph->sp;
96373c
-    sp_copy->hint = hint;
96373c
-
96373c
-    result = (vattr_map_entry *)slapi_ch_calloc(1, sizeof(vattr_map_entry));
96373c
+    vattr_map_entry *result = (vattr_map_entry *)slapi_ch_calloc(1, sizeof(vattr_map_entry));
96373c
     result->type_name = slapi_ch_strdup(type_name);
96373c
-    result->sp_list = sp_copy;
96373c
+    result->sp_list = sph;
96373c
 
96373c
     /* go get schema */
96373c
     result->objectclasses = vattr_map_entry_build_schema(type_name);
96373c
@@ -2273,6 +2271,16 @@ we'd need to hold a lock on the read path, which we don't want to do.
96373c
 So any SP which relinquishes its need to handle a type needs to continue
96373c
 to handle the calls on it, but return nothing */
96373c
 /* DBDB need to sort out memory ownership here, it's not quite right */
96373c
+/*
96373c
+ * This function was inconsistent. We would allocated and "kind of",
96373c
+ * copy the sp_handle here for the vattr_map_entry_new path. But we
96373c
+ * would "take ownership" for the existing entry and the list addition
96373c
+ * path. Instead now, EVERY sp_handle we take, we take ownership of
96373c
+ * and the CALLER must allocate a new one each time.
96373c
+ *
96373c
+ * Better idea, is that regattr should just take the fn pointers
96373c
+ * and callers never *see* the sp_handle structure at all.
96373c
+ */
96373c
 
96373c
 int
96373c
 vattr_map_sp_insert(char *type_to_add, vattr_sp_handle *sp, void *hint)
96373c
-- 
96373c
2.13.6
96373c