From patchwork Tue Jan 30 18:40:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arjun Shankar X-Patchwork-Id: 85044 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9534A385773F for ; Tue, 30 Jan 2024 18:41:24 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7EEA93857703 for ; Tue, 30 Jan 2024 18:40:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7EEA93857703 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 7EEA93857703 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706640058; cv=none; b=SZVAr+ultIBmjLyo2uFyF4VWrUzBACHWasfPf2ntsFPWfINpnaHJXAxo61xOZl94GvWEFm7EiRQLh55bc6mUEoxaoSxNZNFs1MYM/QT1xg5w6IZJufSabQQaQ0w94XOmyX+tajV3+qwqqyaRGw9Tr9Y/As5YLpO1wZkEmLp76QY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706640058; c=relaxed/simple; bh=/ZrKY59HhPk9L8hmx7SJHBFfYFxZFIgITUlJADMfm3M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DuYY5G887E2OAlthfvL2IRFk4tpIXFSkIca/fyyQDfOcLfa/uqcgxdhb2kAtnj6hy4JkEfndSgflw1VKUXI3WigHTPArn5cqqcG0kp3IvIOetvFTmPaFwTthkq3OqYC04o/iZvRsawg8NlPoYWEeDms3ZD+0F4udn83XC6LS9pw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706640056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cAhXJXrP3mqInxwrH3OOmJxmXOvVZwRmst4yDZBMxAc=; b=H7UBnHMmbtx8R97XmPgNmDSHFfMpuchnY2EOYLx4JBdSGh4X8ZdQAxj1+d4jFzVQXAeT30 ezUOW7TM2jiBHB4UzRUuytBJPxoa/nHMg8BWZ2FYLcbOD6hSlpuLkEihiOKPauN4wLNXZu xYtevFgZZOcJpqqgJqqSuOQWFqKZs2k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-335-jagyxadRPmm21xXTk5T9Lg-1; Tue, 30 Jan 2024 13:40:54 -0500 X-MC-Unique: jagyxadRPmm21xXTk5T9Lg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8330285A596 for ; Tue, 30 Jan 2024 18:40:54 +0000 (UTC) Received: from x1carbon9g.. (dhcp129-93.brq.redhat.com [10.34.129.93]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E0EFB2026D66; Tue, 30 Jan 2024 18:40:53 +0000 (UTC) From: Arjun Shankar To: libc-alpha@sourceware.org Cc: Arjun Shankar , Carlos O'Donell Subject: [COMMITTED 2/4] syslog: Fix heap buffer overflow in __vsyslog_internal (CVE-2023-6779) Date: Tue, 30 Jan 2024 19:40:42 +0100 Message-ID: <20240130184045.511720-2-arjun@redhat.com> In-Reply-To: <20240130184045.511720-1-arjun@redhat.com> References: <20240130184045.511720-1-arjun@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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+patchwork=sourceware.org@sourceware.org __vsyslog_internal used the return value of snprintf/vsnprintf to calculate buffer sizes for memory allocation. If these functions (for any reason) failed and returned -1, the resulting buffer would be too small to hold output. This commit fixes that. All snprintf/vsnprintf calls are checked for negative return values and the function silently returns upon encountering them. Reviewed-by: Carlos O'Donell --- misc/syslog.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 814d224a1e..53440e47ad 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -185,11 +185,13 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, else l = __snprintf (bufs, sizeof bufs, SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); + if (l < 0) + goto out; char *pos; size_t len; - if (0 <= l && l < sizeof bufs) + if (l < sizeof bufs) { /* At this point, there is still a chance that we can print the remaining part of the log into bufs and use that. */ @@ -215,12 +217,15 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, __set_errno (saved_errno); vl = __vsnprintf_internal (pos, len, fmt, apc, mode_flags); + va_end (apc); + + if (vl < 0) + goto out; - if (!(0 <= vl && vl < len)) + if (vl >= len) buf = NULL; bufsize = l + vl; - va_end (apc); } if (buf == NULL) @@ -231,25 +236,37 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Tell the cancellation handler to free this buffer. */ clarg.buf = buf; + int cl; if (has_ts) - __snprintf (buf, l + 1, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); + cl = __snprintf (buf, l + 1, + SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); else - __snprintf (buf, l + 1, - SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); + cl = __snprintf (buf, l + 1, + SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); + if (cl != l) + goto out; va_list apc; va_copy (apc, ap); - __vsnprintf_internal (buf + l, bufsize - l + 1, fmt, apc, - mode_flags); + cl = __vsnprintf_internal (buf + l, bufsize - l + 1, fmt, apc, + mode_flags); va_end (apc); + + if (cl != vl) + goto out; } else { + int bl; /* Nothing much to do but emit an error message. */ - bufsize = __snprintf (bufs, sizeof bufs, - "out of memory[%d]", __getpid ()); + bl = __snprintf (bufs, sizeof bufs, + "out of memory[%d]", __getpid ()); + if (bl < 0 || bl >= sizeof bufs) + goto out; + + bufsize = bl; buf = bufs; + msgoff = 0; } }