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 71AB8385C407 for ; Tue, 25 Jan 2022 17:47:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 71AB8385C407 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-453-bwzsVeTeOya63oe2dvVVDg-1; Tue, 25 Jan 2022 12:47:15 -0500 X-MC-Unique: bwzsVeTeOya63oe2dvVVDg-1 Received: by mail-qv1-f71.google.com with SMTP id q5-20020ad45745000000b0041b929322ecso22247354qvx.6 for ; Tue, 25 Jan 2022 09:47:15 -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=pvmQRnWAFDmBOcupter1as9RBUAVWBhOz4w6uVxvsxc=; b=J4nTn9NzY0cF4zkosR+y2qGfuqgWhHbPwPKPLear+YUKP4zCOqL9uBVdGKgeCACV/O 7hhMYVsk9zVgmlT2PeCFnXxGLIvHnk9uC0riyvziV1oX1GeQs0DpXG0aPpnHruSaLS+N o9iuFgqANzC2/06Nd5FOn5UhieEo4EGPDHy7wZkMUGoD0rS2aF0I6SCvW2i7R3xYF4bQ y7gXlnkRC/WoI3ddpi4N9r0jNMHPpruOcmFCbYvajyUNHEOic2GKV1PKM4jVHzaZyk7K 41SfD7f4e0q7E8bbkwyJnPN6XoOXm1mTUdAOJimRkrnuaCSS+ZVN1pssUpDnnnAV0q7/ z4eg== X-Gm-Message-State: AOAM533jQTkermG5g3u83WjpI8xTyuBkkfcwsG6XxmPcYFLH9a0LcpQA bG9fIexHHnzTEMPLwyB929TBR3Orte1I7XJrPUF8pg4IhfNDQf5p4OORk2Dro01kGy/A7wgcOKs 4qJu7AbvAXH6K3ptr3XmW X-Received: by 2002:a05:6214:c66:: with SMTP id t6mr12835192qvj.11.1643132835090; Tue, 25 Jan 2022 09:47:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJwke2UTfs+UC2tw3Zq7gv/aTFfeyIvx/GKrqPF/LUE8XgiqkhmfET3D8Wme/IAyYpdtBTD67A== X-Received: by 2002:a05:6214:c66:: with SMTP id t6mr12835170qvj.11.1643132834821; Tue, 25 Jan 2022 09:47:14 -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 v73sm3982893qkb.51.2022.01.25.09.47.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Jan 2022 09:47:14 -0800 (PST) Message-ID: <95017622-7e3b-9992-82eb-42ebfaa942f6@redhat.com> Date: Tue, 25 Jan 2022 12:47:13 -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 3/5] avoid -Wuse-after-free [BZ #26779] To: Martin Sebor , libc-alpha@sourceware.org References: <53f20975-a2c9-674d-2a43-b1b323ee545c@gmail.com> <73343f8d-74b8-74a8-6097-c2c786c5423a@gmail.com> From: Carlos O'Donell Organization: Red Hat In-Reply-To: <73343f8d-74b8-74a8-6097-c2c786c5423a@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:47:18 -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 > io/ftw.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/io/ftw.c b/io/ftw.c > index 2742541f36..08ccbdd523 100644 > --- a/io/ftw.c > +++ b/io/ftw.c > @@ -323,8 +323,8 @@ open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp) > buf[actsize++] = '\0'; > > /* Shrink the buffer to what we actually need. */ > - data->dirstreams[data->actdir]->content = realloc (buf, actsize); > - if (data->dirstreams[data->actdir]->content == NULL) > + void *content = realloc (buf, actsize); OK. Add a new pointer, and use that instead because ->content and buf may be unspecified at failure. > + if (content == NULL) > { > int save_err = errno; > free (buf); > @@ -338,6 +338,7 @@ open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp) > data->dirstreams[data->actdir]->streamfd = -1; > data->dirstreams[data->actdir] = NULL; > } > + data->dirstreams[data->actdir]->content = content; OK. Then set the content. > } > } > -- Cheers, Carlos.