Re: [PATCH] kcov: ignore an out-of-range comparison count in write_comp_data()
From: Alexander Potapenko
Date: Fri Sep 18 2026 - 11:07:37 EST
On Thu, Sep 17, 2026 at 12:43 PM Fang Xieyan <fangxy@xxxxxxxxxxxx> wrote:
>
> write_comp_data() reads the comparison record count from area[0] and uses
> it to index the coverage buffer:
>
> area = (u64 *)t->kcov_area;
> max_pos = t->kcov_size * sizeof(unsigned long);
>
> count = READ_ONCE(area[0]);
>
> /* Every record is KCOV_WORDS_PER_CMP 64-bit words. */
> start_index = 1 + count * KCOV_WORDS_PER_CMP;
> end_pos = (start_index + KCOV_WORDS_PER_CMP) * sizeof(u64);
> if (likely(end_pos <= max_pos)) {
>
> The buffer is mmap'd writable into the collecting process, so count is
> under its control and end_pos <= max_pos is its only bound. A count that
> wraps the u64 multiply leaves end_pos below max_pos, so the check passes
> while the record store lands 24 bytes before the buffer, in the unmapped
> vmalloc guard page, and faults:
>
> BUG: unable to handle page fault for address: ffa0000000b60fe8
> #PF: supervisor write access in kernel mode
> #PF: error_code(0x0002) - not-present page
> Oops: 0002 [#1] SMP KASAN NOPTI
> RIP: 0010:write_comp_data+0x7e/0xa0
> ...
> Kernel panic - not syncing: Fatal exception
>
> Bound count first: only max_pos / (sizeof(u64) * KCOV_WORDS_PER_CMP)
> records fit, so a larger count is not a valid index and is dropped.
> kcov_move_area() bounds the same untrusted count this way, and no count
> the end_pos <= max_pos check accepts reaches that limit, so no valid
> record is lost.
>
> Fixes: ded97d2c2b2c ("kcov: support comparison operands collection")
> Cc: stable@xxxxxxxxxxxxxxx
> Assisted-by: Hawkeye:GLM-5.3-flash
> Assisted-by: Qoder:Qwen3.8-Max
> Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
Reviewed-by: Alexander Potapenko <glider@xxxxxxxxxx>
I was reviewing
https://lore.kernel.org/all/20260915033345.904567-1-eadavis@xxxxxxxx/
yesterday, but this patch supersedes Edward's.