Re: [PATCH bpf-next v3 1/4] bpf/crib: Introduce task_file open-coded iterator kfuncs
From: Alexei Starovoitov
Date: Wed Nov 06 2024 - 17:14:10 EST
On Wed, Nov 6, 2024 at 2:10 PM Andrii Nakryiko
<andrii.nakryiko@xxxxxxxxx> wrote:
> > > +__bpf_kfunc int bpf_iter_task_file_new(struct bpf_iter_task_file *it,
> > > + struct task_struct *task)
> > > +{
> > > + struct bpf_iter_task_file_kern *kit = (void *)it;
> > > +
> > > + BUILD_BUG_ON(sizeof(struct bpf_iter_task_file_kern) > sizeof(struct bpf_iter_task_file));
> > > + BUILD_BUG_ON(__alignof__(struct bpf_iter_task_file_kern) !=
> > > + __alignof__(struct bpf_iter_task_file));
> > > +
> > > + kit->task = task;
> >
> > This is broken, since task refcnt can drop while iter is running.
>
> I noticed this as well, but I thought that given KF_TRUSTED_ARGS we
> should have a guarantee that the task survives the iteration? Am I
> mistaken?
KF_TRUSTED_ARGS will only guarantee that the task is valid when it's
passed into this kfunc. Right after the prog can call
bpf_task_release() to release the ref and kit->task will become
dangling.
If this object was RCU protected we could have marked this iter
as KF_RCU_PROTECTED, then the verifier would make sure that
RCU unlock doesn't happen between iter_new and iter_destroy.