From patchwork Wed Aug 14 23:35:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1972623 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; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=SHSRLbMv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4Wkl585yTcz1yNr for ; Thu, 15 Aug 2024 09:36:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2B243858403 for ; Wed, 14 Aug 2024 23:36:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A2B243858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1723678586; bh=wNnSg2WhErcIbYptrMgpMjTRR2HBd/MF6YGL3oHEWaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=SHSRLbMv5kqJJp3gAQSpRbqg8lbpKEVc537vKnQOkNwGRtZFmmK+G/0d1SP5PLICH 8lhkF4CJmkc9mysIi6+DWzM7y61VpO4NQddtZmttNDCpgJeJ8MjqDcIonpNYteud3s BuGSbyiztP5CtgG6j+5q6R2RbGw65SzgloWlwHnk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from elephant.ash.relay.mailchannels.net (elephant.ash.relay.mailchannels.net [23.83.222.57]) by sourceware.org (Postfix) with ESMTPS id 9C65A3858C50 for ; Wed, 14 Aug 2024 23:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C65A3858C50 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=sourceware.org Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=sourceware.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9C65A3858C50 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.222.57 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; cv=pass; b=DYWvjzEMWdOJEn1AkhewkLqxBw+vVFILIeofy5NjJVfKYOtQ3lSfIVu6CmPrd8LWYSIYjIvsUY4o5jlNo+4w2I5mqLimCgYosDrZ+oi3HXwxNrtkZZZt//knlcOGMXxylduQHq2YpYGUk+nizjS7ym9YUIY+S9LBpTaljRPbOVQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1723678558; c=relaxed/simple; bh=hE6Fub/GTVwxpPl+wyFbQ20VKALGqovCfYcDcvS55VU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=N2jQfwu1j/Uct4l+PVQb1nkrwB3dbiPdH0nfS5ZizAzkxNsBieyqIfNDUkPbpsZPeFqv657DJgm5bXQrAHtcV4iZToS1viDzebov1UvLq9wRCFcfhMt9pVss1VnhrLVLlxh+4yIgHjgouiL/1m780Q5YaniqZAihW7vkDM3nF0g= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 8055C846B66; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) Received: from pdx1-sub0-mail-a310.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 372F7846A98; Wed, 14 Aug 2024 23:35:54 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1723678554; a=rsa-sha256; cv=none; b=myhKPPBNcJknl61xyioF5oAjbYx67AMkJ5wcM2YzBOBuJVz/z2DROQhL84S9MD9lW/91Av hOxJ6LsMiVsqpY6FCr9Ri814kIOK9WRP/loxVpn662+1MQpyZTunSYRw9ytCOvAmx7rb4u YgsD4agZDyihqnhWBtgBBdSykaPXsSuEpy2WE1RTzuDyVu7p1dbXt3keFrnlN50KunhlCH QfDZPXPY75mz46EN/ef/VfHIlVfgOAFdP8PTSWYVZ8iwWN6PakKbXj2q85cD5JnQT8q8ht sMTeJs1PQpOPQ2kjnOcqhCackFlccMV0+6NyPz3zzaJ04SCBl1rp9BGfPFlM8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1723678554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wNnSg2WhErcIbYptrMgpMjTRR2HBd/MF6YGL3oHEWaI=; b=xfWzIQVe5DkAGksfm0jwTw9L+J6OmaCx9TZzPf/pHV4kSYss88Qdajq8TYN1mZMiKYQMKZ w/cUxvmBTAu23ZR61CP/NHi4IiGtQYnHdmGrM7uw9h0/P/r8LGHxoyYhniz7VXCHOIE9eu qapF6k5CPCALMwqVBLT+M4x/1jJRCyzopEPpygxP0bPreuvtlTGfYt1rTGsKCGn62BJLnG y79oeQAh4XTrLuV/ruW/EaboLF70bxWf+vre7xMpEaII4yS0EaYKmBCxLJ0fGVEH2fQ0Ch d1AMQm960CQHnEjkmDKh3+BSzRPFv08UV5CC9VhkY2x9uVCzj+WiXR+7l5hOEQ== ARC-Authentication-Results: i=1; rspamd-c4b59d8dc-rb4wz; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Cooing-Army: 19abb605254625ae_1723678554435_64807049 X-MC-Loop-Signature: 1723678554435:1328571160 X-MC-Ingress-Time: 1723678554435 Received: from pdx1-sub0-mail-a310.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.121.240.13 (trex/7.0.2); Wed, 14 Aug 2024 23:35:54 +0000 Received: from fedora.redhat.com (unknown [184.146.171.51]) (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) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a310.dreamhost.com (Postfix) with ESMTPSA id 4Wkl4T6KbJzM2; Wed, 14 Aug 2024 16:35:53 -0700 (PDT) From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Cc: fweimer@redhat.com Subject: [PATCH 2/3] ungetc: Fix uninitialized read when putting into unused streams [BZ #27821] Date: Wed, 14 Aug 2024 19:35:33 -0400 Message-ID: <20240814233534.1469084-3-siddhesh@sourceware.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240814233534.1469084-1-siddhesh@sourceware.org> References: <20240814233534.1469084-1-siddhesh@sourceware.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1171.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP, T_SCC_BODY_TEXT_LINE 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 When ungetc is called on an unused stream, the backup buffer is allocated without the main get area being present. This results in two every subsequent ungetc (as the stream remains in the backup area) checking uninitialized memory in the backup buffer when trying to put a character back into the stream. Avoid comparing the input character with buffer contents when in backup to avoid this uninitialized read. The uninitialized read is harmless in this context since the location is promptly overwritten with the input character, thus fulfilling ungetc functionality. Also adjust wording in the manual to drop the paragraph that says glibc cannot do multiple ungetc back to back since with this change, ungetc can actually do this. Signed-off-by: Siddhesh Poyarekar Reviewed-by: Carlos O'Donell --- libio/genops.c | 2 +- manual/stdio.texi | 8 +++----- stdio-common/tst-ungetc.c | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libio/genops.c b/libio/genops.c index 99f5e80f20..b012fa33d2 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -662,7 +662,7 @@ _IO_sputbackc (FILE *fp, int c) { int result; - if (fp->_IO_read_ptr > fp->_IO_read_base + if (fp->_IO_read_ptr > fp->_IO_read_base && !_IO_in_backup (fp) && (unsigned char)fp->_IO_read_ptr[-1] == (unsigned char)c) { fp->_IO_read_ptr--; diff --git a/manual/stdio.texi b/manual/stdio.texi index 8517653507..92614775fa 100644 --- a/manual/stdio.texi +++ b/manual/stdio.texi @@ -1467,11 +1467,9 @@ program; usually @code{ungetc} is used only to unread a character that was just read from the same stream. @Theglibc{} supports this even on files opened in binary mode, but other systems might not. -@Theglibc{} only supports one character of pushback---in other -words, it does not work to call @code{ungetc} twice without doing input -in between. Other systems might let you push back multiple characters; -then reading from the stream retrieves the characters in the reverse -order that they were pushed. +@Theglibc{} supports pushing back multiple characters; subsequently +reading from the stream retrieves the characters in the reverse order +that they were pushed. Pushing back characters doesn't alter the file; only the internal buffering for the stream is affected. If a file positioning function diff --git a/stdio-common/tst-ungetc.c b/stdio-common/tst-ungetc.c index 5c808f0734..388b202493 100644 --- a/stdio-common/tst-ungetc.c +++ b/stdio-common/tst-ungetc.c @@ -48,6 +48,8 @@ do_test (void) TEST_VERIFY_EXIT (getc (fp) == 'b'); TEST_VERIFY_EXIT (getc (fp) == 'l'); TEST_VERIFY_EXIT (ungetc ('m', fp) == 'm'); + TEST_VERIFY_EXIT (ungetc ('n', fp) == 'n'); + TEST_VERIFY_EXIT (getc (fp) == 'n'); TEST_VERIFY_EXIT (getc (fp) == 'm'); TEST_VERIFY_EXIT ((c = getc (fp)) == 'a'); TEST_VERIFY_EXIT (getc (fp) == EOF);