X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubox.git;a=blobdiff_plain;f=kmodloader.c;h=1b6488f0ff4b8f28f4039346660d01afac58c12d;hp=1a63c9810a99e30009ef9a85c39dba6a65fb3264;hb=HEAD;hpb=d54f38a2e2da5a29b00d19b490a87cc5ff910f33 diff --git a/kmodloader.c b/kmodloader.c index 1a63c98..1b6488f 100644 --- a/kmodloader.c +++ b/kmodloader.c @@ -95,8 +95,10 @@ static int init_module_folders(void) if (!stat(path, &st) && S_ISDIR(st.st_mode)) { module_folders = realloc(module_folders, sizeof(p) * (n + 2)); - if (!module_folders) + if (!module_folders) { + ULOG_ERR("out of memory\n"); return -1; + } module_folders[n++] = strdup(path); } @@ -214,6 +216,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 +342,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] = { 0 }; + const char **aliases = NULL; int naliases = 0; struct module *m = NULL; struct stat s; @@ -370,11 +385,13 @@ static struct module* get_module_info(const char *module, const char *name) if (!strncmp(strings, "depends=", len + 1)) dep = sep; else if (!strncmp(strings, "alias=", len + 1)) { - if (naliases < ARRAY_SIZE(aliases)) - aliases[naliases++] = sep; - else - ULOG_WARN("module %s has more than %d aliases: truncated", - name, ARRAY_SIZE(aliases)); + aliases = realloc(aliases, sizeof(sep) * (naliases + 1)); + if (!aliases) { + ULOG_ERR("out of memory\n"); + goto out; + } + + aliases[naliases++] = sep; } strings = &sep[strlen(sep)]; } @@ -391,6 +408,8 @@ out: if (fd >= 0) close(fd); + free(aliases); + return m; } @@ -400,7 +419,7 @@ static int scan_module_folder(const char *dir) struct utsname ver; char *path; glob_t gl; - int j; + int j, rv = 0; uname(&ver); path = alloca(strlen(dir) + sizeof("*.ko") + 1); @@ -417,13 +436,15 @@ static int scan_module_folder(const char *dir) continue; m = find_module(name); - if (!m) - get_module_info(gl.gl_pathv[j], name); + if (!m) { + if (!get_module_info(gl.gl_pathv[j], name)) + rv |= -1; + } } globfree(&gl); - return 0; + return rv; } static int scan_module_folders(void) @@ -550,6 +571,11 @@ static int insert_module(char *path, const char *options) } data = malloc(s.st_size); + if (!data) { + ULOG_ERR("out of memory\n"); + 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 +584,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 +706,12 @@ static int main_insmod(int argc, char **argv) len += strlen(argv[i]) + 1; options = malloc(len); + if (!options) { + ULOG_ERR("out of memory\n"); + ret = -1; + goto err; + } + options[0] = 0; cur = options; for (i = 2; i < argc; i++) { @@ -884,6 +917,11 @@ static int main_loader(int argc, char **argv) dir = argv[1]; path = malloc(strlen(dir) + 2); + if (!path) { + ULOG_ERR("out of memory\n"); + return -1; + } + strcpy(path, dir); strcat(path, "*");