mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [PATCH] Fix off-by-one buffer overflow in getdelim
Date: Sun, 16 Sep 2018 19:32:42 -0400	[thread overview]
Message-ID: <20180916233242.GD17995@brightrain.aerifal.cx> (raw)
In-Reply-To: <20180916183447.GC17995@brightrain.aerifal.cx>

On Sun, Sep 16, 2018 at 02:34:47PM -0400, Rich Felker wrote:
> On Sun, Sep 16, 2018 at 02:25:42PM -0400, Rich Felker wrote:
> > On Sat, Oct 24, 2015 at 10:43:39PM +0200, Felix Janda wrote:
> > > when deciding whether to resize the buffer, the terminating null byte
> > > was not taken into account
> > > ---
> > >  src/stdio/getdelim.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/src/stdio/getdelim.c b/src/stdio/getdelim.c
> > > index a88c393..3077490 100644
> > > --- a/src/stdio/getdelim.c
> > > +++ b/src/stdio/getdelim.c
> > > @@ -27,7 +27,7 @@ ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric
> > >  	for (;;) {
> > >  		z = memchr(f->rpos, delim, f->rend - f->rpos);
> > >  		k = z ? z - f->rpos + 1 : f->rend - f->rpos;
> > > -		if (i+k >= *n) {
> > > +		if (i+k+1 >= *n) {
> > >  			if (k >= SIZE_MAX/2-i) goto oom;
> > >  			*n = i+k+2;
> > >  			if (*n < SIZE_MAX/4) *n *= 2;
> > > -- 
> > > 2.4.9
> > 
> > This patch raised a potential conformance issue, that by a strict
> > reading of the spec, getdelim is only permitted to realloc if the
> > caller-provided buffer length is insufficient:
> > 
> >     "If *lineptr is a null pointer or if the object pointed to by
> >     *lineptr is of insufficient size, an object shall be allocated as
> >     if by malloc() or the object shall be reallocated as if by
> >     realloc(), respectively, ..."
> > 
> > I'm going to change the +1 to +!z and add a comment. The idea is that
> > the +1 was only needed in order for the result to fit if the delimiter
> > has not already been found; if the memchr found it, an exact-sized
> > buffer was being expanded unnecessarily.
> > 
> > I'm replying to this thread and CC'ing in case there are any problems
> > I'm missing in my new fix.
> 
> This fix actually looks insufficient; it doesn't fix the case where
> the getc produces EOF rather than a character.

OK, the problem here is actually a lot more fundamental than I
realized. If you read the standard as disallowing realloc unless it's
necessary for the result to fit, then there's a circular dependency
here. You can't realloc without knowing whether the next getc will
succeed, but you can't getc without knowing there'll be at least 2
additional bytes to store the result and the null terminator.

If you could fit one additional byte without allocating, but not two,
there's no way to proceed.

The only way out I see is to do the first reallocation speculatively:
instead of realloc, malloc a new buffer that will be large enough,
attempt the getc, and then either switch to it (freeing the original
buffer) or free it (keeping the original buffer) depending on whether
EOF is returned.

In almost all cases, this logic can be skipped. It's not necessary at
all if the stdio stream is buffered, since we can just unget back.
(Using unget works mechanically for unbuffered streams too, but it
violates the invariant that no interface except ungetc or scanf
families should leave logical FILE position not equal to underlying
open file descriptor's offset). It's also not necessary for additional
growth after the first time, since enlarging is already committed.

Rich


  reply	other threads:[~2018-09-16 23:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-24 20:43 Felix Janda
2015-10-24 21:36 ` Rich Felker
2015-10-24 22:25   ` Felix Janda
2015-10-24 23:35     ` Rich Felker
2015-10-25  0:32       ` Rich Felker
2015-10-25  6:18         ` Felix Janda
2018-09-16 18:25 ` Rich Felker
2018-09-16 18:34   ` Rich Felker
2018-09-16 23:32     ` Rich Felker [this message]
2018-09-17  2:01       ` Rich Felker

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=20180916233242.GD17995@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).