Skip to contentSkip to conversation

libbpf: Remove redundant check in btf_ext__new()

PATCH In review Yuntao Wang 3 participants 6 messages Mar 12, 2022 – Apr 4, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Remove redundant check in btf_ext__new()

Since 'core_relo_len' is the last field of 'struct btf_ext_header', if 'xxx->hdr_len' is not less than 'offsetofend(xxx, core_relo_len)', then 'xxx->hdr_len' must also be not less than 'offsetofend(xxx, line_info_len)'.

We can check 'xxx->hdr_len < offsetofend(xxx, core_relo_len)' first, if it passes, the 'xxx->hdr_len < offsetofend(xxx, line_info_len)' check will be redundant, it can be removed.

---
 tools/lib/bpf/btf.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
tools/lib/bpf/btf.c +1 -6
index 1383e26c5d1f..d55b44124c3e 100644
@@ -2813,7 +2813,7 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 	if (err)
 		goto done;
 
-	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) {
+	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
 		err = -EINVAL;
 		goto done;
 	}
@@ -2826,11 +2826,6 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 	if (err)
 		goto done;
 
-	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
-		err = -EINVAL;
-		goto done;
-	}
-
 	err = btf_ext_setup_core_relos(btf_ext);
 	if (err)
 		goto done;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 28 lines · Yuntao Wang

Since 'core_relo_len' is the last field of 'struct btf_ext_header', if 'xxx->hdr_len' is not less than 'offsetofend(xxx, core_relo_len)', then 'xxx->hdr_len' must also be not less than 'offsetofend(xxx, line_info_len)'.

We can check 'xxx->hdr_len < offsetofend(xxx, core_relo_len)' first, if it passes, the 'xxx->hdr_len < offsetofend(xxx, line_info_len)' check will be redundant, it can be removed.

---
 tools/lib/bpf/btf.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
tools/lib/bpf/btf.c +1 -1
index 1383e26c5d1f..d55b44124c3e 100644
@@ -2813,7 +2813,7 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
        if (err)
                goto done;
 
-       if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) {
+       if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
                err = -EINVAL;
                goto done;
        }

tools/lib/bpf/btf.c struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) · line 2832

Quoted 7 lines
tools/lib/bpf/btf.c +0 -4
@@ -2826,11 +2826,6 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
        if (err)
                goto done;
 
-       if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
-               err = -EINVAL;
-               goto done;
-       }

it seems like it's actually a bug. If header is smaller then core relos parsing should be skipped, I think. Maybe let's fix that instead?

basically the logic should be:

1. if size of header is exactly == offsetof(core_relo_off) then skip core relos
2. otherwise check that it has enough size to cover core_relo_off and core_relo_len, and error out if not
3. otherwise proceed to parsing core relos

Quoted 7 lines
tools/lib/bpf/btf.c +0 -2
-
        err = btf_ext_setup_core_relos(btf_ext);
        if (err)
                goto done;
--

2.35.1

#2 lore raw

PATCH v2 1/1 libbpf: Don't return -EINVAL if hdr_len < offsetofend(core_relo_len)

Since core relos is an optional part of the .BTF.ext ELF section, we should skip parsing it instead of returning -EINVAL if header size is less than offsetofend(struct btf_ext_header, core_relo_len).

Fixes e9fc3ce99b34 ("libbpf: Streamline error reporting for high-level APIs")

---
v1 -> v2: skip core relos if hdr_len < offsetofend(core_relo_len)

 tools/lib/bpf/btf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +2 -4
index 1383e26c5d1f..9b7196b21498 100644
@@ -2826,10 +2826,8 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 	if (err)
 		goto done;
 
-	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
-		err = -EINVAL;
-		goto done;
-	}
+	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
+		goto done; // skip core relos parsing
 
 	err = btf_ext_setup_core_relos(btf_ext);
 	if (err)
Signature · 1 line
2.35.1
#3 lore raw

Re: [PATCH bpf-next v2] libbpf: Don't return -EINVAL if hdr_len < offsetofend(core_relo_len)

Quoted 10 lines · Yuntao Wang

Since core relos is an optional part of the .BTF.ext ELF section, we should skip parsing it instead of returning -EINVAL if header size is less than offsetofend(struct btf_ext_header, core_relo_len).

Fixes e9fc3ce99b34 ("libbpf: Streamline error reporting for high-level APIs")

---
v1 -> v2: skip core relos if hdr_len < offsetofend(core_relo_len)

Seems like this never made it to Patchworks. Can you please trim down the CC list and resubmit?

Quoted 8 lines
 tools/lib/bpf/btf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +0 -0
index 1383e26c5d1f..9b7196b21498 100644

tools/lib/bpf/btf.c struct btf_ext *btf_ext__new(const __u8 *data, __u32 size) · line 2830

Quoted 9 lines
tools/lib/bpf/btf.c +2 -4
@@ -2826,10 +2826,8 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
        if (err)
                goto done;
 
-       if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
-               err = -EINVAL;
-               goto done;
-       }
+       if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
+               goto done; // skip core relos parsing

don't use C++-style comments, only /* */

Quoted 6 lines
tools/lib/bpf/btf.c +0 -1
        err = btf_ext_setup_core_relos(btf_ext);
        if (err)
--

2.35.1

#4 lore raw

PATCH v3 1/1 libbpf: Don't return -EINVAL if hdr_len < offsetofend(core_relo_len)

Since core relos is an optional part of the .BTF.ext ELF section, we should skip parsing it instead of returning -EINVAL if header size is less than offsetofend(struct btf_ext_header, core_relo_len).

---
v1 -> v2: skip core relos if hdr_len < offsetofend(core_relo_len) v2 -> v3: fix comment style

 tools/lib/bpf/btf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/bpf/btf.c +2 -4
index 1383e26c5d1f..d124e9e533f0 100644
@@ -2826,10 +2826,8 @@ struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 	if (err)
 		goto done;
 
-	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len)) {
-		err = -EINVAL;
-		goto done;
-	}
+	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
+		goto done; /* skip core relos parsing */
 
 	err = btf_ext_setup_core_relos(btf_ext);
 	if (err)
Signature · 1 line
2.35.1
#5 lore raw

Re: [PATCH bpf-next v3] libbpf: Don't return -EINVAL if hdr_len < offsetofend(core_relo_len)

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 10 lines · On Mon, 4 Apr 2022 08:53:20 +0800 you wrote:

Since core relos is an optional part of the .BTF.ext ELF section, we should skip parsing it instead of returning -EINVAL if header size is less than offsetofend(struct btf_ext_header, core_relo_len).

---
v1 -> v2: skip core relos if hdr_len < offsetofend(core_relo_len) v2 -> v3: fix comment style

[...]

Here is the summary with links:
- [bpf-next,v3] libbpf: Don't return -EINVAL if hdr_len < offsetofend(core_relo_len)

    https://git.kernel.org/bpf/bpf-next/c/a6a86da847eb

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html