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: [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits
Date: Sat, 12 Sep 2026 12:47:28 -0700	[thread overview]
Message-ID: <20260912194730.139780-1-sj@kernel.org> (raw)
In-Reply-To: <20260912193641.04AF61F000FF@smtp.kernel.org>

On Sat, 12 Sep 2026 19:36:40 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> 
> New issues:
> - [High] Missing error handling for damos_commit_filter_arg() and damos_commit_filter() leads to uninitialized memory use for DAMON filters.
> 
> Pre-existing issues:
> - [Critical] Memory allocation failure during damos_commit_dests() leaves the active scheme object partially torn down, leading to a deterministic NULL pointer dereference.
> --
> 
> commit 0ffd278b1f3947b2729c57dd026fc9283304d3af
> Author: SJ Park <sj@kernel.org>
> 
> mm/damon/core-kunit: test invalid damos filter commits
>     
> Add test cases for testing the validation of damos filter arguments in
> commit time.
> 
> > diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
> > index 2b0931cf6fb32..527abc2570616 100644
> > --- a/mm/damon/tests/core-kunit.h
> > +++ b/mm/damon/tests/core-kunit.h
> > @@ -1184,6 +1184,13 @@ static void damos_test_commit_filter(struct kunit *test)
> >  			.allow = false,
> >  			.sz_range = {.min = 234, .max = 345},
> >  			}, false);
> > +	damos_test_commit_filter_for(test, &dst,
> > +			&(struct damos_filter){
> > +			.type = DAMOS_FILTER_TYPE_HUGEPAGE_SIZE,
> > +			.matching = false,
> > +			.allow = false,
> > +			.sz_range = {.min = 456, .max = 123},
> > +			}, true);
> 
> [Severity: High]
> Does this patch series leave uninitialized memory in the payload of DAMON
> filters if an invalid range is provided?
> 
> While this patch adds tests for invalid filter ranges, it appears the error
> handling in earlier patches of the series might be incomplete. If a user
> provides an invalid range (e.g., max < min) via DAMON sysfs, the core
> validation returns -EINVAL, but this error is ignored when allocating a new
> core filter:
> 
> mm/damon/core.c:damos_commit_core_filters() {
>     ...
> 	damos_for_each_core_filter_safe(src_filter, next, src) {
> 		if (j++ < i)
> 			continue;
> 
> 		new_filter = damos_new_filter(
> 				src_filter->type, src_filter->matching,
> 				src_filter->allow);
> 		if (!new_filter)
> 			return -ENOMEM;
> 		damos_commit_filter_arg(new_filter, src_filter);
> 		damos_add_filter(dst, new_filter);
> 	}
>     ...
> }
> 
> Similarly, the return values of both damos_commit_filter() and
> damos_commit_filter_arg() are ignored when updating or allocating an ops
> filter:
> 
> mm/damon/core.c:damos_commit_ops_filters() {
>     ...
> 	damos_for_each_ops_filter_safe(dst_filter, next, dst) {
> 		src_filter = damos_nth_ops_filter(i++, src);
> 		if (src_filter)
> 			damos_commit_filter(dst_filter, src_filter);
> 		else
> 			damos_destroy_filter(dst_filter);
> 	}
> 
> 	damos_for_each_ops_filter_safe(src_filter, next, src) {
> 		if (j++ < i)
> 			continue;
> 
> 		new_filter = damos_new_filter(
> 				src_filter->type, src_filter->matching,
> 				src_filter->allow);
> 		if (!new_filter)
> 			return -ENOMEM;
> 		damos_commit_filter_arg(new_filter, src_filter);
> 		damos_add_filter(dst, new_filter);
> 	}
>     ...
> }
> 
> Since damos_new_filter() allocates with kmalloc_obj() (which does not zero
> memory), ignoring the return value of damos_commit_filter_arg() could lead to
> the uninitialized heap data remaining in the filter's payload
> (e.g. sz_range or addr_range) when it is added to the active DAMON context.

Yes, I will fix in the next spin.

> 
> 
> [Severity: Critical]
> This is a pre-existing issue, but can memory allocation failure during
> damos_commit_dests() leave the active scheme partially torn down and lead to a
> NULL pointer dereference?
> 
> If memory allocation fails for dst->weight_arr, damos_commit_dests() leaves
> dst->nr_dests with its old non-zero value, but assigns NULL to the array:
> 
> mm/damon/core.c:damos_commit_dests() {
>     ...
> 		dst->weight_arr = kmalloc_array(src->nr_dests,
> 			sizeof(*dst->weight_arr), GFP_KERNEL);
> 		if (!dst->weight_arr) {
> 			/* ->node_id_arr will be freed by scheme destruction */
> 			return -ENOMEM;
> 		}
>     ...
> }
> 
> Because damon_commit_schemes() breaks its loop on -ENOMEM and returns, the
> partially modified scheme is left physically linked in the live context's
> scheme list:
> 
> mm/damon/core.c:damon_commit_schemes() {
>     ...
> 		src_scheme = damon_nth_scheme(i++, src);
> 		if (src_scheme) {
> 			err = damos_commit(dst_scheme, src_scheme);
> 			if (err)
> 				return err;
> 		}
>     ...
> }
> 
> When the kdamond worker thread later executes damos_va_migrate_dests_add() on
> this corrupted scheme, it appears it will iterate up to dests->nr_dests and
> dereference the NULL weight_arr pointer:
> 
> mm/damon/vaddr.c:damos_va_migrate_dests_add() {
>     ...
> 	for (i = 0; i < dests->nr_dests; i++)
> 		weight_total += dests->weight_arr[i];
>     ...
> }
> 
> Is there a mechanism preventing this kernel panic if memory allocation
> fails on the live destination context?

Yes, we have damon_ctx->maybe_corrupted to prevent this.  So, no issue.

> 
> [ ... ]
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260912191807.138502-1-sj@kernel.org?part=8
> 


Thanks,
SJ

      reply	other threads:[~2026-09-12 19:47 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-12 19:17 [RFC PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
2026-09-12 19:17 ` [RFC PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once SJ Park
2026-09-12 19:37   ` sashiko-bot
2026-09-12 19:18 ` [RFC PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range " SJ Park
2026-09-12 19:26   ` sashiko-bot
2026-09-12 19:18 ` [RFC PATCH 3/8] mm/damon/core: return an error from damos_commit_filter_arg() SJ Park
2026-09-12 19:29   ` sashiko-bot
2026-09-12 19:37     ` SJ Park
2026-09-12 19:18 ` [RFC PATCH 4/8] mm/damon/core: disallow max < min damos filter range arguments commit SJ Park
2026-09-12 19:29   ` sashiko-bot
2026-09-12 19:42     ` SJ Park
2026-09-12 19:18 ` [RFC PATCH 5/8] mm/damon/sysfs-schemes: drop centralized filter range arg validations SJ Park
2026-09-12 19:30   ` sashiko-bot
2026-09-12 19:43     ` SJ Park
2026-09-12 19:18 ` [RFC PATCH 6/8] mm/damon/sysfs-schemes: use switch-case in add_scheme_filters() SJ Park
2026-09-12 19:23   ` sashiko-bot
2026-09-12 19:18 ` [RFC PATCH 7/8] mm/damon/core-kunit: extend damos_commit_filter_for() for wrong input SJ Park
2026-09-12 19:28   ` sashiko-bot
2026-09-12 19:18 ` [RFC PATCH 8/8] mm/damon/core-kunit: test invalid damos filter commits SJ Park
2026-09-12 19:36   ` sashiko-bot
2026-09-12 19:47     ` SJ Park [this message]

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=20260912194730.139780-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