Re: [PATCH bpf-next] bpf: Add document for 'dst_port' of 'struct bpf_sock'

From: Alexei Starovoitov
Date: Wed Jan 19 2022 - 23:18:02 EST


On Thu, Jan 20, 2022 at 11:02:27AM +0800, Menglong Dong wrote:
> Hello!
>
> On Thu, Jan 20, 2022 at 6:03 AM Alexei Starovoitov
> <alexei.starovoitov@xxxxxxxxx> wrote:
> >
> [...]
> >
> > Looks like
> > __sk_buff->remote_port
> > bpf_sock_ops->remote_port
> > sk_msg_md->remote_port
> > are doing the right thing,
> > but bpf_sock->dst_port is not correct?
> >
> > I think it's better to fix it,
> > but probably need to consolidate it with
> > convert_ctx_accesses() that deals with narrow access.
> > I suspect reading u8 from three flavors of 'remote_port'
> > won't be correct.
>
> What's the meaning of 'narrow access'? Do you mean to
> make 'remote_port' u16? Or 'remote_port' should be made
> accessible with u8? In fact, '*((u16 *)&skops->remote_port + 1)'
> won't work, as it only is accessible with u32.

u8 access to remote_port won't pass the verifier,
but u8 access to dst_port will.
Though it will return incorrect data.
See how convert_ctx_accesses() handles narrow loads.
I think we need to generalize it for different endian fields.