]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: clear file->memory if map fails
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 12 Feb 2024 17:23:05 +0000 (17:23 +0000)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 30 Apr 2024 17:33:52 +0000 (12:33 -0500)
On mmap failure file->memory is set to -1, which we'll happily pass down
to munmap later on.

More importantly, since we do a NULL check in kmod_file_load_contents()
we will exit the function without (re)attempting the load again.

Since we ignore the return code for the load function(s), one can end up
calling kmod_elf_get_memory() and feed that -1 into init_module.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-file.c

index abd4723b22c5aa313c3e23fea74fe7227b514ab1..b408aed2f01b79e0c2e8a42d25e65893eb0f6709 100644 (file)
@@ -392,8 +392,10 @@ static int load_reg(struct kmod_file *file)
        file->size = st.st_size;
        file->memory = mmap(NULL, file->size, PROT_READ, MAP_PRIVATE,
                            file->fd, 0);
-       if (file->memory == MAP_FAILED)
+       if (file->memory == MAP_FAILED) {
+               file->memory = NULL;
                return -errno;
+       }
 
        return 0;
 }