Blame SOURCES/0044-Ticket-49246-ns-slapd-crashes-in-role-cache-creation.patch

61f723
From 18491418e661b5dc1b9ca4c6bb4adb85bfb0bf0d Mon Sep 17 00:00:00 2001
61f723
From: Mark Reynolds <mreynolds@redhat.com>
61f723
Date: Tue, 9 May 2017 16:31:52 -0400
61f723
Subject: [PATCH] Ticket 49246 - ns-slapd crashes in role cache creation
61f723
61f723
Bug Description:  Using a nested filter for a filtered role can
61f723
                  cause a crash.  This was due to the way the filter
61f723
                  was being checked by the roles plugin.
61f723
61f723
Fix Description:  Properly resurse over a filter.
61f723
61f723
https://pagure.io/389-ds-base/issue/49246
61f723
61f723
Reviewed by: firstyear & tbordaz(Thanks!!)
61f723
61f723
(cherry picked from commit 54e4fca35899550e0c25b25e7f7c756302d258ce)
61f723
---
61f723
 dirsrvtests/tests/tickets/ticket49122_test.py | 61 ++++++++++++++++++---------
61f723
 ldap/servers/plugins/roles/roles_cache.c      | 34 +++++++++++----
61f723
 2 files changed, 66 insertions(+), 29 deletions(-)
61f723
61f723
diff --git a/dirsrvtests/tests/tickets/ticket49122_test.py b/dirsrvtests/tests/tickets/ticket49122_test.py
61f723
index ff1e8d1..0945122 100644
61f723
--- a/dirsrvtests/tests/tickets/ticket49122_test.py
61f723
+++ b/dirsrvtests/tests/tickets/ticket49122_test.py
61f723
@@ -2,8 +2,7 @@ import time
61f723
 import ldap
61f723
 import logging
61f723
 import pytest
61f723
-from lib389 import DirSrv, Entry, tools, tasks
61f723
-from lib389.tools import DirSrvTools
61f723
+from lib389 import Entry
61f723
 from lib389._constants import *
61f723
 from lib389.properties import *
61f723
 from lib389.tasks import *
61f723
@@ -19,6 +18,15 @@ log = logging.getLogger(__name__)
61f723
 
61f723
 USER_DN = 'uid=user,' + DEFAULT_SUFFIX
61f723
 ROLE_DN = 'cn=Filtered_Role_That_Includes_Empty_Role,' + DEFAULT_SUFFIX
61f723
+filters = ['nsrole=cn=empty,dc=example,dc=com',
61f723
+           '(nsrole=cn=empty,dc=example,dc=com)',
61f723
+           '(&(nsrole=cn=empty,dc=example,dc=com))',
61f723
+           '(!(nsrole=cn=empty,dc=example,dc=com))',
61f723
+           '(&(|(objectclass=person)(sn=app*))(userpassword=*))',
61f723
+           '(&(|(objectclass=person)(nsrole=cn=empty,dc=example,dc=com))(userpassword=*))',
61f723
+           '(&(|(nsrole=cn=empty,dc=example,dc=com)(sn=app*))(userpassword=*))',
61f723
+           '(&(|(objectclass=person)(sn=app*))(nsrole=cn=empty,dc=example,dc=com))',
61f723
+           '(&(|(&(cn=*)(objectclass=person)(nsrole=cn=empty,dc=example,dc=com)))(uid=*))']
61f723
 
61f723
 
61f723
 def test_ticket49122(topo):
61f723
@@ -29,18 +37,6 @@ def test_ticket49122(topo):
61f723
     topo.standalone.plugins.enable(name=PLUGIN_ROLES)
61f723
     topo.standalone.restart()
61f723
 
61f723
-    # Add invalid role
61f723
-    try:
61f723
-        topo.standalone.add_s(Entry((
61f723
-            ROLE_DN, {'objectclass': ['top', 'ldapsubentry', 'nsroledefinition',
61f723
-                                      'nscomplexroledefinition', 'nsfilteredroledefinition'],
61f723
-                      'cn': 'Filtered_Role_That_Includes_Empty_Role',
61f723
-                      'nsRoleFilter': '(!(nsrole=cn=This_Is_An_Empty_Managed_NsRoleDefinition,dc=example,dc=com))',
61f723
-                      'description': 'A filtered role with filter that will crash the server'})))
61f723
-    except ldap.LDAPError as e:
61f723
-        topo.standalone.log.fatal('Failed to add filtered role: error ' + e.message['desc'])
61f723
-        assert False
61f723
-
61f723
     # Add test user
61f723
     try:
61f723
         topo.standalone.add_s(Entry((
61f723
@@ -51,16 +47,39 @@ def test_ticket49122(topo):
61f723
         assert False
61f723
 
61f723
     if DEBUGGING:
61f723
-        # Add debugging steps(if any)...
61f723
         print("Attach gdb")
61f723
         time.sleep(20)
61f723
 
61f723
-    # Search for the role
61f723
-    try:
61f723
-        topo.standalone.search_s(USER_DN, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nsrole'])
61f723
-    except ldap.LDAPError as e:
61f723
-        topo.standalone.log.fatal('Search failed: error ' + str(e))
61f723
-        assert False
61f723
+    # Loop over filters
61f723
+    for role_filter in filters:
61f723
+        log.info('Testing filter: ' + role_filter)
61f723
+
61f723
+        # Add invalid role
61f723
+        try:
61f723
+            topo.standalone.add_s(Entry((
61f723
+                ROLE_DN, {'objectclass': ['top', 'ldapsubentry', 'nsroledefinition',
61f723
+                                          'nscomplexroledefinition', 'nsfilteredroledefinition'],
61f723
+                          'cn': 'Filtered_Role_That_Includes_Empty_Role',
61f723
+                          'nsRoleFilter': role_filter,
61f723
+                          'description': 'A filtered role with filter that will crash the server'})))
61f723
+        except ldap.LDAPError as e:
61f723
+            topo.standalone.log.fatal('Failed to add filtered role: error ' + e.message['desc'])
61f723
+            assert False
61f723
+
61f723
+        # Search for the role
61f723
+        try:
61f723
+            topo.standalone.search_s(USER_DN, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nsrole'])
61f723
+        except ldap.LDAPError as e:
61f723
+            topo.standalone.log.fatal('Search failed: error ' + str(e))
61f723
+            assert False
61f723
+
61f723
+        # Cleanup
61f723
+        try:
61f723
+            topo.standalone.delete_s(ROLE_DN)
61f723
+        except ldap.LDAPError as e:
61f723
+            topo.standalone.log.fatal('delete failed: error ' + str(e))
61f723
+            assert False
61f723
+        time.sleep(1)
61f723
 
61f723
     topo.standalone.log.info('Test Passed')
61f723
 
61f723
diff --git a/ldap/servers/plugins/roles/roles_cache.c b/ldap/servers/plugins/roles/roles_cache.c
61f723
index 4f27c4c..3697eaa 100644
61f723
--- a/ldap/servers/plugins/roles/roles_cache.c
61f723
+++ b/ldap/servers/plugins/roles/roles_cache.c
61f723
@@ -1073,20 +1073,38 @@ static int roles_cache_create_role_under(roles_cache_def** roles_cache_suffix, S
61f723
 }
61f723
 
61f723
 /*
61f723
- * Check that we are not using nsrole in the filter
61f723
+ * Check that we are not using nsrole in the filter, recurse over all the
61f723
+ * nested filters.
61f723
  */
61f723
 static int roles_check_filter(Slapi_Filter *filter_list)
61f723
 {
61f723
 	Slapi_Filter  *f;
61f723
 	char *type = NULL;
61f723
 
61f723
-	for ( f = slapi_filter_list_first( filter_list );
61f723
-	          f != NULL;
61f723
-	          f = slapi_filter_list_next( filter_list, f ) )
61f723
-	{
61f723
-		slapi_filter_get_attribute_type(f, &type);
61f723
-		if (strcasecmp(type, NSROLEATTR) == 0){
61f723
-			return -1;
61f723
+	f = slapi_filter_list_first( filter_list );
61f723
+	if (f == NULL){
61f723
+		/* Single filter */
61f723
+		if (slapi_filter_get_attribute_type(filter_list, &type) == 0){
61f723
+			if (strcasecmp(type, NSROLEATTR) == 0){
61f723
+				return -1;
61f723
+			}
61f723
+		}
61f723
+	}
61f723
+	for ( ; f != NULL; f = slapi_filter_list_next(filter_list, f) ){
61f723
+		/* Complex filter */
61f723
+		if (slapi_filter_list_first(f)) {
61f723
+			/* Another filter list - recurse */
61f723
+			if (roles_check_filter(f) == -1){
61f723
+				/* Done, break out */
61f723
+				return -1;
61f723
+			}
61f723
+		} else {
61f723
+			/* Not a filter list, so check the type */
61f723
+			if (slapi_filter_get_attribute_type(f, &type) == 0){
61f723
+				if (strcasecmp(type, NSROLEATTR) == 0){
61f723
+					return -1;
61f723
+				}
61f723
+			}
61f723
 		}
61f723
 	}
61f723
 
61f723
-- 
61f723
2.9.4
61f723