public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
To: Andrew Morton <akpm@linux-foundation.org>,
	 Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	 Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: linux-kernel@vger.kernel.org,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>
Subject: [PATCH v2 2/2] vsprintf: Add test for restricted kernel pointers
Date: Mon, 04 May 2026 12:43:41 +0200	[thread overview]
Message-ID: <20260504-restricted-pointers-kunit-test-v2-2-19e8b1c0fbeb@linutronix.de> (raw)
In-Reply-To: <20260504-restricted-pointers-kunit-test-v2-0-19e8b1c0fbeb@linutronix.de>

Fill out the tests for restricted kernel pointers, using the %pK format.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>

---
Please note that changes to the kptr_restrict sysctl from the kernel
commandline are only applied *after* the boot-time KUnit tests run.
---
 lib/tests/printf_kunit.c | 22 +++++++++++++++++++++-
 lib/vsprintf.c           |  3 +++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index bb70b9cddadd..58e639b01e83 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -319,7 +319,27 @@ symbol_ptr(struct kunit *kunittest)
 static void
 kernel_ptr(struct kunit *kunittest)
 {
-	/* We can't test this without access to kptr_restrict. */
+	switch (kptr_restrict) {
+	case 0:
+		if (no_hash_pointers) {
+			test(PTR_STR, "%pK", PTR);
+		} else {
+			char buf[PLAIN_BUF_SIZE];
+
+			plain_hash_to_buffer(kunittest, PTR, buf, PLAIN_BUF_SIZE);
+			/* %pK behaves the same as hashing */
+			test(buf, "%pK", PTR);
+		}
+		break;
+	case 1:
+		/* The KUnit kthread has all capabilities, including CAP_SYSLOG */
+		test(PTR_STR, "%pK", PTR);
+		break;
+	case 2:
+	default:
+		test(ZEROS "00000000", "%pK", PTR);
+		break;
+	}
 }
 
 static void
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index f70d240a394c..dc7e012a6ed7 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -859,6 +859,9 @@ static char *default_pointer(char *buf, char *end, const void *ptr,
 }
 
 int kptr_restrict __read_mostly;
+#if IS_MODULE(CONFIG_PRINTF_KUNIT_TEST)
+EXPORT_SYMBOL_FOR_MODULES(kptr_restrict, "printf_kunit");
+#endif
 
 static noinline_for_stack
 char *restricted_pointer(char *buf, char *end, const void *ptr,

-- 
2.53.0


  parent reply	other threads:[~2026-05-04 10:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-04 10:43 [PATCH v2 0/2] vsprintf: Add test for restricted kernel pointers Thomas Weißschuh
2026-05-04 10:43 ` [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module Thomas Weißschuh
2026-05-04 10:57   ` Andy Shevchenko
2026-05-04 11:53     ` Thomas Weißschuh
2026-05-04 14:11       ` Andy Shevchenko
2026-05-04 10:43 ` Thomas Weißschuh [this message]
2026-05-04 10:58   ` [PATCH v2 2/2] vsprintf: Add test for restricted kernel pointers Andy Shevchenko

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=20260504-restricted-pointers-kunit-test-v2-2-19e8b1c0fbeb@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=pmladek@suse.com \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    /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