diff mbox series

[bpf-next] libbpf: Remove redundant check in btf_ext__new()

Message ID 20220312171354.661448-1-ytcoode@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next] libbpf: Remove redundant check in btf_ext__new() | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: line length of 88 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next success VM_Test

Commit Message

Yuntao Wang March 12, 2022, 5:13 p.m. UTC
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.

Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
---
 tools/lib/bpf/btf.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

Comments

Andrii Nakryiko March 15, 2022, 6:36 p.m. UTC | #1
On Sat, Mar 12, 2022 at 9:14 AM Yuntao Wang <ytcoode@gmail.com> wrote:
>
> 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.
>
> Signed-off-by: Yuntao Wang <ytcoode@gmail.com>
> ---
>  tools/lib/bpf/btf.c | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 1383e26c5d1f..d55b44124c3e 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -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;
> -       }

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

> -
>         err = btf_ext_setup_core_relos(btf_ext);
>         if (err)
>                 goto done;
> --
> 2.35.1
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 1383e26c5d1f..d55b44124c3e 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -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;