Skip to contentSkip to conversation

bpf: fix bpf_dynptr_slice() returning ERR_PTR() on erro

PATCH In review Andrii Nakryiko 4 participants 5 messages Nov 2, 2023 BPF

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 bpf: fix bpf_dynptr_slice() returning ERR_PTR() on erro

Let's fix it for real this time. It shouldn't just detect ERR_PTR() return from bpf_xdp_pointer(), but also turn that into NULL to follow bpf_dynptr_slice() contract.

Fixes 5426700e6841 ("bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.")
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 56b0c1f678ee..04049097176c 100644
@@ -2309,7 +2309,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 	{
 		void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
 		if (!IS_ERR_OR_NULL(xdp_ptr))
-			return xdp_ptr;
+			return NULL;
 
 		if (!buffer__opt)
 			return NULL;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 16 lines · Andrii Nakryiko

Let's fix it for real this time. It shouldn't just detect ERR_PTR() return from bpf_xdp_pointer(), but also turn that into NULL to follow bpf_dynptr_slice() contract.

Fixes 5426700e6841 ("bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.")
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 56b0c1f678ee..04049097176c 100644

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2312

Quoted 5 lines
kernel/bpf/helpers.c +1 -1
@@ -2309,7 +2309,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 	{
 		void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
 		if (!IS_ERR_OR_NULL(xdp_ptr))
-			return xdp_ptr;
+			return NULL;

Erm, the check in the if is inverted - so isn't this 'return xdp_ptr' covering the case where bpf_xdp_pointer() *does* in fact return a valid pointer?

-Toke

#2 lore raw
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Andrii Nakryiko

Let's fix it for real this time. It shouldn't just detect ERR_PTR() return from bpf_xdp_pointer(), but also turn that into NULL to follow bpf_dynptr_slice() contract.

Fixes 5426700e6841 ("bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.")
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 15 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2312

Quoted 10 lines
kernel/bpf/helpers.c +1 -1
index 56b0c1f678ee..04049097176c 100644
@@ -2309,7 +2309,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
      {
              void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
              if (!IS_ERR_OR_NULL(xdp_ptr))
-                     return xdp_ptr;
+                     return NULL;

Erm, the check in the if is inverted - so isn't this 'return xdp_ptr' covering the case where bpf_xdp_pointer() *does* in fact return a valid pointer?

Ah, you are right, I missed the ! part... Ok, then I don't think we have an issue, great. Thanks for double checking! Perhaps we should add a simple comment "/* we got a valid direct pointer, return it */", as this looks like an error-handling case.

Quoted 1 line

-Toke

#3 lore raw
Quoted 31 lines · Andrii Nakryiko
Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 11 lines · Andrii Nakryiko

Let's fix it for real this time. It shouldn't just detect ERR_PTR() return from bpf_xdp_pointer(), but also turn that into NULL to follow bpf_dynptr_slice() contract.

Fixes 5426700e6841 ("bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.")
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 15 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2312

Quoted 10 lines
kernel/bpf/helpers.c +1 -1
index 56b0c1f678ee..04049097176c 100644
@@ -2309,7 +2309,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
      {
              void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
              if (!IS_ERR_OR_NULL(xdp_ptr))
-                     return xdp_ptr;
+                     return NULL;

Erm, the check in the if is inverted - so isn't this 'return xdp_ptr' covering the case where bpf_xdp_pointer() *does* in fact return a valid pointer?

Quoted 4 lines

Ah, you are right, I missed the ! part... Ok, then I don't think we have an issue, great. Thanks for double checking! Perhaps we should add a simple comment "/* we got a valid direct pointer, return it */", as this looks like an error-handling case.

Yup, totally agree it's confusing, I had to look at the code three or four times as well just now, to be sure that it wasn't buggy. Adding a comment would certainly be useful! :)

-Toke

#4 lore raw
Quoted 37 lines · Toke Høiland-Jørgensen
Quoted 31 lines · Andrii Nakryiko
Quoted 29 lines · Toke Høiland-Jørgensen
Quoted 16 lines · Andrii Nakryiko

Let's fix it for real this time. It shouldn't just detect ERR_PTR() return from bpf_xdp_pointer(), but also turn that into NULL to follow bpf_dynptr_slice() contract.

Fixes 5426700e6841 ("bpf: fix bpf_dynptr_slice() to stop return an ERR_PTR.")
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 56b0c1f678ee..04049097176c 100644

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset · line 2312

Quoted 5 lines
kernel/bpf/helpers.c +1 -1
@@ -2309,7 +2309,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
     {
             void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);
             if (!IS_ERR_OR_NULL(xdp_ptr))
-                     return xdp_ptr;
+                     return NULL;

Erm, the check in the if is inverted - so isn't this 'return xdp_ptr' covering the case where bpf_xdp_pointer() *does* in fact return a valid pointer?

Quoted 8 lines
Quoted 4 lines

Ah, you are right, I missed the ! part... Ok, then I don't think we have an issue, great. Thanks for double checking! Perhaps we should add a simple comment "/* we got a valid direct pointer, return it */", as this looks like an error-handling case.

Yup, totally agree it's confusing, I had to look at the code three or four times as well just now, to be sure that it wasn't buggy. Adding a comment would certainly be useful! :)

Aha, I was confused by this for more than a month. I am glad this is not an issue.

Thanks,
Song