From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DFB8606A31A for ; Tue, 1 Dec 2026 20:04:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=LziGzu87; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PtNiMmId DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=BQK6e/iuPn9jZCcPBs468bvlZnfyzgZZJCR+viBY7Ko=; b=LziGzu872QwitkVUo9f2QamDeb GcM1dZrU4K+zjYyhSXmg1BeE0QmEu/vZ2o7r0SSJv0DzWjGdCDc/vs+A1XpiQt6XuPslah4txxCS7 FV8mZyvPg6Tllylpi1FfRzHMkeZGiDLIh+psoWOg1kCUU6FAhWRTibcm8CG8hqkzj8ZWHr3CTPYnA so5SeDARQOy58EyEDOFcCnImb4Pk6gElAjI5YFrZL5Q+gKWF6UvRXlvzZ1B7vlv0d4h6V2VGfuDDH fUeRK4EeR+F3/Skz3B2urhISQx8qg4CPfDIe6uIYQMWGtYuDKrLipsOFQeI4dTsNYWwJrrTzBY0Z3 n1zEJdkg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYU58-00000009IHT-0AQD; Tue, 01 Dec 2026 20:04:06 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYU57-00000009IHE-0V5r for kexec@lists.infradead.org; Tue, 01 Dec 2026 20:04:05 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id A03FE4096B; Tue, 1 Dec 2026 20:04:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED09A1F000C1; Tue, 1 Dec 2026 20:04:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796155444; bh=BQK6e/iuPn9jZCcPBs468bvlZnfyzgZZJCR+viBY7Ko=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=PtNiMmId1sHuTS+CnWmwT/Pj1fV4S98Cf6Y4nDmPlNjmGyksTqLKFTlY+xlrCd9TT RMLrChML5pRqciN+TdXqya2tIBBEydnDfa7kU8wM7n1RG1DFSKVen5PDTN4V94B/cp gJeMvluueK+cHCw6FIaw2q5H62qnm/2OpV2fnTR1fWe3xonHGotrNze/eMy8fwfKPR kONmDEbBigtOqNBHNo3klvqbbgqd9+b7y44U92mWtf9yUAki+r/NZme8sc3XPBwMfT dWE0l2jgF61OHE5SSrNiEksv8FkWXoSlCq5eS2GpF/SNpb+RFjDiToATuCdalqBOnv DfRfv0FKSz5Xw== Date: Tue, 1 Dec 2026 20:04:02 +0000 From: Simon Horman To: litenglong Cc: kexec@lists.infradead.org Subject: Re: [PATCH] Fix buffer overflow in dump_dmesg_structured and dump_record Message-ID: <20261201200402.GB456681@horms.kernel.org> References: <20261112131022.1076471-1-litenglong@kylinos.cn> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261112131022.1076471-1-litenglong@kylinos.cn> X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Thu, Nov 12, 2026 at 09:10:23PM +0800, litenglong wrote: > Both functions accumulate output across multiple dmesg records into a > fixed-size stack buffer out_buf[OUT_BUF_SIZE] (4096 bytes) via sprintf > with a cumulative offset that is never reset across loop iterations. > > The hex-byte escape loops already have a post-write flush guard at each > character boundary (len >= OUT_BUF_SIZE - 64), which was present in the > original upstream code. However, two write sites in each function lacked > any bounds check: > > dump_dmesg_structured: > - timestamp prefix sprintf: len += sprintf(out_buf + len, "[%5llu.%06llu] ", ...) > - trailing newline: out_buf[len++] = '\n' > > dump_record: > - timestamp prefix sprintf: offset += sprintf(out_buf + offset, "[%5llu.%06llu] ", ...) > - trailing newline at out:: out_buf[offset++] = '\n' > > On a long log record, the cumulative offset can approach OUT_BUF_SIZE. > Without a guard before these writes, the timestamp sprintf (up to ~30 > bytes) or the newline byte can write past out_buf[OUT_BUF_SIZE - 1], > causing a stack buffer overflow. > > Fix: add a flush-and-reset guard before each of the four unprotected > write sites, using the same threshold (OUT_BUF_SIZE - 64) as the > existing loop guards. When the buffer is nearly full, the accumulated > output is emitted via the handler callback and the offset resets to > zero, so subsequent writes start from the beginning of the buffer. > > The 64-byte margin is sufficient: the timestamp prefix is at most ~30 > bytes, and the newline is a single byte. The loop guards continue to > protect the character-by-character escape writes as before. > > Signed-off-by: litenglong Thanks, applied. - Fix buffer overflow in dump_dmesg_structured and dump_record https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=57fc3341e3e0