linux/mm/damon
SeongJae Park 4c04c6b47c mm/damon/stat: deallocate damon_call() failure leaking damon_ctx
damon_stat_start() always allocates the module's damon_ctx object
(damon_stat_context).  Meanwhile, if damon_call() in the function fails,
the damon_ctx object is not deallocated.  Hence, if the damon_call() is
failed, and the user writes Y to “enabled” again, the previously
allocated damon_ctx object is leaked.

This cannot simply be fixed by deallocating the damon_ctx object when
damon_call() fails.  That's because damon_call() failure doesn't guarantee
the kdamond main function, which accesses the damon_ctx object, is
completely finished.  In other words, if damon_stat_start() deallocates
the damon_ctx object after damon_call() failure, the not-yet-terminated
kdamond could access the freed memory (use-after-free).

Fix the leak while avoiding the use-after-free by keeping returning
damon_stat_start() without deallocating the damon_ctx object after
damon_call() failure, but deallocating it when the function is invoked
again and the kdamond is completely terminated.  If the kdamond is not yet
terminated, simply return -EAGAIN, as the kdamond will soon be terminated.

The issue was discovered [1] by sashiko.

Link: https://lkml.kernel.org/r/20260402134418.74121-1-sj@kernel.org
Link: https://lore.kernel.org/20260401012428.86694-1-sj@kernel.org [1]
Fixes: 405f61996d ("mm/damon/stat: use damon_call() repeat mode instead of damon_callback")
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org> # 6.17.x
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2026-04-06 11:13:43 -07:00
..
tests Convert remaining multi-line kmalloc_obj/flex GFP_KERNEL uses 2026-02-22 08:26:33 -08:00
core.c mm/damon/core: avoid use of half-online-committed context 2026-03-21 17:36:33 -07:00
Kconfig mm/damon/Kconfig: make DAMON_STAT_ENABLED_DEFAULT depend on DAMON_STAT 2025-09-13 16:55:07 -07:00
lru_sort.c mm/damon: rename min_sz_region of damon_ctx to min_region_sz 2026-01-31 14:22:47 -08:00
Makefile mm/damon: introduce DAMON_STAT module 2025-07-09 22:41:55 -07:00
modules-common.c mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
modules-common.h mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
ops-common.c memcg: rename mem_cgroup_ino() to mem_cgroup_id() 2026-01-26 20:02:25 -08:00
ops-common.h mm/damon/paddr: move filters existence check function to ops-common 2025-09-13 16:54:47 -07:00
paddr.c mm/damon: remove damon_operations->cleanup() 2026-01-31 14:22:45 -08:00
reclaim.c mm/damon: rename min_sz_region of damon_ctx to min_region_sz 2026-01-31 14:22:47 -08:00
stat.c mm/damon/stat: deallocate damon_call() failure leaking damon_ctx 2026-04-06 11:13:43 -07:00
sysfs-common.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
sysfs-common.h mm/damon: s/primitives/code/ on comments 2025-06-05 21:55:41 -07:00
sysfs-schemes.c Convert more 'alloc_obj' cases to default GFP_KERNEL arguments 2026-02-21 20:03:00 -08:00
sysfs.c mm/damon/sysfs: dealloc repeat_call_control if damon_call() fails 2026-04-06 11:13:42 -07:00
vaddr.c Convert more 'alloc_obj' cases to default GFP_KERNEL arguments 2026-02-21 20:03:00 -08:00