Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 0/8] mm/damon: move damos filter range arguments validation to core
@ 2026-09-14 14:23 SJ Park
  2026-09-14 14:23 ` [PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once SJ Park
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: SJ Park @ 2026-09-14 14:23 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SJ Park, stable, Brendan Higgins, David Gow, Usama Arif, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

DAMOS filter range arguments are being validated in the DAMON sysfs
interface.  Some of those have minor time-of-check to time-of-use
(TOCTOU) bugs.  In future, other callers might have duplicated
validations with similar bugs.

Fix the bugs and further refactor the code to move the validation to the
core layer.  Also add kunit test cases for the validations.

Patches 1 and 2 fix the existing TOCTOU bugs.  Patches 3 and 4 adds the
validation in the core layer.  Patch 5 drops the replicated validation
in DAMON sysfs interface.  Patch 6 further cleanup the code.  Patches 7
and 8 extends kunit tests to test the validation.

Changes from RFC v2
- RFC v2: https://lore.kernel.org/20260913171706.103052-1-sj@kernel.org
- Drop RFC tag.
- Rebase to latest mm-new.
Changes from RFC
- RFC: https://lore.kernel.org/20260912191807.138502-1-sj@kernel.org
- Handle error from damos_commit_filter_arg() on all call sites.

SJ Park (8):
  mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once
  mm/damon/sysfs-schemes: read sysfs_filter->sz_range only once
  mm/damon/core: return an error from damos_commit_filter_arg()
  mm/damon/core: disallow max < min damos filter range arguments commit
  mm/damon/sysfs-schemes: drop centralized filter range arg validations
  mm/damon/sysfs-schemes: use switch-case in add_scheme_filters()
  mm/damon/core-kunit: extend damos_commit_filter_for() for wrong input
  mm/damon/core-kunit: test invalid damos filter commits

 mm/damon/core.c             | 47 +++++++++++++++++++++++++++----------
 mm/damon/sysfs-schemes.c    | 32 ++++++++++---------------
 mm/damon/tests/core-kunit.h | 38 +++++++++++++++++++++++-------
 3 files changed, 76 insertions(+), 41 deletions(-)


base-commit: 5e38c61dbb4349ff7e900d97d96f369c64678eea
-- 
2.47.3

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

* [PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once
  2026-09-14 14:23 [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
@ 2026-09-14 14:23 ` SJ Park
  2026-09-14 14:23 ` [PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range " SJ Park
  2026-09-14 23:43 ` [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
  2 siblings, 0 replies; 4+ messages in thread
From: SJ Park @ 2026-09-14 14:23 UTC (permalink / raw)
  To: Andrew Morton; +Cc: SJ Park, stable, damon, linux-kernel, linux-mm

DAMON sysfs interface reads the user-provided address range arguments
for addr type DAMOS filter twice.  Once for validation, and once again
for assignments to the variable that will be passed to the core
layer. If the user updates the argument in parallel, an invalid address
range could be passed to the core layer.  Avoid it by doing the
assignments first, and then validating the assigned variables before
passing those to the core layer.

User impact of the bug should be trivial.  From the core layer's
perspective, the invalid address range is not really invalid.  It just
works as having a weird address range.  No critical issues such as a
crash or a leak could happen.  And sane users ain't do such parallel
arguments update anyway.  If they do, such racy behavior is arguably
somewhat expected and deserved.  That said, there is no reason to keep
such races.

Fixes: 2f1abcfccd86 ("mm/damon/sysfs-schemes: support address range type DAMOS filter")
Cc: <stable@vger.kernel.org> # 6.6.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/sysfs-schemes.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
index 3de4d804e049f..3c1c1cb387fec 100644
--- a/mm/damon/sysfs-schemes.c
+++ b/mm/damon/sysfs-schemes.c
@@ -2831,12 +2831,12 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,
 				return err;
 			}
 		} else if (filter->type == DAMOS_FILTER_TYPE_ADDR) {
-			if (sysfs_filter->addr_range.end <
-					sysfs_filter->addr_range.start) {
+			filter->addr_range = sysfs_filter->addr_range;
+			if (filter->addr_range.end <
+					filter->addr_range.start) {
 				damos_destroy_filter(filter);
 				return -EINVAL;
 			}
-			filter->addr_range = sysfs_filter->addr_range;
 		} else if (filter->type == DAMOS_FILTER_TYPE_TARGET) {
 			filter->target_idx = sysfs_filter->target_idx;
 		} else if (filter->type == DAMOS_FILTER_TYPE_HUGEPAGE_SIZE) {
-- 
2.47.3

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

* [PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range only once
  2026-09-14 14:23 [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
  2026-09-14 14:23 ` [PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once SJ Park
@ 2026-09-14 14:23 ` SJ Park
  2026-09-14 23:43 ` [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
  2 siblings, 0 replies; 4+ messages in thread
From: SJ Park @ 2026-09-14 14:23 UTC (permalink / raw)
  To: Andrew Morton; +Cc: SJ Park, stable, Usama Arif, damon, linux-kernel, linux-mm

DAMON sysfs interface reads the user-provided size range arguments for
hugepage_size type DAMOS filter twice.  Once for validation, and once
again for assignments to the variable that will be passed to the core
layer.  If the user updates the arguments in parallel, an invalid size
range could be passed to the core layer.  Avoid it by doing the
assignments first, and then validating the assigned variables before
passing those to the core layer.

User impact of the bug should be trivial.  From the core layer's
perspective, the invalid size range is not really invalid.  It just
works as having a weird size range.  No critical issues such as a crash
or a leak could happen.  And sane users ain't do such parallel arguments
update anyway.  If they do, such racy behavior is arguably somewhat
expected and deserved.  That said, there is no reason to keep such
races.

Fixes: ea1f204ba29a ("mm/damon/sysfs-schemes: add files for setting damos_filter->sz_range")
Cc: <stable@vger.kernel.org> # 6.15.x
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/sysfs-schemes.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
index 3c1c1cb387fec..8c8ab82c8facc 100644
--- a/mm/damon/sysfs-schemes.c
+++ b/mm/damon/sysfs-schemes.c
@@ -2840,13 +2840,12 @@ static int damon_sysfs_add_scheme_filters(struct damos *scheme,
 		} else if (filter->type == DAMOS_FILTER_TYPE_TARGET) {
 			filter->target_idx = sysfs_filter->target_idx;
 		} else if (filter->type == DAMOS_FILTER_TYPE_HUGEPAGE_SIZE) {
-			if (sysfs_filter->range_min >
-					sysfs_filter->range_max) {
+			filter->sz_range.min = sysfs_filter->range_min;
+			filter->sz_range.max = sysfs_filter->range_max;
+			if (filter->range_min > filter->range_max) {
 				damos_destroy_filter(filter);
 				return -EINVAL;
 			}
-			filter->sz_range.min = sysfs_filter->range_min;
-			filter->sz_range.max = sysfs_filter->range_max;
 		} else if (filter->type == DAMOS_FILTER_TYPE_PROBE_HITS_WSUM) {
 			filter->range_min = sysfs_filter->range_min;
 			filter->range_max = sysfs_filter->range_max;
-- 
2.47.3

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

* Re: [PATCH 0/8] mm/damon: move damos filter range arguments validation to core
  2026-09-14 14:23 [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
  2026-09-14 14:23 ` [PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once SJ Park
  2026-09-14 14:23 ` [PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range " SJ Park
@ 2026-09-14 23:43 ` SJ Park
  2 siblings, 0 replies; 4+ messages in thread
From: SJ Park @ 2026-09-14 23:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SJ Park, stable, Brendan Higgins, David Gow, Usama Arif, damon,
	kunit-dev, linux-kernel, linux-kselftest, linux-mm

On Mon, 14 Sep 2026 07:23:17 -0700 SJ Park <sj@kernel.org> wrote:

> DAMOS filter range arguments are being validated in the DAMON sysfs
> interface.  Some of those have minor time-of-check to time-of-use
> (TOCTOU) bugs.  In future, other callers might have duplicated
> validations with similar bugs.
> 
> Fix the bugs and further refactor the code to move the validation to the
> core layer.  Also add kunit test cases for the validations.

Sashiko found no blocker for this series.  Sashiko send its findings to damon@
mailing list [1], and I reply those if anything needs to be clarified.  Please
refer to those for details.

[1] https://lore.kernel.org/damon/


Thanks,
SJ

[...]

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

end of thread, other threads:[~2026-09-14 23:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-14 14:23 [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park
2026-09-14 14:23 ` [PATCH 1/8] mm/damon/sysfs-schemes: read sysfs_filter->addr_range only once SJ Park
2026-09-14 14:23 ` [PATCH 2/8] mm/damon/sysfs-schemes: read sysfs_filter->sz_range " SJ Park
2026-09-14 23:43 ` [PATCH 0/8] mm/damon: move damos filter range arguments validation to core SJ Park

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