Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20170410134236.GT17319@brightrain.aerifal.cx>
Date: Mon, 10 Apr 2017 09:42:36 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: *scanf, wrong types in va_arg, and strict aliasing

On Mon, Apr 10, 2017 at 09:31:48AM +0000, Pascal Cuoq wrote:
> Hello again,
> 
> 
> the scanf implementation does the same thing as the printf implementation, in vfscanf.c, line 110:
> 
> https://git.musl-libc.org/cgit/musl/tree/src/stdio/vfscanf.c?id=54807d47acecab778498ced88ce8f62bfa16e379#n110
> 
> This line is eventually reached, for instance, for the snippet:
> 
> int n; sscanf("0", "%d", &n);
> 
> And the argument being consumed has type int*. This is not a case that 7.16..1.1:2 allows.

Indeed, that's wrong, but POSIX makes it hard or even impossible to do
right because of the %n$ localization forms. Whereas printf's
requirement for these is:

    "When numbered argument specifications are used, specifying the
    Nth argument requires that all the leading arguments, from the
    first to the (N-1)th, are specified in the format string."

scanf's requirement is a bit different:

    "When numbered argument specifications are used, specifying the
    Nth argument requires that all the leading arguments, from the
    first to the (N-1)th, are pointers."

This means that use of %2$ without a %1$ is permitted as long as a
pointer (what kind?!) is passed in the first variadic position after
the format string.

I think the best we could do is to _try_ to do it right, by tracking
the types like we do for printf, and only fallback to generic void*
when a conversion specifier is missing. But that's a lot of extra code
needed for something with no user-visible improvements.

Alternatively, we could write a compiler-barrier in trivial asm to
wrap an external va_arg, which is ugly but would also be useful in
places like fcntl/ioctl that might read variadic args with unknown
type to pass to the kernel.

Any opinions on what we should do?

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.