]> granicus.if.org Git - p11-kit/commitdiff
Safer initialization of individually initialized module.
authorStef Walter <stefw@collabora.co.uk>
Sun, 14 Aug 2011 16:45:19 +0000 (18:45 +0200)
committerStef Walter <stefw@collabora.co.uk>
Sun, 14 Aug 2011 16:48:47 +0000 (18:48 +0200)
 * More checks for out of memory.
 * Take more of the same code paths when initializing a single
   module as when initializing registered, or loading from file.
 * Cleanup halfway initialized globals if fail during init.

p11-kit/modules.c

index 9771e6b227459da6f5ce95b9df0608156e817ad2..693d342523f1405ad70038a8438d47e49f42dfbb 100644 (file)
@@ -971,21 +971,37 @@ p11_kit_initialize_module (CK_FUNCTION_LIST_PTR module)
                        if (mod == NULL) {
                                debug ("allocating new module");
                                allocated = mod = alloc_module_unlocked ();
-                               mod->funcs = module;
+                               if (mod == NULL)
+                                       rv = CKR_HOST_MEMORY;
+                               else
+                                       mod->funcs = module;
                        }
 
-                       /* WARNING: Reentrancy can occur here */
-                       rv = initialize_module_unlocked_reentrant (mod);
-
                        /* If this was newly allocated, add it to the list */
                        if (rv == CKR_OK && allocated) {
-                               hash_set (gl.modules, allocated->funcs, allocated);
-                               allocated = NULL;
+                               if (hash_set (gl.modules, allocated->funcs, allocated))
+                                       allocated = NULL;
+                               else
+                                       rv = CKR_HOST_MEMORY;
+                       }
+
+                       if (rv == CKR_OK) {
+
+                               /* WARNING: Reentrancy can occur here */
+                               rv = initialize_module_unlocked_reentrant (mod);
                        }
 
                        free (allocated);
                }
 
+               /*
+                * If initialization failed, we may need to cleanup.
+                * If we added this module above, then this will
+                * clean things up as expected.
+                */
+               if (rv != CKR_OK)
+                       free_modules_when_no_refs_unlocked ();
+
                _p11_kit_default_message (rv);
 
        _p11_unlock ();
@@ -1109,6 +1125,14 @@ p11_kit_load_initialize_module (const char *module_path,
                if (rv == CKR_OK && module)
                        *module = mod->funcs;
 
+               /*
+                * If initialization failed, we may need to cleanup.
+                * If we added this module above, then this will
+                * clean things up as expected.
+                */
+               if (rv != CKR_OK)
+                       free_modules_when_no_refs_unlocked ();
+
                _p11_kit_default_message (rv);
 
        _p11_unlock ();