<p>Corey Farrell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/7518">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">loader: Minor fix to module registration.<br><br>This protects the module loader itself against crashing if dlopen is<br>called on a module from outside loader.c.<br><br>* Expand scope of lock inside ast_module_register to include reading of<br> resource_being_loaded.<br>* NULL check resource_being_loaded.<br>* Set resource_being_loaded NULL as soon as dlopen returns. This fixes<br> some error paths where it was not NULL'ed.<br>* Create module_destroy function to deduplicate code from<br> ast_module_unregister and modules_shutdown.<br><br>Change-Id: I40f07a315e55b92df4fc7faf525ed6d4f396e7d2<br>---<br>M main/loader.c<br>1 file changed, 52 insertions(+), 34 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/18/7518/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/main/loader.c b/main/loader.c<br>index 49151cc..3bd7735 100644<br>--- a/main/loader.c<br>+++ b/main/loader.c<br>@@ -167,17 +167,39 @@<br> <br> static AST_DLLIST_HEAD_STATIC(reload_queue, reload_queue_item);<br> <br>-/* when dynamic modules are being loaded, ast_module_register() will<br>- need to know what filename the module was loaded from while it<br>- is being registered<br>-*/<br>+/*!<br>+ * \internal<br>+ *<br>+ * This variable is set by load_dynamic_module so ast_module_register<br>+ * can know what pointer is being registered.<br>+ *<br>+ * This is protected by the module_list lock.<br>+ */<br> static struct ast_module *resource_being_loaded;<br> <br>-/* XXX: should we check for duplicate resource names here? */<br>-<br>+/*!<br>+ * \internal<br>+ * \brief Used by AST_MODULE_INFO to register with the module loader.<br>+ *<br>+ * This function is automatically called when each module is opened.<br>+ * It must never be used from outside AST_MODULE_INFO.<br>+ */<br> void ast_module_register(const struct ast_module_info *info)<br> {<br>- struct ast_module *mod = resource_being_loaded;<br>+ struct ast_module *mod;<br>+<br>+ /*<br>+ * This lock protects resource_being_loaded as well as the module<br>+ * list. Normally we already have a lock on module_list when we<br>+ * begin the load but locking again from here prevents corruption<br>+ * if an asterisk module is dlopen'ed from outside the module loader.<br>+ */<br>+ AST_DLLIST_LOCK(&module_list);<br>+ mod = resource_being_loaded;<br>+ if (!mod) {<br>+ AST_DLLIST_UNLOCK(&module_list);<br>+ return;<br>+ }<br> <br> ast_debug(5, "Registering module %s\n", info->name);<br> <br>@@ -187,23 +209,19 @@<br> }<br> AST_LIST_HEAD_INIT(&mod->users);<br> <br>- /* during startup, before the loader has been initialized,<br>- there are no threads, so there is no need to take the lock<br>- on this list to manipulate it. it is also possible that it<br>- might be unsafe to use the list lock at that point... so<br>- let's avoid it altogether<br>- */<br>- AST_DLLIST_LOCK(&module_list);<br>- /* it is paramount that the new entry be placed at the tail of<br>- the list, otherwise the code that uses dlopen() to load<br>- dynamic modules won't be able to find out if the module it<br>- just opened was registered or failed to load<br>- */<br>+ /* The module must be placed at the tail or load_dynamic_module will fail. */<br> AST_DLLIST_INSERT_TAIL(&module_list, mod, entry);<br> AST_DLLIST_UNLOCK(&module_list);<br> <br> /* give the module a copy of its own handle, for later use in registrations and the like */<br> *((struct ast_module **) &(info->self)) = mod;<br>+}<br>+<br>+static void module_destroy(struct ast_module *mod)<br>+{<br>+ AST_LIST_HEAD_DESTROY(&mod->users);<br>+ ao2_cleanup(mod->ref_debug);<br>+ ast_free(mod);<br> }<br> <br> void ast_module_unregister(const struct ast_module_info *info)<br>@@ -226,9 +244,7 @@<br> <br> if (mod) {<br> ast_debug(5, "Unregistering module %s\n", info->name);<br>- AST_LIST_HEAD_DESTROY(&mod->users);<br>- ao2_cleanup(mod->ref_debug);<br>- ast_free(mod);<br>+ module_destroy(mod);<br> }<br> }<br> <br>@@ -511,6 +527,8 @@<br> int space; /* room needed for the descriptor */<br> int missing_so = 0;<br> <br>+ ast_assert(!resource_being_loaded);<br>+<br> space = sizeof(*resource_being_loaded) + strlen(resource_in) + 1;<br> if (strcasecmp(resource_in + strlen(resource_in) - 3, ".so")) {<br> missing_so = 1;<br>@@ -523,18 +541,20 @@<br> any symbols, and don't export any symbols. this will allow us to peek into<br> the module's info block (if available) to see what flags it has set */<br> <br>- resource_being_loaded = ast_calloc(1, space);<br>+ mod = resource_being_loaded = ast_calloc(1, space);<br> if (!resource_being_loaded)<br> return NULL;<br> strcpy(resource_being_loaded->resource, resource_in);<br> if (missing_so)<br> strcat(resource_being_loaded->resource, ".so");<br> <br>- if (!(lib = dlopen(fn, RTLD_LAZY | RTLD_GLOBAL))) {<br>+ lib = dlopen(fn, RTLD_LAZY | RTLD_GLOBAL);<br>+ resource_being_loaded = NULL;<br>+ if (!lib) {<br> if (!suppress_logging) {<br> ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());<br> }<br>- ast_free(resource_being_loaded);<br>+ ast_free(mod);<br> return NULL;<br> }<br> <br>@@ -545,7 +565,7 @@<br> constructor) places the new module at the tail of the<br> module_list<br> */<br>- if (resource_being_loaded != (mod = AST_DLLIST_LAST(&module_list))) {<br>+ if (mod != AST_DLLIST_LAST(&module_list)) {<br> ast_log(LOG_WARNING, "Module '%s' did not register itself during load\n", resource_in);<br> /* no, it did not, so close it and return */<br> logged_dlclose(resource_in, lib);<br>@@ -564,19 +584,20 @@<br> }<br> <br> logged_dlclose(resource_in, lib);<br>- resource_being_loaded = NULL;<br> <br> /* start the load process again */<br>- resource_being_loaded = ast_calloc(1, space);<br>+ mod = resource_being_loaded = ast_calloc(1, space);<br> if (!resource_being_loaded)<br> return NULL;<br> strcpy(resource_being_loaded->resource, resource_in);<br> if (missing_so)<br> strcat(resource_being_loaded->resource, ".so");<br> <br>- if (!(lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL))) {<br>+ lib = dlopen(fn, wants_global ? RTLD_LAZY | RTLD_GLOBAL : RTLD_NOW | RTLD_LOCAL);<br>+ resource_being_loaded = NULL;<br>+ if (!lib) {<br> ast_log(LOG_WARNING, "Error loading module '%s': %s\n", resource_in, dlerror());<br>- ast_free(resource_being_loaded);<br>+ ast_free(mod);<br> return NULL;<br> }<br> <br>@@ -585,7 +606,6 @@<br> time too :) */<br> <br> AST_DLLIST_LAST(&module_list)->lib = lib;<br>- resource_being_loaded = NULL;<br> <br> return AST_DLLIST_LAST(&module_list);<br> }<br>@@ -621,9 +641,7 @@<br> ast_verb(1, "Unloading %s\n", mod->resource);<br> mod->info->unload();<br> }<br>- AST_LIST_HEAD_DESTROY(&mod->users);<br>- ao2_cleanup(mod->ref_debug);<br>- ast_free(mod);<br>+ module_destroy(mod);<br> somethingchanged = 1;<br> }<br> AST_DLLIST_TRAVERSE_BACKWARDS_SAFE_END;<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/7518">change 7518</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/7518"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I40f07a315e55b92df4fc7faf525ed6d4f396e7d2 </div>
<div style="display:none"> Gerrit-Change-Number: 7518 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>