From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 77BDC385C406 for ; Tue, 25 Jan 2022 17:49:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 77BDC385C406 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-211-saPCcHrSNIaOW0zx8GQflA-1; Tue, 25 Jan 2022 12:49:14 -0500 X-MC-Unique: saPCcHrSNIaOW0zx8GQflA-1 Received: by mail-qt1-f199.google.com with SMTP id e14-20020ac84b4e000000b002cfbbdf8206so4369973qts.10 for ; Tue, 25 Jan 2022 09:49:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:organization:in-reply-to :content-transfer-encoding; bh=5EgGw5KL2KJx0SZF1qJIXIidiWyrThoW+gCms4y9oBg=; b=jAE0QozMSmrGY1Qqi1Sr8eFDi+AvyoE59MHP5Oc4GPSjEtuy/aeUnGLsMaqF5dlGnr d6vMsBGms+rXmoBTf4+QTodCdYwD5/XAMWZhbkWem04aF1iMi5zHcE3uLpIq78hmdnS/ ven6r6pcnBJa37Vrnzfd8ufpaCej06fhHdIet3R5XGcoDFUoOPY2wnl+R5Fr5XHn6a/S HBc9TeNoqnOfOMCiZ4OAtQUiQFbB6+jLRborvEgCJEq5ZXtJMaw/5KVGAcidDEd6Bq5M Rulip0QWCioMMMHL7vZ1LVaP+V+YCi/+T8ox+osW/hp51HHUeIioOJOe0ZuIJV1k8fLG Df0Q== X-Gm-Message-State: AOAM530FiNhgLgP3i2pXuVLt5U+0BXcwksPAhzfw6ZJzz39IuMA42DjL tE4ouiFDaKT8F9XJSldGchucMs7a/xuj7hrBi0+gEGA35iSLrm6JsosXMv5jOsyvA/b7bDRastO an61g7mrUiNA2s1wZzfdk X-Received: by 2002:ad4:596d:: with SMTP id eq13mr1843263qvb.131.1643132954198; Tue, 25 Jan 2022 09:49:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJzfRiQyKzD4SgpBNt0db3IaX6OA56IokT7/9zz6gj7tr/LCOmKjAvMqadsfcmS1Rm4sHLyZTg== X-Received: by 2002:ad4:596d:: with SMTP id eq13mr1843239qvb.131.1643132953938; Tue, 25 Jan 2022 09:49:13 -0800 (PST) Received: from [192.168.0.241] (135-23-175-80.cpe.pppoe.ca. [135.23.175.80]) by smtp.gmail.com with ESMTPSA id l202sm4128039qke.66.2022.01.25.09.49.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Jan 2022 09:49:13 -0800 (PST) Message-ID: Date: Tue, 25 Jan 2022 12:49:12 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH v2 4/5] avoid -Wuse-after-free [BZ #26779] To: Martin Sebor , libc-alpha@sourceware.org References: <53f20975-a2c9-674d-2a43-b1b323ee545c@gmail.com> <05e683bb-96d7-ebff-b0e1-f988e261b19b@gmail.com> From: Carlos O'Donell Organization: Red Hat In-Reply-To: <05e683bb-96d7-ebff-b0e1-f988e261b19b@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Jan 2022 17:49:20 -0000 On 1/24/22 19:58, Martin Sebor via Libc-alpha wrote: > On 1/24/22 17:52, Martin Sebor wrote: >> This is a repost of the original patch but broken down by source >> file and with some suppression done by #pragma GCC diagnostic >> instead of conversion to intptr_t.  It also adds fixes for >> the same problem in the test suite that I overlooked before. > > The attached patch suppresses the -Wuse-after-free instance in > stdlib/setenv.c. > >> >> On 1/15/22 17:21, Martin Sebor wrote: >>> GCC 12 features a couple of new warnings designed to detect uses >>> of pointers made invalid by the pointees lifetimes having ended. >>> Building Glibc with the enhanced GCC exposes a few such uses, >>> mostly after successful calls to realloc.  The attached patch >>> avoids the new warnings by converting the pointers to uintptr_t >>> first and using the converted integers instead. >>> >>> The patch suppresses all instances of the warning at the strictest >>> setting (-Wuse-after-free=3), which includes even uses in equality >>> expressions.  The default setting approved for GCC 12 is >>> -Wuse-after-free=2, which doesn't warn on such uses to accommodate >>> the pointer-adjustment-after-realloc idiom.  At the default setting, >>> the changes to ldconfig.c and setenv are not necessary. >>> >>> Martin >> OK for glibc 2.35, please push this commit. Expected commit message (three lines) ~~~ io: Fix use-after-free in ftw [BZ #26779] Reviewed-by: Carlos O'Donell ~~~ Reviewed-by: Carlos O'Donell > diff --git a/stdlib/setenv.c b/stdlib/setenv.c > index c3d2cee7b6..2176cbac31 100644 > --- a/stdlib/setenv.c > +++ b/stdlib/setenv.c > @@ -150,7 +150,9 @@ __add_to_environ (const char *name, const char *value, const char *combined, > { > char **new_environ; > > - /* We allocated this space; we can extend it. */ > + /* We allocated this space; we can extend it. Avoid using the raw > + reallocated pointer to avoid GCC -Wuse-after-free. */ > + uintptr_t ip_last_environ = (uintptr_t)last_environ; OK. Create a temporary pointer. > new_environ = (char **) realloc (last_environ, > (size + 2) * sizeof (char *)); > if (new_environ == NULL) > @@ -159,7 +161,7 @@ __add_to_environ (const char *name, const char *value, const char *combined, > return -1; > } > > - if (__environ != last_environ) > + if ((uintptr_t)__environ != ip_last_environ) OK. Lastly, use the temporary pointer for the comparison. > memcpy ((char *) new_environ, (char *) __environ, > size * sizeof (char *)); > -- Cheers, Carlos.