lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite for Android: free password hash cracker in your pocket
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAEf4BzbXNK0bb_7Wa6=y2BsQjmQ_ynYT1YNhO4eP0uT4nwJmpg@mail.gmail.com>
Date:   Fri, 16 Jul 2021 17:32:47 -0700
From:   Andrii Nakryiko <andrii.nakryiko@...il.com>
To:     Alan Maguire <alan.maguire@...cle.com>
Cc:     Alexei Starovoitov <ast@...nel.org>,
        Daniel Borkmann <daniel@...earbox.net>,
        Andrii Nakryiko <andrii@...nel.org>, Martin Lau <kafai@...com>,
        Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
        john fastabend <john.fastabend@...il.com>,
        KP Singh <kpsingh@...nel.org>,
        Bill Wendling <morbo@...gle.com>,
        Shuah Khan <shuah@...nel.org>, bpf <bpf@...r.kernel.org>,
        Networking <netdev@...r.kernel.org>,
        "open list:KERNEL SELFTEST FRAMEWORK" 
        <linux-kselftest@...r.kernel.org>,
        open list <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v2 bpf-next 1/3] libbpf: clarify/fix unaligned data issues
 for btf typed dump

On Fri, Jul 16, 2021 at 3:47 PM Alan Maguire <alan.maguire@...cle.com> wrote:
>
> If data is packed, data structures can store it outside of usual
> boundaries.  For example a 4-byte int can be stored on a unaligned
> boundary in a case like this:
>
> struct s {
>         char f1;
>         int f2;
> } __attribute((packed));
>
> ...the int is stored at an offset of one byte.  Some platforms have
> problems dereferencing data that is not aligned with its size, and
> code exists to handle most cases of this for BTF typed data display.
> However pointer display was missed, and a simple function to test if
> "ptr_is_aligned(data, data_sz)" would help clarify this code.
>
> Suggested-by: Andrii Nakryiko <andrii@...nel.org>
> Signed-off-by: Alan Maguire <alan.maguire@...cle.com>
> ---
>  tools/lib/bpf/btf_dump.c | 30 ++++++++++++++++++++++++++----
>  1 file changed, 26 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
> index 929cf93..814a538 100644
> --- a/tools/lib/bpf/btf_dump.c
> +++ b/tools/lib/bpf/btf_dump.c
> @@ -1654,6 +1654,11 @@ static int btf_dump_base_type_check_zero(struct btf_dump *d,
>         return 0;
>  }
>
> +static bool ptr_is_aligned(const void *data, int data_sz)
> +{
> +       return ((uintptr_t)data) % data_sz == 0;
> +}
> +
>  static int btf_dump_int_data(struct btf_dump *d,
>                              const struct btf_type *t,
>                              __u32 type_id,
> @@ -1672,7 +1677,7 @@ static int btf_dump_int_data(struct btf_dump *d,
>         /* handle packed int data - accesses of integers not aligned on
>          * int boundaries can cause problems on some platforms.
>          */
> -       if (((uintptr_t)data) % sz)
> +       if (!ptr_is_aligned(data, sz))
>                 return btf_dump_bitfield_data(d, t, data, 0, 0);
>
>         switch (sz) {
> @@ -1739,7 +1744,7 @@ static int btf_dump_float_data(struct btf_dump *d,
>         int sz = t->size;
>
>         /* handle unaligned data; copy to local union */
> -       if (((uintptr_t)data) % sz) {
> +       if (!ptr_is_aligned(data, sz)) {
>                 memcpy(&fl, data, sz);
>                 flp = &fl;
>         }
> @@ -1892,12 +1897,29 @@ static int btf_dump_struct_data(struct btf_dump *d,
>         return err;
>  }
>
> +union ptr_data {
> +       unsigned int p;
> +       unsigned long lp;

long can be 32-bit on 4-byte architectures, plus %llx implies long
long (or we'll get another annoying warning from the compiler)

> +};
> +
>  static int btf_dump_ptr_data(struct btf_dump *d,
>                               const struct btf_type *t,
>                               __u32 id,
>                               const void *data)
>  {
> -       btf_dump_type_values(d, "%p", *(void **)data);
> +       bool ptr_sz_matches = d->ptr_sz == sizeof(void *);

used just once and clear what it does, I inlined this, no point in
separate variable


> +
> +       if (ptr_sz_matches && ptr_is_aligned(data, d->ptr_sz)) {
> +               btf_dump_type_values(d, "%p", *(void **)data);
> +       } else {
> +               union ptr_data pt;
> +
> +               memcpy(&pt, data, d->ptr_sz);
> +               if (d->ptr_sz == 4)
> +                       btf_dump_type_values(d, "0x%x", pt.p);
> +               else
> +                       btf_dump_type_values(d, "0x%llx", pt.lp);
> +       }
>         return 0;
>  }
>
> @@ -1910,7 +1932,7 @@ static int btf_dump_get_enum_value(struct btf_dump *d,
>         int sz = t->size;
>
>         /* handle unaligned enum value */
> -       if (((uintptr_t)data) % sz) {
> +       if (!ptr_is_aligned(data, sz)) {
>                 *value = (__s64)btf_dump_bitfield_get_data(d, t, data, 0, 0);
>                 return 0;
>         }
> --
> 1.8.3.1
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ