Invalid pointer subtractions in __shlim and __shgetc

Submitted by Pascal Cuoq on April 24, 2020, 9:40 a.m.

Details

Message ID 1587721262422.2983@trust-in-soft.com
State New
Series "Invalid pointer subtractions in __shlim and __shgetc"
Headers show

Commit Message

Pascal Cuoq April 24, 2020, 9:40 a.m.
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};”:

Unfortunately, the function __shlim also subtracts f.rpos from f.buf, at this line:

  f->shcnt = f->buf - f->rpos;

(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.

Pascal

Patch hide | download patch | download mbox

--- 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;

Comments

Rich Felker April 24, 2020, 2:12 p.m.
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