Re: [PATCH v6 9/9] vsprintf: Avoid confusion between invalid address and value
From: Andy Shevchenko
Date: Tue Feb 19 2019 - 06:06:31 EST
On Tue, Feb 19, 2019 at 5:07 AM Sergey Senozhatsky
<sergey.senozhatsky.work@xxxxxxxxx> wrote:
> Suppose, in my driver I want to sprintf() IPv4 address. The longest
> possible address is 3 * 4 (%d%d%d) + 3 bytes (dots) + terminating NULL.
> E.g. 111.111.111.111
>
> So I can allocate a 16-bytes buffer (stack or slab) and accidentally
> do an %piS sprintf() on a corrupted in_addr struct:
>
> char buf[16];
> sprintf(buf, "%piS", in_addr);
>
> forcing sprintf() to write "(invalid address)" to a 16-bytes buffer,
> but the thing is - strlen("(invalid address)") > 16.
It would print as many characters as buffer allows. In your case above
the use of sprintf() is a bit fragile.
But yes, the error messages should not be longer than 8 / 16 bytes
depending on 32- or 64-bit build we have.
--
With Best Regards,
Andy Shevchenko