From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, BUG6152_INVALID_DATE_TZ_ABSURD,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, INVALID_DATE_TZ_ABSURD,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB7D1C433E0 for ; Wed, 20 Jan 2021 19:48:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 82F1C2343F for ; Wed, 20 Jan 2021 19:48:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392769AbhATTsL (ORCPT ); Wed, 20 Jan 2021 14:48:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392725AbhATTl4 (ORCPT ); Wed, 20 Jan 2021 14:41:56 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36724C0613ED for ; Wed, 20 Jan 2021 11:41:09 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1611171667; 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=4k/xczm0+tWviY/LentZQRmnbNHrpN9j2j3dvysbK0c=; b=svmLqO22hLkJncX/7hrUumxXUwysOp+nZArHw6VGrEg6aAaZXAeBVU9kXxNIRxxXVV1D73 +LuImkeMlKhucW/wJq2F1nObst+bgl187No0cm48lWGVf+LeMnJhmqS2kvW2u6MQkE0GXa 9I+eGAcTaRHBGqo+g5zjdPMf6P7M0ezZSx56qc6cIIBUU6kcqBIcsU83PQ46B8qEEwLp7/ y4J+yBrlzP6s90ySCAkQIX6hrI2H48sg2fA8//tDUrxcBr45tncEMCx7d6PdSF3vwJlKDx /WaPwKNzOPvvrNPhBNaWkvG40W2ev1OcenfqZhS9Ih3A+06txlLjGN8dkniRMQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1611171667; 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=4k/xczm0+tWviY/LentZQRmnbNHrpN9j2j3dvysbK0c=; b=eTdLovGogdRe5iI7NBNADQih5PCLBzNngGtl/iHfZ1w+LEMRZLcerjysCGZciEHX91yW0u e5tzmjubFbNChhCA== To: Petr Mladek Cc: Sergey Senozhatsky , Sergey Senozhatsky , Steven Rostedt , linux-kernel@vger.kernel.org Subject: [PATCH 1/1] printk: fix syslog_print_all() 1024-byte edge case Date: Wed, 20 Jan 2021 20:47:06 +0106 Message-Id: <20210120194106.26441-2-john.ogness@linutronix.de> In-Reply-To: <20210120194106.26441-1-john.ogness@linutronix.de> References: <20210120194106.26441-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If klogctl(SYSLOG_ACTION_READ_ALL) is called with a buffer size of 1024 and the message data will exactly fill 1024 bytes and the last message of that is multi-line, the last line of the last message will be silently dropped. This is because syslog_print_all() is assuming record_print_text() will fill @size bytes and chooses the first record on this basis. But since record_print_text() only fills @size-1 bytes, it will truncate the last message. This behavior exists since the introduction of msg_print_text() in commit 3ce9a7c0ac28 ("printk() - restore prefix/timestamp printing for multi-newline strings"). SYSLOG_ACTION_READ_ALL is only supposed to print full messages, so the expected behavior would be to drop the full multi-line message. Fix this edge case by changing syslog_print_all() to correctly choose the first message with the knowledge that record_print_text() will only fill up to @size-1 bytes. To test the syslog interface, a simple "kmsg" tool was written to call klogctl() based on provided parameters. The tool prints the syslog data it read and the size returned by klogctl(). A wrapper script was used to generate a single multi-line test message and run kmsg. ----- BEGIN syslog-1024-test.sh ----- #!/bin/sh msg="" for i in `seq $1`; do msg="$msg."; done msg="$msg\nhello1" msg="$msg\nhello2" dmesg -c > /dev/null echo -e "$msg" > /dev/kmsg ./kmsg SYSLOG_ACTION_READ_ALL 1024 ----- END syslog-1024-test.sh ----- When $1 is 928, the syslog data will be 1024 bytes and trigger the edge case (assuming early timestamps and CALLER_ID enabled). BEFORE this commit: # ./syslog-1024-test.sh 927 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes <12>[ 115.933677][ T1138] ................ <12>[ 115.933677][ T1138] hello1 <12>[ 115.933677][ T1138] hello2 read 1023 bytes from klogctl # ./syslog-1024-test.sh 928 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes <12>[ 124.834804][ T1143] ................ <12>[ 124.834804][ T1143] hello1 read 990 bytes from klogctl # ./syslog-1024-test.sh 929 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes read 0 bytes from klogctl AFTER this commit: # ./syslog-1024-test.sh 927 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes <12>[ 43.079094][ T1096] ................ <12>[ 43.079094][ T1096] hello1 <12>[ 43.079094][ T1096] hello2 read 1023 bytes from klogctl # ./syslog-1024-test.sh 928 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes read 0 bytes from klogctl # ./syslog-1024-test.sh 929 kmsg: klogctl(SYSLOG_ACTION_READ_ALL) with buffer of 1024 bytes read 0 bytes from klogctl Fixes: 3ce9a7c0ac28 ("printk() - restore prefix/timestamp printing for multi-newline strings") Signed-off-by: John Ogness --- 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 6639a0cfe0ac..b640d34e0351 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1513,7 +1513,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear) /* move first record forward until length fits into the buffer */ prb_for_each_info(clear_seq, prb, seq, &info, &line_count) { - if (len <= size) + if (len < size) break; len -= get_record_print_text_size(&info, line_count, true, time); } -- 2.20.1