RE: [PATCH] printk: ringbuffer: Fix truncating buffer size min_t cast
From: David Laight
Date: Mon Aug 14 2023 - 06:43:53 EST
From: Kees Cook
> Sent: 11 August 2023 06:46
>
> If an output buffer size exceeded U16_MAX, the min_t(u16, ...) cast in
> copy_data() was causing writes to truncate. This manifested as output
> bytes being skipped, seen as %NUL bytes in pstore dumps when the available
> record size was larger than 65536. Fix the cast to no longer truncate
> the calculation.
>
...
> diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c
> index 2dc4d5a1f1ff..fde338606ce8 100644
> --- a/kernel/printk/printk_ringbuffer.c
> +++ b/kernel/printk/printk_ringbuffer.c
> @@ -1735,7 +1735,7 @@ static bool copy_data(struct prb_data_ring *data_ring,
> if (!buf || !buf_size)
> return true;
>
> - data_size = min_t(u16, buf_size, len);
> + data_size = min_t(unsigned int, buf_size, len);
I'd noticed that during one of my test compiles while looking
at making min() less fussy.
A better fix would be:
data_size = min(buf_size + 0u, len);
Or put an ack on my patch 3/5 to minmax.h and then min(buf_size, len)
will be fine (because both arguments are unsigned).
David
>
> memcpy(&buf[0], data, data_size); /* LMM(copy_data:A) */
> return true;
> --
> 2.34.1
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)