vsprintf: Add test for restricted kernel pointers
PATCH 1/1 vsprintf: Add test for restricted kernel pointers
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.
---
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(-)
lib/tests/printf_kunit.c +25 -1
@@ -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,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Petr Mladek
maintainerQuoted 3 lines · 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_restrictknob.
I think that we could export "kptr_restrict" like we did with no_hash_pointers. AFAIK, it has been exported just because of the test module as well.
Quoted 2 lines
Please note that changes to the
kptr_restrictsysctl from the kernel commandline are only applied *after* the boot-time KUnit tests run.
This is another motivation to export the symbol. Otherwise, it is really hard to test the non-default variants.
BTW: I have recently heard about EXPORT_SYMBOL_NS(). It would allow
to export the symbol only for some specific modules.
I am not sure how exactly it works. I wonder if there already exists a namespace for KUnit tests.
It would be nice to use it, even for "no_hash_pointers"...
Quoted 16 lines
lib/tests/printf_kunit.c +9
-1@@ -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 );
lib/tests/printf_kunit.c symbol_ptr(struct kunit *kunittest) · line 337
Quoted 10 lines
lib/tests/printf_kunit.c +10
-0+ }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 );
Hmm, the behavior of %pK depends on capabilities of the caller. The above code would work when the test is built in.
But we might need to check the capabilities when using test module. If I get it correctly, the module loaded requires CAP_SYS_MODULE while %pK behavior depends on CAP_SYSLOG...
lib/tests/printf_kunit.c symbol_ptr(struct kunit *kunittest) · line 346
Quoted 9 lines
lib/tests/printf_kunit.c +6
-0+break ; +case 2 : +default : +test (ZEROS "00000000" ,"%pK" ,PTR ); +break ; + } } static void
Anyway, thanks for working on this.
Best Regards,
Petr
Andy Shevchenko
reviewerQuoted 1 line · Petr Mladek
On Wed 2026-01-14 08:27:34, Thomas Weißschuh wrote:
...
Quoted 14 lines
Quoted 3 lines
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_restrictknob.I think that we could export "
kptr_restrict" like we did withno_hash_pointers. AFAIK, it has been exported just because of the test module as well.Quoted 2 lines
Please note that changes to the
kptr_restrictsysctl from the kernel commandline are only applied *after* the boot-time KUnit tests run.This is another motivation to export the symbol. Otherwise, it is really hard to test the non-default variants.
Quoted 7 lines
BTW: I have recently heard about
EXPORT_SYMBOL_NS(). It would allow
to export the symbol only for some specific modules.I am not sure how exactly it works. I wonder if there already exists a namespace for KUnit tests.
It would be nice to use it, even for "
no_hash_pointers"...
This is a variant for really strict use:EXPORT_SYNBOL_FOR_MODULES().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Petr Mladek
Quoted 3 lines · 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_restrictknob.I think that we could export "
kptr_restrict" like we did withno_hash_pointers. AFAIK, it has been exported just because of the test module as well.
Quoted 5 lines
Quoted 2 lines
Please note that changes to the
kptr_restrictsysctl from the kernel commandline are only applied *after* the boot-time KUnit tests run.This is another motivation to export the symbol. Otherwise, it is really hard to test the non-default variants.
This might a good place to pitch an idea I have for %pK, which would also make testing easier:
I'd like to split out the configuration lookup out of vsprintf() into a dedicated function and let the user of vsprintf pass its explicitly. This allows us the enforce its usage only from file context and makes the caller responsible for handling the potential sleeping calls into the security subsystem. vsprintf() becomes a predictable string formatting function again.
/* include/linux/sprintf.h */
enum ptr_format {
PTR_FORMAT_HASHED,
PTR_FORMAT_PLAIN,
PTR_FORMAT_ZEROS,
};
/* Also calls might_sleep() */
enum ptr_format ptr_format_for_file(struct file *);
/* regular source file */
/* Perform kernel configuration and capabilities lookup */ enum ptr_format format = ptr_format_for_file(file);
/* potentially stash format in file-private data for caching */
/* Actually format the pointer */printk("%*pK\n, format, some_pointer);
Note: I removed all the non-file usages of %pK in the tree. Except for the one in arch/arm/mm/physaddr.c.
Quoted 7 lines
BTW: I have recently heard about
EXPORT_SYMBOL_NS(). It would allow
to export the symbol only for some specific modules.I am not sure how exactly it works. I wonder if there already exists a namespace for KUnit tests.
It would be nice to use it, even for "
no_hash_pointers"...
We also have EXPORT_SYMBOL_IF_KUNIT(). That would make the symbol visible to all kunit tests, but only if kunit is enabled.
Quoted 24 lines
Quoted 24 lines
lib/tests/printf_kunit.c +17
-1@@ -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 ;
Quoted 8 lines
lib/tests/printf_kunit.c symbol_ptr(struct kunit *kunittest) · line 337
Quoted 2 lines
lib/tests/printf_kunit.c +2
-0+case 1 : +test (PTR_STR ,"%pK" ,PTR );Hmm, the behavior of %pK depends on capabilities of the caller. The above code would work when the test is built in.
But we might need to check the capabilities when using test module. If I get it correctly, the module loaded requiresCAP_SYS_MODULEwhile %pK behavior depends onCAP_SYSLOG...
The kunit test executes in a dedicated kthread.
That should have its own capabilities, independently from the user loading the module. I think...
A comment is in order.
Quoted 14 lines
lib/tests/printf_kunit.c symbol_ptr(struct kunit *kunittest) · line 346
Quoted 9 lines
lib/tests/printf_kunit.c +6
-0+break ; +case 2 : +default : +test (ZEROS "00000000" ,"%pK" ,PTR ); +break ; + } } static voidAnyway, thanks for working on this.
Best Regards,
Petr