Re: [PATCH v2 2/3] mm/damon/sysfs: check contexts->nr before accessing contexts_arr[0]
From: SeongJae Park
Date: Fri Mar 20 2026 - 20:58:19 EST
On Fri, 20 Mar 2026 16:35:58 +0000 Josh Law <objecting@xxxxxxxxxxxxx> wrote:
> Multiple sysfs command paths dereference contexts_arr[0] without first
> verifying that nr_contexts >= 1.
Nit. There is no 'nr_contexts' in the code. Let's use kdamond->contexts->nr
or contexts->nr instead.
> A user can set nr_contexts to 0 via
> sysfs while DAMON is running, causing NULL pointer dereferences.
It would be nice to explain how users can reproducer the issue. Could you
please add below to this part of the commit message?
'''
The issue can be triggered by privileged users like below.
First, start DAMON and make contexts directory empty (kdamond->contexts->nr ==
0).
# damo start
# cd /sys/kernel/mm/damon/admin/kdamonds/0
# echo 0 > contexts/nr_contexts
Then, any of below commands will cause the NULL pointer dereference.
# echo update_schemes_stats > state
# echo update_schemes_tried_regions > state
# echo update_schemes_tried_bytes > state
# echo update_schemes_effective_quotas > state
# echo update_tuned_intervals > state
'''
>
> Guard all commands (except OFF) at the entry point of
> damon_sysfs_handle_cmd().
>
> Fixes: 0ac32b8affb5 ("mm/damon/sysfs: support DAMOS stats")
> Cc: <stable@xxxxxxxxxxxxxxx> # 5.18.x
> Signed-off-by: Josh Law <objecting@xxxxxxxxxxxxx>
> Reviewed-by: SeongJae Park <sj@xxxxxxxxxx>
I suggested [1] this patch. But I didn't give you 'Revied-by:' tag. So the
above Reviewed-by: is not valid.
And this patch looks good to me, so please take my valid Reviewed-by: tag.
Reviewed-by: SeongJae Park <sj@xxxxxxxxxx>
> ---
>From the next time, please add patch version changelog [2] here.
I added this patch into my tree after resolving the trivial things I mentioned
above. I will post it as v3 tomorrow, unless Andrew add this to mm.git after
resolving the trivial things as I mentioned.
[1] https://lore.kernel.org/20260320155115.101025-1-sj@xxxxxxxxxx
[2] https://docs.kernel.org/process/submitting-patches.html#commentary
Thanks,
SJ
[...]