public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] vsprintf: Add test for restricted kernel pointers
@ 2026-01-14  7:27 Thomas Weißschuh
  2026-01-16 17:06 ` Petr Mladek
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Weißschuh @ 2026-01-14  7:27 UTC (permalink / raw)
  To: Petr Mladek, Steven Rostedt, Andy Shevchenko, Rasmus Villemoes,
	Sergey Senozhatsky
  Cc: linux-kernel, Thomas Weißschuh

Fill out the tests for restricted kernel pointers, using the %pK format.
This test can only be executed when built into the kernel, as modules
do not have access to the kptr_restrict knob.

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 | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c
index 7617e5b8b02c..af15d2ab4046 100644
--- a/lib/tests/printf_kunit.c
+++ b/lib/tests/printf_kunit.c
@@ -316,7 +316,31 @@ symbol_ptr(struct kunit *kunittest)
 static void
 kernel_ptr(struct kunit *kunittest)
 {
-	/* We can't test this without access to kptr_restrict. */
+#ifdef MODULE
+	kunit_skip(kunittest, "cannot access kptr_restrict from test module");
+	return;
+#endif
+
+	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:
+		test(PTR_STR, "%pK", PTR);
+		break;
+	case 2:
+	default:
+		test(ZEROS "00000000", "%pK", PTR);
+		break;
+	}
 }
 
 static void

---
base-commit: 8f0b4cce4481fb22653697cced8d0d04027cb1e8
change-id: 20260109-restricted-pointers-kunit-test-ff4ec887bcd8

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh@linutronix.de>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-01-19  7:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-14  7:27 [PATCH] vsprintf: Add test for restricted kernel pointers Thomas Weißschuh
2026-01-16 17:06 ` Petr Mladek
2026-01-19  7:34   ` Andy Shevchenko
2026-01-19  7:43   ` Thomas Weißschuh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox