double free if plugin load failed

This commit is contained in:
Olof hagsand 2017-09-09 17:42:08 +02:00
parent ef0fce10d5
commit 4d82d4f6ea
2 changed files with 38 additions and 38 deletions

View file

@ -115,8 +115,8 @@ struct plugin {
/* /*
* Local variables * Local variables
*/ */
static int nplugins = 0; static int _nplugins = 0;
static struct plugin *plugins = NULL; static struct plugin *_plugins = NULL;
/*! Find a plugin by name and return the dlsym handl /*! Find a plugin by name and return the dlsym handl
* Used by libclicon code to find callback funcctions in plugins. * Used by libclicon code to find callback funcctions in plugins.
@ -132,8 +132,8 @@ config_find_plugin(clicon_handle h,
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++){ for (i = 0; i < _nplugins; i++){
p = &plugins[i]; p = &_plugins[i];
if (strcmp(p->p_name, name) == 0) if (strcmp(p->p_name, name) == 0)
return p->p_handle; return p->p_handle;
} }
@ -260,8 +260,8 @@ plugin_reset_state(clicon_handle h,
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_reset) { if (p->p_reset) {
clicon_debug(1, "Calling plugin_reset() for %s\n", clicon_debug(1, "Calling plugin_reset() for %s\n",
p->p_name); p->p_name);
@ -290,8 +290,8 @@ plugin_start_hooks(clicon_handle h,
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_start) { if (p->p_start) {
optind = 0; optind = 0;
if (((p->p_start)(h, argc, argv)) < 0) { if (((p->p_start)(h, argc, argv)) < 0) {
@ -314,15 +314,15 @@ plugin_append(struct plugin *p)
{ {
struct plugin *new; struct plugin *new;
if ((new = realloc(plugins, (nplugins+1) * sizeof (*p))) == NULL) { if ((new = realloc(_plugins, (_nplugins+1) * sizeof (*p))) == NULL) {
clicon_err(OE_UNIX, errno, "realloc"); clicon_err(OE_UNIX, errno, "realloc");
return -1; return -1;
} }
memset (&new[nplugins], 0, sizeof(new[nplugins])); memset (&new[_nplugins], 0, sizeof(new[_nplugins]));
memcpy (&new[nplugins], p, sizeof(new[nplugins])); memcpy (&new[_nplugins], p, sizeof(new[_nplugins]));
plugins = new; _plugins = new;
nplugins++; _nplugins++;
return 0; return 0;
} }
@ -400,14 +400,15 @@ backend_plugin_load_dir(clicon_handle h,
quit: quit:
if (retval != 0) { if (retval != 0) {
/* XXX p is always NULL */ /* XXX p is always NULL */
if (plugins) { if (_plugins) {
while (--np >= 0){ while (--np >= 0){
if ((p = &plugins[np]) == NULL) if ((p = &_plugins[np]) == NULL)
continue; continue;
backend_plugin_unload(h, p); backend_plugin_unload(h, p);
free(p); free(p);
} }
free(plugins); free(_plugins);
_plugins=0;
} }
} }
if (dp) if (dp)
@ -452,15 +453,15 @@ plugin_finish(clicon_handle h)
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
backend_plugin_unload(h, p); backend_plugin_unload(h, p);
} }
if (plugins){ if (_plugins){
free(plugins); free(_plugins);
plugins = NULL; _plugins = NULL;
} }
nplugins = 0; _nplugins = 0;
return 0; return 0;
} }
@ -516,8 +517,8 @@ plugin_transaction_begin(clicon_handle h,
int retval = 0; int retval = 0;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_begin) if (p->p_trans_begin)
if ((retval = (p->p_trans_begin)(h, (transaction_data)td)) < 0){ if ((retval = (p->p_trans_begin)(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */ if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
@ -546,8 +547,8 @@ plugin_transaction_validate(clicon_handle h,
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++){ for (i = 0; i < _nplugins; i++){
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_validate) if (p->p_trans_validate)
if ((retval = (p->p_trans_validate)(h, (transaction_data)td)) < 0){ if ((retval = (p->p_trans_validate)(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */ if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
@ -576,8 +577,8 @@ plugin_transaction_complete(clicon_handle h,
int retval = 0; int retval = 0;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++){ for (i = 0; i < _nplugins; i++){
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_complete) if (p->p_trans_complete)
if ((retval = (p->p_trans_complete)(h, (transaction_data)td)) < 0){ if ((retval = (p->p_trans_complete)(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */ if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
@ -623,7 +624,7 @@ plugin_transaction_revert(clicon_handle h,
tr.td_tcvec = td->td_scvec; tr.td_tcvec = td->td_scvec;
for (i = nr-1; i>=0; i--){ for (i = nr-1; i>=0; i--){
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_commit) if (p->p_trans_commit)
if ((p->p_trans_commit)(h, (transaction_data)&tr) < 0){ if ((p->p_trans_commit)(h, (transaction_data)&tr) < 0){
clicon_log(LOG_NOTICE, "Plugin '%s' %s revert callback failed", clicon_log(LOG_NOTICE, "Plugin '%s' %s revert callback failed",
@ -651,8 +652,8 @@ plugin_transaction_commit(clicon_handle h,
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++){ for (i = 0; i < _nplugins; i++){
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_commit) if (p->p_trans_commit)
if ((retval = (p->p_trans_commit)(h, (transaction_data)td)) < 0){ if ((retval = (p->p_trans_commit)(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */ if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
@ -680,8 +681,8 @@ plugin_transaction_end(clicon_handle h,
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_end) if (p->p_trans_end)
if ((retval = (p->p_trans_end)(h, (transaction_data)td)) < 0){ if ((retval = (p->p_trans_end)(h, (transaction_data)td)) < 0){
if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */ if (!clicon_errno) /* sanity: log if clicon_err() is not called ! */
@ -708,8 +709,8 @@ plugin_transaction_abort(clicon_handle h,
int i; int i;
struct plugin *p; struct plugin *p;
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_trans_abort) if (p->p_trans_abort)
(p->p_trans_abort)(h, (transaction_data)td); /* dont abort on error */ (p->p_trans_abort)(h, (transaction_data)td); /* dont abort on error */
} }
@ -750,8 +751,8 @@ backend_statedata_call(clicon_handle h,
clicon_err(OE_CFG, ENOENT, "XML tree expected"); clicon_err(OE_CFG, ENOENT, "XML tree expected");
goto done; goto done;
} }
for (i = 0; i < nplugins; i++) { for (i = 0; i < _nplugins; i++) {
p = &plugins[i]; p = &_plugins[i];
if (p->p_statedata) { if (p->p_statedata) {
if ((x = xml_new("config", NULL)) == NULL) if ((x = xml_new("config", NULL)) == NULL)
goto done; goto done;

View file

@ -42,7 +42,6 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#include <fcgi_stdio.h>
#include <signal.h> #include <signal.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>