From patchwork Mon Feb 2 05:55:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 4871 Received: (qmail 14853 invoked by alias); 2 Feb 2015 05:56:23 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 14844 invoked by uid 89); 2 Feb 2015 05:56:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail-vc0-f179.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=4eSNBouT7wHnvr0Km0LhHMwFo8COirvB00i8K/31pQo=; b=Dt+RRHW0nUW7YPIrSK3G14sNUGLWkSkfg5W9Rl8FfcB0Ob4atZbNmnhuGEd5VUHEUJ bfRa/s+XKvoLhB0Kea3ICa9kTAu6WRe7EC+N3nPq7cBpw0SkwnH/A4m9KkkOD+DnSV9c ymqkcjV2waxvbtiR61AIVA86TyLmOGqQSYDXhtBexlqXh11z695NL6hkLCyS3K1bOWw/ /D5UeOw4C8Nljqw3y9fIEjm/hrihlTpxRawe6YEAzwpNTPaVvkqLNrkysG8bzfm3gFu1 xmQ2cE5CI2zcfTBRyALqOliP0ZczPIb6u1XNNVrzYxXrUeTk3x+KWelqUUBbS4YDd5bo r66g== X-Gm-Message-State: ALoCoQkz5BWsOToWohHcqNwWLg3U5Bq9ynqpjMs1DNS6J99cZxKNxGzWrS9DbrcDMsXKFgq2N3YD X-Received: by 10.52.11.106 with SMTP id p10mr7817409vdb.29.1422856579686; Sun, 01 Feb 2015 21:56:19 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <20150202050906.GF23507@brightrain.aerifal.cx> References: <20150202050906.GF23507@brightrain.aerifal.cx> From: Paul Pluzhnikov Date: Sun, 1 Feb 2015 21:55:49 -0800 Message-ID: Subject: Re: [patch] Fix for heap overflow in wscanf (BZ 16618) To: Rich Felker Cc: libc-alpha@sourceware.org On Sun, Feb 1, 2015 at 9:09 PM, Rich Felker wrote: > Offhand, the multiplication newsize * sizeof (CHAR_T) looks like a > potential integer overflow. Are you sure it's okay? Joseph noted that in the BZ entry: JM> The logic also has a problem that the comparison JM> UCHAR_MAX + 1 > 2 * wpmax doesn't allow for 2 * wpmax overflowing, JM> though that would only apply if half the address space gets allocated. I guess I could fix that while I am at it. Patch updated. Re-tested. Thanks, diff --git a/NEWS b/NEWS index c91b9fc..85b2948 100644 --- a/NEWS +++ b/NEWS @@ -10,15 +10,16 @@ Version 2.21 * The following bugs are resolved with this release: 6652, 10672, 12674, 12847, 12926, 13862, 14132, 14138, 14171, 14498, - 15215, 15378, 15884, 16009, 16418, 16191, 16469, 16576, 16617, 16619, - 16657, 16740, 16857, 17192, 17266, 17273, 17344, 17363, 17370, 17371, - 17411, 17460, 17475, 17485, 17501, 17506, 17508, 17522, 17555, 17570, - 17571, 17572, 17573, 17574, 17582, 17583, 17584, 17585, 17589, 17594, - 17601, 17608, 17616, 17625, 17630, 17633, 17634, 17635, 17647, 17653, - 17657, 17658, 17664, 17665, 17668, 17682, 17702, 17717, 17719, 17722, - 17723, 17724, 17725, 17732, 17733, 17744, 17745, 17746, 17747, 17748, - 17775, 17777, 17780, 17781, 17782, 17791, 17793, 17796, 17797, 17801, - 17803, 17806, 17834, 17844, 17848, 17868, 17869, 17870, 17885, 17892. + 15215, 15378, 15884, 16009, 16418, 16191, 16469, 16576, 16617, 16618, + 16619, 16657, 16740, 16857, 17192, 17266, 17273, 17344, 17363, 17370, + 17371, 17411, 17460, 17475, 17485, 17501, 17506, 17508, 17522, 17555, + 17570, 17571, 17572, 17573, 17574, 17582, 17583, 17584, 17585, 17589, + 17594, 17601, 17608, 17616, 17625, 17630, 17633, 17634, 17635, 17647, + 17653, 17657, 17658, 17664, 17665, 17668, 17682, 17702, 17717, 17719, + 17722, 17723, 17724, 17725, 17732, 17733, 17744, 17745, 17746, 17747, + 17748, 17775, 17777, 17780, 17781, 17782, 17791, 17793, 17796, 17797, + 17801, 17803, 17806, 17834, 17844, 17848, 17868, 17869, 17870, 17885, + 17892. * A new semaphore algorithm has been implemented in generic C code for all machines. Previous custom assembly implementations of semaphore were diff --git a/stdio-common/tst-sscanf.c b/stdio-common/tst-sscanf.c index aece3f2..a12333c 100644 --- a/stdio-common/tst-sscanf.c +++ b/stdio-common/tst-sscanf.c @@ -233,5 +233,23 @@ main (void) } } + /* BZ 16618 */ + { +#define SIZE 131072 + CHAR *s = malloc ((SIZE + 1) * sizeof (*s)); + if (s == NULL) + abort (); + for (size_t i = 0; i < SIZE; i++) + s[i] = L('0'); + s[SIZE] = L('\0'); + int i = 42; + if (SSCANF (s, L("%d"), &i) != 1) + result = 1; + if (i != 0) + result = 1; + free (s); +#undef SIZE + } + return result; } diff --git a/stdio-common/vfscanf.c b/stdio-common/vfscanf.c index cd129a8..71cba38 100644 --- a/stdio-common/vfscanf.c +++ b/stdio-common/vfscanf.c @@ -274,9 +274,17 @@ _IO_vfscanf_internal (_IO_FILE *s, const char *format, _IO_va_list argptr, CHAR_T *old = wp; \ size_t newsize = (UCHAR_MAX + 1 > 2 * wpmax \ ? UCHAR_MAX + 1 : 2 * wpmax); \ - if (use_malloc || !__libc_use_alloca (newsize)) \ + if (__glibc_unlikely (wpmax > SIZE_MAX / 2) \ + || __glibc_unlikely (newsize > SIZE_MAX / sizeof (CHAR_T))) \ { \ - wp = realloc (use_malloc ? wp : NULL, newsize); \ + if (use_malloc) \ + free (old); \ + done = EOF; \ + goto errout; \ + } \ + if (use_malloc || !__libc_use_alloca (newsize * sizeof (CHAR_T))) \ + { \ + wp = realloc (use_malloc ? wp : NULL, newsize * sizeof (CHAR_T)); \ if (wp == NULL) \ { \ if (use_malloc) \