selftests/lsm: Fix memory leak in attr_lsm_count
Subsystems (2)
PATCH 1/1 selftests/lsm: Fix memory leak in attr_lsm_count
The calloc-allocated buffer in attr_lsm_count() is never released on any exit path, including both the normal return path and the early return when read_sysfs_lsms fails, resulting in a heap memory leak.
Add free() for the buffer on all return branches to fix the leak.
--- tools/testing/selftests/lsm/common.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/lsm/common.c +2 -0
@@ -76,6 +76,7 @@ int attr_lsm_count(void)return 0 ;if (read_sysfs_lsms (names,sysconf (_SC_PAGESIZE))) +free (names);return 0 ;if (strstr (names,"selinux" ))
@@ -85,5 +86,6 @@ int attr_lsm_count(void)if (strstr (names,"apparmor" )) count++; +free (names);return count; }
Signature · 1 line
2.25.1
Quoted 18 lines · Wang Yan
The calloc-allocated buffer in
attr_lsm_count()is never released on any exit path, including both the normal return path and the early return whenread_sysfs_lsmsfails, resulting in a heap memory leak.Add
free()for the buffer on all return branches to fix the leak.Fixes d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")Signed-off-by Wang Yan <[email protected]>--- tools/testing/selftests/lsm/common.c | 2 ++ 1 file changed, 2 insertions(+)tools/testing/selftests/lsm/common.c +0
-0
tools/testing/selftests/lsm/common.c int attr_lsm_count(void) · line 80
Quoted 5 lines
tools/testing/selftests/lsm/common.c +1
-0@@ -76,6 +76,7 @@ int attr_lsm_count(void)return 0 ;if (read_sysfs_lsms (names,sysconf (_SC_PAGESIZE))) +free (names);return 0 ;
Did you test this, let alone even compile it? All the C code after this return is dead code.
Please ensure that you test and compile your patches checking for warnings. Sending
untested patches is "spammy".
tools/testing/selftests/lsm/common.c int attr_lsm_count(void) · line 89
Quoted 7 lines
tools/testing/selftests/lsm/common.c +1
-0if (strstr (names,"selinux" ))@@ -85,5 +86,6 @@ int attr_lsm_count(void)if (strstr (names,"apparmor" )) count++; +free (names);
Do this instead:
if (read_sysfs_lsms(names, sysconf(_SC_PAGESIZE)))
goto out;
<existing code>
out:
free(names);
Quoted 6 lines
tools/testing/selftests/lsm/common.c +0
-1return count; } --2.25.1
PATCH v2 1/1 selftests/lsm: Fix memory leak in attr_lsm_count
The calloc-allocated buffer in attr_lsm_count() is never released on any exit path, including both the normal return path and the early return when read_sysfs_lsms fails, resulting in a heap memory leak.
Add free() for the buffer on all return branches to fix the leak.
--- tools/testing/selftests/lsm/common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/lsm/common.c +3 -1
@@ -76,7 +76,7 @@ int attr_lsm_count(void)return 0 ;if (read_sysfs_lsms (names,sysconf (_SC_PAGESIZE))) -return 0 ; + goto out;if (strstr (names,"selinux" )) count++;
@@ -85,5 +85,7 @@ int attr_lsm_count(void)if (strstr (names,"apparmor" )) count++; +out: +free (names);return count; }
Signature · 1 line
2.25.1
Quoted 35 lines · Wang Yan
The calloc-allocated buffer in
attr_lsm_count()is never released on any exit path, including both the normal return path and the early return whenread_sysfs_lsmsfails, resulting in a heap memory leak.Add
free()for the buffer on all return branches to fix the leak.Fixes d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")Signed-off-by Wang Yan <[email protected]>--- tools/testing/selftests/lsm/common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)tools/testing/selftests/lsm/common.c +3
-2@@ -76,7 +76,7 @@ int attr_lsm_count(void)return 0 ;if (read_sysfs_lsms (names,sysconf (_SC_PAGESIZE))) -return 0 ; + goto out;if (strstr (names,"selinux" )) count++;@@ -85,5 +85,7 @@ int attr_lsm_count(void)if (strstr (names,"apparmor" )) count++; +out: +free (names);return count; } --
Quoted 2 lines
2.25.1
Quoted 7 lines · On Jul 3, 2026 Wang Yan <[email protected]> wrote:
The calloc-allocated buffer in
attr_lsm_count()is never released on any exit path, including both the normal return path and the early return whenread_sysfs_lsmsfails, resulting in a heap memory leak.Add
free()for the buffer on all return branches to fix the leak.
Quoted 7 lines
Fixes d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")Signed-off-by Wang Yan <[email protected]>Reviewed-by William Roberts <[email protected]>Tested-by William Roberts <[email protected]>--- tools/testing/selftests/lsm/common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Merged into lsm/dev, thanks!
--
paul-moore.com