From patchwork Sun Sep 20 20:15:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 520041 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8FAC314027C for ; Mon, 21 Sep 2015 06:16:35 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=M4Xxzb5j; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; q=dns; s=default; b=NHe8 ILE4vCyKVKeEqeE/GoPPszL4+ZN/5DCresGhDbnMjuTjhnect8IAoHJkjDaf/jsK aJP+k5TWSBpd5I4z7zRzhjoBKyX1yad/VQ/OSM3MES/q1430V8zIiG7UQRMLoRq8 Mb9R56oOrnLcsuWJGt+uvM2wnZu1UqxgKygXAQs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; s=default; bh=IcrUeR7lqJ 3HdwBGxI+bpToU/4A=; b=M4Xxzb5j8aNXXwZFGyZXiz5mATBFXeENFtIBCnutUf M8mhSyWvM0u+12oYj5wRDXwYvRMuFlBHFvuUmQ3cyIPHbS0cN3+qilZ7uTr6QPXM xiuoAbr5Lvtv8MRHSWWFnUO6ppv/+6b1jYqYz6iHdBg3B2IayzOFgRQXxkEQg4Mw s= Received: (qmail 110305 invoked by alias); 20 Sep 2015 20:16:28 -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 110292 invoked by uid 89); 20 Sep 2015 20:16:26 -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-wi0-f172.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=24Z1srvSh4HxDSjAl69EiKiR4txPuk/drbzWX1bqCWU=; b=j/4ySi+nrUPGOSDD41yrU/X9h6B5hmTJccQuOmmV0zh+4Ty2Qn5Lq2Hpby5FYpGtX6 sGAZrsROdPMq6uuPS2oZiITmszgn4CjBZUS/OawuW4rT8yqA049k7GcfguA7VmPmZdCa +kIeEGI+N1r/baaSjox9cxLWJL6XSIm4rNJWOKT2e55IG5B51CwAAgNgYkTF3QMi+hGv N2gSaL7XwWY1SMXCfXp5UOU+qfyYLwheBpmQ+D5pBSTv3V9R7Qn7qzEyPlLJKjrU1bFK GtwHsdmK9tF0rVXDZycF1lrr028lWmRRy137SZtsJRv1HQCmglZnM8clQC3ZYHfTLF+T Ql1g== X-Gm-Message-State: ALoCoQlhrWOCWOxeibQqCrlYO996JXrS4XyehVt2WlexncyePeKYYDpouhSATTrPuJPteFm0BgCD X-Received: by 10.194.5.35 with SMTP id p3mr22132826wjp.132.1442780182594; Sun, 20 Sep 2015 13:16:22 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <55FE5473.7030305@cs.ucla.edu> <55FEFD47.4090401@cs.ucla.edu> From: Paul Pluzhnikov Date: Sun, 20 Sep 2015 13:15:52 -0700 Message-ID: Subject: Re: [patch] Fix BZ 18985 out of bounds access in strftime To: Paul Eggert Cc: GLIBC Devel On Sun, Sep 20, 2015 at 11:45 AM, Paul Pluzhnikov wrote: > On Sun, Sep 20, 2015 at 11:39 AM, Paul Eggert wrote: >> Don't forget out-of-range tm_isdst: there's one place the code has undefined >> behavior if tm_isdst exceeds 1. Let's try again, now with a test case :-) 2015-09-20 Paul Pluzhnikov [BZ #18985] * time/strftime_l.c (a_wkday, f_wkday, a_month, f_month): Range check. (__strftime_internal): Likewise. * time/tst-strftime.c (do_bz18985): New test. (do_test): Call it. diff --git a/time/strftime_l.c b/time/strftime_l.c index b48ef34..619e4e3 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -510,13 +510,17 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument only a few elements. Dereference the pointers only if the format requires this. Then it is ok to fail if the pointers are invalid. */ # define a_wkday \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday)) + ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 ? \ + "?" : _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday))) # define f_wkday \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday)) + ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 ? \ + "?" : _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday))) # define a_month \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon)) + ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 ? \ + "?" : _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon))) # define f_month \ - ((const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon)) + ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 ? \ + "?" : _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon))) # define ampm \ ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11 \ ? NLW(PM_STR) : NLW(AM_STR))) @@ -526,8 +530,10 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument # define ap_len STRLEN (ampm) #else # if !HAVE_STRFTIME -# define f_wkday (weekday_name[tp->tm_wday]) -# define f_month (month_name[tp->tm_mon]) +# define f_wkday (tp->tm_wday < 0 || tp->tm_wday > 6 ? \ + "?" : weekday_name[tp->tm_wday]) +# define f_month (tp->tm_mon < 0 || tp->tm_mon > 11 ? \ + "?" : month_name[tp->tm_mon]) # define a_wkday f_wkday # define a_month f_month # define ampm (L_("AMPM") + 2 * (tp->tm_hour > 11)) @@ -1321,7 +1327,7 @@ __strftime_internal (s, maxsize, format, tp, tzset_called ut_argument *tzset_called = true; } # endif - zone = tzname[tp->tm_isdst]; + zone = tp->tm_isdst <= 1 ? tzname[tp->tm_isdst] : "?"; } #endif if (! zone) diff --git a/time/tst-strftime.c b/time/tst-strftime.c index 374fba4..5a592c2 100644 --- a/time/tst-strftime.c +++ b/time/tst-strftime.c @@ -4,6 +4,24 @@ #include +static int +do_bz18985 (void) +{ + char buf[1000]; + struct tm ttm; + + memset (&ttm, 1, sizeof (ttm)); + ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */ + strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm); + + /* Check negative values as well. */ + memset (&ttm, 0xFF, sizeof (ttm)); + ttm.tm_zone = NULL; /* Dereferenced directly if non-NULL. */ + strftime (buf, sizeof (buf), "%a %A %b %B %c %z %Z", &ttm); + + return 0; +} + static struct { const char *fmt; @@ -104,7 +122,7 @@ do_test (void) } } - return result; + return result + do_bz18985 (); } #define TEST_FUNCTION do_test ()