From: Petr Mladek <pmladek@suse.com>
To: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"John Ogness" <john.ogness@linutronix.de>,
"Dan Carpenter" <dan.carpenter@linaro.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Kees Cook <kees@kernel.org>,
"Gustavo A . R . Silva" <gustavoars@kernel.org>,
David Gow <davidgow@google.com>, Arnd Bergmann <arnd@kernel.org>,
Arnd Bergmann <arnd@arndb.de>,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org,
Petr Mladek <pmladek@suse.com>
Subject: [PATCH 3/3] printk: kunit: Fix __counted_by() in struct prbtest_rbdata
Date: Wed, 2 Jul 2025 11:51:57 +0200 [thread overview]
Message-ID: <20250702095157.110916-4-pmladek@suse.com> (raw)
In-Reply-To: <20250702095157.110916-1-pmladek@suse.com>
__counted_by() has to point to a variable which defines the size
of the related array. The code must never access the array
beyond this limit.
struct prbtest_rbdata currently stores the length of the string.
And the code access the array beyond the limit when writing
or reading the trailing '\0'.
Store the size of the string, including the trailing '\0' if
we wanted to keep __counted_by().
Consistently use "_size" suffix when the trailing '\0' is counted.
Note that MAX_RBDATA_TEXT_SIZE was originally used to limit
the text length.
When touching the code, make sure that @text_size produced by
get_random_u32_inclusive() stays within the limits.
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/r/eaea66b9-266a-46e7-980d-33f40ad4b215@sabinyo.mountain
Suggested-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Signed-off-by: Petr Mladek <pmladek@suse.com>
---
kernel/printk/printk_ringbuffer_kunit_test.c | 47 +++++++++++---------
1 file changed, 27 insertions(+), 20 deletions(-)
diff --git a/kernel/printk/printk_ringbuffer_kunit_test.c b/kernel/printk/printk_ringbuffer_kunit_test.c
index 0c3030fde8c2..088fe4d8c9b6 100644
--- a/kernel/printk/printk_ringbuffer_kunit_test.c
+++ b/kernel/printk/printk_ringbuffer_kunit_test.c
@@ -52,13 +52,12 @@ module_param(runtime_ms, ulong, 0400);
/* test data structure */
struct prbtest_rbdata {
- unsigned int len;
- char text[] __counted_by(len);
+ unsigned int size;
+ char text[] __counted_by(size);
};
-#define MAX_RBDATA_TEXT_SIZE 0x7f
-/* +1 for terminator. */
-#define MAX_PRB_RECORD_SIZE (sizeof(struct prbtest_rbdata) + MAX_RBDATA_TEXT_SIZE + 1)
+#define MAX_RBDATA_TEXT_SIZE 0x80
+#define MAX_PRB_RECORD_SIZE (sizeof(struct prbtest_rbdata) + MAX_RBDATA_TEXT_SIZE)
struct prbtest_data {
struct kunit *test;
@@ -74,25 +73,29 @@ struct prbtest_thread_data {
static void prbtest_fail_record(struct kunit *test, const struct prbtest_rbdata *dat, u64 seq)
{
- KUNIT_FAIL(test, "BAD RECORD: seq=%llu len=%u text=%.*s\n",
- seq, dat->len,
- dat->len <= MAX_RBDATA_TEXT_SIZE ? dat->len : -1,
- dat->len <= MAX_RBDATA_TEXT_SIZE ? dat->text : "<invalid>");
+ unsigned int len;
+
+ len = dat->size - 1;
+
+ KUNIT_FAIL(test, "BAD RECORD: seq=%llu size=%u text=%.*s\n",
+ seq, dat->size,
+ len < MAX_RBDATA_TEXT_SIZE ? len : -1,
+ len < MAX_RBDATA_TEXT_SIZE ? dat->text : "<invalid>");
}
static bool prbtest_check_data(const struct prbtest_rbdata *dat)
{
unsigned int len;
- /* Sane length? */
- if (dat->len < 1 || dat->len > MAX_RBDATA_TEXT_SIZE)
+ /* Sane size? At least one character + trailing '\0' */
+ if (dat->size < 2 || dat->size > MAX_RBDATA_TEXT_SIZE)
return false;
- if (dat->text[dat->len] != '\0')
+ len = dat->size - 1;
+ if (dat->text[len] != '\0')
return false;
/* String repeats with the same character? */
- len = dat->len;
while (len--) {
if (dat->text[len] != dat->text[0])
return false;
@@ -114,10 +117,14 @@ static int prbtest_writer(void *data)
kunit_info(tr->test_data->test, "start thread %03lu (writer)\n", tr->num);
for (;;) {
- /* ensure at least 1 character */
- text_size = get_random_u32_inclusive(1, MAX_RBDATA_TEXT_SIZE);
- /* +1 for terminator. */
- record_size = sizeof(struct prbtest_rbdata) + text_size + 1;
+ /* ensure at least 1 character + trailing '\0' */
+ text_size = get_random_u32_inclusive(2, MAX_RBDATA_TEXT_SIZE);
+ if (WARN_ON_ONCE(text_size < 2))
+ text_size = 2;
+ if (WARN_ON_ONCE(text_size > MAX_RBDATA_TEXT_SIZE))
+ text_size = MAX_RBDATA_TEXT_SIZE;
+
+ record_size = sizeof(struct prbtest_rbdata) + text_size;
WARN_ON_ONCE(record_size > MAX_PRB_RECORD_SIZE);
/* specify the text sizes for reservation */
@@ -140,9 +147,9 @@ static int prbtest_writer(void *data)
r.info->text_len = record_size;
dat = (struct prbtest_rbdata *)r.text_buf;
- dat->len = text_size;
- memset(dat->text, text_id, text_size);
- dat->text[text_size] = 0;
+ dat->size = text_size;
+ memset(dat->text, text_id, text_size - 1);
+ dat->text[text_size - 1] = '\0';
prb_commit(&e);
--
2.50.0
next prev parent reply other threads:[~2025-07-02 9:52 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 9:51 [PATCH 0/3] printk: KUnit: Followup fixes for the new KUnit test Petr Mladek
2025-07-02 9:51 ` [PATCH 1/3] printk: ringbuffer: Explain why the KUnit test ignores failed writes Petr Mladek
2025-07-04 11:28 ` John Ogness
2025-07-02 9:51 ` [PATCH 2/3] printk: kunit: support offstack cpumask Petr Mladek
2025-07-02 20:28 ` Nathan Chancellor
2025-07-08 14:24 ` Petr Mladek
2025-07-08 14:48 ` Arnd Bergmann
2025-07-09 11:36 ` Petr Mladek
2025-07-09 12:53 ` Thomas Weißschuh
2025-07-10 13:51 ` Petr Mladek
2025-07-10 14:08 ` Arnd Bergmann
2025-09-02 13:55 ` Petr Mladek
2025-07-03 14:36 ` kernel test robot
2025-07-02 9:51 ` Petr Mladek [this message]
2025-07-04 11:41 ` [PATCH 3/3] printk: kunit: Fix __counted_by() in struct prbtest_rbdata John Ogness
2025-07-02 15:48 ` [PATCH 0/3] printk: KUnit: Followup fixes for the new KUnit test Thomas Weißschuh
2025-07-10 15:29 ` Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250702095157.110916-4-pmladek@suse.com \
--to=pmladek@suse.com \
--cc=arnd@arndb.de \
--cc=arnd@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=davidgow@google.com \
--cc=gustavoars@kernel.org \
--cc=john.ogness@linutronix.de \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=thomas.weissschuh@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox