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: <968A8194-61C0-4F9A-ADB6-8A6BB57E2A57@toblux.com>
Date: Tue, 13 Aug 2024 22:51:44 +0200
From: Thorsten Blum <thorsten.blum@...lux.com>
To: Alexei Starovoitov <alexei.starovoitov@...il.com>
Cc: Martin KaFai Lau <martin.lau@...ux.dev>,
 Alexei Starovoitov <ast@...nel.org>,
 Daniel Borkmann <daniel@...earbox.net>,
 Andrii Nakryiko <andrii@...nel.org>,
 Eddy Z <eddyz87@...il.com>,
 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 13. Aug 2024, at 20:57, Alexei Starovoitov <alexei.starovoitov@...il.com> 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?

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@...lux.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ