From 4d56efe69decb086796b68826aad2a4eeb9b788d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Mon, 4 May 2026 12:43:41 +0200 Subject: [PATCH] vsprintf: Add test for restricted kernel pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fill out the tests for restricted kernel pointers, using the %pK format. Signed-off-by: Thomas Weißschuh Reviewed-by: Petr Mladek Tested-by: Petr Mladek Link: https://patch.msgid.link/20260504-restricted-pointers-kunit-test-v2-2-19e8b1c0fbeb@linutronix.de [pmladek@suse.com: Removed questionable ifdeffery.] Signed-off-by: Petr Mladek --- lib/tests/printf_kunit.c | 22 +++++++++++++++++++++- lib/vsprintf.c | 1 + 2 files changed, 22 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 cbcc47bf8510..d700dd0a5261 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -857,6 +857,7 @@ static char *default_pointer(char *buf, char *end, const void *ptr, } int kptr_restrict __read_mostly; +EXPORT_SYMBOL_FOR_MODULES(kptr_restrict, "printf_kunit"); static noinline_for_stack char *restricted_pointer(char *buf, char *end, const void *ptr,