]> git.ipfire.org Git - thirdparty/kernel/stable.git/commit
printk_ringbuffer: Fix get_data() size sanity check
authorJohn Ogness <john.ogness@linutronix.de>
Thu, 26 Mar 2026 13:38:01 +0000 (14:44 +0106)
committerPetr Mladek <pmladek@suse.com>
Fri, 27 Mar 2026 16:12:37 +0000 (17:12 +0100)
commit8e81ecbf1cb46b8d2d13e772d5924b09bd60169a
treec97f1202ebee186b345218ea4f72317a3dd5aae7
parent9095f233c0258e9a05e958c7d822eb38681e7a5a
printk_ringbuffer: Fix get_data() size sanity check

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 <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
Link: https://patch.msgid.link/20260326133809.8045-1-john.ogness@linutronix.de
Signed-off-by: Petr Mladek <pmladek@suse.com>
kernel/printk/printk_ringbuffer.c