DAMON development mailing list
 help / color / mirror / Atom feed
From: SJ Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SJ Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [PATCH 08/11] samples/damon/mtier: stop all contexts with single damon_stop() call
Date: Mon,  6 Jul 2026 07:35:52 -0700	[thread overview]
Message-ID: <20260706143554.94084-1-sj@kernel.org> (raw)
In-Reply-To: <20260706142148.B2B1A1F000E9@smtp.kernel.org>

On Mon, 06 Jul 2026 14:21:47 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] Missing synchronization between module initialization and the module parameter sysfs store callback can lead to Use-After-Free and NULL pointer dereference.
> --
> 
> commit dbce89ab1942b321167e83b5cb17dfd9002ce893
> Author: SJ Park <sj@kernel.org>
> 
> samples/damon/mtier: stop all contexts with single damon_stop() call
> 
> This commit refactors damon_sample_mtier_stop() to use a single
> damon_stop() call to stop all requested DAMON contexts. The previous
> implementation used two separate calls because damon_stop() could
> theoretically return a failure, but it now always stops all contexts
> successfully.
> 
> > diff --git a/samples/damon/mtier.c b/samples/damon/mtier.c
> > index 90881e8bd441f..ac9c24b92ead8 100644
> > --- a/samples/damon/mtier.c
> > +++ b/samples/damon/mtier.c
> > @@ -200,8 +200,7 @@ static int damon_sample_mtier_start(void)
> >  
> >  static void damon_sample_mtier_stop(void)
> >  {
> > -	damon_stop(ctxs, 1);
> > -	damon_stop(&ctxs[1], 1);
> > +	damon_stop(ctxs, 2);
> [Severity: High]
> This is a pre-existing issue, but could a concurrent sysfs write
> trigger a use-after-free and null pointer dereference here?
> 
> When the module is loaded with the enabled parameter set to true,
> damon_sample_mtier_init() attempts to initialize the ctxs array. If
> initialization fails, such as failing to allocate ctxs[1], it destroys
> ctxs[0] but leaves it as a dangling pointer.
> 
> Concurrently, a userspace process writing to the enable sysfs parameter
> triggers damon_sample_mtier_enable_store(). If this write observes the
> global enabled state as true before the initialization failure resets it
> to false, it proceeds to call damon_sample_mtier_stop().
> 
> When damon_sample_mtier_stop() calls damon_stop(ctxs, 2), it loops over
> both array elements, calling __damon_stop() on them. This dereferences
> the pointers to lock ctx->kdamond_lock, potentially causing a
> use-after-free on ctxs[0] and a null pointer dereference on ctxs[1].

Wrong.  mtier is not a loadable module, so this race cannot happen.

> 
> >  	damon_destroy_ctx(ctxs[0]);
> >  	damon_destroy_ctx(ctxs[1]);
> >  }
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260706140628.87414-1-sj@kernel.org?part=8

Thanks,
SJ

[...]

  reply	other threads:[~2026-07-06 14:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06 14:06 [PATCH 00/11] mm/damon: refactor damon_{start,stop,commit}() for simple error handling SJ Park
2026-07-06 14:06 ` [PATCH 01/11] mm/damon/core: stop ctxs in damon_start() before returning an error SJ Park
2026-07-06 14:06 ` [PATCH 02/11] samples/damon/mtier: do not stop first context for damon_start() failure SJ Park
2026-07-06 14:06 ` [PATCH 03/11] mm/damon/core: make damon_stop() never fails SJ Park
2026-07-06 14:06 ` [PATCH 04/11] mm/damon/sysfs: ignore damon_stop() return value SJ Park
2026-07-06 14:06 ` [PATCH 05/11] mm/damon/reclaaim: " SJ Park
2026-07-06 14:06 ` [PATCH 06/11] mm/damon/lru_sort: " SJ Park
2026-07-06 14:06 ` [PATCH 07/11] mm/damon/core: change damon_stop() return type to void SJ Park
2026-07-06 14:06 ` [PATCH 08/11] samples/damon/mtier: stop all contexts with single damon_stop() call SJ Park
2026-07-06 14:21   ` sashiko-bot
2026-07-06 14:35     ` SJ Park [this message]
2026-07-06 14:06 ` [PATCH 09/11] mm/damon/core: wait ctx stop in damon_call() before reruning an error SJ Park
2026-07-06 14:06 ` [PATCH 10/11] samples/damon/wsse: do not stop ctx for damon_call() failure SJ Park
2026-07-06 14:06 ` [PATCH 11/11] samples/damon/prcl: do not stop DAMON " SJ Park

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=20260706143554.94084-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-bot@kernel.org \
    /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