From e63dcab1093b37a0ec05dbbfdf8be8d75e380705 Mon Sep 17 00:00:00 2001 From: Jiangtian Feng Date: Mon, 8 May 2023 10:52:53 +0800 Subject: [PATCH] backport community patches to fix mem leak in sysfs_attr.c, sysfs_device.c, and sysfs_driver.c Signed-off-by: Jiangtian Feng (cherry picked from commit f77d45dea5cd3e0494add9c6176aa41713b23b9b) --- ...-a-memory-leak-in-lib-sysfs_driver.c.patch | 29 +++++++++ ...d-memory-leaks-in-lib-sysfs_device.c.patch | 38 ++++++++++++ ...xed-memory-leaks-in-lib-sysfs_attr.c.patch | 62 +++++++++++++++++++ sysfsutils.spec | 8 ++- 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 0010-lib-Fixed-a-memory-leak-in-lib-sysfs_driver.c.patch create mode 100644 0011-lib-Fixed-memory-leaks-in-lib-sysfs_device.c.patch create mode 100644 0012-lib-Fixed-memory-leaks-in-lib-sysfs_attr.c.patch diff --git a/0010-lib-Fixed-a-memory-leak-in-lib-sysfs_driver.c.patch b/0010-lib-Fixed-a-memory-leak-in-lib-sysfs_driver.c.patch new file mode 100644 index 0000000..929811d --- /dev/null +++ b/0010-lib-Fixed-a-memory-leak-in-lib-sysfs_driver.c.patch @@ -0,0 +1,29 @@ +From 5777dc78d306f7b0ab407d666f8c9537bd8535fe Mon Sep 17 00:00:00 2001 +From: Chris White +Date: Fri, 25 Jun 2021 18:00:25 +0000 +Subject: [PATCH] lib: Fixed a memory leak in lib/sysfs_driver.c + +- sysfs_get_driver_devices() had a case where the function returned + before the dev pointer was closed. + +Warned-by: covscan +Signed-off-by: Chris White +--- + lib/sysfs_driver.c | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/lib/sysfs_driver.c b/lib/sysfs_driver.c +index a0b9dd5..13c33cf 100644 +--- a/lib/sysfs_driver.c ++++ b/lib/sysfs_driver.c +@@ -252,6 +252,7 @@ struct dlist *sysfs_get_driver_devices(struct sysfs_driver *drv) + if (!drv->devices) { + dprintf("Error creating device list\n"); + sysfs_close_list(linklist); ++ sysfs_close_device(dev); + return NULL; + } + } +-- +2.27.0 + diff --git a/0011-lib-Fixed-memory-leaks-in-lib-sysfs_device.c.patch b/0011-lib-Fixed-memory-leaks-in-lib-sysfs_device.c.patch new file mode 100644 index 0000000..2456c12 --- /dev/null +++ b/0011-lib-Fixed-memory-leaks-in-lib-sysfs_device.c.patch @@ -0,0 +1,38 @@ +From e2267a5def65380db738e434da4a3920e6136f95 Mon Sep 17 00:00:00 2001 +From: Chris White +Date: Fri, 25 Jun 2021 17:55:00 +0000 +Subject: [PATCH] lib: Fixed memory leaks in lib/sysfs_device.c + +- sysfs_open_device_tree() has two case where the function returns + before the devlist pointer is closed. + +Warned-by: covscan +Signed-off-by: Chris White +--- + lib/sysfs_device.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/lib/sysfs_device.c b/lib/sysfs_device.c +index 5f815f9..78ed48e 100644 +--- a/lib/sysfs_device.c ++++ b/lib/sysfs_device.c +@@ -247,6 +247,7 @@ struct sysfs_device *sysfs_open_device_tree(const char *path) + if (new == NULL) { + dprintf("Error opening device tree at %s\n", + cur->path); ++ sysfs_close_device(devlist); + sysfs_close_device_tree(rootdev); + return NULL; + } +@@ -257,7 +258,7 @@ struct sysfs_device *sysfs_open_device_tree(const char *path) + dlist_unshift_sorted(rootdev->children, new, sort_list); + } + } +- ++ sysfs_close_device(devlist); + return rootdev; + } + +-- +2.27.0 + diff --git a/0012-lib-Fixed-memory-leaks-in-lib-sysfs_attr.c.patch b/0012-lib-Fixed-memory-leaks-in-lib-sysfs_attr.c.patch new file mode 100644 index 0000000..ad4e531 --- /dev/null +++ b/0012-lib-Fixed-memory-leaks-in-lib-sysfs_attr.c.patch @@ -0,0 +1,62 @@ +From 9a4aac68658a61df1c97ecdd7d9a9e67f572fb3e Mon Sep 17 00:00:00 2001 +From: Chris White +Date: Fri, 25 Jun 2021 17:32:10 +0000 +Subject: [PATCH] lib: Fixed memory leaks in lib/sysfs_attr.c + +- read_dir_links() has a case where the function returns before + closing the dir pointer. + +- sysfs_read_dir_subdirs() has a case where the function returns + before closing the dev pointer. + +- read_dir_subdirs() has a case where the function returns before + closing the dir pointer. + +- get_attributes_list() has a case where the function returns before + closing the dir pointer. + +Warned-by: covscan +Signed-off-by: Chris White +--- + lib/sysfs_attr.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/lib/sysfs_attr.c b/lib/sysfs_attr.c +index fe27dbe..82aacf4 100644 +--- a/lib/sysfs_attr.c ++++ b/lib/sysfs_attr.c +@@ -415,6 +415,7 @@ struct dlist *read_dir_links(const char *path) + (SYSFS_NAME_LEN, sysfs_del_name); + if (!linklist) { + dprintf("Error creating list\n"); ++ closedir(dir); + return NULL; + } + } +@@ -470,6 +471,7 @@ struct sysfs_device *sysfs_read_dir_subdirs(const char *path) + dir = opendir(path); + if (!dir) { + dprintf("Error opening directory %s\n", path); ++ sysfs_close_device(dev); + return NULL; + } + while ((dirent = readdir(dir)) != NULL) { +@@ -524,6 +526,7 @@ struct dlist *read_dir_subdirs(const char *path) + (SYSFS_NAME_LEN, sysfs_del_name); + if (!dirlist) { + dprintf("Error creating list\n"); ++ closedir(dir); + return NULL; + } + } +@@ -573,6 +576,7 @@ struct dlist *get_attributes_list(struct dlist *alist, const char *path) + sysfs_del_attribute); + if (!alist) { + dprintf("Error creating list\n"); ++ closedir(dir); + return NULL; + } + } +-- +2.27.0 + diff --git a/sysfsutils.spec b/sysfsutils.spec index b2ec51a..ebdd899 100644 --- a/sysfsutils.spec +++ b/sysfsutils.spec @@ -1,6 +1,6 @@ Name: sysfsutils Version: 2.1.0 -Release: 33 +Release: 34 Summary: A set of utilities for interfacing with sysfs License: GPLv2 and LGPLv2+ URL: http://sourceforge.net/projects/linux-diag/ @@ -16,6 +16,9 @@ Patch6: 0006-fix-of-FUNC_TABLE_SIZE-mentioned-in-prev-commit.patch Patch7: 0007-Fix-compiler-complain-about-multiple-defs-of-my_stdo.patch Patch8: 0008-Use-stat-not-lstat-to-find-link-target.patch Patch9: 0009-path_is_file-should-call-stat-not-lstat.patch +Patch10: 0010-lib-Fixed-a-memory-leak-in-lib-sysfs_driver.c.patch +Patch11: 0011-lib-Fixed-memory-leaks-in-lib-sysfs_device.c.patch +Patch12: 0012-lib-Fixed-memory-leaks-in-lib-sysfs_attr.c.patch BuildRequires: gcc chrpath Provides: libsysfs libsysfs%{?_isa} @@ -88,6 +91,9 @@ chrpath -d $(find $RPM_BUILD_ROOT -name systool) %changelog +* Mon May 8 2023 Jiangtian Feng - 2.1.0-34 +- backport community patches to fix mem leak in sysfs_attr.c, sysfs_device.c, and sysfs_driver.c + * Wed Mar 30 2022 yanglongkang - 2.1.0-33 - correcting errors in 0009-path_is_file-should-call-stat-not-lstat.patch