RE: [RFC PATCH 0/1] Integer overflows while scanning for integers

From: David Laight
Date: Sat Jun 10 2023 - 15:31:55 EST


From: Rasmus Villemoes
> Sent: 09 June 2023 11:10
>
> On 08/06/2023 17.27, Petr Mladek wrote:
> > On Wed 2023-06-07 16:36:12, Kees Cook wrote:
>
> > It seems that userspace implementation of sscanf() and vsscanf()
> > returns -ERANGE in this case. It might be a reasonable solution.
>
> Well. _Some_ userspace implementation does that. It's not in POSIX.
> While "man scanf" lists that ERANGE error, it also explicitly says that:
>
> CONFORMING TO
> The functions fscanf(), scanf(), and sscanf() conform to C89 and
> C99 and POSIX.1-2001. These standards do not specify the
> ERANGE error.
>
> I can't figure out what POSIX actually says should or could happen with
> sscanf("99999999999999", "%i", &x);

Possibly 'undefined behaviour' - they like that one.

But I'm sure I remember the ToG 'Unix' definition not requiring that
'utilities' check for overflow on numeric command line parameters.
(It might even have said they wouldn't check.)
So it was perfectly valid for a stupidly out of range value
to be treated as a different (possibly valid) value.

What is clearly wrong is to just stop processing the
input string.
sscanf("9999999999999999999scale", "%i%s", &x, &scale)
writing any '9' to scale is clearly broken.

Personally I avoid scanf() - it is far too easy for it to do
something you didn't really intend.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)