mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [musl] Invalid pointer subtractions in __shlim and __shgetc
Date: Fri, 24 Apr 2020 10:12:39 -0400	[thread overview]
Message-ID: <20200424141239.GE11469@brightrain.aerifal.cx> (raw)
In-Reply-To: <1587721262422.2983@trust-in-soft.com>

On Fri, Apr 24, 2020 at 09:40:15AM +0000, Pascal Cuoq wrote:
> Hello,
> 
> Rich Felker <dalias@libc.org> wrote:
> > The attached should fix it, I think.
> 
> The patch sets f.rpos and f.rend to buf+4, but it also leaves
> f.buf containing 0 from “FILE f = {0};”:
> --- a/src/stdlib/wcstol.c
> +++ b/src/stdlib/wcstol.c
> @@ -35,8 +35,7 @@ static unsigned long long wcstox(const wchar_t *s, wchar_t **p, int base, unsign
>  	unsigned char buf[64];
>  	FILE f = {0};
>  	f.flags = 0;
> -	f.rpos = f.rend = 0;
> -	f.buf = buf + 4;
> +	f.rpos = f.rend = buf + 4;
>  	f.buf_size = sizeof buf - 4;
>  	f.lock = -1;
>  	f.read = do_read;
> 
> Unfortunately, the function __shlim also subtracts f.rpos from f.buf, at this line:
> 
>   f->shcnt = f->buf - f->rpos;

Uhg, this was purely a mechanical error in the edit (selecting too
much text to delete) and I should have tested before sending. Should
be:

-	f.rpos = f.rend = 0;
-	f.buf = buf + 4;
+	f.rpos = f.rend = f.buf = buf + 4;

> (https://git.musl-libc.org/cgit/musl/tree/src/internal/shgetc.c?id=33338ebc853d37c80f0f236cc7a92cb0acc6aace#n11 )
> 
> So that is now where the invalid subtraction happens.
> 
> For what it's worth, we have tested the patch consisting in
> initializing all three of f.rpos, f.rend and f.buf to buf+4, and that
> does not cause UB in this test. But we can't tell if if provides the
> correct functional behavior for this test and for other inputs.

Yep, that's what I intended. Sorry for wasting your time with a bad
patch.

Rich

      reply	other threads:[~2020-04-24 14:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-17 15:56 Pascal Cuoq
2020-04-17 16:13 ` Rich Felker
2020-04-17 16:48   ` Rich Felker
2020-04-17 17:56     ` Rich Felker
2020-04-23 11:34       ` Pascal Cuoq
2020-04-23 16:14         ` Rich Felker
2020-04-24  9:40           ` Pascal Cuoq
2020-04-24 14:12             ` Rich Felker [this message]

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=20200424141239.GE11469@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).