bpf: Don't use %pK through printk
PATCH 1/1 bpf: Don't use %pK through printk
In the past %pK was preferable to %p as it would not leak raw pointer values into the kernel log.
Since commit ad67b74d2469 ("printk: hash addresses printed with %p") the regular %p has been improved to avoid this issue. Furthermore, restricted pointers ("%pK") were never meant to be used through printk(). They can still unintentionally leak raw pointers or acquire sleeping locks in atomic contexts.
Switch to the regular pointer formatting which is safer and easier to reason about.
--- include/linux/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
@@ -1296,7 +1296,7 @@ void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);static inline void bpf_jit_dump (unsigned int flen,unsigned int proglen,u32 pass,void *image) { -pr_err ("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n" , flen, +pr_err ("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n" , flen, proglen, pass, image, current->comm,task_pid_nr (current));if (image)
---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250811-restricted-pointers-bpf-04da04ea1b8a
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
Quoted 22 lines · Thomas Weißschuh
In the past %pK was preferable to %p as it would not leak raw pointer values into the kernel log.
Since commitad67b74d2469("printk: hash addresses printed with %p") the regular %p has been improved to avoid this issue. Furthermore, restricted pointers ("%pK") were never meant to be used throughprintk(). They can still unintentionally leak raw pointers or acquire sleeping locks in atomic contexts.Switch to the regular pointer formatting which is safer and easier to reason about.
Signed-off-by Thomas Weißschuh <[email protected]>--- include/linux/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/filter.h +0
-0
include/linux/filter.h void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other); · line 1300
Quoted 6 lines
include/linux/filter.h +1
-1@@ -1296,7 +1296,7 @@ void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);static inline void bpf_jit_dump (unsigned int flen,unsigned int proglen,u32 pass,void *image) { -pr_err ("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n" , flen, +pr_err ("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n" , flen, proglen, pass, image, current->comm,task_pid_nr (current));
this particular printk won't ever be called from atomic context, so I don't think the leak from atomic context matters much here. On the other hand, %pK behavior is controlled by kptr_restrict and that might be useful for debugging, so not sure there is much of a benefit to switching to always obfuscated %p? Or am I missing something else here?
Quoted 11 lines
include/linux/filter.h +0
-1if (image) ---base-commit:
8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250811-restricted-pointers-bpf-04da04ea1b8aBest regards,
--
Thomas Weißschuh <[email protected]>
Quoted 29 lines · Andrii Nakryiko
Quoted 27 lines · Thomas Weißschuh
In the past %pK was preferable to %p as it would not leak raw pointer values into the kernel log.
Since commitad67b74d2469("printk: hash addresses printed with %p") the regular %p has been improved to avoid this issue. Furthermore, restricted pointers ("%pK") were never meant to be used throughprintk(). They can still unintentionally leak raw pointers or acquire sleeping locks in atomic contexts.Switch to the regular pointer formatting which is safer and easier to reason about.
Signed-off-by Thomas Weißschuh <[email protected]>--- include/linux/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/filter.h +1
-1@@ -1296,7 +1296,7 @@ void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);static inline void bpf_jit_dump (unsigned int flen,unsigned int proglen,u32 pass,void *image) { - pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen, + pr_err("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n", flen,
Quoted 8 lines
include/linux/filter.h void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other); · line 1300
Quoted 1 line
include/linux/filter.h +0
-0proglen, pass, image, current->comm, task_pid_nr(current));this particular printk won't ever be called from atomic context, so I don't think the leak from atomic context matters much here. On the other hand, %pK behavior is controlled by
kptr_restrictand that might be useful for debugging, so not sure there is much of a benefit to switching to always obfuscated %p? Or am I missing something else here?
As %pK is so easy to abuse and the breakage is very non-obvious, I want to rework it to enforce its usage from "file context".
For that, the printk users need to be gone first.
For debugging, there is still "no_hash_pointers".
How would the image pointer be used for debugging? It is logged from nowhere else.
And the raw image is dumped right after anyways.
Quoted 12 lines
Quoted 12 lines
include/linux/filter.h +0
-1if (image) ---base-commit:
8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250811-restricted-pointers-bpf-04da04ea1b8aBest regards,
--
Thomas Weißschuh <[email protected]>
Quoted 40 lines · Thomas Weißschuh
Quoted 29 lines · Andrii Nakryiko
Quoted 27 lines · Thomas Weißschuh
In the past %pK was preferable to %p as it would not leak raw pointer values into the kernel log.
Since commitad67b74d2469("printk: hash addresses printed with %p") the regular %p has been improved to avoid this issue. Furthermore, restricted pointers ("%pK") were never meant to be used throughprintk(). They can still unintentionally leak raw pointers or acquire sleeping locks in atomic contexts.Switch to the regular pointer formatting which is safer and easier to reason about.
Signed-off-by Thomas Weißschuh <[email protected]>--- include/linux/filter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/filter.h +1
-1@@ -1296,7 +1296,7 @@ void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other);static inline void bpf_jit_dump (unsigned int flen,unsigned int proglen,u32 pass,void *image) { - pr_err("flen=%u proglen=%u pass=%u image=%pK from=%s pid=%d\n", flen, + pr_err("flen=%u proglen=%u pass=%u image=%p from=%s pid=%d\n", flen,Quoted 8 lines
include/linux/filter.h void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other); · line 1300
Quoted 1 line
include/linux/filter.h +0
-0proglen, pass, image, current->comm, task_pid_nr(current));this particular printk won't ever be called from atomic context, so I don't think the leak from atomic context matters much here. On the other hand, %pK behavior is controlled by
kptr_restrictand that might be useful for debugging, so not sure there is much of a benefit to switching to always obfuscated %p? Or am I missing something else here?
Quoted 6 lines
As %pK is so easy to abuse and the breakage is very non-obvious, I want to rework it to enforce its usage from "file context".
For that, the printk users need to be gone first.
For debugging, there is still "no_hash_pointers".How would the image pointer be used for debugging?
I chatted with Daniel about this offline, and it seems like this is not that critical nowadays. So I went ahead and applied the patch to bpf-next.
Quoted 15 lines
It is logged from nowhere else.
And the raw image is dumped right after anyways.Quoted 12 lines
Quoted 12 lines
include/linux/filter.h +0
-1if (image) ---base-commit:
8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250811-restricted-pointers-bpf-04da04ea1b8aBest regards,
--
Thomas Weißschuh <[email protected]>
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 9 lines · On Mon, 11 Aug 2025 14:08:04 +0200 you wrote:
In the past %pK was preferable to %p as it would not leak raw pointer values into the kernel log.
Since commitad67b74d2469("printk: hash addresses printed with %p") the regular %p has been improved to avoid this issue. Furthermore, restricted pointers ("%pK") were never meant to be used throughprintk(). They can still unintentionally leak raw pointers or acquire sleeping locks in atomic contexts.[...]
Here is the summary with links:
- bpf: Don't use %pK through printk
https://git.kernel.org/bpf/bpf-next/c/2caa6b88e0ba
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html