kmodloader: fix possible segfaults
[project/ubox.git] / kmodloader.c
index ed8f833fc3932caa7bfcc54228e21bd460a1a728..3dc7665bd207237d8b9b9474e802174250d8f952 100644 (file)
@@ -155,10 +155,10 @@ static char* get_module_path(char *name)
 
 static char* get_module_name(char *path)
 {
-       static char name[32];
+       static char name[33];
        char *t;
 
-       strncpy(name, basename(path), sizeof(name));
+       strncpy(name, basename(path), sizeof(name) - 1);
 
        t = strstr(name, ".ko");
        if (t)
@@ -214,6 +214,19 @@ static int elf32_find_section(char *map, const char *section, unsigned int *offs
 static int elf_find_section(char *map, const char *section, unsigned int *offset, unsigned int *size)
 {
        int clazz = map[EI_CLASS];
+       int endian = map[EI_DATA];
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+       if (endian != ELFDATA2LSB)
+#elif __BYTE_ORDER == __BIG_ENDIAN
+       if (endian != ELFDATA2MSB)
+#else
+#error "unsupported endian"
+#endif
+       {
+               ULOG_ERR("invalid endianess: %d\n", endian);
+               return -1;
+       }
 
        if (clazz == ELFCLASS32)
                return elf32_find_section(map, section, offset, size);
@@ -327,7 +340,7 @@ static struct module* get_module_info(const char *module, const char *name)
        int fd = open(module, O_RDONLY);
        unsigned int offset, size;
        char *map = MAP_FAILED, *strings, *dep = NULL;
-       const char *aliases[32];
+       const char *aliases[32] = { 0 };
        int naliases = 0;
        struct module *m = NULL;
        struct stat s;
@@ -550,6 +563,9 @@ static int insert_module(char *path, const char *options)
        }
 
        data = malloc(s.st_size);
+       if (!data)
+               goto out;
+
        if (read(fd, data, s.st_size) == s.st_size) {
                ret = syscall(__NR_init_module, data, (unsigned long) s.st_size, options);
                if (errno == EEXIST)
@@ -558,6 +574,7 @@ static int insert_module(char *path, const char *options)
        else
                ULOG_ERR("failed to read full module %s\n", path);
 
+out:
        close(fd);
        free(data);
 
@@ -679,6 +696,11 @@ static int main_insmod(int argc, char **argv)
                len += strlen(argv[i]) + 1;
 
        options = malloc(len);
+       if (!options) {
+               ret = -1;
+               goto err;
+       }
+
        options[0] = 0;
        cur = options;
        for (i = 2; i < argc; i++) {
@@ -691,22 +713,24 @@ static int main_insmod(int argc, char **argv)
 
        if (init_module_folders()) {
                fprintf(stderr, "Failed to find the folder holding the modules\n");
-               return -1;
+               ret = -1;
+               goto err;
        }
 
        if (get_module_path(argv[1])) {
                name = argv[1];
        } else if (!get_module_path(name)) {
                fprintf(stderr, "Failed to find %s. Maybe it is a built in module ?\n", name);
-               return -1;
+               ret = -1;
+               goto err;
        }
 
        ret = insert_module(get_module_path(name), options);
-       free(options);
-
        if (ret)
                ULOG_ERR("failed to insert %s\n", get_module_path(name));
 
+err:
+       free(options);
        return ret;
 }
 
@@ -882,6 +906,9 @@ static int main_loader(int argc, char **argv)
                dir = argv[1];
 
        path = malloc(strlen(dir) + 2);
+       if (!path)
+               return -1;
+
        strcpy(path, dir);
        strcat(path, "*");