From: Emil Velikov Date: Wed, 7 May 2025 18:50:06 +0000 (+0100) Subject: libkmod: enforce non-null memory in kmod_elf_new() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1b110b9bde5627ca9ed10900eb746c518cd1d56e;p=thirdparty%2Fkmod.git libkmod: enforce non-null memory in kmod_elf_new() 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 Link: https://github.com/kmod-project/kmod/pull/346 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-elf.c b/libkmod/libkmod-elf.c index 4590543c..9b2ceca7 100644 --- a/libkmod/libkmod-elf.c +++ b/libkmod/libkmod-elf.c @@ -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; diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 8d322b65..3761ea18 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -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); diff --git a/testsuite/init_module.c b/testsuite/init_module.c index a8fa58bb..ccb43b0c 100644 --- a/testsuite/init_module.c +++ b/testsuite/init_module.c @@ -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;