From patchwork Wed Jun 26 17:36:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1952739 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DBAMGWpn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W8TRT27Wyz20X6 for ; Thu, 27 Jun 2024 03:37:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03D013875438 for ; Wed, 26 Jun 2024 17:37:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id ECB593871016 for ; Wed, 26 Jun 2024 17:37:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ECB593871016 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ECB593871016 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719423427; cv=none; b=HWu0UOl0YO7k4wB3Yrp4v7QVD2cZSFSHC5Qd9o2m7hT3QUvXjLox3ROdAWM5lOpIgnIb81nq1XMm/6n6NdongaJTxN6mJc2jsaKEtR4tkq9j+SSeTPeiWutGvuoSZ6P4AEDOl9nNowoNQynaFgd8d1RC8Sup6pDjmeGHXPwmemQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719423427; c=relaxed/simple; bh=w/xDxwMc4WKFSYYsRnNYc4JoVnuR4APGVKWhyJye2Zs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YhiGJDsLv3BIhnzZMMfHgnDnv7Btw6XOXy4Zv9FX9AM9SpdhlYnNV/5KWdSisqg7nVxW5Juy7QLMSWypL9CUjOADAU0zP86m+HiddNo/7Cy/uCIfPoxB8wATGeyE+aZ7Shp+qXeDR/sRJEvtifGo5Xa76xapVC+iFd8ZHGBq8mc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719423425; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=k6YykkO8YXr5IcS3sR0FtckYN8T8tq/AWLq0PemzmuM=; b=DBAMGWpnpIecM8ph/pTeGDkhkbr2Sb+RADCsvQzRp3qt6JVHMFZedNNuZKj5lBUCMnGkOY Vadskax7OsvsdrSHdhQjEiA7o1ZwdOxcQPq/gL2/S0U43jfJTlgbUM4L6NaZcFNYhKIbjy gKplOiN6sSsL8DpkVn1SSMFFyojlDu0= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-54-GbDu-AgvOIypfNls1_j2uQ-1; Wed, 26 Jun 2024 13:37:03 -0400 X-MC-Unique: GbDu-AgvOIypfNls1_j2uQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1C68D19560B6 for ; Wed, 26 Jun 2024 17:37:03 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.72]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F2E49300021A for ; Wed, 26 Jun 2024 17:37:01 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2] time: Avoid memcmp overread in tzset (bug 31931) Date: Wed, 26 Jun 2024 19:36:58 +0200 Message-ID: <878qyrvc5h.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org The test does not necessarily trigger the crash, depending on memcmp behavior. A crash was observed in __memcmp_ia32 on i686 builds. Tested on i686-linux-gnu. Reviewed-by: Paul Eggert --- v2: Style fix for boolean comparison. Fix putenv argument in the test. time/Makefile | 5 ++++- time/tst-tzfile-fault.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ time/tzfile.c | 5 +++-- 3 files changed, 51 insertions(+), 3 deletions(-) base-commit: a10b6ad471d7b528149f5ff32eef2f1c1dc1213c diff --git a/time/Makefile b/time/Makefile index 5b541fb9d3..f4c75b786d 100644 --- a/time/Makefile +++ b/time/Makefile @@ -50,7 +50,8 @@ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \ tst-adjtime tst-ctime tst-difftime tst-mktime4 tst-clock_settime \ tst-settimeofday tst-itimer tst-gmtime tst-timegm \ - tst-timespec_get tst-timespec_getres tst-strftime4 + tst-timespec_get tst-timespec_getres tst-strftime4 \ + tst-tzfile-fault tests-time64 := \ tst-adjtime-time64 \ @@ -110,3 +111,5 @@ tst-tzname-ENV = TZDIR=${common-objpfx}timezone/testdata CPPFLAGS-tst-tzname.c += -DTZDEFRULES='"$(posixrules-file)"' bug-getdate1-ARGS = ${objpfx}bug-getdate1-fmt + +tst-tzfile-fault-ENV = GLIBC_TUNABLES=glibc.rtld.enable_secure=1 diff --git a/time/tst-tzfile-fault.c b/time/tst-tzfile-fault.c new file mode 100644 index 0000000000..105c2ac7ce --- /dev/null +++ b/time/tst-tzfile-fault.c @@ -0,0 +1,44 @@ +/* Attempt to trigger fault with very short TZ variable (bug 31931). + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + + +#include +#include +#include +#include + +static char tz[] = "TZ=/"; + +static int +do_test (void) +{ + struct support_next_to_fault ntf + = support_next_to_fault_allocate (sizeof (tz)); + memcpy (ntf.buffer, tz, sizeof (tz)); + putenv (ntf.buffer); + + tzset (); + + /* Avoid dangling pointer in environ. */ + putenv (tz); + support_next_to_fault_free (&ntf); + + return 0; +} + +#include diff --git a/time/tzfile.c b/time/tzfile.c index 4147539964..964a9b7f12 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -134,8 +134,9 @@ __tzfile_read (const char *file, size_t extra, char **extrap) and which is not the system wide default TZDEFAULT. */ if (__libc_enable_secure && ((*file == '/' - && memcmp (file, TZDEFAULT, sizeof TZDEFAULT) - && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1)) + && strcmp (file, TZDEFAULT) != 0 + && (strncmp (file, default_tzdir, sizeof (default_tzdir) - 1) + != 0)) || strstr (file, "../") != NULL)) /* This test is certainly a bit too restrictive but it should catch all critical cases. */