* [PATCH] kcsan: Avoid division by zero in kcsan_prandom_u32_max()
@ 2026-09-30 13:38 Andrea Parri
2026-09-30 14:46 ` Marco Elver
0 siblings, 1 reply; 2+ messages in thread
From: Andrea Parri @ 2026-09-30 13:38 UTC (permalink / raw)
To: Marco Elver
Cc: Andrea Parri, Dmitry Vyukov, Paul E. McKenney, kasan-dev,
linux-kernel, stable
KCSAN's skip_watch, udelay_task and udelay_interrupt parameters can be
changed at runtime through /sys/module/kcsan/parameters/, where 0 means
watching every access or not delaying at all. With the default
CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE=y and CONFIG_KCSAN_DELAY_RANDOMIZE=y,
writing 0 to any of them makes the next watchpoint setup divide by zero:
# echo 0 > /sys/module/kcsan/parameters/udelay_task
Oops: divide error: 0000 [#1] SMP NOPTI
CPU: 0 UID: 0 PID: 93 Comm: sh Not tainted 7.3.0-rc5 #3 PREEMPT(lazy)
RIP: 0010:kcsan_setup_watchpoint+0x1b5/0x410
Code: ... 65 89 05 ad 8d 98 02 d3 ee <f7> f6 29 d7 e8 22 0e 0f 01 ...
RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
...
__show_trace_log_lvl+0x1bd/0x315
__die_body.cold+0x19/0x2b
die+0x34/0x50
...
BUG: TASK stack guard page was hit at ffffd3ab001fbfe8 (...)
reset_kcsan_skip() and delay_access() pass these values unchecked to
kcsan_prandom_u32_max(), which returns "state % ep_ro". delay_access()
halves the delay for compound and assert accesses, so
udelay_interrupt=1 is enough there.
The oops path runs instrumented code, which faults again until the stack
overflows. With skip_watch=0 the guest hangs without printing anything.
The division came in with commit 71a076f4a61a ("kcsan: Rewrite
kcsan_prandom_u32_max() without prandom_u32_state()"). Before it, the
helper scaled the random value with a multiply-shift, which yields 0 for
ep_ro == 0. Go back to that via reciprocal_scale().
Tested in an x86-64 KCSAN guest with virtme-ng: after this patch,
writing 0 to each parameter, or 1 to udelay_interrupt, no longer
oopses. skip_watch=0 now watches every access, which is very slow with
the default delays but completes when they are set to 0.
Fixes: 71a076f4a61a ("kcsan: Rewrite kcsan_prandom_u32_max() without prandom_u32_state()")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
---
kernel/kcsan/core.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 2db82661cd60a..a3aa5e5b039dc 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -290,8 +290,9 @@ should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int t
}
/*
- * Returns a pseudo-random number in interval [0, ep_ro). Simple linear
- * congruential generator, using constants from "Numerical Recipes".
+ * Returns a pseudo-random number in interval [0, ep_ro), or 0 if ep_ro is 0,
+ * which the runtime-tunable callers may pass. Simple linear congruential
+ * generator, using constants from "Numerical Recipes".
*/
static u32 kcsan_prandom_u32_max(u32 ep_ro)
{
@@ -300,7 +301,7 @@ static u32 kcsan_prandom_u32_max(u32 ep_ro)
state = 1664525 * state + 1013904223;
this_cpu_write(kcsan_rand_state, state);
- return state % ep_ro;
+ return reciprocal_scale(state, ep_ro);
}
static inline void reset_kcsan_skip(void)
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] kcsan: Avoid division by zero in kcsan_prandom_u32_max()
2026-09-30 13:38 [PATCH] kcsan: Avoid division by zero in kcsan_prandom_u32_max() Andrea Parri
@ 2026-09-30 14:46 ` Marco Elver
0 siblings, 0 replies; 2+ messages in thread
From: Marco Elver @ 2026-09-30 14:46 UTC (permalink / raw)
To: Andrea Parri
Cc: Dmitry Vyukov, Paul E. McKenney, kasan-dev, linux-kernel, stable
On Wed, 30 Sept 2026 at 15:38, Andrea Parri <parri.andrea@gmail.com> wrote:
>
> KCSAN's skip_watch, udelay_task and udelay_interrupt parameters can be
> changed at runtime through /sys/module/kcsan/parameters/, where 0 means
> watching every access or not delaying at all. With the default
> CONFIG_KCSAN_SKIP_WATCH_RANDOMIZE=y and CONFIG_KCSAN_DELAY_RANDOMIZE=y,
> writing 0 to any of them makes the next watchpoint setup divide by zero:
>
> # echo 0 > /sys/module/kcsan/parameters/udelay_task
> Oops: divide error: 0000 [#1] SMP NOPTI
> CPU: 0 UID: 0 PID: 93 Comm: sh Not tainted 7.3.0-rc5 #3 PREEMPT(lazy)
> RIP: 0010:kcsan_setup_watchpoint+0x1b5/0x410
> Code: ... 65 89 05 ad 8d 98 02 d3 ee <f7> f6 29 d7 e8 22 0e 0f 01 ...
> RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000000
> ...
> __show_trace_log_lvl+0x1bd/0x315
> __die_body.cold+0x19/0x2b
> die+0x34/0x50
> ...
> BUG: TASK stack guard page was hit at ffffd3ab001fbfe8 (...)
>
> reset_kcsan_skip() and delay_access() pass these values unchecked to
> kcsan_prandom_u32_max(), which returns "state % ep_ro". delay_access()
> halves the delay for compound and assert accesses, so
> udelay_interrupt=1 is enough there.
>
> The oops path runs instrumented code, which faults again until the stack
> overflows. With skip_watch=0 the guest hangs without printing anything.
>
> The division came in with commit 71a076f4a61a ("kcsan: Rewrite
> kcsan_prandom_u32_max() without prandom_u32_state()"). Before it, the
> helper scaled the random value with a multiply-shift, which yields 0 for
> ep_ro == 0. Go back to that via reciprocal_scale().
>
> Tested in an x86-64 KCSAN guest with virtme-ng: after this patch,
> writing 0 to each parameter, or 1 to udelay_interrupt, no longer
> oopses. skip_watch=0 now watches every access, which is very slow with
> the default delays but completes when they are set to 0.
>
> Fixes: 71a076f4a61a ("kcsan: Rewrite kcsan_prandom_u32_max() without prandom_u32_state()")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
Reviewed-by: Marco Elver <elver@google.com>
Thanks.
> ---
> kernel/kcsan/core.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
> index 2db82661cd60a..a3aa5e5b039dc 100644
> --- a/kernel/kcsan/core.c
> +++ b/kernel/kcsan/core.c
> @@ -290,8 +290,9 @@ should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int t
> }
>
> /*
> - * Returns a pseudo-random number in interval [0, ep_ro). Simple linear
> - * congruential generator, using constants from "Numerical Recipes".
> + * Returns a pseudo-random number in interval [0, ep_ro), or 0 if ep_ro is 0,
> + * which the runtime-tunable callers may pass. Simple linear congruential
> + * generator, using constants from "Numerical Recipes".
> */
> static u32 kcsan_prandom_u32_max(u32 ep_ro)
> {
> @@ -300,7 +301,7 @@ static u32 kcsan_prandom_u32_max(u32 ep_ro)
> state = 1664525 * state + 1013904223;
> this_cpu_write(kcsan_rand_state, state);
>
> - return state % ep_ro;
> + return reciprocal_scale(state, ep_ro);
> }
>
> static inline void reset_kcsan_skip(void)
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-30 14:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-30 13:38 [PATCH] kcsan: Avoid division by zero in kcsan_prandom_u32_max() Andrea Parri
2026-09-30 14:46 ` Marco Elver
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®