From bf83566ef9487d01a89d83cb394af384b33f2a0d Mon Sep 17 00:00:00 2001 From: zhangyaqi Date: Fri, 10 May 2024 10:17:51 +0800 Subject: [PATCH] clear file memory if map fails (cherry picked from commit fa9d30ce9bffa941dc28c2ea37a730ebb20232fa) --- 0003-clear-file-memory-if-map-fails.patch | 40 +++++++++++++++++++++++ kmod.spec | 3 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 0003-clear-file-memory-if-map-fails.patch diff --git a/0003-clear-file-memory-if-map-fails.patch b/0003-clear-file-memory-if-map-fails.patch new file mode 100644 index 0000000..fcf1b40 --- /dev/null +++ b/0003-clear-file-memory-if-map-fails.patch @@ -0,0 +1,40 @@ +From 90b271fbd2b9708a8fa79b7e98d90c7919e7ed73 Mon Sep 17 00:00:00 2001 +From: Emil Velikov +Date: Mon, 12 Feb 2024 17:23:05 +0000 +Subject: libkmod: clear file->memory if map fails + +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 +Reviewed-by: Lucas De Marchi +Signed-off-by: Lucas De Marchi +--- + libkmod/libkmod-file.c | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c +index b6a8cc9..1e1dd35 100644 +--- a/libkmod/libkmod-file.c ++++ b/libkmod/libkmod-file.c +@@ -401,8 +401,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; ++ } + file->direct = true; + return 0; + } +-- +2.27.0 + diff --git a/kmod.spec b/kmod.spec index 8a58ac5..b61184d 100644 --- a/kmod.spec +++ b/kmod.spec @@ -1,6 +1,6 @@ Name: kmod Version: 30 -Release: 3 +Release: 4 Summary: Kernel module management # GPLv2+ is used by programs, LGPLv2+ is used for libraries. License: GPLv2+ and LGPLv2+ @@ -16,6 +16,7 @@ Provides: module-init-tools = 4.0-1 Provides: /sbin/modprobe Patch: 0001-Module-replace-the-module-with-new-module.patch Patch: 0002-Module-suspend-the-module-by-rmmod-r-option.patch +Patch: 0003-clear-file-memory-if-map-fails.patch %description The kmod package provides several commands to manage the kernel modules, -- Gitee