mbox series

[v2,bpf,0/2] Fix BTF verification of enum members with a selftest

Message ID 1583825550-18606-1-git-send-email-komachi.yoshiki@gmail.com
Headers show
Series Fix BTF verification of enum members with a selftest | expand

Message

Yoshiki Komachi March 10, 2020, 7:32 a.m. UTC
btf_enum_check_member() checked if the size of "enum" as a struct
member exceeded struct_size or not. Then, the function compared it
with the size of "int". Although the size of "enum" is 4-byte by
default (i.e., equivalent to "int"), the packing feature enables
us to reduce it, as illustrated by the following example:

struct A {
        char m;
        enum { E0, E1 } __attribute__((packed)) n;
};

With such a setup above, the bpf loader gave an error attempting
to load it:

------------------------------------------------------------------
...

[3] ENUM (anon) size=1 vlen=2
        E0 val=0
        E1 val=1
[4] STRUCT A size=2 vlen=2
        m type_id=2 bits_offset=0
        n type_id=3 bits_offset=8

[4] STRUCT A size=2 vlen=2
        n type_id=3 bits_offset=8 Member exceeds struct_size

libbpf: Error loading .BTF into kernel: -22.

------------------------------------------------------------------

The related issue was previously fixed by the commit 9eea98497951 ("bpf:
fix BTF verification of enums"). On the other hand, this series fixes
this issue as well, and adds a selftest program for it.

Changes in v2:
- change an example in commit message based on Andrii's review
- add a selftest program for packed "enum" type members in struct/union

Yoshiki Komachi (2):
  bpf/btf: Fix BTF verification of enum members in struct/union
  selftests/bpf: Add test for the packed enum member in struct/union

 kernel/bpf/btf.c                       |  2 +-
 tools/testing/selftests/bpf/test_btf.c | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)

Comments

Andrii Nakryiko March 10, 2020, 3:39 p.m. UTC | #1
On Tue, Mar 10, 2020 at 12:32 AM Yoshiki Komachi
<komachi.yoshiki@gmail.com> wrote:
>
> btf_enum_check_member() checked if the size of "enum" as a struct
> member exceeded struct_size or not. Then, the function compared it
> with the size of "int". Although the size of "enum" is 4-byte by
> default (i.e., equivalent to "int"), the packing feature enables
> us to reduce it, as illustrated by the following example:
>
> struct A {
>         char m;
>         enum { E0, E1 } __attribute__((packed)) n;
> };
>
> With such a setup above, the bpf loader gave an error attempting
> to load it:
>
> ------------------------------------------------------------------
> ...
>
> [3] ENUM (anon) size=1 vlen=2
>         E0 val=0
>         E1 val=1
> [4] STRUCT A size=2 vlen=2
>         m type_id=2 bits_offset=0
>         n type_id=3 bits_offset=8
>
> [4] STRUCT A size=2 vlen=2
>         n type_id=3 bits_offset=8 Member exceeds struct_size
>
> libbpf: Error loading .BTF into kernel: -22.
>
> ------------------------------------------------------------------
>
> The related issue was previously fixed by the commit 9eea98497951 ("bpf:
> fix BTF verification of enums"). On the other hand, this series fixes
> this issue as well, and adds a selftest program for it.
>
> Changes in v2:
> - change an example in commit message based on Andrii's review
> - add a selftest program for packed "enum" type members in struct/union
>
> Yoshiki Komachi (2):
>   bpf/btf: Fix BTF verification of enum members in struct/union
>   selftests/bpf: Add test for the packed enum member in struct/union
>
>  kernel/bpf/btf.c                       |  2 +-
>  tools/testing/selftests/bpf/test_btf.c | 42 ++++++++++++++++++++++++++++++++++
>  2 files changed, 43 insertions(+), 1 deletion(-)
>
> --
> 1.8.3.1
>

You should have updated patch prefix for patch #1 and #2 to [PATCH v2
bpf] as well, please do it next time,

For the series:

Acked-by: Andrii Nakryiko <andriin@fb.com>
Martin KaFai Lau March 10, 2020, 4:46 p.m. UTC | #2
On Tue, Mar 10, 2020 at 04:32:28PM +0900, Yoshiki Komachi wrote:
> btf_enum_check_member() checked if the size of "enum" as a struct
> member exceeded struct_size or not. Then, the function compared it
> with the size of "int". Although the size of "enum" is 4-byte by
> default (i.e., equivalent to "int"), the packing feature enables
> us to reduce it, as illustrated by the following example:
> 
> struct A {
>         char m;
>         enum { E0, E1 } __attribute__((packed)) n;
> };
> 
> With such a setup above, the bpf loader gave an error attempting
> to load it:
> 
> ------------------------------------------------------------------
> ...
> 
> [3] ENUM (anon) size=1 vlen=2
>         E0 val=0
>         E1 val=1
> [4] STRUCT A size=2 vlen=2
>         m type_id=2 bits_offset=0
>         n type_id=3 bits_offset=8
> 
> [4] STRUCT A size=2 vlen=2
>         n type_id=3 bits_offset=8 Member exceeds struct_size
> 
> libbpf: Error loading .BTF into kernel: -22.
> 
> ------------------------------------------------------------------
> 
> The related issue was previously fixed by the commit 9eea98497951 ("bpf:
> fix BTF verification of enums"). On the other hand, this series fixes
> this issue as well, and adds a selftest program for it.
> 
> Changes in v2:
> - change an example in commit message based on Andrii's review
> - add a selftest program for packed "enum" type members in struct/union
Acked-by: Martin KaFai Lau <kafai@fb.com>
Alexei Starovoitov March 10, 2020, 5:26 p.m. UTC | #3
On Tue, Mar 10, 2020 at 9:46 AM Martin KaFai Lau <kafai@fb.com> wrote:
>
> On Tue, Mar 10, 2020 at 04:32:28PM +0900, Yoshiki Komachi wrote:
> > btf_enum_check_member() checked if the size of "enum" as a struct
> > member exceeded struct_size or not. Then, the function compared it
> > with the size of "int". Although the size of "enum" is 4-byte by
> > default (i.e., equivalent to "int"), the packing feature enables
> > us to reduce it, as illustrated by the following example:
> >
> > struct A {
> >         char m;
> >         enum { E0, E1 } __attribute__((packed)) n;
> > };
> >
> > With such a setup above, the bpf loader gave an error attempting
> > to load it:
> >
> > ------------------------------------------------------------------
> > ...
> >
> > [3] ENUM (anon) size=1 vlen=2
> >         E0 val=0
> >         E1 val=1
> > [4] STRUCT A size=2 vlen=2
> >         m type_id=2 bits_offset=0
> >         n type_id=3 bits_offset=8
> >
> > [4] STRUCT A size=2 vlen=2
> >         n type_id=3 bits_offset=8 Member exceeds struct_size
> >
> > libbpf: Error loading .BTF into kernel: -22.
> >
> > ------------------------------------------------------------------
> >
> > The related issue was previously fixed by the commit 9eea98497951 ("bpf:
> > fix BTF verification of enums"). On the other hand, this series fixes
> > this issue as well, and adds a selftest program for it.
> >
> > Changes in v2:
> > - change an example in commit message based on Andrii's review
> > - add a selftest program for packed "enum" type members in struct/union
> Acked-by: Martin KaFai Lau <kafai@fb.com>

Applied to bpf tree. Thanks