diff options
author | Alan Jenkins <alan-jenkins@tuffmail.co.uk> | 2009-09-30 17:22:10 +0100 |
---|---|---|
committer | Alan Jenkins <alan-jenkins@tuffmail.co.uk> | 2010-02-25 15:29:24 +0000 |
commit | 6392a1a21c0c2a6edb6579f27d897b5c880ad98d (patch) | |
tree | 74b393077f3df987eae3b1705e78e1db310121d2 | |
parent | 6c20c7fccae46cd5ac70e27fced1a7f93c96ff59 (diff) | |
download | module-init-tools-6392a1a21c0c2a6edb6579f27d897b5c880ad98d.tar.gz |
modprobe: fix memory leak when built with zlib support
We don't use posix file locks anymore, so we don't need to play games
with fds, and we don't need to worry about dropping locks when we close
fds. This allows us to simplify the code a bit and remove the
intentional memory leak.
Signed-off-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk>
-rw-r--r-- | elfops.c | 26 | ||||
-rw-r--r-- | elfops.h | 1 | ||||
-rw-r--r-- | modprobe.c | 36 | ||||
-rw-r--r-- | zlibsupport.c | 17 |
4 files changed, 5 insertions, 75 deletions
@@ -62,30 +62,6 @@ static int elf_ident(void *file, unsigned long fsize, int *conv) */ struct elf_file *grab_elf_file(const char *pathname) { - int fd; - int err; - struct elf_file *file; - - fd = open(pathname, O_RDONLY, 0); - if (fd < 0) - return NULL; - file = grab_elf_file_fd(pathname, fd); - - err = errno; - close(fd); - errno = err; - return file; -} - -/* - * grab_elf_file_fd - read ELF file from file descriptor into memory - * @pathame: name of file to load - * @fd: file descriptor of file to load - * - * Returns NULL, and errno set on error. - */ -struct elf_file *grab_elf_file_fd(const char *pathname, int fd) -{ struct elf_file *file; file = malloc(sizeof(*file)); @@ -98,7 +74,7 @@ struct elf_file *grab_elf_file_fd(const char *pathname, int fd) errno = ENOMEM; goto fail_free_file; } - file->data = grab_fd(fd, &file->len); + file->data = grab_file(pathname, &file->len); if (!file->data) goto fail_free_pathname; @@ -79,7 +79,6 @@ struct module_ops extern struct module_ops mod_ops32, mod_ops64; struct elf_file *grab_elf_file(const char *pathname); -struct elf_file *grab_elf_file_fd(const char *pathname, int fd); void release_elf_file(struct elf_file *file); #endif /* MODINITTOOLS_MODULEOPS_H */ @@ -100,24 +100,6 @@ static struct module *find_module(const char *filename, struct list_head *list) return NULL; } -/* We used to lock with a write flock but that allows regular users to block - * module load by having a read lock on the module file (no way to bust the - * existing locks without killing the offending process). Instead, we now - * do the system call/init_module and allow the kernel to fail us instead. - */ -static int open_file(const char *filename) -{ - int fd = open(filename, O_RDONLY, 0); - - return fd; -} - -static void close_file(int fd) -{ - /* Valgrind is picky... */ - close(fd); -} - static void add_module(char *filename, int namelen, struct list_head *list) { struct module *mod; @@ -1193,7 +1175,7 @@ static int insmod(struct list_head *list, errfn_t error, modprobe_flags_t flags) { - int ret, fd; + int ret; struct elf_file *module; const char *command; struct module *mod = list_entry(list->next, struct module, list); @@ -1218,13 +1200,6 @@ static int insmod(struct list_head *list, } } - fd = open_file(mod->filename); - if (fd < 0) { - error("Could not open '%s': %s\n", - mod->filename, strerror(errno)); - goto out; - } - /* Don't do ANYTHING if already in kernel. */ already_loaded = module_in_kernel(newname ?: mod->modname, NULL); @@ -1232,7 +1207,7 @@ static int insmod(struct list_head *list, if (flags & mit_first_time) error("Module %s already in kernel.\n", newname ?: mod->modname); - goto out_unlock; + goto out; } command = find_command(mod->modname, commands); @@ -1244,19 +1219,18 @@ static int insmod(struct list_head *list, " in case it is already loaded.\n", newname ?: mod->modname); } else { - close_file(fd); do_command(mod->modname, command, flags & mit_dry_run, error, "install", cmdline_opts); goto out; } } - module = grab_elf_file_fd(mod->filename, fd); + module = grab_elf_file(mod->filename); if (!module) { error("Could not read '%s': %s\n", mod->filename, (errno == ENOEXEC) ? "Invalid module format" : strerror(errno)); - goto out_unlock; + goto out; } if (newname) rename_module(module, mod->modname, newname); @@ -1291,8 +1265,6 @@ static int insmod(struct list_head *list, } out_elf_file: release_elf_file(module); - out_unlock: - close_file(fd); free(opts); out: return rc; diff --git a/zlibsupport.c b/zlibsupport.c index b159765..597820e 100644 --- a/zlibsupport.c +++ b/zlibsupport.c @@ -40,23 +40,6 @@ void *grab_contents(gzFile *gzfd, unsigned long *size) return buffer; } -void *grab_fd(int fd, unsigned long *size) -{ - gzFile gzfd; - - gzfd = gzdopen(fd, "rb"); - if (!gzfd) { - if (errno == ENOMEM) - fatal("Memory allocation failure in gzdopen\n"); - return NULL; - } - - /* gzclose(gzfd) would close fd, which would drop locks. - Don't blame zlib: POSIX locking semantics are so horribly - broken that they should be ripped out. */ - return grab_contents(gzfd, size); -} - /* gzopen handles uncompressed files transparently. */ void *grab_file(const char *filename, unsigned long *size) { |