From 03d0d1eba4c9f9ee92de625dee86345f20139d65 Mon Sep 17 00:00:00 2001 From: George Joseph Date: Fri, 13 Sep 2024 08:23:08 -0600 Subject: [PATCH] res_stir_shaken.c: Fix crash when stir_shaken.conf is invalid * If the call to ast_config_load() returns CONFIG_STATUS_FILEINVALID, check_for_old_config() now returns LOAD_DECLINE instead of continuing on with a bad pointer. * If CONFIG_STATUS_FILEMISSING is returned, check_for_old_config() assumes the config is being loaded from realtime and now returns LOAD_SUCCESS. If it's actually not being loaded from realtime, sorcery will catch that later on. * Also refactored the error handling in load_module() a bit. Resolves: #884 --- res/res_stir_shaken.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/res/res_stir_shaken.c b/res/res_stir_shaken.c index 4e268508bf..2405ee527f 100644 --- a/res/res_stir_shaken.c +++ b/res/res_stir_shaken.c @@ -311,13 +311,22 @@ static int check_for_old_config(void) char *cat = NULL; cfg = ast_config_load("stir_shaken.conf", config_flags); - if (cfg == NULL) { + if (cfg == CONFIG_STATUS_FILEMISSING) { /* * They may be loading from realtime so the fact that there's * no stir-shaken.conf file isn't an issue for this purpose. */ + return AST_MODULE_LOAD_SUCCESS; + } else if (cfg == CONFIG_STATUS_FILEINVALID) { + cfg = NULL; + ast_log(LOG_ERROR, "The stir_shaken.conf file is invalid\n"); return AST_MODULE_LOAD_DECLINE; + } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { + /* This can never happen but is included for completeness */ + cfg = NULL; + return AST_MODULE_LOAD_SUCCESS; } + while ((cat = ast_category_browse(cfg, cat))) { const char *val; if (strcasecmp(cat, "general") == 0) { @@ -339,13 +348,14 @@ static int load_module(void) { int res = 0; - if (check_for_old_config()) { - return AST_MODULE_LOAD_DECLINE; + res = check_for_old_config(); + if (res != AST_MODULE_LOAD_SUCCESS) { + return res; } - if (crypto_load()) { - unload_module(); - return AST_MODULE_LOAD_DECLINE; + res = crypto_load(); + if (res != AST_MODULE_LOAD_SUCCESS) { + return res; } tn_auth_list_nid = crypto_register_x509_extension(TN_AUTH_LIST_OID, @@ -355,14 +365,19 @@ static int load_module(void) return AST_MODULE_LOAD_DECLINE; } - if (common_config_load()) { + res = common_config_load(); + if (res != AST_MODULE_LOAD_SUCCESS) { unload_module(); - return AST_MODULE_LOAD_DECLINE; + return res; } - res |= ast_custom_function_register(&stir_shaken_function); + res = ast_custom_function_register(&stir_shaken_function); + if (res != 0) { + unload_module(); + return AST_MODULE_LOAD_DECLINE; + } - return res; + return AST_MODULE_LOAD_SUCCESS; } AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "STIR/SHAKEN Module for Asterisk",