From: Thomas Weißschuh Date: Mon, 4 May 2026 10:43:41 +0000 (+0200) Subject: vsprintf: Add test for restricted kernel pointers X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4d56efe69decb086796b68826aad2a4eeb9b788d;p=thirdparty%2Fkernel%2Flinux.git vsprintf: Add test for restricted kernel pointers 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 --- diff --git a/lib/tests/printf_kunit.c b/lib/tests/printf_kunit.c index bb70b9cddadd8..58e639b01e836 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 cbcc47bf85109..d700dd0a52610 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,