]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
vsprintf: Add test for restricted kernel pointers
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Mon, 4 May 2026 10:43:41 +0000 (12:43 +0200)
committerPetr Mladek <pmladek@suse.com>
Thu, 21 May 2026 14:04:54 +0000 (16:04 +0200)
Fill out the tests for restricted kernel pointers, using the %pK format.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
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 <pmladek@suse.com>
lib/tests/printf_kunit.c
lib/vsprintf.c

index bb70b9cddadd8b9acc61aeb6a62a8f441266355f..58e639b01e836fe7762f78f8f6b008013351c2a6 100644 (file)
@@ -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
index cbcc47bf85109c5af55c37e629a332e996973380..d700dd0a52610f6f827bfc7fd1d2203ed428113d 100644 (file)
@@ -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,