vsprintf: Add test for restricted kernel pointers
Subsystems (2)
v2 cover vsprintf: Add test for restricted kernel pointers
Fill out the tests for restricted kernel pointers, using the %pK format.
---
Changes in v2:
- Restrict the visibility of no_hash_pointers.
- Handle printf_kunit being a module.
- Add note about KUnit capabilities for kptr_restrict==1.
- Link to v1: https://lore.kernel.org/r/20260114-restricted-pointers-kunit-test-v1-1-a9d8a49c6b6f@linutronix.de
To: Andrew Morton <[email protected]>
To: Petr Mladek <[email protected]>
To: Steven Rostedt <[email protected]>
To: Andy Shevchenko <[email protected]>
To: Rasmus Villemoes <[email protected]>
To: Sergey Senozhatsky <[email protected]>
---
Thomas Weißschuh (2):
vsprintf: Only export no_hash_pointers to test module
vsprintf: Add test for restricted kernel pointers
lib/tests/printf_kunit.c | 22 +++++++++++++++++++++- lib/vsprintf.c | 7 ++++++- 2 files changed, 27 insertions(+), 2 deletions(-)
---
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
change-id: 20260109-restricted-pointers-kunit-test-ff4ec887bcd8
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH v2 1/2 vsprintf: Only export no_hash_pointers to test module
Aside from the printf test module, no module should ever use this symbol.
--- lib/vsprintf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/vsprintf.c +3 -1
@@ -59,7 +59,9 @@/* Disable pointer hashing if requested */ bool no_hash_pointers__ro_after_init ; -EXPORT_SYMBOL_GPL (no_hash_pointers); +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif /* * Hashed pointers policy selected by "hash_pointers=..." boot param
Signature · 1 line
2.53.0
Andy Shevchenko
reviewerRe: [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module
Quoted 1 line · Thomas Weißschuh
Aside from the printf test module, no module should ever use this symbol.
...
lib/vsprintf.c line 64
Quoted 5 lines
lib/vsprintf.c +3
-1-EXPORT_SYMBOL_GPL (no_hash_pointers); +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif
But do we need that ugly ifdeffery? the infrastructure should handle that. (Also, if I build a module separately after the kernel, it won't work.)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module
Quoted 5 lines · Andy Shevchenko
Quoted 1 line · Thomas Weißschuh
Aside from the printf test module, no module should ever use this symbol.
...
Quoted 7 lines
lib/vsprintf.c line 64
Quoted 5 lines
lib/vsprintf.c +3
-1-EXPORT_SYMBOL_GPL (no_hash_pointers); +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif But do we need that ugly ifdeffery? the infrastructure should handle that.
Where does the generic infrastructure take the kconfig option into account?
Quoted 1 line
(Also, if I build a module separately after the kernel, it won't work.)
Only if it was not enabled when the kernel was built.
But if people are unhappy with the ifdeffery, I'm fine with dropping it. It is not a hard requirement at all.
Thomas
Andy Shevchenko
reviewerRe: [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Andy Shevchenko
On Mon, May 04, 2026 at 12:43:40PM +0200, Thomas Weißschuh wrote:
...
Quoted 9 lines
Quoted 7 lines
lib/vsprintf.c line 64
Quoted 5 lines
lib/vsprintf.c +3
-1-EXPORT_SYMBOL_GPL (no_hash_pointers); +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif But do we need that ugly ifdeffery? the infrastructure should handle that.
Where does the generic infrastructure take the kconfig option into account?
Nowhere, but my point that it's taken care of at run-time.
Quoted 6 lines
Quoted 1 line
(Also, if I build a module separately after the kernel, it won't work.)
Only if it was not enabled when the kernel was built.
But if people are unhappy with the ifdeffery, I'm fine with dropping it. It is not a hard requirement at all.
I think it's unnecessary to have. But I'm not a maintainer of this code.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module
In reply to Thomas WeißschuhQuoted 8 lines · Thomas Weißschuh
Quoted 7 lines · Andy Shevchenko
Quoted 1 line · Thomas Weißschuh
Aside from the printf test module, no module should ever use this symbol.
...
Quoted 1 line
-EXPORT_SYMBOL_GPL(no_hash_pointers);
Quoted 7 lines
Quoted 5 lines
lib/vsprintf.c line 64
Quoted 3 lines
lib/vsprintf.c +3
-0+#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif But do we need that ugly ifdeffery? the infrastructure should handle that.
Where does the generic infrastructure take the kconfig option into account?
My understanding is that it does not harm when the symbol is exported even when the related module is built-in or not built at all. The important thing is that it is exported only for this particular module (name).
BTW: I have just tried to remove the ifdeffery and use a built-in printf_kunit and it works fine.
Quoted 6 lines
Quoted 1 line
(Also, if I build a module separately after the kernel, it won't work.)
Only if it was not enabled when the kernel was built.
But if people are unhappy with the ifdeffery, I'm fine with dropping it. It is not a hard requirement at all.
Yeah, I would prefer to remove the ifdeffery. It does not look worth it.
Without the ifdeffery:
Best Regards,
Petr
PS: No need to send v3, I could remove the ifdeffery when pushing the patch.
Re: [PATCH v2 1/2] vsprintf: Only export no_hash_pointers to test module
Quoted 15 lines · Petr Mladek
Quoted 14 lines · Thomas Weißschuh
Quoted 12 lines · Andy Shevchenko
Quoted 1 line · Thomas Weißschuh
Aside from the printf test module, no module should ever use this symbol.
...
lib/vsprintf.c line 64
Quoted 5 lines
lib/vsprintf.c +3
-1-EXPORT_SYMBOL_GPL (no_hash_pointers); +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (no_hash_pointers,"printf_kunit" ); +#endif But do we need that ugly ifdeffery? the infrastructure should handle that.
Quoted 4 lines
Quoted 1 line
Where does the generic infrastructure take the kconfig option into account?
My understanding is that it does not harm when the symbol is exported even when the related module is built-in or not built at all.
I think it wastes a bit of space in the kernel image, not that it would matter.
Quoted 1 line
The important thing is that it is exported only for this particular module (name).
Theoretically an out-of-tree module could assume this name to get access to the symbol, which I was trying to prevent. But that is a theoretical edge case and I am absolutely fine with not caring about it.
My OCD insists that we should have a variant of EXPORT_SYMBOL_FOR_MODULES() which does handle the corresponding kconfig settings, maybe even automatically. Something for the wishlist.
Quoted 15 lines
BTW: I have just tried to remove the ifdeffery and use a built-in
printf_kunitand it works fine.Quoted 6 lines
Quoted 1 line
(Also, if I build a module separately after the kernel, it won't work.)
Only if it was not enabled when the kernel was built.
But if people are unhappy with the ifdeffery, I'm fine with dropping it. It is not a hard requirement at all.
Yeah, I would prefer to remove the ifdeffery. It does not look worth it.
Without the ifdeffery:
Quoted 7 lines
Reviewed-by Petr Mladek <[email protected]>Tested-by Petr Mladek <[email protected]>Best Regards,
PetrPS: No need to send v3, I could remove the ifdeffery when pushing the patch.
Works for me, thanks!
Thomas
PATCH v2 2/2 vsprintf: Add test for restricted kernel pointers
In reply to Thomas WeißschuhFill out the tests for restricted kernel pointers, using the %pK format.
---
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 | 22 +++++++++++++++++++++- lib/vsprintf.c | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-)
lib/tests/printf_kunit.c +21 -1
@@ -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
lib/vsprintf.c +3 -0
@@ -859,6 +859,9 @@ static char *default_pointer(char *buf, char *end, const void *ptr, }int kptr_restrict__read_mostly ; +#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (kptr_restrict,"printf_kunit" ); +#endif static noinline_for_stack char *restricted_pointer(char *buf, char *end, const void *ptr,
Signature · 1 line
2.53.0
Andy Shevchenko
reviewerQuoted 1 line · Thomas Weißschuh
Fill out the tests for restricted kernel pointers, using the %pK format.
...
lib/vsprintf.c static char *default_pointer(char *buf, char *end, const void *ptr, · line 864
Quoted 3 lines
lib/vsprintf.c +3
-0+#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (kptr_restrict,"printf_kunit" ); +#endif
Same Q here.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 1 line · Thomas Weißschuh
Fill out the tests for restricted kernel pointers, using the %pK format.
...
lib/vsprintf.c static char *default_pointer(char *buf, char *end, const void *ptr, · line 864
Quoted 3 lines
lib/vsprintf.c +3
-0+#if IS_MODULE (CONFIG_PRINTF_KUNIT_TEST ) +EXPORT_SYMBOL_FOR_MODULES (kptr_restrict,"printf_kunit" ); +#endif Same Q here.
Same here.
With the ifdeffs removed:
Best Regards,
Petr
PS: No need to send v3. I could remove the ifdeffs when committing
the patchset. I am going to commit it tomorrow unless anyone complains.
I am sorry for the late review and short notice before committing. I had vacation last 5 days. And I am traveling next week.
Petr Mladek
maintainerQuoted 9 lines · Thomas Weißschuh
Fill out the tests for restricted kernel pointers, using the %pK format.
---
Changes in v2:
- Restrict the visibility ofno_hash_pointers.
- Handleprintf_kunitbeing a module.
- Add note about KUnit capabilities for kptr_restrict==1.
- Link to v1: https://lore.kernel.org/r/20260114-restricted-pointers-kunit-test-v1-1-a9d8a49c6b6f@linutronix.de
Quoted 6 lines
vsprintf: Only export
no_hash_pointersto test module
vsprintf: Add test for restricted kernel pointerslib/tests/printf_kunit.c | 22 +++++++++++++++++++++- lib/vsprintf.c | 7 ++++++- 2 files changed, 27 insertions(+), 2 deletions(-)
JFYI, the patchset has been committed into printk/linux.git, branch for-7.2.
I have removed the questionable ifdeffery as discussed, see the result at
https://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git/log/?h=for-7.2
Best Regards,
Petr