mailing list of musl libc
 help / color / mirror / code / Atom feed
* musl security advisory #001: stack buffer overflow in vfprintf with long output
@ 2012-04-17 15:24 Rich Felker
  0 siblings, 0 replies; only message in thread
From: Rich Felker @ 2012-04-17 15:24 UTC (permalink / raw)
  To: musl

This issue affects all recent versions of musl up through 0.8.7, and
is presumably exploitable if you can get a program to print arbitrary
strings to stderr using fprintf. The following commit fixes the issue
in current git, and the patch should apply cleanly to any previous
version of musl released within the past year.


commit b5a8b28915aad17b6f49ccacd6d3fef3890844d1
Author: Rich Felker <dalias@aerifal.cx>
Date:   Tue Apr 17 10:58:02 2012 -0400

    fix buffer overflow in vfprintf on long writes to unbuffered files
    
    vfprintf temporarily swaps in a local buffer (for the duration of the
    operation) when the target stream is unbuffered; this both simplifies
    the implementation of functions like dprintf (they don't need their
    own buffers) and eliminates the pathologically bad performance of
    writing the formatted output with one or more write syscalls per
    formatting field.
    
    in cases like dprintf where we are dealing with a virgin FILE
    structure, everything worked correctly. however for long-lived files
    (like stderr), it's possible that the buffer bounds were already set
    for the internal zero-size buffer. on the next write, __stdio_write
    would pick up and use the new buffer provided by vfprintf, but the
    bound (wend) field was still pointing at the internal zero-size
    buffer's end. this in turn allowed unbounded writes to the temporary
    buffer.

diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index 928c8c1..91c6b93 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -652,8 +652,9 @@ int vfprintf(FILE *f, const char *fmt, va_list ap)
 	FLOCK(f);
 	if (!f->buf_size) {
 		saved_buf = f->buf;
-		f->buf = internal_buf;
+		f->wpos = f->wbase = f->buf = internal_buf;
 		f->buf_size = sizeof internal_buf;
+		f->wend = internal_buf + sizeof internal_buf;
 	}
 	ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
 	if (saved_buf) {



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-04-17 15:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-17 15:24 musl security advisory #001: stack buffer overflow in vfprintf with long output Rich Felker

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