amoralej / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 years ago
Clone

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

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