bpf: clear user buf when bpf_d_path failed
Subsystems (4)
PATCH 1/1 bpf: clear user buf when bpf_d_path failed
The bpf_d_path() function may fail. If it does,
clear the user buf, like bpf_probe_read etc.
--- kernel/trace/bpf_trace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +4 -1
@@ -916,11 +916,14 @@ BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)* potentially broken verifier. */ len =copy_from_kernel_nofault (©, path,sizeof (*path)); -if (len <0 ) +if (len <0 ) { +memset (buf,0 , sz);return len; + } p =d_path (©, buf, sz);if (IS_ERR (p)) { +memset (buf,0 , sz); len =PTR_ERR (p); }else { len = buf + sz - p;
Signature · 1 line
2.48.1
Quoted 5 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.Signed-off-by Tao Chen <[email protected]>
[...]
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 7 lines · On Wed, 11 Jun 2025 23:48:58 +0800 you wrote:
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.Signed-off-by Tao Chen <[email protected]>--- kernel/trace/bpf_trace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
Here is the summary with links:
- [bpf-next] bpf: clear user buf when bpf_d_path failed
https://git.kernel.org/bpf/bpf-next/c/3b55a9e6738b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.
But that doesn't mean we *have to* do memset(0) for bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (BPF_F_PAD_ZEROS) for newer APIs. And if anything, bpf_d_path() is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.
In short, I feel like we should revert this and let users do zero-filling, if they really need to. bpf_probe_read_kernel(dst, sz, NULL) would do. But we should think about adding dynptr-based bpf_dynptr_memset() API for cases when the size is not known statically, IMO.
Quoted 29 lines
Signed-off-by Tao Chen <[email protected]>--- kernel/trace/bpf_trace.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)kernel/trace/bpf_trace.c +4
-2@@ -916,11 +916,14 @@ BPF_CALL_3(bpf_d_path, struct path *, path, char *, buf, u32, sz)* potentially broken verifier. */ len =copy_from_kernel_nofault (©, path,sizeof (*path)); -if (len <0 ) +if (len <0 ) { +memset (buf,0 , sz);return len; + } p =d_path (©, buf, sz);if (IS_ERR (p)) { +memset (buf,0 , sz); len =PTR_ERR (p); }else { len = buf + sz - p; --2.48.1
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.But that doesn't mean we *have to* do memset(0) for
bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).
Quoted 6 lines
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (
BPF_F_PAD_ZEROS) for newer APIs. And if anything,bpf_d_path()is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.
All old helpers had this BPF_F_PAD_ZEROS behavior (or rather should have had).
So it makes sense to zero in this helper too for consistency. I don't share performance concerns. This is an error path.
Quoted 20 lines · Alexei Starovoitov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.But that doesn't mean we *have to* do memset(0) for
bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).Quoted 6 lines
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (
BPF_F_PAD_ZEROS) for newer APIs. And if anything,bpf_d_path()is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.
Quoted 4 lines
All old helpers had this
BPF_F_PAD_ZEROSbehavior (or rather should have had).
So it makes sense to zero in this helper too for consistency. I don't share performance concerns. This is an error path.
It's just a bizarre behavior as it stands right now.
On error, you'll have a zeroed out buffer, OK, good so far.
On success, though, you'll have a buffer where first N bytes are filled out with good path information, but then the last sizeof(buf) - N bytes would be, effectively, garbage.
All in all, you can't use that buffer as a key for hashmap looking (because of leftover non-zeroed bytes at the end), yet on error we still zero out bytes for no apparently useful reason.
And then for the bpf_path_d_path(). What do we do about that one? It doesn't have zeroing out either in the error path, nor in the success path. So just more inconsistency all around.
Quoted 36 lines · Andrii Nakryiko
Quoted 20 lines · Alexei Starovoitov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.But that doesn't mean we *have to* do memset(0) for
bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).Quoted 6 lines
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (
BPF_F_PAD_ZEROS) for newer APIs. And if anything,bpf_d_path()is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.Quoted 4 lines
All old helpers had this
BPF_F_PAD_ZEROSbehavior (or rather should have had).
So it makes sense to zero in this helper too for consistency. I don't share performance concerns. This is an error path.It's just a bizarre behavior as it stands right now.
On error, you'll have a zeroed out buffer, OK, good so far.
On success, though, you'll have a buffer where first N bytes are filled out with good path information, but then the last sizeof(buf) - N bytes would be, effectively, garbage.
Quoted 7 lines
All in all, you can't use that buffer as a key for hashmap looking (because of leftover non-zeroed bytes at the end), yet on error we still zero out bytes for no apparently useful reason.
And then for the
bpf_path_d_path(). What do we do about that one? It doesn't have zeroing out either in the error path, nor in the success path. So just more inconsistency all around.
Consistency with bpf_path_d_path() kfunc is indeed missing.
Ok, since you insist, dropped this patch, and force pushed.
Quoted 43 lines · Alexei Starovoitov
Quoted 40 lines · Andrii Nakryiko
Quoted 20 lines · Alexei Starovoitov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.But that doesn't mean we *have to* do memset(0) for
bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).Quoted 6 lines
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (
BPF_F_PAD_ZEROS) for newer APIs. And if anything,bpf_d_path()is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.Quoted 4 lines
All old helpers had this
BPF_F_PAD_ZEROSbehavior (or rather should have had).
So it makes sense to zero in this helper too for consistency. I don't share performance concerns. This is an error path.It's just a bizarre behavior as it stands right now.
On error, you'll have a zeroed out buffer, OK, good so far.
On success, though, you'll have a buffer where first N bytes are filled out with good path information, but then the last sizeof(buf) - N bytes would be, effectively, garbage.
All in all, you can't use that buffer as a key for hashmap looking (because of leftover non-zeroed bytes at the end), yet on error we still zero out bytes for no apparently useful reason.
Quoted 7 lines
Quoted 3 lines
And then for the
bpf_path_d_path(). What do we do about that one? It doesn't have zeroing out either in the error path, nor in the success path. So just more inconsistency all around.Consistency with
bpf_path_d_path()kfunc is indeed missing.Ok, since you insist, dropped this patch, and force pushed.
Great, thank you!
Quoted 49 lines · Andrii Nakryiko
Quoted 47 lines · Alexei Starovoitov
Quoted 43 lines · Andrii Nakryiko
Quoted 20 lines · Alexei Starovoitov
Quoted 10 lines · Andrii Nakryiko
Quoted 4 lines · Tao Chen
The
bpf_d_path()function may fail. If it does,
clear the user buf, likebpf_probe_readetc.But that doesn't mean we *have to* do memset(0) for
bpf_d_path(), though. Especially given that path buffer can be pretty large (4KB).Quoted 6 lines
Is there an issue you are trying to address with this, or is it more of a consistency clean up? Note, that more or less recently we made this zero filling behavior an option with an extra flag (
BPF_F_PAD_ZEROS) for newer APIs. And if anything,bpf_d_path()is more akin to variable-sized string probing APIs rather than fixed-sized bpf_probe_read* family.Quoted 4 lines
All old helpers had this
BPF_F_PAD_ZEROSbehavior (or rather should have had).
So it makes sense to zero in this helper too for consistency. I don't share performance concerns. This is an error path.It's just a bizarre behavior as it stands right now.
On error, you'll have a zeroed out buffer, OK, good so far.
On success, though, you'll have a buffer where first N bytes are filled out with good path information, but then the last sizeof(buf) - N bytes would be, effectively, garbage.
All in all, you can't use that buffer as a key for hashmap looking (because of leftover non-zeroed bytes at the end), yet on error we still zero out bytes for no apparently useful reason.
And then for the
bpf_path_d_path(). What do we do about that one? It doesn't have zeroing out either in the error path, nor in the success path. So just more inconsistency all around.
Quoted 5 lines
Quoted 3 lines
Consistency with
bpf_path_d_path()kfunc is indeed missing.Ok, since you insist, dropped this patch, and force pushed.
Great, thank you!
The changes in this patch are relatively simple, but the discussion between the two of you is more meaningful to me. I agree with Andrii's point of view. Thank you both for discussing this patch.
Signature · 2 lines
Best Regards Tao Chen