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 v1.1 4/7] mm/damon/core-kunit: handle region split failure in filter_out()
Date: Fri, 17 Jul 2026 07:56:34 -0700	[thread overview]
Message-ID: <20260717145635.92237-1-sj@kernel.org> (raw)
In-Reply-To: <20260717144904.836FC1F000E9@smtp.kernel.org>

On Fri, 17 Jul 2026 14:49:04 +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] This problem wasn't introduced by this patch, but it shares the exact same logic flaw fixed here. In `damon_apply_min_nr_regions()`, if `damon_split_region_at()` fails to allocate a new region (e.g., due to memory pressure), it leaves `r` unmodified. However, the inner `while` loop unconditionally executes `r = damon_next_region(r);`. If `r` was the last region in the list, `damon_next_region(r)` will return a pointer derived from the list head (`&t->regions_list`), resulting in a fake `damon_region` pointer pointing into the middle of the `damon_target` struct or preceding memory. The loop then evaluates `damon_sz_region(r)` on this fake region, reading garbage memory. If the garbage evaluates to a size greater than `max_region_sz`, it will invoke `damon_split_region_at()` on the fake region, causing an out-of-bounds write and list corruption (overwriting `r_fake->ar.end` and `t->regions_list`), or an infinite loop lockup if allocations keep failing.
> --
> 
> --- Patch [4]: [RFC PATCH v1.1 4/7] mm/damon/core-kunit: handle region split failure in filter_out() ---
> commit 571c1400b8cb843c3acdb1ded0915fd9807f302f
> Author: SJ Park <sj@kernel.org>
> 
> mm/damon/core-kunit: handle region split failure in filter_out()
> 
> This commit fixes a bug in the DAMON kunit tests where test execution
> continued even if a region failed to split as expected. By exiting early
> when the number of regions does not match expectations, it prevents
> dereferencing invalid pointers and potential memory corruption.
> 
> Link: https://lore.kernel.org/20260714142352.100478-1-sj@kernel.org
> 
> > diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
> > index 322a295ac2cec..eddf3a0484583 100644
> > --- a/mm/damon/tests/core-kunit.h
> > +++ b/mm/damon/tests/core-kunit.h
> > @@ -1365,6 +1365,8 @@ static void damos_test_filter_out(struct kunit *test)
> >  	KUNIT_EXPECT_EQ(test, r->ar.start, 1);
> >  	KUNIT_EXPECT_EQ(test, r->ar.end, 2);
> >  	KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 2);
> > +	if (damon_nr_regions(t) != 2)
> > +		goto out;
> 
> [Severity: High]
> This isn't a bug introduced by this patch, but it shares the exact same logic
> flaw fixed here.
> 
> In mm/damon/core.c:damon_apply_min_nr_regions():
> 
> 		damon_for_each_region_safe(r, next, t) {
> 			while (damon_sz_region(r) > max_region_sz) {
> 				damon_split_region_at(t, r, max_region_sz);
> 				r = damon_next_region(r);
> 			}
> 		}
> 
> If damon_split_region_at() fails to allocate a new region (e.g., due to memory
> pressure), it leaves r unmodified. However, the inner while loop
> unconditionally executes r = damon_next_region(r).
> 
> If r was the last region in the list, damon_next_region(r) will return a
> pointer derived from the list head (&t->regions_list), resulting in a fake
> damon_region pointer. The loop then evaluates damon_sz_region(r) on this fake
> region, reading garbage memory.
> 
> If the garbage evaluates to a size greater than max_region_sz, it will invoke
> damon_split_region_at() on the fake region.
> 
> Can this lead to an out-of-bounds write and list corruption, or an infinite
> loop lockup if allocations keep failing?

This is a known issue that I'm separately working on.

> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260717143445.91207-1-sj@kernel.org?part=4


Thanks,
SJ

  reply	other threads:[~2026-07-17 14:56 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17 14:34 [RFC PATCH v1.1 0/7] mm/damon: fix uninitialized DAMOS field and kunit exec expectation bugs SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 1/7] mm/damon/core: initialize damos->last_applied SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 2/7] mm/damon/core-kunit: check region count before testing in split_at() SJ Park
2026-07-17 14:51   ` sashiko-bot
2026-07-17 14:54     ` SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 3/7] mm/damon/vaddr-kunit: check region count in three_regions test SJ Park
2026-07-17 14:50   ` sashiko-bot
2026-07-17 14:55     ` SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 4/7] mm/damon/core-kunit: handle region split failure in filter_out() SJ Park
2026-07-17 14:49   ` sashiko-bot
2026-07-17 14:56     ` SJ Park [this message]
2026-07-17 14:34 ` [RFC PATCH v1.1 5/7] mm/damon/core-kunit: skip wrong dest walk in commit_dests_for() SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 6/7] mm/damon/core-kunit: skip wrong quota goal walk in commit_quota_goals() SJ Park
2026-07-17 14:34 ` [RFC PATCH v1.1 7/7] mm/damon/core-kunit: skip wrong region walk in commit_target_regions() 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=20260717145635.92237-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