From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 538F126C3A2 for ; Thu, 26 Mar 2026 13:38:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774532293; cv=none; b=sJCJwsRypmd8/BCFHagPf+7ueMSLpG0qL+8LHMb/PvtDlzoFFbBSL2g3rY0hs85V5AveFiBDTxBNfr3YJFSOP+FEnQS1fxH4pgSHIVuOFASSNdaeo2f0Sb5Cjn8255Nev8kjqo8HXRxU1Ec+d6zSUQO+SxpxD8d7lEXwXqQDc5A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774532293; c=relaxed/simple; bh=IE4I8pCGJWtrHO/j/PItVDPAxE+63kRqCPMMA5N/viY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=grWvZvyaM5WmekX8ChJXR6O7nRKXwANjgXw/U7BIThI03vaGD/0SWW4pzNY3wHV/xfsIa/NO69eA0U/OqI3SG7CyYwzpRHPJNOyXF2ekkONMrkt2yRWiiR05PT1GYoijPEBj92obckZzsoqilP5K5y0wm1w+GTNz4TdBgauL3D0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jo+Yl6MY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=a7YrI6dy; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jo+Yl6MY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="a7YrI6dy" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1774532289; 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; bh=JBHBf2CCj19BRM9FQQcodpzUbsSyVJQECLiAc42ZaMw=; b=jo+Yl6MY9XI7AN9vnUaW8Hl+FTc4NrxygV4pYFYKheKW3SsijZxTKsbZwm6IFwAysGA6Zu vATY0LqGBc/AOJEVKMN9MXDeArACMXBgA8x/3fxAy5BFFpuFiT4q3/4VRbsASqrFgz4PUV y25Z77sWE0F5z3imVupmJvTG4aG9HyGVfx33+TnMKwK/PphJEo7lhBBAzMXmHKsv+pr9G1 y5aMgmZ7g5cA4myfnlPCBgSUoyMXcXmsVfZxbfCR+6ZeuM4osDPu7DAfN9oNtD8Oy/LwXN Z0JM4CbEtCNO6XQePK0/53iljWqDfV3aGG5RJNcwGKkftoskJtMxV+zUWZZG8w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1774532289; 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; bh=JBHBf2CCj19BRM9FQQcodpzUbsSyVJQECLiAc42ZaMw=; b=a7YrI6dynclYxgWc5A7cQzfpgFWVYi8uQ1qoyT4UIs1yAQY/EsdGSZzxjMALI+hNooLINe MwlLTzi5txe+GTBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 1/2] printk_ringbuffer: Fix get_data() size sanity check Date: Thu, 26 Mar 2026 14:44:01 +0106 Message-ID: <20260326133809.8045-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit cc3bad11de6e ("printk_ringbuffer: Fix check of valid data size when blk_lpos overflows") added sanity checking to get_data() to avoid returning data of illegal sizes (too large or too small). It uses the helper function data_check_size() for the check. However, data_check_size() expects the size of the data, not the size of the data block. get_data() is providing the size of the data block. This means that if the data size (text_buf_size) is at or near the maximum legal size: sizeof(prb_data_block) + text_buf_size == DATA_SIZE(data_ring) / 2 data_check_size() will report failure because it adds sizeof(prb_data_block) to the provided size. The sanity check in get_data() is counting the data block header twice. The result is that the reader fails to read the legal record. Since get_data() subtracts the data block header size before returning, move the sanity check to after the subtraction. Luckily printk() is not vulnerable to this problem because truncate_msg() limits printk-messages to 1/4 of the ringbuffer. Indeed, by adjusting the printk_ringbuffer KUnit test, which does not use printk() and its truncate_msg() check, it is easy to see that the reader fails and the WARN_ON is triggered. Fixes: cc3bad11de6e ("printk_ringbuffer: Fix check of valid data size when blk_lpos overflows") Signed-off-by: John Ogness --- kernel/printk/printk_ringbuffer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c index 56c8e3d031f49..a3526bdd4e10d 100644 --- a/kernel/printk/printk_ringbuffer.c +++ b/kernel/printk/printk_ringbuffer.c @@ -1302,10 +1302,6 @@ static const char *get_data(struct prb_data_ring *data_ring, return NULL; } - /* Sanity check. Data-less blocks were handled earlier. */ - if (WARN_ON_ONCE(!data_check_size(data_ring, *data_size) || !*data_size)) - return NULL; - /* A valid data block will always be aligned to the ID size. */ if (WARN_ON_ONCE(blk_lpos->begin != ALIGN(blk_lpos->begin, sizeof(db->id))) || WARN_ON_ONCE(blk_lpos->next != ALIGN(blk_lpos->next, sizeof(db->id)))) { @@ -1319,6 +1315,10 @@ static const char *get_data(struct prb_data_ring *data_ring, /* Subtract block ID space from size to reflect data size. */ *data_size -= sizeof(db->id); + /* Sanity check the max size of the regular data block. */ + if (WARN_ON_ONCE(!data_check_size(data_ring, *data_size))) + return NULL; + return &db->data[0]; } base-commit: 9095f233c0258e9a05e958c7d822eb38681e7a5a -- 2.47.3