Linux Security Modules development
 help / color / mirror / Atom feed
* [PATCH] selftests/lsm: Fix memory leak in attr_lsm_count
@ 2026-07-03  2:52 Wang Yan
  2026-07-03  3:01 ` William Roberts
  0 siblings, 1 reply; 5+ messages in thread
From: Wang Yan @ 2026-07-03  2:52 UTC (permalink / raw)
  To: linux-security-module
  Cc: paul, jmorris, serge, shuah, casey, mic, linux-kselftest,
	linux-kernel, 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")
Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
---
 tools/testing/selftests/lsm/common.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
index 9ad258912646..4fa8310750a0 100644
--- a/tools/testing/selftests/lsm/common.c
+++ b/tools/testing/selftests/lsm/common.c
@@ -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;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] selftests/lsm: Fix memory leak in attr_lsm_count
  2026-07-03  2:52 [PATCH] selftests/lsm: Fix memory leak in attr_lsm_count Wang Yan
@ 2026-07-03  3:01 ` William Roberts
  2026-07-03 12:09   ` [PATCH v2] " Wang Yan
  0 siblings, 1 reply; 5+ messages in thread
From: William Roberts @ 2026-07-03  3:01 UTC (permalink / raw)
  To: Wang Yan
  Cc: linux-security-module, paul, jmorris, serge, shuah, casey, mic,
	linux-kselftest, linux-kernel

On Thu, Jul 2, 2026 at 9:53 PM Wang Yan <wangyan01@kylinos.cn> 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.
>
> Fixes: d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")
> Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
> ---
>  tools/testing/selftests/lsm/common.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
> index 9ad258912646..4fa8310750a0 100644
> --- a/tools/testing/selftests/lsm/common.c
> +++ b/tools/testing/selftests/lsm/common.c
> @@ -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".

>
>         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);
>         return count;
>  }
> --
> 2.25.1
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] selftests/lsm: Fix memory leak in attr_lsm_count
  2026-07-03  3:01 ` William Roberts
@ 2026-07-03 12:09   ` Wang Yan
  2026-07-03 17:56     ` William Roberts
  2026-07-07 19:13     ` Paul Moore
  0 siblings, 2 replies; 5+ messages in thread
From: Wang Yan @ 2026-07-03 12:09 UTC (permalink / raw)
  To: bill.c.roberts
  Cc: casey, jmorris, linux-kernel, linux-kselftest,
	linux-security-module, mic, paul, serge, shuah, wangyan01

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")
Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
---
 tools/testing/selftests/lsm/common.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
index 9ad258912646..927dce4f04cb 100644
--- a/tools/testing/selftests/lsm/common.c
+++ b/tools/testing/selftests/lsm/common.c
@@ -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;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] selftests/lsm: Fix memory leak in attr_lsm_count
  2026-07-03 12:09   ` [PATCH v2] " Wang Yan
@ 2026-07-03 17:56     ` William Roberts
  2026-07-07 19:13     ` Paul Moore
  1 sibling, 0 replies; 5+ messages in thread
From: William Roberts @ 2026-07-03 17:56 UTC (permalink / raw)
  To: Wang Yan
  Cc: casey, jmorris, linux-kernel, linux-kselftest,
	linux-security-module, mic, paul, serge, shuah

On Fri, Jul 3, 2026 at 7:10 AM Wang Yan <wangyan01@kylinos.cn> 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.
>
> Fixes: d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")
> Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
> ---
>  tools/testing/selftests/lsm/common.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/lsm/common.c b/tools/testing/selftests/lsm/common.c
> index 9ad258912646..927dce4f04cb 100644
> --- a/tools/testing/selftests/lsm/common.c
> +++ b/tools/testing/selftests/lsm/common.c
> @@ -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;
>  }
> --
> 2.25.1
>

Reviewed-by: William Roberts <bill.c.roberts@gmail.com>
Tested-by: William Roberts <bill.c.roberts@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] selftests/lsm: Fix memory leak in attr_lsm_count
  2026-07-03 12:09   ` [PATCH v2] " Wang Yan
  2026-07-03 17:56     ` William Roberts
@ 2026-07-07 19:13     ` Paul Moore
  1 sibling, 0 replies; 5+ messages in thread
From: Paul Moore @ 2026-07-07 19:13 UTC (permalink / raw)
  To: Wang Yan, bill.c.roberts
  Cc: casey, jmorris, linux-kernel, linux-kselftest,
	linux-security-module, mic, serge, shuah, wangyan01

On Jul  3, 2026 Wang Yan <wangyan01@kylinos.cn> 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.
> 
> Fixes: d3d929a8b0cd ("LSM: selftests for Linux Security Module syscalls")
> Signed-off-by: Wang Yan <wangyan01@kylinos.cn>
> Reviewed-by: William Roberts <bill.c.roberts@gmail.com>
> Tested-by: William Roberts <bill.c.roberts@gmail.com>
> ---
>  tools/testing/selftests/lsm/common.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)

Merged into lsm/dev, thanks!

--
paul-moore.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-07 19:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-03  2:52 [PATCH] selftests/lsm: Fix memory leak in attr_lsm_count Wang Yan
2026-07-03  3:01 ` William Roberts
2026-07-03 12:09   ` [PATCH v2] " Wang Yan
2026-07-03 17:56     ` William Roberts
2026-07-07 19:13     ` Paul Moore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox