DAMON development mailing list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: yahia <yahia.a.abdrabou@gmail.com>
Cc: damon@lists.linux.dev
Subject: Re: [PATCH 2/2] mm/damon/reclaim: fix potential memory leak
Date: Fri, 19 Jun 2026 23:56:24 +0000	[thread overview]
Message-ID: <20260619235625.21A191F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260619234606.221163-3-yahia.a.abdrabou@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] The patch attempts to fix a memory leak by destroying the global `ctx` pointer on the error path, but fails to set it to `NULL`, leaving a dangling pointer that causes deterministic Use-After-Free vulnerabilities upon any sysfs interaction.

Pre-existing issues:
- [High] Pre-existing memory leak of `param_ctx` and its associated components when `damon_reclaim_apply_parameters()` succeeds.
--

commit 87008af2e1842854e4e34be35814335307b60672
Author: yahia ahmed <yahia.a.abdrabou@gmail.com>

mm/damon/reclaim: fix potential memory leak

Fix a potential memory leak if initialization encounters
an error, ctx won't be destroyed.

> diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
> index 15bc06ae75053..dfe52717d92cf 100644
> --- a/mm/damon/reclaim.c
> +++ b/mm/damon/reclaim.c
> @@ -481,6 +481,10 @@ static int __init damon_reclaim_init(void)
>  out:
>  	if (err && enabled)
>  		enabled = false;
> +
> +	/* Prevent potenial memory leakage */
> +	if (err && ctx)
> +		damon_destroy_ctx(ctx);

[Severity: Critical]
Does this leave a dangling pointer in the global ctx variable?

Because ctx is global, if it is freed here but not set to NULL, sysfs
callbacks like damon_reclaim_enabled() will bypass their safety checks:

mm/damon/reclaim.c:damon_reclaim_enabled() {
	if (!ctx)
		return false;
	return damon_is_running(ctx);
}

This would pass the freed context to damon_is_running(), which blindly
acquires the mutex:

mm/damon/core.c:damon_is_running() {
	...
	mutex_lock(&ctx->kdamond_lock);
	...
}

Can this trigger a use-after-free when sysfs parameters are accessed after
initialization fails?

>  	return err;
>  }

[Severity: High]
This is a pre-existing issue, but does damon_reclaim_apply_parameters() leak
param_ctx on the success path?

In mm/damon/reclaim.c:damon_reclaim_apply_parameters():

	err = damon_commit_ctx(ctx, param_ctx);

	if (!err)
		return 0;
out:
	damon_destroy_ctx(param_ctx);
	return err;

Since damon_commit_ctx() copies components and does not take ownership of
param_ctx, returning 0 directly without destroying param_ctx appears to leak
the locally allocated context and its attached components.

Could this lead to memory exhaustion if parameters are successfully applied
multiple times?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260619234606.221163-1-yahia.a.abdrabou@gmail.com?part=2

  reply	other threads:[~2026-06-19 23:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-19 23:46 [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues yahia
2026-06-19 23:46 ` [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx yahia
2026-06-19 23:51   ` sashiko-bot
2026-06-19 23:46 ` [PATCH 2/2] mm/damon/reclaim: fix potential memory leak yahia
2026-06-19 23:56   ` sashiko-bot [this message]
2026-06-20  0:26 ` [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues SeongJae Park
2026-06-20  0:35   ` [PATCH v2 0/1] fix initialization " yahia
2026-06-20  0:35     ` [PATCH v2 1/1] mm/damon/reclaim: fix potential memory leak yahia
2026-06-20  2:48       ` sashiko-bot
2026-06-20  3:29       ` [PATCH v3] " yahia
2026-06-20 16:40         ` SeongJae Park
2026-06-20 16:50           ` [PATCH v1] " yahia
2026-06-20 17:09             ` SeongJae Park
2026-06-20 17:20               ` yahia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260619235625.21A191F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yahia.a.abdrabou@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox