public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Vincent Lefevre <vincent@vinc17.net>
To: libc-alpha@sourceware.org
Subject: Re: UB status of snprintf on invalid ptr+size combination?
Date: Mon, 20 Mar 2023 18:36:06 +0100	[thread overview]
Message-ID: <20230320173606.GH203866@cventin.lip.ens-lyon.fr> (raw)
In-Reply-To: <af58e366-df86-a6a4-9faa-c391c93dc179@gotplt.org>

On 2023-03-20 12:56:34 -0400, Siddhesh Poyarekar wrote:
> On 2023-03-20 09:50, Vincent Lefevre wrote:
> > On 2023-03-20 08:05:32 -0400, Siddhesh Poyarekar wrote:
> > > I think on the glibc front it makes sense from a security
> > > perspective to interpret this through POSIX than the C standard.
> > > Even if the C standard is clarified to be contrary to POSIX and
> > > explicitly state that n is not the size of the buffer (which would
> > > be a terrible mistake IMO), I'd lean towards violating the C
> > > standard and conforming to POSIX instead.
> > 
> > I disagree about the POSIX behavior (assuming it is intentional).
> 
> Why do you think it may be unintentional?

Yes, because it does not warn about a difference with the C standard
(and see below...).

> The POSIX wording seems pretty deliberate and clear to me.

Standards may have clear text, while some corner cases have been
overlooked. This occurs frequently. As an example, see

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61399
  (LDBL_MAX is incorrect with IBM long double format)

and a DR eventually changed the definition, even though it was clear:

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61399#c4

Note: This change of definition was not much an issue in practice
because it made the standard match the actual implementations
(probably all of them). So it did not break anything.

Back to POSIX: The text was probably written like that because in
almost all the cases, n is a valid size of the array and the change
of formulation made the text a bit simpler. But this broke some
legitimate uses, and at that time, the working group may not have
been aware of that. It would be useful to know whether this had been
brought in the discussions at that time.

> The C standard wording on the other hand leaves things to the
> imagination, which is why we're having this discussion.

What things to the imagination? IMHO, the C standard wording is very
clear.

> > With it, if the compiler detects that n is larger than the actual
> > buffer size, then due to undefined behavior, the compiler could
> > assume that this is dead code and introduce erratic behavior in
> > code written with the C standard in mind (or when it was introduced
> > in BSD).
> 
> In fact, with _FORTIFY_SOURCE, if the runtime detects that n is larger than
> the actual buffer size, the code will abort, see pr28989[1].  But that's a
> runtime feature, nothing to do with gcc.
> 
> gcc at the moment doesn't have any such check AFAICT but if it does, I
> reckon that's a discussion to be had on the gcc mailing list.

If the ISO C standard is meant to be changed, all compilers could
potentially consider the code as dead code, not just GCC.

IMHO, it would be very bad to consider the code as UB. It would be
better to standardize _FORTIFY_SOURCE (that would add restrictions,
but with a controlled behavior).

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

  reply	other threads:[~2023-03-20 17:36 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-14 19:47 Simon Chopin
2023-03-14 21:39 ` Paul Eggert
2023-03-15  9:22   ` Andreas Schwab
2023-03-15 15:54     ` Siddhesh Poyarekar
2023-03-15 18:34     ` Michael Hudson-Doyle
2023-03-19 14:45     ` manfred
2023-03-19 23:07       ` Vincent Lefevre
2023-03-20 12:05         ` Siddhesh Poyarekar
2023-03-20 12:17           ` Alejandro Colomar
2023-03-20 12:29             ` Siddhesh Poyarekar
2023-03-20 13:36             ` Vincent Lefevre
2023-03-20 13:50           ` Vincent Lefevre
2023-03-20 16:56             ` Siddhesh Poyarekar
2023-03-20 17:36               ` Vincent Lefevre [this message]
2023-03-20 15:09       ` Vincent Lefevre
2023-03-20 16:15         ` Alejandro Colomar
2023-03-20 16:33           ` Vincent Lefevre
2023-03-20 17:00           ` Vincent Lefevre
2023-03-20 17:31             ` Siddhesh Poyarekar
2023-03-20 17:45               ` Vincent Lefevre
2023-03-15 12:39   ` Vincent Lefevre
2023-03-16 10:29     ` Stephan Bergmann
2023-03-18  2:07       ` Vincent Lefevre
2023-03-18  2:30         ` Alejandro Colomar
2023-03-18 10:58           ` Vincent Lefevre
2023-03-18 15:01             ` Andreas Schwab
2023-03-19 22:48               ` Vincent Lefevre
2023-03-19 23:24                 ` Andreas Schwab
2023-03-20  4:10                   ` Vincent Lefevre
2023-03-20  9:19                     ` Andreas Schwab
2023-03-20 10:42                       ` Vincent Lefevre
2023-03-20 10:44                         ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230320173606.GH203866@cventin.lip.ens-lyon.fr \
    --to=vincent@vinc17.net \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).