Blame SOURCES/0024-Ticket-47937-Crash-in-entry_add_present_values_wsi_m.patch

f92ce9
From 0fb6ca5fe49774a646e36fd7acd59638cc57041c Mon Sep 17 00:00:00 2001
f92ce9
From: Mark Reynolds <mreynolds@redhat.com>
f92ce9
Date: Fri, 24 Oct 2014 14:14:25 -0400
f92ce9
Subject: [PATCH 24/28] Ticket 47937 - Crash in
f92ce9
 entry_add_present_values_wsi_multi_valued
f92ce9
f92ce9
Bug Description:  If the DNA plugin uses an invlid attribute for "dnaType"
f92ce9
                  this can lead to crash when DNA attempts to update this
f92ce9
                  attribute.
f92ce9
f92ce9
Fix Description:  In the DNA plugin, verify that the attribute exists.
f92ce9
                  Also in entrywsi.c, pass in the type from the attribute
f92ce9
                  struct(which does do some basic normalization).
f92ce9
f92ce9
https://fedorahosted.org/389/ticket/47937
f92ce9
f92ce9
Reviewed by: nhosoi(Thanks!)
f92ce9
f92ce9
(cherry picked from commit 3cdf0eb571d120573f2cda2c140fd2b4215c94fa)
f92ce9
(cherry picked from commit 738d985dc6f52660e13d94a118271ce288a75ea6)
f92ce9
---
f92ce9
 dirsrvtests/tickets/ticket47937_test.py | 237 ++++++++++++++++++++++++++++++++
f92ce9
 ldap/servers/plugins/dna/dna.c          |   8 ++
f92ce9
 ldap/servers/slapd/entrywsi.c           |   5 +-
f92ce9
 3 files changed, 248 insertions(+), 2 deletions(-)
f92ce9
 create mode 100644 dirsrvtests/tickets/ticket47937_test.py
f92ce9
f92ce9
diff --git a/dirsrvtests/tickets/ticket47937_test.py b/dirsrvtests/tickets/ticket47937_test.py
f92ce9
new file mode 100644
f92ce9
index 0000000..09ee714
f92ce9
--- /dev/null
f92ce9
+++ b/dirsrvtests/tickets/ticket47937_test.py
f92ce9
@@ -0,0 +1,237 @@
f92ce9
+import os
f92ce9
+import sys
f92ce9
+import time
f92ce9
+import ldap
f92ce9
+import logging
f92ce9
+import socket
f92ce9
+import pytest
f92ce9
+from lib389 import DirSrv, Entry, tools
f92ce9
+from lib389.tools import DirSrvTools
f92ce9
+from lib389._constants import *
f92ce9
+from lib389.properties import *
f92ce9
+from constants import *
f92ce9
+
f92ce9
+log = logging.getLogger(__name__)
f92ce9
+
f92ce9
+installation_prefix = None
f92ce9
+
f92ce9
+
f92ce9
+class TopologyStandalone(object):
f92ce9
+    def __init__(self, standalone):
f92ce9
+        standalone.open()
f92ce9
+        self.standalone = standalone
f92ce9
+
f92ce9
+
f92ce9
+@pytest.fixture(scope="module")
f92ce9
+def topology(request):
f92ce9
+    '''
f92ce9
+        This fixture is used to standalone topology for the 'module'.
f92ce9
+        At the beginning, It may exists a standalone instance.
f92ce9
+        It may also exists a backup for the standalone instance.
f92ce9
+
f92ce9
+        Principle:
f92ce9
+            If standalone instance exists:
f92ce9
+                restart it
f92ce9
+            If backup of standalone exists:
f92ce9
+                create/rebind to standalone
f92ce9
+
f92ce9
+                restore standalone instance from backup
f92ce9
+            else:
f92ce9
+                Cleanup everything
f92ce9
+                    remove instance
f92ce9
+                    remove backup
f92ce9
+                Create instance
f92ce9
+                Create backup
f92ce9
+    '''
f92ce9
+    global installation_prefix
f92ce9
+
f92ce9
+    if installation_prefix:
f92ce9
+        args_instance[SER_DEPLOYED_DIR] = installation_prefix
f92ce9
+
f92ce9
+    standalone = DirSrv(verbose=False)
f92ce9
+
f92ce9
+    # Args for the standalone instance
f92ce9
+    args_instance[SER_HOST] = HOST_STANDALONE
f92ce9
+    args_instance[SER_PORT] = PORT_STANDALONE
f92ce9
+    args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
f92ce9
+    args_standalone = args_instance.copy()
f92ce9
+    standalone.allocate(args_standalone)
f92ce9
+
f92ce9
+    # Get the status of the backups
f92ce9
+    backup_standalone = standalone.checkBackupFS()
f92ce9
+
f92ce9
+    # Get the status of the instance and restart it if it exists
f92ce9
+    instance_standalone = standalone.exists()
f92ce9
+    if instance_standalone:
f92ce9
+        # assuming the instance is already stopped, just wait 5 sec max
f92ce9
+        standalone.stop(timeout=5)
f92ce9
+        standalone.start(timeout=10)
f92ce9
+
f92ce9
+    if backup_standalone:
f92ce9
+        # The backup exist, assuming it is correct
f92ce9
+        # we just re-init the instance with it
f92ce9
+        if not instance_standalone:
f92ce9
+            standalone.create()
f92ce9
+            # Used to retrieve configuration information (dbdir, confdir...)
f92ce9
+            standalone.open()
f92ce9
+
f92ce9
+        # restore standalone instance from backup
f92ce9
+        standalone.stop(timeout=10)
f92ce9
+        standalone.restoreFS(backup_standalone)
f92ce9
+        standalone.start(timeout=10)
f92ce9
+
f92ce9
+    else:
f92ce9
+        # We should be here only in two conditions
f92ce9
+        #      - This is the first time a test involve standalone instance
f92ce9
+        #      - Something weird happened (instance/backup destroyed)
f92ce9
+        #        so we discard everything and recreate all
f92ce9
+
f92ce9
+        # Remove the backup. So even if we have a specific backup file
f92ce9
+        # (e.g backup_standalone) we clear backup that an instance may have created
f92ce9
+        if backup_standalone:
f92ce9
+            standalone.clearBackupFS()
f92ce9
+
f92ce9
+        # Remove the instance
f92ce9
+        if instance_standalone:
f92ce9
+            standalone.delete()
f92ce9
+
f92ce9
+        # Create the instance
f92ce9
+        standalone.create()
f92ce9
+
f92ce9
+        # Used to retrieve configuration information (dbdir, confdir...)
f92ce9
+        standalone.open()
f92ce9
+
f92ce9
+        # Time to create the backups
f92ce9
+        standalone.stop(timeout=10)
f92ce9
+        standalone.backupfile = standalone.backupFS()
f92ce9
+        standalone.start(timeout=10)
f92ce9
+
f92ce9
+    # clear the tmp directory
f92ce9
+    standalone.clearTmpDir(__file__)
f92ce9
+
f92ce9
+    #
f92ce9
+    # Here we have standalone instance up and running
f92ce9
+    # Either coming from a backup recovery
f92ce9
+    # or from a fresh (re)init
f92ce9
+    # Time to return the topology
f92ce9
+    return TopologyStandalone(standalone)
f92ce9
+
f92ce9
+
f92ce9
+def test_ticket47937(topology):
f92ce9
+    """
f92ce9
+        Test that DNA plugin only accepts valid attributes for "dnaType"
f92ce9
+    """
f92ce9
+
f92ce9
+    log.info("Creating \"ou=people\"...")
f92ce9
+    try:
f92ce9
+        topology.standalone.add_s(Entry(('ou=people,' + SUFFIX, {
f92ce9
+                                         'objectclass': 'top organizationalunit'.split(),
f92ce9
+                                         'ou': 'people'
f92ce9
+                                         })))
f92ce9
+
f92ce9
+    except ldap.ALREADY_EXISTS:
f92ce9
+        pass
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.error('Failed to add ou=people org unit: error ' + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Creating \"ou=ranges\"...")
f92ce9
+    try:
f92ce9
+        topology.standalone.add_s(Entry(('ou=ranges,' + SUFFIX, {
f92ce9
+                                         'objectclass': 'top organizationalunit'.split(),
f92ce9
+                                         'ou': 'ranges'
f92ce9
+                                         })))
f92ce9
+
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.error('Failed to add ou=ranges org unit: error ' + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Creating \"cn=entry\"...")
f92ce9
+    try:
f92ce9
+        topology.standalone.add_s(Entry(('cn=entry,ou=people,' + SUFFIX, {
f92ce9
+                                         'objectclass': 'top groupofuniquenames'.split(),
f92ce9
+                                         'cn': 'entry'
f92ce9
+                                         })))
f92ce9
+
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.error('Failed to add test entry: error ' + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Creating DNA shared config entry...")
f92ce9
+    try:
f92ce9
+        topology.standalone.add_s(Entry(('dnaHostname=localhost.localdomain+dnaPortNum=389,ou=ranges,%s' % SUFFIX, {
f92ce9
+                                         'objectclass': 'top dnaSharedConfig'.split(),
f92ce9
+                                         'dnaHostname': 'localhost.localdomain',
f92ce9
+                                         'dnaPortNum': '389',
f92ce9
+                                         'dnaSecurePortNum': '636',
f92ce9
+                                         'dnaRemainingValues': '9501'
f92ce9
+                                         })))
f92ce9
+
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.error('Failed to add shared config entry: error ' + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Add dna plugin config entry...")
f92ce9
+    try:
f92ce9
+        topology.standalone.add_s(Entry(('cn=dna config,cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config', {
f92ce9
+                                         'objectclass': 'top dnaPluginConfig'.split(),
f92ce9
+                                         'dnaType': 'description',
f92ce9
+                                         'dnaMaxValue': '10000',
f92ce9
+                                         'dnaMagicRegen': '0',
f92ce9
+                                         'dnaFilter': '(objectclass=top)',
f92ce9
+                                         'dnaScope': 'ou=people,%s' % SUFFIX,
f92ce9
+                                         'dnaNextValue': '500',
f92ce9
+                                         'dnaSharedCfgDN': 'ou=ranges,%s' % SUFFIX
f92ce9
+                                         })))
f92ce9
+
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.error('Failed to add DNA config entry: error ' + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Enable the DNA plugin...")
f92ce9
+    try:
f92ce9
+        topology.standalone.plugins.enable(name=PLUGIN_DNA)
f92ce9
+    except e:
f92ce9
+        log.error("Failed to enable DNA Plugin: error " + e.message['desc'])
f92ce9
+        assert False
f92ce9
+
f92ce9
+    log.info("Restarting the server...")
f92ce9
+    topology.standalone.stop(timeout=120)
f92ce9
+    time.sleep(1)
f92ce9
+    topology.standalone.start(timeout=120)
f92ce9
+    time.sleep(3)
f92ce9
+
f92ce9
+    log.info("Apply an invalid attribute to the DNA config(dnaType: foo)...")
f92ce9
+
f92ce9
+    try:
f92ce9
+        topology.standalone.modify_s('cn=dna config,cn=Distributed Numeric Assignment Plugin,cn=plugins,cn=config',
f92ce9
+                                     [(ldap.MOD_REPLACE, 'dnaType', 'foo')])
f92ce9
+    except ldap.LDAPError, e:
f92ce9
+        log.info('Operation failed as expected (error: %s)' % e.message['desc'])
f92ce9
+    else:
f92ce9
+        log.error('Operation incorectly succeeded!  Test Failed!')
f92ce9
+        assert False
f92ce9
+
f92ce9
+    topology.standalone.log.info('Test 47937 Passed.')
f92ce9
+
f92ce9
+
f92ce9
+def test_ticket47937_final(topology):
f92ce9
+    topology.standalone.stop(timeout=10)
f92ce9
+
f92ce9
+
f92ce9
+def run_isolated():
f92ce9
+    '''
f92ce9
+        run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
f92ce9
+        To run isolated without py.test, you need to
f92ce9
+            - edit this file and comment '@pytest.fixture' line before 'topology' function.
f92ce9
+            - set the installation prefix
f92ce9
+            - run this program
f92ce9
+    '''
f92ce9
+    global installation_prefix
f92ce9
+    installation_prefix = None
f92ce9
+
f92ce9
+    topo = topology(True)
f92ce9
+    test_ticket47937(topo)
f92ce9
+
f92ce9
+if __name__ == '__main__':
f92ce9
+    run_isolated()
f92ce9
\ No newline at end of file
f92ce9
diff --git a/ldap/servers/plugins/dna/dna.c b/ldap/servers/plugins/dna/dna.c
f92ce9
index bc280a4..75edca8 100644
f92ce9
--- a/ldap/servers/plugins/dna/dna.c
f92ce9
+++ b/ldap/servers/plugins/dna/dna.c
f92ce9
@@ -936,6 +936,14 @@ dna_parse_config_entry(Slapi_PBlock *pb, Slapi_Entry * e, int apply)
f92ce9
     }
f92ce9
 
f92ce9
     for (i = 0; entry->types && entry->types[i]; i++) {
f92ce9
+        if (!slapi_attr_syntax_exists(entry->types[i])){
f92ce9
+            slapi_log_error(SLAPI_LOG_FATAL, DNA_PLUGIN_SUBSYSTEM,
f92ce9
+                            "dna_parse_config_entry: dnaType (%s) does "
f92ce9
+                            "not exist.\n",
f92ce9
+                            entry->types[i]);
f92ce9
+            ret = DNA_FAILURE;
f92ce9
+            goto bail;
f92ce9
+        }
f92ce9
         slapi_log_error(SLAPI_LOG_CONFIG, DNA_PLUGIN_SUBSYSTEM,
f92ce9
                         "----------> %s [%s]\n", DNA_TYPE, entry->types[i]);
f92ce9
     }
f92ce9
diff --git a/ldap/servers/slapd/entrywsi.c b/ldap/servers/slapd/entrywsi.c
f92ce9
index 7039bfc..41afe1a 100644
f92ce9
--- a/ldap/servers/slapd/entrywsi.c
f92ce9
+++ b/ldap/servers/slapd/entrywsi.c
f92ce9
@@ -423,6 +423,7 @@ static void resolve_attribute_state_present_to_deleted(Slapi_Entry *e, Slapi_Att
f92ce9
 static void resolve_attribute_state_to_present_or_deleted(Slapi_Entry *e, Slapi_Attr *a, Slapi_Value **valuestoupdate, int attribute_state);
f92ce9
 static int entry_add_present_values_wsi_single_valued(Slapi_Entry *e, const char *type, struct berval **bervals, const CSN *csn, int urp, long flags);
f92ce9
 static int entry_add_present_values_wsi_multi_valued(Slapi_Entry *e, const char *type, struct berval **bervals, const CSN *csn, int urp, long flags);
f92ce9
+
f92ce9
 static int
f92ce9
 entry_add_present_values_wsi(Slapi_Entry *e, const char *type, struct berval **bervals, const CSN *csn, int urp, long flags)
f92ce9
 {
f92ce9
@@ -439,11 +440,11 @@ entry_add_present_values_wsi(Slapi_Entry *e, const char *type, struct berval **b
f92ce9
 
f92ce9
 	if(slapi_attr_flag_is_set(a,SLAPI_ATTR_FLAG_SINGLE))
f92ce9
 	{
f92ce9
-		retVal = entry_add_present_values_wsi_single_valued( e, type, bervals, csn, urp, 0 );
f92ce9
+		retVal = entry_add_present_values_wsi_single_valued( e, a->a_type, bervals, csn, urp, 0 );
f92ce9
 	}
f92ce9
 	else
f92ce9
 	{
f92ce9
-		retVal = entry_add_present_values_wsi_multi_valued( e, type, bervals, csn, urp, 0 );
f92ce9
+		retVal = entry_add_present_values_wsi_multi_valued( e, a->a_type, bervals, csn, urp, 0 );
f92ce9
 	}
f92ce9
 	return retVal;
f92ce9
 }
f92ce9
-- 
f92ce9
1.9.3
f92ce9