mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: segfault on sscanf
Date: Thu, 14 Mar 2019 18:40:12 -0400	[thread overview]
Message-ID: <20190314224012.GI23599@brightrain.aerifal.cx> (raw)
In-Reply-To: <20190314162814.GI28106@voyager>

On Thu, Mar 14, 2019 at 05:28:14PM +0100, Markus Wichmann wrote:
> On Thu, Mar 14, 2019 at 10:46:17AM +0100, Marian Buschsieweke wrote:
> > Hi,
> > 
> > running pdflatex on Alpine Linux for a specific document resulted in a
> > segfault, which I could trace down to a specific call to sscanf. This is a
> > minimum example to reproduce that segfault:
> > 
> > 	#include <stdio.h>
> > 	
> > 	int main(void) {
> > 		const char *too_parse = "0 1 -1 0";
> > 		double f1,f2,f3,f4;
> > 		char dummy;
> > 		sscanf(too_parse, " %lf %lf %lf %lf %c", &f1, &f2, &f3, &f4, &dummy);
> > 	
> > 		printf("f1=%f, f2=%f, f3=%f, f4=%f, dummy=\"%c\"\n", f1, f2, f3, f4, dummy);
> > 	
> > 		return 0;
> > 	}
> > 
> > This is the backtrace:
> > 
> > 	#0  0x00007ffff7fb7eba in vfscanf (f=f@entry=0x7fffffffe6f8, 
> > 	    fmt=<optimized out>, ap=ap@entry=0x7fffffffe7f8) at src/stdio/vfscanf.c:262
> > 	#1  0x00007ffff7fb971a in vsscanf (s=<optimized out>, fmt=<optimized out>, 
> > 	    ap=ap@entry=0x7fffffffe7f8) at src/stdio/vsscanf.c:14
> > 	#2  0x00007ffff7fb594d in sscanf (s=<optimized out>, fmt=<optimized out>)
> > 	    at src/stdio/sscanf.c:9
> > 	#3  0x0000555555555213 in main () at test.c:7
> > 
> > I have the package Alpine Linux package musl-1.1.21-r0 installed, which is musl
> > version 1.1.21 with minimal changes.
> > 
> > Kind regards,
> > Marian
> 
> OK, so here's the crashing line:
> 
> 				while (scanset[(c=shgetc(f))+1])
> 					s[i++] = c;
> 
> It is (unsurprisingly) inside the %c parsing case. At the end of input,
> shgetc() returns EOF, which is -1. EOF+1 is therefore 0. And scanset[0]
> should be set to 0 (that happens a few lines further up). So the
> crashing line should never occur (the line number of the crash is for
> the loop body itself).
> 
> The error is reproducible whenever sscanf() runs out of input within a
> %f conversion, and another conversion happens after it. I would not be
> surprised if __floatscan() manages to set the file state wrong on EOF.
> 
> The above isn't actually minimal. Here's an even shorter segfault.
> 
>   #include <stdio.h>
> 
>         int main(void) {
>                 const char *too_parse = "0";
>                 double f1;
>                 char dummy;
>                 sscanf(too_parse, "%f%c", &f1, &dummy);
> 
>                 printf("f1=%f, dummy=\"%c\"\n", f1, dummy);
> 
>                 return 0;
>         }
> 
> So, I'm off to read __floatscan(). As I recall, it was complicated, so
> expect me back in about 10 years or so...

The above test is invalid due to UB; f1 should have type float not
double, and dummy should be initialized so that it's not trying to
print an indeterminate value on success. Fixing those aspects, my
proposed fix seems to work as long as it doesn't break anything else
(setting f->shend = f->rpos instead of 0 on eof).

Rich


      parent reply	other threads:[~2019-03-14 22:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-14  9:46 Marian Buschsieweke
2019-03-14 12:44 ` A. Wilcox
2019-03-14 13:29   ` Szabolcs Nagy
2019-03-14 14:34     ` Pascal Cuoq
2019-03-14 16:28 ` Markus Wichmann
2019-03-14 16:53   ` Markus Wichmann
2019-03-14 18:19     ` Szabolcs Nagy
2019-03-14 18:38       ` Markus Wichmann
2019-03-14 19:49         ` Szabolcs Nagy
2019-03-14 20:15           ` Szabolcs Nagy
2019-03-14 22:34           ` Rich Felker
2019-03-14 22:43             ` Szabolcs Nagy
2019-03-14 22:52               ` Rich Felker
2019-03-15  1:54                 ` Rich Felker
2019-03-14 22:40   ` 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=20190314224012.GI23599@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).