From patchwork Tue Jan 17 16:10:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Mladek X-Patchwork-Id: 13104850 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9AC0C3DA78 for ; Tue, 17 Jan 2023 16:12:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232315AbjAQQMN (ORCPT ); Tue, 17 Jan 2023 11:12:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231584AbjAQQML (ORCPT ); Tue, 17 Jan 2023 11:12:11 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18A283B3D3; Tue, 17 Jan 2023 08:12:10 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 9E4E01F385; Tue, 17 Jan 2023 16:12:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1673971929; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=185VNqtOqrWbrz+iqnibvY2OKcoNRPZvWlFEs/lSiyw=; b=DFisV2MPDS3tQ97+t5ehDa1oiSns+MsIadujPEsel0Q3rDNaZ88r2bgBdYYDX/JAu3gGpr phicHXPF6isX3ScM6jjUBVLwHeex3nLoYufaOlT/aVGoU7XFuWz4hSLRG5F8adkUylQhyi VcXUynz/RJ7SEwQw3AdBsjbPa8ESaJ8= Received: from alley.suse.cz (unknown [10.100.208.146]) by relay2.suse.de (Postfix) with ESMTP id 4A24C2C141; Tue, 17 Jan 2023 16:12:08 +0000 (UTC) From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Steven Rostedt , coverity-bot , "Gustavo A . R . Silva" , linux-next@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek , coverity-bot Subject: [PATCH] printk: Use scnprintf() to print the message about the dropped messages on a console Date: Tue, 17 Jan 2023 17:10:31 +0100 Message-Id: <20230117161031.15499-1-pmladek@suse.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org Use scnprintf() for printing the message about dropped messages on a console. It returns the really written length of the message. It prevents potential buffer overflow when the returned length is later used to copy the buffer content. Note that the previous code was safe because the scratch buffer was big enough and the message always fit in. But scnprintf() makes it more safe, definitely. Reported-by: coverity-bot Addresses-Coverity-ID: 1530570 ("Memory - corruptions") Fixes: c4fcc617e148 ("printk: introduce console_prepend_dropped() for dropped messages") Link: https://lore.kernel.org/r/202301131544.D9E804CCD@keescook Signed-off-by: Petr Mladek Reviewed-by: John Ogness Reviewed-by: Sergey Senozhatsky --- kernel/printk/printk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 55338bfd3b55..a9b7dade0d2e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2716,7 +2716,7 @@ static void console_prepend_dropped(struct printk_message *pmsg, unsigned long d char *outbuf = &pbufs->outbuf[0]; size_t len; - len = snprintf(scratchbuf, scratchbuf_sz, + len = scnprintf(scratchbuf, scratchbuf_sz, "** %lu printk messages dropped **\n", dropped); /*