kmodloader: fix memleak adding to avl tree
[project/ubox.git] / kmodloader.c
index 373694278217411186387f558431c37a060446ea..b705c0cb76220b0bf3e38e7a1c1199a68edc4b53 100644 (file)
@@ -132,6 +132,17 @@ static int init_module_folders(void)
        return 0;
 }
 
+static void free_module_folders(void)
+{
+       int n = 0;
+
+       if (!module_folders)
+               return;
+       while (module_folders[n])
+               free(module_folders[n++]);
+       free(module_folders);
+}
+
 static struct module *find_module(const char *name)
 {
        struct module_node *mn;
@@ -271,8 +282,10 @@ alloc_module_node(const char *name, struct module *m, bool is_alias)
                mn->avl.key = strcpy(_name, name);
                mn->m = m;
                mn->is_alias = is_alias;
-               avl_insert(&modules, &mn->avl);
-               m->refcnt += 1;
+               if (avl_insert(&modules, &mn->avl) == 0)
+                       m->refcnt += 1;
+               else
+                       free(mn);
        }
        return mn;
 }
@@ -437,12 +450,13 @@ static struct module* get_module_info(const char *module, const char *name)
 
        strings = map + offset;
        while (true) {
+               char *end = map + offset + size;
                char *sep;
                int len;
 
-               while (!strings[0])
+               while ((strings < end) && !strings[0])
                        strings++;
-               if (strings >= map + offset + size)
+               if (strings >= end)
                        break;
                if (is_builtin) {
                        sep = strstr(strings, ".");
@@ -624,13 +638,14 @@ static int print_modinfo(const struct module *m)
                printf("name:\t\t%s\n", m->name);
        printf("filename:\t%s\n", is_builtin ? "(builtin)" : mpath);
        while (true) {
+               char *end = map + offset + size;
                char *pname, *pdata;
                char *dup = NULL;
                char *sep, *sep2;
 
-               while (!strings[0])
+               while ((strings < end) && !strings[0])
                        strings++;
-               if (strings >= map + offset + size)
+               if (strings >= end)
                        break;
                if (is_builtin) {
                        sep = strstr(strings, ".");
@@ -879,8 +894,8 @@ static int print_usage(char *arg)
 
 static int main_insmod(int argc, char **argv)
 {
-       char *name, *cur, *options;
-       int i, ret, len;
+       char *name, *path, *cur, *opts = NULL;
+       int i, ret = -1, len;
 
        if (argc < 2)
                return print_insmod_usage();
@@ -896,26 +911,23 @@ static int main_insmod(int argc, char **argv)
 
        if (find_module(name)) {
                ULOG_ERR("module is already loaded - %s\n", name);
-               return -1;
+               goto err;
 
        }
 
-       free_modules();
-
-       for (len = 0, i = 2; i < argc; i++)
+       for (len = 1, i = 2; i < argc; i++)
                len += strlen(argv[i]) + 1;
 
-       options = malloc(len);
-       if (!options) {
+       opts = malloc(len);
+       if (!opts) {
                ULOG_ERR("out of memory\n");
-               ret = -1;
                goto err;
        }
 
-       options[0] = 0;
-       cur = options;
+       opts[0] = 0;
+       cur = opts;
        for (i = 2; i < argc; i++) {
-               if (options[0]) {
+               if (opts[0]) {
                        *cur = ' ';
                        cur++;
                }
@@ -924,24 +936,24 @@ static int main_insmod(int argc, char **argv)
 
        if (init_module_folders()) {
                fprintf(stderr, "Failed to find the folder holding the modules\n");
-               ret = -1;
                goto err;
        }
 
-       if (get_module_path(argv[1])) {
-               name = argv[1];
-       } else if (!get_module_path(name)) {
+       if (!(path = get_module_path(argv[1])) ||
+            (path = get_module_path(name))) {
                fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
-               ret = -1;
                goto err;
        }
 
-       ret = insert_module(get_module_path(name), options);
+       ret = insert_module(path, opts);
        if (ret)
                ULOG_ERR("failed to insert %s\n", get_module_path(name));
 
 err:
-       free(options);
+       free(opts);
+       free_modules();
+       free_module_folders();
+
        return ret;
 }
 
@@ -949,7 +961,7 @@ static int main_rmmod(int argc, char **argv)
 {
        struct module *m;
        char *name;
-       int ret;
+       int ret = -1;
 
        if (argc != 2)
                return print_usage("rmmod");
@@ -964,18 +976,20 @@ static int main_rmmod(int argc, char **argv)
        m = find_module(name);
        if (!m) {
                ULOG_ERR("module is not loaded\n");
-               return -1;
+               goto err;
        }
        if (m->state == BUILTIN) {
                ULOG_ERR("module is builtin\n");
-               return -1;
+               goto err;
        }
        ret = syscall(__NR_delete_module, m->name, 0);
 
        if (ret)
                ULOG_ERR("unloading the module failed\n");
 
+err:
        free_modules();
+       free_module_folders();
 
        return ret;
 }
@@ -1017,6 +1031,7 @@ static int main_lsmod(int argc, char **argv)
 static int main_modinfo(int argc, char **argv)
 {
        struct module *m;
+       int rv = -1;
        char *name;
 
        if (argc != 2)
@@ -1032,12 +1047,17 @@ static int main_modinfo(int argc, char **argv)
        m = find_module(name);
        if (!m) {
                ULOG_ERR("cannot find module - %s\n", argv[1]);
-               return -1;
+               goto err;
        }
 
        print_modinfo(m);
 
-       return 0;
+       rv = 0;
+err:
+       free_modules();
+       free_module_folders();
+
+       return rv;
 }
 
 static int main_modprobe(int argc, char **argv)
@@ -1126,6 +1146,7 @@ static int main_modprobe(int argc, char **argv)
        }
 
        free_modules();
+       free_module_folders();
 
        return exit_code;
 }
@@ -1238,6 +1259,8 @@ static int main_loader(int argc, char **argv)
 
 out:
        globfree(&gl);
+       free_modules();
+       free_module_folders();
 free_path:
        free(path);