bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.
PATCH 1/1 bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from bpf_xdp_pointer() is either an error or NULL before returning it.
The function bpf_dynptr_slice() mistakenly returned an ERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned by bpf_xdp_pointer() is an error or NULL.
--- kernel/bpf/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +1 -1
@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offsetcase BPF_DYNPTR_TYPE_XDP : {void *xdp_ptr =bpf_xdp_pointer (ptr->data, ptr->offset + offset, len); -if (xdp_ptr) +if (!IS_ERR_OR_NULL (xdp_ptr))return xdp_ptr;if (!buffer__opt)
Signature · 1 line
2.34.1
Quoted 9 lines · On 8/3/23 4:12 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.
Quoted 5 lines
Reported-by Dan Carpenter <[email protected]>Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>
Quoted 12 lines · Yonghong Song
Quoted 9 lines · On 8/3/23 4:12 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.
Quoted 7 lines
Quoted 5 lines
Reported-by Dan Carpenter <[email protected]>Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>Acked-by Yonghong Song <[email protected]>
Thanks!
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Martin KaFai Lau <[email protected]>:
Quoted 10 lines · On Thu, 3 Aug 2023 16:12:06 -0700 you wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.[...]
Here is the summary with links:
- [bpf-next] bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.
https://git.kernel.org/bpf/bpf-next/c/5426700e6841
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Martin KaFai Lau
reviewerQuoted 23 lines · On 8/3/23 4:12 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.Reported-by Dan Carpenter <[email protected]>Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>--- kernel/bpf/helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2273
Quoted 5 lines
kernel/bpf/helpers.c +1
-1@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offsetcase BPF_DYNPTR_TYPE_XDP : {void *xdp_ptr =bpf_xdp_pointer (ptr->data, ptr->offset + offset, len); -if (xdp_ptr) +if (!IS_ERR_OR_NULL (xdp_ptr))
Considering the earlier bpf_dynptr_check_off_len() should have avoided the IS_ERR() case here, I think targeting bpf-next makes sense. Applied.
Quoted 19 lines · Martin KaFai Lau
Quoted 18 lines · On 8/3/23 4:12 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.Reported-by Dan Carpenter <[email protected]>Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/helpers.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 11 lines
kernel/bpf/helpers.c +0
-0@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct
bpf_dynptr_kern*ptr, u32 offset
caseBPF_DYNPTR_TYPE_XDP:
{
void*xdp_ptr=bpf_xdp_pointer(ptr->data, ptr->offset + offset, len); - if (xdp_ptr)
+ if (!IS_ERR_OR_NULL(xdp_ptr))Considering the earlier
bpf_dynptr_check_off_len()should have avoided theIS_ERR()case here, I think targeting bpf-next makes sense. Applied.
It is a good point. I think the bpf_dynptr_check_off_len() check is wrong as well. According to the behavior of the rest of the function, it should be
err = bpf_dynptr_check_off_len(ptr, ptr->offset + offset, len);
How do you think?
Martin KaFai Lau
reviewerQuoted 36 lines · Kui-Feng Lee
Quoted 19 lines · Martin KaFai Lau
Quoted 18 lines · On 8/3/23 4:12 PM, [email protected] wrote:
From: Kui-Feng Lee <[email protected]>
Verify if the pointer obtained from
bpf_xdp_pointer()is either an error or NULL before returning it.The function
bpf_dynptr_slice()mistakenly returned anERR_PTR. Instead of solely checking for NULL, it should also verify if the pointer returned bybpf_xdp_pointer()is an error or NULL.Reported-by Dan Carpenter <[email protected]>Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Kui-Feng Lee <[email protected]>---
kernel/bpf/helpers.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 13 lines
Quoted 11 lines
kernel/bpf/helpers.c +0
-0@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct
bpf_dynptr_kern*ptr, u32 offset
caseBPF_DYNPTR_TYPE_XDP:
{
void*xdp_ptr=bpf_xdp_pointer(ptr->data, ptr->offset + offset, len); - if (xdp_ptr)
+ if (!IS_ERR_OR_NULL(xdp_ptr))Considering the earlier
bpf_dynptr_check_off_len()should have avoided theIS_ERR()case here, I think targeting bpf-next makes sense. Applied.
Quoted 5 lines
It is a good point. I think the
bpf_dynptr_check_off_len()check is wrong as well. According to the behavior of the rest of the function, it should beerr =
bpf_dynptr_check_off_len(ptr, ptr->offset + offset, len);
Not sure why it is needed either.
The bpf_dynptr_adjust() has updated the size after updating the offset. Did I missing other offset update places?