Skip to content

Commit

Permalink
use local talloc ctx for modules
Browse files Browse the repository at this point in the history
because if we use the global ctx, AND it's mprotected, THEN
we can't open new connections after startup.  Because the new
connections link to the module and increase its reference count
  • Loading branch information
alandekok committed Jun 20, 2019
1 parent beca3cf commit eb84b91
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/bin/radiusd.c
Expand Up @@ -167,6 +167,7 @@ int main(int argc, char *argv[])
size_t pool_size = 0;
void *pool_page_start = NULL, *pool_page_end = NULL;
bool do_mprotect;
dl_module_loader_t *dl_modules = NULL;

/*
* Setup talloc callbacks so we get useful errors
Expand Down Expand Up @@ -456,7 +457,8 @@ int main(int argc, char *argv[])
* config file parser. Note that we pass an empty path
* here, as we haven't yet read the configuration file.
*/
if (!dl_module_loader_init(global_ctx, NULL)) {
dl_modules = dl_module_loader_init(NULL);
if (!dl_modules) {
fr_perror("%s", program);
EXIT_WITH_FAILURE;
}
Expand Down Expand Up @@ -1007,6 +1009,11 @@ int main(int argc, char *argv[])
*/
main_config_free(&config);

/*
* Free the modules that we loaded.
*/
if (dl_modules) talloc_free(dl_modules);

/*
* Cleanup everything else
*/
Expand Down
5 changes: 4 additions & 1 deletion src/bin/unit_test_attribute.c
Expand Up @@ -1450,6 +1450,7 @@ int main(int argc, char *argv[])
fr_dict_t *dict = NULL;
int ret = EXIT_SUCCESS;
TALLOC_CTX *autofree = talloc_autofree_context();
dl_module_loader_t *dl_modules = NULL;

#ifndef NDEBUG
if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0]) < 0) {
Expand Down Expand Up @@ -1524,7 +1525,8 @@ int main(int argc, char *argv[])
EXIT_WITH_FAILURE;
}

if (!dl_module_loader_init(autofree, NULL)) {
dl_modules = dl_module_loader_init(NULL);
if (!dl_modules) {
fr_perror("unit_test_attribute");
EXIT_WITH_FAILURE;
}
Expand Down Expand Up @@ -1579,6 +1581,7 @@ int main(int argc, char *argv[])
* memory, so we get clean talloc reports.
*/
cleanup:
if (dl_modules) talloc_free(dl_modules);
fr_dict_free(&dict);
xlat_free();
fr_strerror_free();
Expand Down
6 changes: 5 additions & 1 deletion src/bin/unit_test_module.c
Expand Up @@ -601,6 +601,7 @@ int main(int argc, char *argv[])

char *p;
main_config_t *config;
dl_module_loader_t *dl_modules = NULL;

config = main_config_alloc(autofree);
if (!config) {
Expand Down Expand Up @@ -743,7 +744,8 @@ int main(int argc, char *argv[])
* Initialize the DL infrastructure, which is used by the
* config file parser.
*/
if (!dl_module_loader_init(autofree, config->lib_dir)) {
dl_modules = dl_module_loader_init(config->lib_dir);
if (!dl_modules) {
fr_perror("%s", config->name);
EXIT_WITH_FAILURE;
}
Expand Down Expand Up @@ -1180,6 +1182,8 @@ int main(int argc, char *argv[])
*/
fr_dict_free(&dict);

if (dl_modules) talloc_free(dl_modules);

/*
* Now we're sure no more triggers can fire, free the
* trigger tree
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/dl_module.c
Expand Up @@ -582,7 +582,7 @@ dl_loader_t *dl_loader_from_module_loader(dl_module_loader_t *dl_module_l)
/** Initialise structures needed by the dynamic linker
*
*/
dl_module_loader_t *dl_module_loader_init(TALLOC_CTX *ctx, char const *lib_dir)
dl_module_loader_t *dl_module_loader_init(char const *lib_dir)
{
if (dl_module_loader) {
/*
Expand All @@ -595,7 +595,7 @@ dl_module_loader_t *dl_module_loader_init(TALLOC_CTX *ctx, char const *lib_dir)
return dl_module_loader;
}

dl_module_loader = talloc_zero(ctx, dl_module_loader_t);
dl_module_loader = talloc_zero(NULL, dl_module_loader_t);
if (!dl_module_loader) {
ERROR("Failed initialising uctx for dl_loader");
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/dl_module.h
Expand Up @@ -174,7 +174,7 @@ char const *dl_module_search_path(void);

dl_loader_t *dl_loader_from_module_loader(dl_module_loader_t *dl_module_loader);

dl_module_loader_t *dl_module_loader_init(TALLOC_CTX *ctx, char const *lib_dir);
dl_module_loader_t *dl_module_loader_init(char const *lib_dir);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/main_config.c
Expand Up @@ -401,7 +401,7 @@ static int lib_dir_parse(UNUSED TALLOC_CTX *ctx, UNUSED void *out, UNUSED void *
* Initialize the DL infrastructure, which is used by the
* config file parser. And also add in the search path.
*/
if (!dl_module_loader_init(NULL, main_config->lib_dir)) {
if (!dl_module_loader_init(main_config->lib_dir)) {
cf_log_err(ci, "Failed initializing 'lib_dir': %s",
fr_strerror());
return -1;
Expand Down

0 comments on commit eb84b91

Please sign in to comment.