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: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20ea4046cacfb774b0fe5e9dd3337999da2b63dc.camel@gmail.com>
Date: Tue, 13 Aug 2024 13:12:24 -0700
From: Eduard Zingerman <eddyz87@...il.com>
To: Alexei Starovoitov <alexei.starovoitov@...il.com>, Thorsten Blum
	 <thorsten.blum@...lux.com>
Cc: Martin KaFai Lau <martin.lau@...ux.dev>, Alexei Starovoitov
 <ast@...nel.org>,  Daniel Borkmann <daniel@...earbox.net>, Andrii Nakryiko
 <andrii@...nel.org>, Song Liu <song@...nel.org>,  Yonghong Song
 <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>, KP
 Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo
 <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>, Kees Cook
 <kees@...nel.org>,  "Gustavo A. R. Silva" <gustavoars@...nel.org>, bpf
 <bpf@...r.kernel.org>, LKML <linux-kernel@...r.kernel.org>,
 linux-hardening@...r.kernel.org
Subject: Re: [PATCH] bpf: Annotate struct bpf_cand_cache with __counted_by()

On Tue, 2024-08-13 at 11:57 -0700, Alexei Starovoitov wrote:
> On Tue, Aug 13, 2024 at 10:59 AM Thorsten Blum <thorsten.blum@...lux.com> wrote:
> >
> > On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@...il.com> wrote:
> > > On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@...lux.com> wrote:
> > > >
> > > > Add the __counted_by compiler attribute to the flexible array member
> > > > cands to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> > > > CONFIG_FORTIFY_SOURCE.
> > > >
> > > > Increment cnt before adding a new struct to the cands array.
> > >
> > > why? What happens otherwise?
> >
> > If you try to access cands->cands[cands->cnt] without incrementing
> > cands->cnt first, you're essentially accessing the array out of bounds
> > which will fail during runtime.
>
> What kind of error/warn do you see ?
> Is it runtime or compile time?
>
> Is this the only place?
> what about:
>         new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
>
> cnt field gets copied with other fields.
> Can compiler/runtime catch that?

I think that generated check is mechanical, sanitizer wraps access to
array with size check using the value of associated counter, e.g:

    12:52:20 tmp$ clang -fsanitize=undefined ./test.c
    12:52:53 tmp$ ./a.out
    test.c:11:3: runtime error: index 0 out of bounds for type 'int[]'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.c:11:3
    12:52:55 tmp$ cat test.c
    #include <alloca.h>

    struct arr {
      int cnt;
      int items[] __attribute__((__counted_by__(cnt)));
    };

    int main(int argc, char **argv) {
      struct arr *arr = alloca(sizeof(struct arr) + sizeof(int));
      arr->cnt = 0;
      arr->items[arr->cnt] = 42;
      arr->cnt++;
      asm volatile (""::"r"(arr));
      return 0;
    }
    12:53:07 tmp$ clang -fsanitize=undefined ./test.c
    12:53:10 tmp$ ./a.out
    test.c:11:3: runtime error: index 0 out of bounds for type 'int[]'
    SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.c:11:3
    12:53:13 tmp$ cat test.c
    #include <alloca.h>

    struct arr {
      int cnt;
      int items[] __attribute__((__counted_by__(cnt)));
    };

    int main(int argc, char **argv) {
      struct arr *arr = alloca(sizeof(struct arr) + sizeof(int));
      arr->cnt = 1;
      arr->items[arr->cnt - 1] = 42;
      asm volatile (""::"r"(arr));
      return 0;
    }
    12:53:34 tmp$ clang -fsanitize=undefined ./test.c
    12:53:36 tmp$ ./a.out
    12:53:38 tmp$ echo $?
    0

Or here is the IR generated for C program:

    struct arr {
      unsigned int cnt;
      int items[] __attribute__((__counted_by__(cnt)));
    };

    void push(int i, struct arr *arr) {
      arr->items[arr->cnt] = 42;
      arr->cnt++;
    }

Note the 'cnt' passed as a parameter to '@...bsan_handle_out_of_bounds':

    define dso_local void @push(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 !func_sanitize !3 {
      ...
      %11 = load i32, ptr %1, align 4
      %12 = zext i32 %11 to i64
      tail call void @__ubsan_handle_out_of_bounds(ptr nonnull @6, i64 %12) #2, !nosanitize !4

[...]


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ