On Fri, Apr 11, 2025 at 11:37 AM Martin KaFai Lau <martin.lau@xxxxxxxxx> wrote: > > On 4/11/25 10:08 AM, Kumar Kartikeya Dwivedi wrote: > > On Fri, 11 Apr 2025 at 18:59, Amery Hung <ameryhung@xxxxxxxxx> wrote: > >> > >> On Fri, Apr 11, 2025 at 6:32 AM Kumar Kartikeya Dwivedi > >> <memxor@xxxxxxxxx> wrote: > >>> > >>> On Wed, 9 Apr 2025 at 23:46, Amery Hung <ameryhung@xxxxxxxxx> wrote: > >>>> > >>>> From: Amery Hung <amery.hung@xxxxxxxxxxxxx> > >>>> > >>>> Add basic kfuncs for working on skb in qdisc. > >>>> > >>>> Both bpf_qdisc_skb_drop() and bpf_kfree_skb() can be used to release > >>>> a reference to an skb. However, bpf_qdisc_skb_drop() can only be called > >>>> in .enqueue where a to_free skb list is available from kernel to defer > >>>> the release. bpf_kfree_skb() should be used elsewhere. It is also used > >>>> in bpf_obj_free_fields() when cleaning up skb in maps and collections. > >>>> > >>>> bpf_skb_get_hash() returns the flow hash of an skb, which can be used > >>>> to build flow-based queueing algorithms. > >>>> > >>>> Finally, allow users to create read-only dynptr via bpf_dynptr_from_skb(). > >>>> > >>>> Signed-off-by: Amery Hung <amery.hung@xxxxxxxxxxxxx> > >>>> Acked-by: Toke Høiland-Jørgensen <toke@xxxxxxxxxx> > >>>> --- > >>> > >>> How do we prevent UAF when dynptr is accessed after bpf_kfree_skb? > >>> > >> > >> Good question... > >> > >> Maybe we can add a ref_obj_id field to bpf_reg_state->dynptr to track > >> the ref_obj_id of the object underlying a dynptr? > >> > >> Then, in release_reference(), in addition to finding ref_obj_id in > >> registers, verifier will also search stack slots and invalidate all > >> dynptrs with the ref_obj_id. > >> > >> Does this sound like a feasible solution? > > > > Yes, though I talked with Andrii and he has better ideas for doing > > this generically, but for now I think we can make this fix as a > > stopgap. > > In case the better fix will take longer, just want to mention that an option is > to remove the bpf_dynptr_from_skb() from bpf qdisc. I don't see an urgent need > for the bpf qdisc to be able to directly access the skb->data. btw, I don't > think bpf qdisc should write to the skb->data. > > The same goes for the bpf_kfree_skb(). I was thinking if it is useful at all > considering there is already a bpf_qdisc_skb_drop(). I kept it there because it > is a little more intuitive in case the .reset/.destroy wanted to do a "skb = > bpf_kptr_xchg(&skbn->skb, NULL);" and then explicitly free the > bpf_kfree_skb(skb). However, the bpf prog can also directly do the > bpf_obj_drop(skbn) and then bpf_kfree_skb() is not needed, right? > > My rationale for keeping two skb releasing kfuncs: bpf_kfree_skb() is the dtor and since dtor can only have one argument, so bpf_qdisc_skb_drop() can not replace it. Since bpf_kfree_skb() is here to stay, I allow users to call it directly for convenience. Only exposing bpf_qdisc_skb_drop() and calling kfree_skb() in bpf_qdisc_skb_drop() when to_free is NULL will also do. I don’t have a strong opinion. Yes, bpf_kfree_skb() will not be needed if doing bpf_obj_drop(skbn). bpf_obj_drop() internally will call the dtor of a kptr (i.e., in this case, bpf_kfree_skb()) in an allocated object.