|
|
b40826 |
From libc-alpha-return-25252-listarch-libc-alpha=sources dot redhat dot com at sourceware dot org Thu Feb 16 16:21:17 2012
|
|
|
b40826 |
Return-Path: <libc-alpha-return-25252-listarch-libc-alpha=sources dot redhat dot com at sourceware dot org>
|
|
|
b40826 |
Delivered-To: listarch-libc-alpha at sources dot redhat dot com
|
|
|
b40826 |
Received: (qmail 5187 invoked by alias); 16 Feb 2012 16:21:14 -0000
|
|
|
b40826 |
Delivered-To: moderator for libc-alpha at sourceware dot org
|
|
|
b40826 |
Received: (qmail 2174 invoked by uid 22791); 16 Feb 2012 16:17:18 -0000
|
|
|
b40826 |
X-SWARE-Spam-Status: No, hits=-2.0 required=5.0
|
|
|
b40826 |
tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,TW_TV,TW_VB,TW_VF,T_RP_MATCHES_RCVD
|
|
|
b40826 |
X-Spam-Check-By: sourceware.org
|
|
|
b40826 |
Date: Thu, 16 Feb 2012 08:16:13 -0800
|
|
|
b40826 |
From: Kees Cook <kees at outflux dot net>
|
|
|
b40826 |
To: "Ryan S dot Arnold" <ryan dot arnold at gmail dot com>
|
|
|
b40826 |
Cc: libc-alpha at sourceware dot org, Paul Eggert <eggert at cs dot ucla dot edu>,
|
|
|
b40826 |
Roland McGrath <roland at hack dot frob dot com>,
|
|
|
b40826 |
Andreas Schwab <schwab at linux-m68k dot org>
|
|
|
b40826 |
Subject: Re: [PATCH] vfprintf: validate nargs and maybe allocate from heap
|
|
|
b40826 |
Message-ID: <20120216161613.GZ20420@outflux.net>
|
|
|
b40826 |
References: <20120206062537.GM4979@outflux.net>
|
|
|
b40826 |
<20120207000509 dot GP4989 at outflux dot net>
|
|
|
b40826 |
<20120210192457 dot GF20420 at outflux dot net>
|
|
|
b40826 |
<CAAKybw8AgkGsKAx=kvX4Tsi74f+HtuVnatTCB0VfsHi7vVFi1Q at mail dot gmail dot com>
|
|
|
b40826 |
<20120214223048 dot GM20420 at outflux dot net>
|
|
|
b40826 |
<CAAKybw_HS+cav+YcDw3ns7UXu6_xA7EHPrkiB87P+OGwEB0PVQ at mail dot gmail dot com>
|
|
|
b40826 |
<20120214224543 dot GN20420 at outflux dot net>
|
|
|
b40826 |
MIME-Version: 1.0
|
|
|
b40826 |
Content-Type: text/plain; charset=us-ascii
|
|
|
b40826 |
Content-Disposition: inline
|
|
|
b40826 |
In-Reply-To: <20120214224543 dot GN20420 at outflux dot net>
|
|
|
b40826 |
X-MIMEDefang-Filter: outflux$Revision: 1.316 $
|
|
|
b40826 |
X-HELO: www.outflux.net
|
|
|
b40826 |
Mailing-List: contact libc-alpha-help at sourceware dot org; run by ezmlm
|
|
|
b40826 |
Precedence: bulk
|
|
|
b40826 |
List-Id: <libc-alpha.sourceware.org>
|
|
|
b40826 |
List-Subscribe: <mailto:libc-alpha-subscribe at sourceware dot org>
|
|
|
b40826 |
List-Archive: <http://sourceware.org/ml/libc-alpha/>
|
|
|
b40826 |
List-Post: <mailto:libc-alpha at sourceware dot org>
|
|
|
b40826 |
List-Help: <mailto:libc-alpha-help at sourceware dot org>, <http://sourceware dot org/ml/#faqs>
|
|
|
b40826 |
Sender: libc-alpha-owner at sourceware dot org
|
|
|
b40826 |
Delivered-To: mailing list libc-alpha at sourceware dot org
|
|
|
b40826 |
|
|
|
b40826 |
The nargs value can overflow when doing allocations, allowing arbitrary
|
|
|
b40826 |
memory writes via format strings, bypassing _FORTIFY_SOURCE:
|
|
|
b40826 |
http://www.phrack.org/issues.html?issue=67&id=9
|
|
|
b40826 |
|
|
|
b40826 |
This checks for nargs overflow and possibly allocates from heap instead of
|
|
|
b40826 |
stack, and adds a regression test for the situation.
|
|
|
b40826 |
|
|
|
b40826 |
I have FSF assignment via Google. (Sent from @outflux since that's how I'm
|
|
|
b40826 |
subscribed here, but CL shows @chromium.org as part of my Google work.)
|
|
|
b40826 |
|
|
|
b40826 |
This version disables the useless test on non-32-bit platforms.
|
|
|
b40826 |
|
|
|
b40826 |
2012-02-16 Kees Cook <keescook@chromium.org>
|
|
|
b40826 |
|
|
|
b40826 |
[BZ #13656]
|
|
|
b40826 |
* stdio-common/vfprintf.c (vfprintf): Check for nargs overflow and
|
|
|
b40826 |
possibly allocate from heap instead of stack.
|
|
|
b40826 |
* stdio-common/bug-vfprintf-nargs.c: New file.
|
|
|
b40826 |
* stdio-common/Makefile (tests): Add nargs overflow test.
|
|
|
b40826 |
|
|
|
b40826 |
|
|
|
b40826 |
diff -rup a/stdio-common/Makefile b/stdio-common/Makefile
|
|
|
b40826 |
--- a/stdio-common/Makefile 2010-05-04 05:27:23.000000000 -0600
|
|
|
b40826 |
+++ b/stdio-common/Makefile 2012-02-20 21:57:52.983040992 -0700
|
|
|
b40826 |
@@ -60,7 +60,7 @@ tests := tstscanf test_rdwr test-popen t
|
|
|
b40826 |
tst-popen tst-unlockedio tst-fmemopen2 tst-put-error tst-fgets \
|
|
|
b40826 |
tst-fwrite bug16 bug17 tst-swscanf tst-sprintf2 bug18 bug18a \
|
|
|
b40826 |
bug19 bug19a tst-popen2 scanf13 scanf14 scanf15 bug20 bug21 bug22 \
|
|
|
b40826 |
- scanf16 scanf17 tst-setvbuf1
|
|
|
b40826 |
+ scanf16 scanf17 tst-setvbuf1 bug-vfprintf-nargs
|
|
|
b40826 |
|
|
|
b40826 |
test-srcs = tst-unbputc tst-printf
|
|
|
b40826 |
|
|
|
b40826 |
diff --git a/stdio-common/bug-vfprintf-nargs.c b/stdio-common/bug-vfprintf-nargs.c
|
|
|
b40826 |
new file mode 100644
|
|
|
b40826 |
index 0000000..13c66c0
|
|
|
b40826 |
--- /dev/null
|
|
|
b40826 |
+++ b/stdio-common/bug-vfprintf-nargs.c
|
|
|
b40826 |
@@ -0,0 +1,78 @@
|
|
|
b40826 |
+/* Test for vfprintf nargs allocation overflow (BZ #13656).
|
|
|
b40826 |
+ Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
b40826 |
+ This file is part of the GNU C Library.
|
|
|
b40826 |
+ Contributed by Kees Cook <keescook@chromium.org>, 2012.
|
|
|
b40826 |
+
|
|
|
b40826 |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
|
b40826 |
+ modify it under the terms of the GNU Lesser General Public
|
|
|
b40826 |
+ License as published by the Free Software Foundation; either
|
|
|
b40826 |
+ version 2.1 of the License, or (at your option) any later version.
|
|
|
b40826 |
+
|
|
|
b40826 |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
|
b40826 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
b40826 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
b40826 |
+ Lesser General Public License for more details.
|
|
|
b40826 |
+
|
|
|
b40826 |
+ You should have received a copy of the GNU Lesser General Public
|
|
|
b40826 |
+ License along with the GNU C Library; if not, write to the Free
|
|
|
b40826 |
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
b40826 |
+ 02111-1307 USA. */
|
|
|
b40826 |
+
|
|
|
b40826 |
+#include <stdio.h>
|
|
|
b40826 |
+#include <stdlib.h>
|
|
|
b40826 |
+#include <stdint.h>
|
|
|
b40826 |
+#include <unistd.h>
|
|
|
b40826 |
+#include <inttypes.h>
|
|
|
b40826 |
+#include <string.h>
|
|
|
b40826 |
+#include <signal.h>
|
|
|
b40826 |
+
|
|
|
b40826 |
+static int
|
|
|
b40826 |
+format_failed (const char *fmt, const char *expected)
|
|
|
b40826 |
+{
|
|
|
b40826 |
+ char output[80];
|
|
|
b40826 |
+
|
|
|
b40826 |
+ printf ("%s : ", fmt);
|
|
|
b40826 |
+
|
|
|
b40826 |
+ memset (output, 0, sizeof output);
|
|
|
b40826 |
+ /* Having sprintf itself detect a failure is good. */
|
|
|
b40826 |
+ if (sprintf (output, fmt, 1, 2, 3, "test") > 0
|
|
|
b40826 |
+ && strcmp (output, expected) != 0)
|
|
|
b40826 |
+ {
|
|
|
b40826 |
+ printf ("FAIL (output '%s' != expected '%s')\n", output, expected);
|
|
|
b40826 |
+ return 1;
|
|
|
b40826 |
+ }
|
|
|
b40826 |
+ puts ("ok");
|
|
|
b40826 |
+ return 0;
|
|
|
b40826 |
+}
|
|
|
b40826 |
+
|
|
|
b40826 |
+static int
|
|
|
b40826 |
+do_test (void)
|
|
|
b40826 |
+{
|
|
|
b40826 |
+ int rc = 0;
|
|
|
b40826 |
+ char buf[64];
|
|
|
b40826 |
+
|
|
|
b40826 |
+ /* Regular positionals work. */
|
|
|
b40826 |
+ if (format_failed ("%1$d", "1") != 0)
|
|
|
b40826 |
+ rc = 1;
|
|
|
b40826 |
+
|
|
|
b40826 |
+ /* Regular width positionals work. */
|
|
|
b40826 |
+ if (format_failed ("%1$*2$d", " 1") != 0)
|
|
|
b40826 |
+ rc = 1;
|
|
|
b40826 |
+
|
|
|
b40826 |
+ /* Positional arguments are constructed via read_int, so nargs can only
|
|
|
b40826 |
+ overflow on 32-bit systems. On 64-bit systems, it will attempt to
|
|
|
b40826 |
+ allocate a giant amount of memory and possibly crash, which is the
|
|
|
b40826 |
+ expected situation. Since the 64-bit behavior is arch-specific, only
|
|
|
b40826 |
+ test this on 32-bit systems. */
|
|
|
b40826 |
+ if (sizeof (long int) == 4)
|
|
|
b40826 |
+ {
|
|
|
b40826 |
+ sprintf (buf, "%%1$d %%%" PRIdPTR "$d", UINT32_MAX / sizeof (int));
|
|
|
b40826 |
+ if (format_failed (buf, "1 %$d") != 0)
|
|
|
b40826 |
+ rc = 1;
|
|
|
b40826 |
+ }
|
|
|
b40826 |
+
|
|
|
b40826 |
+ return rc;
|
|
|
b40826 |
+}
|
|
|
b40826 |
+
|
|
|
b40826 |
+#define TEST_FUNCTION do_test ()
|
|
|
b40826 |
+#include "../test-skeleton.c"
|
|
|
b40826 |
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
|
|
|
b40826 |
index 863cd5d..022e72b 100644
|
|
|
b40826 |
--- a/stdio-common/vfprintf.c
|
|
|
b40826 |
+++ b/stdio-common/vfprintf.c
|
|
|
b40826 |
@@ -235,6 +235,9 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap)
|
|
|
b40826 |
0 if unknown. */
|
|
|
b40826 |
int readonly_format = 0;
|
|
|
b40826 |
|
|
|
b40826 |
+ /* For the argument descriptions, which may be allocated on the heap. */
|
|
|
b40826 |
+ void *args_malloced = NULL;
|
|
|
b40826 |
+
|
|
|
b40826 |
/* This table maps a character into a number representing a
|
|
|
b40826 |
class. In each step there is a destination label for each
|
|
|
b40826 |
class. */
|
|
|
b40826 |
@@ -1647,9 +1650,10 @@ do_positional:
|
|
|
b40826 |
determine the size of the array needed to store the argument
|
|
|
b40826 |
attributes. */
|
|
|
b40826 |
size_t nargs = 0;
|
|
|
b40826 |
- int *args_type;
|
|
|
b40826 |
- union printf_arg *args_value = NULL;
|
|
|
b40826 |
+ size_t bytes_per_arg;
|
|
|
b40826 |
+ union printf_arg *args_value;
|
|
|
b40826 |
int *args_size;
|
|
|
b40826 |
+ int *args_type;
|
|
|
b40826 |
|
|
|
b40826 |
/* Positional parameters refer to arguments directly. This could
|
|
|
b40826 |
also determine the maximum number of arguments. Track the
|
|
|
b40826 |
@@ -1698,13 +1702,33 @@ do_positional:
|
|
|
b40826 |
|
|
|
b40826 |
/* Determine the number of arguments the format string consumes. */
|
|
|
b40826 |
nargs = MAX (nargs, max_ref_arg);
|
|
|
b40826 |
+ bytes_per_arg = sizeof (*args_value) + sizeof (*args_size)
|
|
|
b40826 |
+ + sizeof (*args_type);
|
|
|
b40826 |
+
|
|
|
b40826 |
+ /* Check for potential integer overflow. */
|
|
|
b40826 |
+ if (nargs > SIZE_MAX / bytes_per_arg)
|
|
|
b40826 |
+ {
|
|
|
b40826 |
+ done = -1;
|
|
|
b40826 |
+ goto all_done;
|
|
|
b40826 |
+ }
|
|
|
b40826 |
|
|
|
b40826 |
/* Allocate memory for the argument descriptions. */
|
|
|
b40826 |
- args_type = alloca (nargs * sizeof (int));
|
|
|
b40826 |
+ if (__libc_use_alloca (nargs * bytes_per_arg))
|
|
|
b40826 |
+ args_value = alloca (nargs * bytes_per_arg);
|
|
|
b40826 |
+ else
|
|
|
b40826 |
+ {
|
|
|
b40826 |
+ args_value = args_malloced = malloc (nargs * bytes_per_arg);
|
|
|
b40826 |
+ if (args_value == NULL)
|
|
|
b40826 |
+ {
|
|
|
b40826 |
+ done = -1;
|
|
|
b40826 |
+ goto all_done;
|
|
|
b40826 |
+ }
|
|
|
b40826 |
+ }
|
|
|
b40826 |
+
|
|
|
b40826 |
+ args_size = &args_value[nargs].pa_int;
|
|
|
b40826 |
+ args_type = &args_size[nargs];
|
|
|
b40826 |
memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
|
|
|
b40826 |
- nargs * sizeof (int));
|
|
|
b40826 |
- args_value = alloca (nargs * sizeof (union printf_arg));
|
|
|
b40826 |
- args_size = alloca (nargs * sizeof (int));
|
|
|
b40826 |
+ nargs * sizeof (*args_type));
|
|
|
b40826 |
|
|
|
b40826 |
/* XXX Could do sanity check here: If any element in ARGS_TYPE is
|
|
|
b40826 |
still zero after this loop, format is invalid. For now we
|
|
|
b40826 |
@@ -1973,8 +1997,8 @@ do_positional:
|
|
|
b40826 |
}
|
|
|
b40826 |
|
|
|
b40826 |
all_done:
|
|
|
b40826 |
- if (__builtin_expect (workstart != NULL, 0))
|
|
|
b40826 |
- free (workstart);
|
|
|
b40826 |
+ free (args_malloced);
|
|
|
b40826 |
+ free (workstart);
|
|
|
b40826 |
/* Unlock the stream. */
|
|
|
b40826 |
_IO_funlockfile (s);
|
|
|
b40826 |
_IO_cleanup_region_end (0);
|
|
|
b40826 |
--
|
|
|
b40826 |
1.7.5.4
|
|
|
b40826 |
|
|
|
b40826 |
--
|
|
|
b40826 |
Kees Cook @outflux.net
|
|
|
b40826 |
|