2021-03-30 23:18:22

by Alexei Starovoitov

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2] bpf: check flags in 'bpf_ringbuf_discard()' and 'bpf_ringbuf_submit()'

On Tue, Mar 30, 2021 at 3:54 PM Pedro Tammela <[email protected]> wrote:
>
> BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
> {
> + if (unlikely(flags & ~(BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP)))
> + return -EINVAL;
> +
> bpf_ringbuf_commit(sample, flags, false /* discard */);
> +
> return 0;

I think ringbuf design was meant for bpf_ringbuf_submit to never fail.
If we do flag validation it probably should be done at the verifier time.


2021-03-31 07:05:21

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2] bpf: check flags in 'bpf_ringbuf_discard()' and 'bpf_ringbuf_submit()'

On Tue, Mar 30, 2021 at 4:16 PM Alexei Starovoitov
<[email protected]> wrote:
>
> On Tue, Mar 30, 2021 at 3:54 PM Pedro Tammela <[email protected]> wrote:
> >
> > BPF_CALL_2(bpf_ringbuf_submit, void *, sample, u64, flags)
> > {
> > + if (unlikely(flags & ~(BPF_RB_NO_WAKEUP | BPF_RB_FORCE_WAKEUP)))
> > + return -EINVAL;
> > +
> > bpf_ringbuf_commit(sample, flags, false /* discard */);
> > +
> > return 0;
>
> I think ringbuf design was meant for bpf_ringbuf_submit to never fail.
> If we do flag validation it probably should be done at the verifier time.

Oops, replied on another version already. But yes, BPF verifier relies
on it succeeding. I don't think we can do flags validation at BPF
verification time, though, because it is defined as non-const integer
and we do have valid cases where we dynamically determine whether to
FORCE_WAKEUP or NO_WAKEUP, based on application-driven criteria (e.g.,
amount of enqueued data).