From 551ffc67f770fb982795d5a3d2a36d8e8dcecbe8 Mon Sep 17 00:00:00 2001 From: jinzhimin369 Date: Thu, 14 Jan 2021 14:20:29 +0800 Subject: [PATCH] fix memory leakinmodinfo --- ...-depmod-do-not-output-.bin-to-stdout.patch | 57 +++++++++++++++++++ ...iltin_alias_bin-free-idx-on-error-pa.patch | 56 ++++++++++++++++++ ...ltin_get_modinfo-free-modinfo-on-err.patch | 35 ++++++++++++ kmod.spec | 8 ++- 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 backport-depmod-do-not-output-.bin-to-stdout.patch create mode 100644 backport-depmod-output_builtin_alias_bin-free-idx-on-error-pa.patch create mode 100644 backport-libkmod-kmod_builtin_get_modinfo-free-modinfo-on-err.patch diff --git a/backport-depmod-do-not-output-.bin-to-stdout.patch b/backport-depmod-do-not-output-.bin-to-stdout.patch new file mode 100644 index 0000000..be01ce8 --- /dev/null +++ b/backport-depmod-do-not-output-.bin-to-stdout.patch @@ -0,0 +1,57 @@ +From 53b30aeba2dedae9f5558f560231d9462e063dfc Mon Sep 17 00:00:00 2001 +From: Lucas De Marchi +Date: Thu, 5 Mar 2020 13:33:10 -0800 +Subject: [PATCH] depmod: do not output .bin to stdout + +reason:do not output .bin to stdout +Conflict:NA +Reference:https://lore.kernel.org/linux-modules/20200306075934.3104-1-lucas.demarchi@intel.com/T/#t + +index_write() relies on fseek/ftell to manage the position to which we +are write and thus needs the file stream to support it. + +Right now when trying to write the index to stdout we fail with: + + depmod: tools/depmod.c:416: index_write: Assertion `initial_offset >= 0' failed. + Aborted (core dumped) + +We have no interest in outputting our index to stdout, so just skip it +like is done with other indexes. + +While at it, add/remove some newlines to improve readability. + +Reported-by: Yanko Kaneti +Fix: b866b2165ae6 ("Lookup aliases in the modules.builtin.modinfo") +--- + tools/depmod.c | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/tools/depmod.c b/tools/depmod.c +index fbbce10..875e314 100644 +--- a/tools/depmod.c ++++ b/tools/depmod.c +@@ -2408,8 +2408,10 @@ static int output_builtin_alias_bin(struct depmod *depmod, FILE *out) + struct index_node *idx; + struct kmod_list *l, *builtin = NULL; + +- idx = index_create(); ++ if (out == stdout) ++ return 0; + ++ idx = index_create(); + if (idx == NULL) { + ret = -ENOMEM; + goto fail; +@@ -2456,7 +2458,9 @@ static int output_builtin_alias_bin(struct depmod *depmod, FILE *out) + + if (count) + index_write(idx, out); ++ + index_destroy(idx); ++ + fail: + if (builtin) + kmod_module_unref_list(builtin); +-- +2.23.0 + diff --git a/backport-depmod-output_builtin_alias_bin-free-idx-on-error-pa.patch b/backport-depmod-output_builtin_alias_bin-free-idx-on-error-pa.patch new file mode 100644 index 0000000..a8fe60e --- /dev/null +++ b/backport-depmod-output_builtin_alias_bin-free-idx-on-error-pa.patch @@ -0,0 +1,56 @@ +From bd96d05256db2ff9a89dbe2e8bb6e26fcb800052 Mon Sep 17 00:00:00 2001 +From: Yauheni Kaliuta +Date: Sun, 29 Nov 2020 18:47:36 +0200 +Subject: [PATCH] depmod: output_builtin_alias_bin: free idx on error path + +reason:depmod: output_builtin_alias_bin free idx on error path +Conflict:NA +Reference:https://lore.kernel.org/linux-modules/20201129164737.135866-2-yauheni.kaliuta@redhat.com/T/#u + +idx is allocated in the beginning but it's not freed if there is +a failure after the allocation. + +Change the error path: return immediately if idx allocation fails +and then free it in both success and error path at the end. + +Signed-off-by: Yauheni Kaliuta +--- + tools/depmod.c | 11 ++++------- + 1 file changed, 4 insertions(+), 7 deletions(-) + +diff --git a/tools/depmod.c b/tools/depmod.c +index 875e314..2c03dfe 100644 +--- a/tools/depmod.c ++++ b/tools/depmod.c +@@ -2412,10 +2412,8 @@ static int output_builtin_alias_bin(struct depmod *depmod, FILE *out) + return 0; + + idx = index_create(); +- if (idx == NULL) { +- ret = -ENOMEM; +- goto fail; +- } ++ if (idx == NULL) ++ return -ENOMEM; + + ret = kmod_module_get_builtin(depmod->ctx, &builtin); + if (ret < 0) { +@@ -2458,13 +2456,12 @@ static int output_builtin_alias_bin(struct depmod *depmod, FILE *out) + + if (count) + index_write(idx, out); +- +- index_destroy(idx); +- + fail: + if (builtin) + kmod_module_unref_list(builtin); + ++ index_destroy(idx); ++ + return ret; + } + +-- +2.23.0 + diff --git a/backport-libkmod-kmod_builtin_get_modinfo-free-modinfo-on-err.patch b/backport-libkmod-kmod_builtin_get_modinfo-free-modinfo-on-err.patch new file mode 100644 index 0000000..3f234c7 --- /dev/null +++ b/backport-libkmod-kmod_builtin_get_modinfo-free-modinfo-on-err.patch @@ -0,0 +1,35 @@ +From 47807c4cfa5ffe1e5da27e3d3056d9b47ba998c5 Mon Sep 17 00:00:00 2001 +From: Yauheni Kaliuta +Date: Sun, 29 Nov 2020 18:47:35 +0200 +Subject: [PATCH] libkmod: kmod_builtin_get_modinfo: free modinfo on error + +reason:kmod_builtin_get_modinfo: free modinfo on error +Conflict:NA +Reference:https://lore.kernel.org/linux-modules/CAKi4VA+kaWfLZ_Ue-teaJAvDQjfM6G-WK3KmMWVinR-Zg6T64A@mail.gmail.com/T/#t + +The function allocates array but on building it if get_string() +fails it returns the error leaving the array allocated. The caller +does not care about it in error case either. + +Free it to fix memory leak. + +Signed-off-by: Yauheni Kaliuta +--- + libkmod/libkmod-builtin.c | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/libkmod/libkmod-builtin.c b/libkmod/libkmod-builtin.c +index aaec5dd..fc9a376 100644 +--- a/libkmod/libkmod-builtin.c ++++ b/libkmod/libkmod-builtin.c +@@ -314,6 +314,7 @@ ssize_t kmod_builtin_get_modinfo(struct kmod_ctx *ctx, const char *modname, + offset = get_string(iter, pos, &line, &linesz); + if (offset <= 0) { + count = (offset) ? -errno : -EOF; ++ free(*modinfo); + goto fail; + } + +-- +2.23.0 + diff --git a/kmod.spec b/kmod.spec index cbce6e6..81bc25d 100644 --- a/kmod.spec +++ b/kmod.spec @@ -1,6 +1,6 @@ Name: kmod Version: 27 -Release: 6 +Release: 7 Summary: Kernel module management # GPLv2+ is used by programs, LGPLv2+ is used for libraries. License: GPLv2+ and LGPLv2+ @@ -12,6 +12,9 @@ Source2: depmod.conf.dist Patch6000: backport-libkmod-fix-return-error-when-opening-index.patch Patch6001: backport-libkmod-allow-modules.alias.builtin-to-be-optional.patch Patch6002: backport-libkmod-kmod_log_null-qualify-ctx-argument-as-const.patch +Patch6003: backport-depmod-do-not-output-.bin-to-stdout.patch +Patch6004: backport-libkmod-kmod_builtin_get_modinfo-free-modinfo-on-err.patch +Patch6005: backport-depmod-output_builtin_alias_bin-free-idx-on-error-pa.patch Patch9000: bugfix-kmod-20-8-depmod-Don-t-unlinkat-orig-depfile-and-add-fsync.patch Recommends: %{name}-help = %{version}-%{release} @@ -108,6 +111,9 @@ install -m 0644 %{SOURCE2} $RPM_BUILD_ROOT%{_sysconfdir}/depmod.d/dist.conf %doc TODO NEWS README %changelog +* Thu Jan 14 2021 xinghe - 27-7 +- fix memory leak in kmodinfo + * Fri Jan 8 2020 xinghe - 27-6 - fix compilation warning -- Gitee