]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-8537: Passing nil to various lua functions causes segfault
authorMark Lipscombe <mlipscombe@gmail.com>
Sat, 14 Nov 2015 23:40:20 +0000 (10:40 +1100)
committerMichael Jerris <mike@jerris.com>
Mon, 23 Nov 2015 18:36:45 +0000 (13:36 -0500)
Various functions exposed via lua do not check their parameters for null
causing freeswitch to segfault.

This change adds checking for null parameters and returns an error
instead of segfaulting.

src/mod/languages/mod_lua/freeswitch_lua.cpp
src/switch_core.c
src/switch_cpp.cpp
src/switch_utils.c

index 593ff8a6c40201a252f01206c465508a2b6878e1..0f2071aec0c174d7c68dbd190e8ba17a4037aa35 100644 (file)
@@ -88,6 +88,11 @@ void Session::setLUA(lua_State * state)
 
 int Session::originate(CoreSession *a_leg_session, char *dest, int timeout)
 {
+       if (zstr(dest)) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing destination.\n");
+               return 0;
+       }
+
        int x = CoreSession::originate(a_leg_session, dest, timeout);
 
        if (x) {
@@ -356,7 +361,7 @@ Dbh::Dbh(char *dsn, char *user, char *pass)
                dsn = tmp;
        }
 
-       if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) == SWITCH_STATUS_SUCCESS) {
+       if (!zstr(dsn) && switch_cache_db_get_db_handle_dsn(&dbh, dsn) == SWITCH_STATUS_SUCCESS) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DBH handle %p Connected.\n", (void *) dbh);
        } else {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Connection failed.  DBH NOT Connected.\n");
@@ -391,8 +396,12 @@ bool Dbh::connected()
 bool Dbh::test_reactive(char *test_sql, char *drop_sql, char *reactive_sql)
 {
   if (dbh) {
-    if (switch_cache_db_test_reactive(dbh, test_sql, drop_sql, reactive_sql) == SWITCH_TRUE) {
-      return true;
+    if (!zstr(test_sql) && !zstr(reactive_sql)) {
+      if (switch_cache_db_test_reactive(dbh, test_sql, drop_sql, reactive_sql) == SWITCH_TRUE) {
+        return true;
+      }
+    } else {
+      switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing parameters.\n");
     }
   } else {
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
@@ -431,6 +440,11 @@ int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
 
 bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
 {
+  if (zstr(sql)) {
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing SQL query.\n");
+    return false;
+  }
+
   if (dbh) {
     if (lua_fun.L) {
       if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) {
@@ -459,6 +473,11 @@ int Dbh::affected_rows()
 
 int Dbh::load_extension(const char *extension)
 {
+  if (zstr(extension)) {
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing extension name.\n");
+    return 0;
+  }
+
   if (dbh) {
     return switch_cache_db_load_extension(dbh, extension);
   }
index 79b5711cddd7eecf4ceba7a0f1cb9ffd4bffdfa3..9d359e85507d9b073286e459584e1a71c1c9d445 100644 (file)
@@ -392,11 +392,13 @@ SWITCH_DECLARE(char *) switch_core_get_variable_dup(const char *varname)
 {
        char *val = NULL, *v;
 
-       switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
-       if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
-               val = strdup(v);
+       if (varname) {
+               switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
+               if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
+                       val = strdup(v);
+               }
+               switch_thread_rwlock_unlock(runtime.global_var_rwlock);
        }
-       switch_thread_rwlock_unlock(runtime.global_var_rwlock);
 
        return val;
 }
@@ -405,11 +407,13 @@ SWITCH_DECLARE(char *) switch_core_get_variable_pdup(const char *varname, switch
 {
        char *val = NULL, *v;
 
-       switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
-       if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
-               val = switch_core_strdup(pool, v);
+       if (varname) {
+               switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
+               if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
+                       val = switch_core_strdup(pool, v);
+               }
+               switch_thread_rwlock_unlock(runtime.global_var_rwlock);
        }
-       switch_thread_rwlock_unlock(runtime.global_var_rwlock);
 
        return val;
 }
index f861b80908c1722d6f64c001be18fd78d3de0397..f92ed54ea3cb01f98553e0b7b8855cc82f6faaec 100644 (file)
@@ -266,17 +266,24 @@ SWITCH_DECLARE(const char *) API::executeString(const char *cmd)
 
        this_check("");
 
-       mycmd = strdup(cmd);
+       SWITCH_STANDARD_STREAM(stream);
+
+       if (zstr(cmd)) {
+               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No application specified\n");
+               stream.write_function(&stream, "-ERR No application specified");
+       } else {
+               mycmd = strdup(cmd);
 
-       switch_assert(mycmd);
+               switch_assert(mycmd);
+
+               if ((arg = strchr(mycmd, ' '))) {
+                       *arg++ = '\0';
+               }
 
-       if ((arg = strchr(mycmd, ' '))) {
-               *arg++ = '\0';
+               switch_api_execute(mycmd, arg, session, &stream);
+               switch_safe_free(mycmd);
        }
 
-       SWITCH_STANDARD_STREAM(stream);
-       switch_api_execute(mycmd, arg, session, &stream);
-       switch_safe_free(mycmd);
        return (char *) stream.data;
 }
 
@@ -425,6 +432,11 @@ SWITCH_DECLARE(const char *)Event::getHeader(const char *header_name)
 {
        this_check("");
 
+    if (zstr(header_name)) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Trying to getHeader an invalid header!\n");
+               return NULL;
+    }
+
        if (event) {
                return switch_event_get_header(event, header_name);
        } else {
@@ -450,6 +462,11 @@ SWITCH_DECLARE(bool) Event::delHeader(const char *header_name)
 {
        this_check(false);
 
+       if (zstr(header_name)) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Trying to delHeader an invalid header!\n");
+               return false;
+       }
+
        if (event) {
                return switch_event_del_header(event, header_name) == SWITCH_STATUS_SUCCESS ? true : false;
        } else {
index 1e14fb17ea822ee135edcedc5181f68b43a41b10..83498385273dc01fa11cc2be52c3df77764a6841 100644 (file)
@@ -775,6 +775,11 @@ SWITCH_DECLARE(switch_bool_t) switch_simple_email(const char *to,
        switch_bool_t rval = SWITCH_FALSE;
        const char *err = NULL;
 
+       if (zstr(to)) {
+               err = "No to address specified";
+               goto end;
+       }
+
        if (!zstr(file) && !zstr(convert_cmd) && !zstr(convert_ext)) {
                if ((ext = strrchr(file, '.'))) {
                        dupfile = strdup(file);