Skip to contentSkip to conversation

bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.

PATCH Applied [email protected] 5 participants 7 messages Aug 3, 2023 – Aug 7, 2023 BPF

lore
Share & tools
lore
#0 lore raw

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.

Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")
---
 kernel/bpf/helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +1 -1
index 56ce5008aedd..eb91cae0612a 100644
@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 	case 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
reviewer Acked-by on v1 1/1
#1 lore raw
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 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.

Quoted 5 lines
Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")
#2 lore raw
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 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.

Quoted 7 lines
Quoted 5 lines
Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")

Thanks!

#3 lore raw
In reply to [email protected]
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 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.

[...]

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
#4 lore raw
In reply to [email protected]
Quoted 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 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.

Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")
---
  kernel/bpf/helpers.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/helpers.c +0 -0
index 56ce5008aedd..eb91cae0612a 100644

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 offset
  	case 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.

#5 lore raw
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 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.

Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")

---
  kernel/bpf/helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 11 lines
kernel/bpf/helpers.c +0 -0
index 56ce5008aedd..eb91cae0612a 100644
@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct 

bpf_dynptr_kern *ptr, u32 offset
      case 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.

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?

#6 lore raw
Quoted 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 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.

Fixes 66e3a13e7c2c ("bpf: Add bpf_dynptr_slice and bpf_dynptr_slice_rdwr")

---
  kernel/bpf/helpers.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
Quoted 11 lines
kernel/bpf/helpers.c +0 -0
index 56ce5008aedd..eb91cae0612a 100644
@@ -2270,7 +2270,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct 

bpf_dynptr_kern *ptr, u32 offset
      case 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 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 be

    err = 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?