]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: enforce non-null memory in kmod_elf_new()
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 7 May 2025 18:50:06 +0000 (19:50 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 20 May 2025 02:51:44 +0000 (21:51 -0500)
In practise none of our code-paths will use a NULL pointer so we might
as well enforce that. To stay aligned with the kernel behaviour update
our init_module() preload library to return EFAULT... Should we get
confused and pass NULL in the future.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/346
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-elf.c
libkmod/libkmod-internal.h
testsuite/init_module.c

index 4590543cd5f3c6adb67ed788f47816bf796a6413..9b2ceca77f61e1ddc18c4ec95138123f5840e6e6 100644 (file)
@@ -317,11 +317,6 @@ struct kmod_elf *kmod_elf_new(const void *memory, off_t size)
        assert_cc(sizeof(uint32_t) == sizeof(Elf32_Word));
        assert_cc(sizeof(uint32_t) == sizeof(Elf64_Word));
 
-       if (!memory) {
-               errno = -EINVAL;
-               return NULL;
-       }
-
        elf = malloc(sizeof(struct kmod_elf));
        if (elf == NULL) {
                return NULL;
index 8d322b65b4f60c15a4ddbaa579c92a86cdec0e9c..3761ea18458dbd440960f81733d962621cbc0acf 100644 (file)
@@ -152,7 +152,7 @@ struct kmod_modversion {
        const char *symbol;
 };
 
-struct kmod_elf *kmod_elf_new(const void *memory, off_t size);
+_nonnull_all_ struct kmod_elf *kmod_elf_new(const void *memory, off_t size);
 _nonnull_all_ void kmod_elf_unref(struct kmod_elf *elf);
 _must_check_ _nonnull_all_ const void *kmod_elf_get_memory(const struct kmod_elf *elf);
 _must_check_ _nonnull_all_ int kmod_elf_get_modinfo_strings(const struct kmod_elf *elf, char ***array);
index a8fa58bb74a1bd72f165407455c17e1f5e770112..ccb43b0c7c67c26bc2a562836b06e0096d9d3498 100644 (file)
@@ -234,6 +234,11 @@ long init_module(void *mem, unsigned long len, const char *args)
 
        init_retcodes();
 
+       if (mem == NULL) {
+               errno = EFAULT;
+               return -1;
+       }
+
        elf = kmod_elf_new(mem, len);
        if (elf == NULL)
                return -1;