From: Eduard Zingerman <eddyz87@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
Thorsten Blum <thorsten.blum@toblux.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>, Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>,
bpf <bpf@vger.kernel.org>, LKML <linux-kernel@vger.kernel.org>,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH] bpf: Annotate struct bpf_cand_cache with __counted_by()
Date: Tue, 13 Aug 2024 13:12:24 -0700 [thread overview]
Message-ID: <20ea4046cacfb774b0fe5e9dd3337999da2b63dc.camel@gmail.com> (raw)
In-Reply-To: <CAADnVQKw5x6sTwj62p4vxSqtjdisHEKhtKdPp_zK4t7rtDuWhQ@mail.gmail.com>
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
[...]
next prev parent reply other threads:[~2024-08-13 20:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-13 15:17 Thorsten Blum
2024-08-13 16:28 ` Alexei Starovoitov
2024-08-13 17:59 ` Thorsten Blum
2024-08-13 18:57 ` Alexei Starovoitov
2024-08-13 20:12 ` Eduard Zingerman [this message]
2024-08-13 20:51 ` Thorsten Blum
2024-08-14 15:46 ` Alexei Starovoitov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20ea4046cacfb774b0fe5e9dd3337999da2b63dc.camel@gmail.com \
--to=eddyz87@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=gustavoars@kernel.org \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kees@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=thorsten.blum@toblux.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®