lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 14 Aug 2023 14:56:10 +0200
From:   Petr Mladek <pmladek@...e.com>
To:     David Laight <David.Laight@...lab.com>
Cc:     'Kees Cook' <keescook@...omium.org>,
        Sergey Senozhatsky <senozhatsky@...omium.org>,
        Steven Rostedt <rostedt@...dmis.org>,
        John Ogness <john.ogness@...utronix.de>,
        Vijay Balakrishna <vijayb@...ux.microsoft.com>,
        "stable@...r.kernel.org" <stable@...r.kernel.org>,
        Tony Luck <tony.luck@...el.com>,
        "Guilherme G. Piccoli" <gpiccoli@...lia.com>,
        "Paul E. McKenney" <paulmck@...nel.org>,
        "linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
        "linux-hardening@...r.kernel.org" <linux-hardening@...r.kernel.org>
Subject: Re: [PATCH] printk: ringbuffer: Fix truncating buffer size min_t cast

On Mon 2023-08-14 10:42:26, David Laight wrote:
> 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);

This looks like a magic to me. The types are:

	unsigned int data_size;
	unsigned int buf_size;
	u16 len

I would naively expect that

	data_size = min(buf_size, len);

would do the right job and expand @len to "unsigned int".

I do not remember why "min_t" was used. Was it an optimization?
Did we miss the problem with casting "u32" down to "u16"?

I tried to read the discussion at
https://lore.kernel.org/lkml/b6a49ed73aba427ca8bb433763fa94e9@AcuMS.aculab.com/
but it is more about "signed" vs. "unsigned" problem. Maybe
it is more complicated that I expected.

> 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).

Do you mean
https://lore.kernel.org/lkml/6dc20ac7cb6f4570a0160f076e8362e3@AcuMS.aculab.com/ ?
It seems to be just indentation cleanup.

Best Regards,
Petr

PS: I have already pushed the patch because it looked reasonable and
    got testing. I have to admit that I am probably in a pre-vacation
    hurry mode.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ