Skip to contentSkip to conversation

selftests/lsm: Fix memory leak in attr_lsm_count

PATCH Superseded Wang Yan 3 participants 5 messages Jul 3 – Jul 7

Subsystems (2)
lore
Share & tools
lore

Wang Yan

author
#0 lore raw

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.

Fixes d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")
---
 tools/testing/selftests/lsm/common.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/lsm/common.c +2 -0
index 9ad258912646..4fa8310750a0 100644
@@ -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
#1 lore raw
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 when read_sysfs_lsms fails, 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")
---
 tools/testing/selftests/lsm/common.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/lsm/common.c +0 -0
index 9ad258912646..4fa8310750a0 100644

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 -0
        if (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 -1
        return count;
 }
--

2.25.1

author
#2 lore raw

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.

Fixes d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")
---
 tools/testing/selftests/lsm/common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/lsm/common.c +3 -1
index 9ad258912646..927dce4f04cb 100644
@@ -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
Reviewed-by Tested-by on v2 1/1
#3 lore raw
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 when read_sysfs_lsms fails, 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")
---
 tools/testing/selftests/lsm/common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/lsm/common.c +3 -2
index 9ad258912646..927dce4f04cb 100644
@@ -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

maintainer Applied
#4 lore raw
In reply to Wang Yan
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 when read_sysfs_lsms fails, 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")
---
 tools/testing/selftests/lsm/common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Merged into lsm/dev, thanks!

--
paul-moore.com