diff mbox series

bpf: Annotate struct bpf_cand_cache with __counted_by()

Message ID 20240813151752.95161-2-thorsten.blum@toblux.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf: Annotate struct bpf_cand_cache with __counted_by() | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 29 this patch: 29
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 16 of 16 maintainers
netdev/build_clang success Errors and warnings before: 29 this patch: 29
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 31 this patch: 31
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 19 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Thorsten Blum Aug. 13, 2024, 3:17 p.m. UTC
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.

Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
---
 kernel/bpf/btf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Alexei Starovoitov Aug. 13, 2024, 4:28 p.m. UTC | #1
On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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?

>
> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> ---
>  kernel/bpf/btf.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 520f49f422fe..42bc70a56fcd 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -7240,7 +7240,7 @@ struct bpf_cand_cache {
>         struct {
>                 const struct btf *btf;
>                 u32 id;
> -       } cands[];
> +       } cands[] __counted_by(cnt);
>  };
>
>  static DEFINE_MUTEX(cand_cache_mutex);
> @@ -8784,9 +8784,9 @@ bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
>                 memcpy(new_cands, cands, sizeof_cands(cands->cnt));
>                 bpf_free_cands(cands);
>                 cands = new_cands;
> -               cands->cands[cands->cnt].btf = targ_btf;
> -               cands->cands[cands->cnt].id = i;
>                 cands->cnt++;
> +               cands->cands[cands->cnt - 1].btf = targ_btf;
> +               cands->cands[cands->cnt - 1].id = i;
>         }
>         return cands;
>  }
> --
> 2.46.0
>
Thorsten Blum Aug. 13, 2024, 5:59 p.m. UTC | #2
On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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.

You can read more about it at [1] and [2].

> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
>> ---
>> kernel/bpf/btf.c | 6 +++---
>> 1 file changed, 3 insertions(+), 3 deletions(-)
>> 
>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>> index 520f49f422fe..42bc70a56fcd 100644
>> --- a/kernel/bpf/btf.c
>> +++ b/kernel/bpf/btf.c
>> @@ -7240,7 +7240,7 @@ struct bpf_cand_cache {
>>        struct {
>>                const struct btf *btf;
>>                u32 id;
>> -       } cands[];
>> +       } cands[] __counted_by(cnt);
>> };
>> 
>> static DEFINE_MUTEX(cand_cache_mutex);
>> @@ -8784,9 +8784,9 @@ bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
>>                memcpy(new_cands, cands, sizeof_cands(cands->cnt));
>>                bpf_free_cands(cands);
>>                cands = new_cands;
>> -               cands->cands[cands->cnt].btf = targ_btf;
>> -               cands->cands[cands->cnt].id = i;
>>                cands->cnt++;
>> +               cands->cands[cands->cnt - 1].btf = targ_btf;
>> +               cands->cands[cands->cnt - 1].id = i;
>>        }
>>        return cands;
>> }
>> --
>> 2.46.0
>> 

[1] https://opensource.googleblog.com/2024/07/bounds-checking-flexible-array-members.html
[2] https://embeddedor.com/blog/2024/06/18/how-to-use-the-new-counted_by-attribute-in-c-and-linux/
Alexei Starovoitov Aug. 13, 2024, 6:57 p.m. UTC | #3
On Tue, Aug 13, 2024 at 10:59 AM Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> > On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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?

> You can read more about it at [1] and [2].
>
> > Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
> >> ---
> >> kernel/bpf/btf.c | 6 +++---
> >> 1 file changed, 3 insertions(+), 3 deletions(-)
> >>
> >> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> >> index 520f49f422fe..42bc70a56fcd 100644
> >> --- a/kernel/bpf/btf.c
> >> +++ b/kernel/bpf/btf.c
> >> @@ -7240,7 +7240,7 @@ struct bpf_cand_cache {
> >>        struct {
> >>                const struct btf *btf;
> >>                u32 id;
> >> -       } cands[];
> >> +       } cands[] __counted_by(cnt);
> >> };
> >>
> >> static DEFINE_MUTEX(cand_cache_mutex);
> >> @@ -8784,9 +8784,9 @@ bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
> >>                memcpy(new_cands, cands, sizeof_cands(cands->cnt));
> >>                bpf_free_cands(cands);
> >>                cands = new_cands;
> >> -               cands->cands[cands->cnt].btf = targ_btf;
> >> -               cands->cands[cands->cnt].id = i;
> >>                cands->cnt++;
> >> +               cands->cands[cands->cnt - 1].btf = targ_btf;
> >> +               cands->cands[cands->cnt - 1].id = i;
> >>        }
> >>        return cands;
> >> }
> >> --
> >> 2.46.0
> >>
>
> [1] https://opensource.googleblog.com/2024/07/bounds-checking-flexible-array-members.html
> [2] https://embeddedor.com/blog/2024/06/18/how-to-use-the-new-counted_by-attribute-in-c-and-linux/
Eduard Zingerman Aug. 13, 2024, 8:12 p.m. UTC | #4
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@toblux.com> wrote:
> >
> > On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> > > On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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 '@__ubsan_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

[...]
Thorsten Blum Aug. 13, 2024, 8:51 p.m. UTC | #5
On 13. Aug 2024, at 20:57, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> On Tue, Aug 13, 2024 at 10:59 AM Thorsten Blum <thorsten.blum@toblux.com> wrote:
>> On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
>>> On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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?

I get a runtime error with Clang 18 [3].

> Is this the only place?

I think so.

> 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 this is ok and there's nothing to catch.

> You can read more about it at [1] and [2].
>> 
>>> Signed-off-by: Thorsten Blum <thorsten.blum@toblux.com>
>>>> ---
>>>> kernel/bpf/btf.c | 6 +++---
>>>> 1 file changed, 3 insertions(+), 3 deletions(-)
>>>> 
>>>> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
>>>> index 520f49f422fe..42bc70a56fcd 100644
>>>> --- a/kernel/bpf/btf.c
>>>> +++ b/kernel/bpf/btf.c
>>>> @@ -7240,7 +7240,7 @@ struct bpf_cand_cache {
>>>>      struct {
>>>>              const struct btf *btf;
>>>>              u32 id;
>>>> -       } cands[];
>>>> +       } cands[] __counted_by(cnt);
>>>> };
>>>> 
>>>> static DEFINE_MUTEX(cand_cache_mutex);
>>>> @@ -8784,9 +8784,9 @@ bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
>>>>              memcpy(new_cands, cands, sizeof_cands(cands->cnt));
>>>>              bpf_free_cands(cands);
>>>>              cands = new_cands;
>>>> -               cands->cands[cands->cnt].btf = targ_btf;
>>>> -               cands->cands[cands->cnt].id = i;
>>>>              cands->cnt++;
>>>> +               cands->cands[cands->cnt - 1].btf = targ_btf;
>>>> +               cands->cands[cands->cnt - 1].id = i;
>>>>      }
>>>>      return cands;
>>>> }
>>>> --
>>>> 2.46.0
>>>> 
>> 
>> [1] https://opensource.googleblog.com/2024/07/bounds-checking-flexible-array-members.html
>> [2] https://embeddedor.com/blog/2024/06/18/how-to-use-the-new-counted_by-attribute-in-c-and-linux/

[3] https://godbolt.org/z/cKee95777
Alexei Starovoitov Aug. 14, 2024, 3:46 p.m. UTC | #6
On Tue, Aug 13, 2024 at 1:51 PM Thorsten Blum <thorsten.blum@toblux.com> wrote:
>
> On 13. Aug 2024, at 20:57, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> > On Tue, Aug 13, 2024 at 10:59 AM Thorsten Blum <thorsten.blum@toblux.com> wrote:
> >> On 13. Aug 2024, at 18:28, Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> >>> On Tue, Aug 13, 2024 at 8:19 AM Thorsten Blum <thorsten.blum@toblux.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?
>
> I get a runtime error with Clang 18 [3].

...

> [3] https://godbolt.org/z/cKee95777

This is user space.
I'm not asking about generic description of the counted_by feature.
I want to see the actual runtime report from the kernel.
Can it even compile the kernel with -fsanitize=undefined ?

pw-bot: cr
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 520f49f422fe..42bc70a56fcd 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -7240,7 +7240,7 @@  struct bpf_cand_cache {
 	struct {
 		const struct btf *btf;
 		u32 id;
-	} cands[];
+	} cands[] __counted_by(cnt);
 };
 
 static DEFINE_MUTEX(cand_cache_mutex);
@@ -8784,9 +8784,9 @@  bpf_core_add_cands(struct bpf_cand_cache *cands, const struct btf *targ_btf,
 		memcpy(new_cands, cands, sizeof_cands(cands->cnt));
 		bpf_free_cands(cands);
 		cands = new_cands;
-		cands->cands[cands->cnt].btf = targ_btf;
-		cands->cands[cands->cnt].id = i;
 		cands->cnt++;
+		cands->cands[cands->cnt - 1].btf = targ_btf;
+		cands->cands[cands->cnt - 1].id = i;
 	}
 	return cands;
 }