From 8783199e115f7abb1433e3f47fd199d82687152f Mon Sep 17 00:00:00 2001 From: Scott Griepentrog Date: Mon, 16 Dec 2013 15:53:59 +0000 Subject: [PATCH] pbx.c: put copy of ast_exten.data on stack to prevent memory corruption MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit During dialplan execution in pbx_extension_helper(), the contexts global read lock prevents link list corruption, but was released with a pointer to the ast_exten and data later used in variable substitution. Instead, this patch removes pbx_substitute_variables() and locates a copy of the ast_exten data on the stack before releasing the lock, where ast_exten could get free'd by another thread performing a module reload. (issue AST-1179) Reported by: Thomas Arimont (issue AST-1246) Reported by: Alexander Hömig Review: https://reviewboard.asterisk.org/r/3055/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.8@403862 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/pbx.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/main/pbx.c b/main/pbx.c index ba9834031e..996ac2597e 100644 --- a/main/pbx.c +++ b/main/pbx.c @@ -4394,25 +4394,6 @@ void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, pbx_substitute_variables_helper_full(NULL, headp, cp1, cp2, count, &used); } -static void pbx_substitute_variables(char *passdata, int datalen, struct ast_channel *c, struct ast_exten *e) -{ - const char *tmp; - - /* Nothing more to do */ - if (!e->data) { - *passdata = '\0'; - return; - } - - /* No variables or expressions in e->data, so why scan it? */ - if ((!(tmp = strchr(e->data, '$'))) || (!strstr(tmp, "${") && !strstr(tmp, "$["))) { - ast_copy_string(passdata, e->data, datalen); - return; - } - - pbx_substitute_variables_helper(c, e->data, passdata, datalen - 1); -} - /*! * \brief The return value depends on the action: * @@ -4437,6 +4418,7 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con, { struct ast_exten *e; struct ast_app *app; + char *substitute = NULL; int res; struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */ char passdata[EXT_DATA_SIZE]; @@ -4462,6 +4444,18 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con, if (!e->cached_app) e->cached_app = pbx_findapp(e->app); app = e->cached_app; + if (ast_strlen_zero(e->data)) { + *passdata = '\0'; + } else { + const char *tmp; + if ((!(tmp = strchr(e->data, '$'))) || (!strstr(tmp, "${") && !strstr(tmp, "$["))) { + /* no variables to substitute, copy on through */ + ast_copy_string(passdata, e->data, sizeof(passdata)); + } else { + /* save e->data on stack for later processing after lock released */ + substitute = ast_strdupa(e->data); + } + } ast_unlock_contexts(); if (!app) { ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority); @@ -4472,7 +4466,9 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con, if (c->exten != exten) ast_copy_string(c->exten, exten, sizeof(c->exten)); c->priority = priority; - pbx_substitute_variables(passdata, sizeof(passdata), c, e); + if (substitute) { + pbx_substitute_variables_helper(c, substitute, passdata, sizeof(passdata)-1); + } #ifdef CHANNEL_TRACE ast_channel_trace_update(c); #endif -- 2.47.3