[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CADxym3b-Q6LyjKqTFcrssK9dVJ8hL6QkMb0MzLyn64r4LS=xtw@mail.gmail.com>
Date: Thu, 20 Jan 2022 22:14:14 +0800
From: Menglong Dong <menglong8.dong@...il.com>
To: Alexei Starovoitov <alexei.starovoitov@...il.com>
Cc: Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <kafai@...com>,
Song Liu <songliubraving@...com>, Yonghong Song <yhs@...com>,
John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>,
Network Development <netdev@...r.kernel.org>,
bpf <bpf@...r.kernel.org>, LKML <linux-kernel@...r.kernel.org>,
Mengen Sun <mengensun@...cent.com>, flyingpeng@...cent.com,
mungerjiang@...cent.com, Menglong Dong <imagedong@...cent.com>
Subject: Re: [PATCH bpf-next] bpf: Add document for 'dst_port' of 'struct bpf_sock'
On Thu, Jan 20, 2022 at 12:17 PM Alexei Starovoitov
<alexei.starovoitov@...il.com> wrote:
>
> 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@...il.com> 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.
Yeah, I understand narrower load in convert_ctx_accesses()
now. Seems u8 access to dst_port can't pass the verifier too,
which can be seen form bpf_sock_is_valid_access():
$ switch (off) {
$ case offsetof(struct bpf_sock, state):
$ case offsetof(struct bpf_sock, family):
$ case offsetof(struct bpf_sock, type):
$ case offsetof(struct bpf_sock, protocol):
$ case offsetof(struct bpf_sock, dst_port): // u8 access is not allowed
$ case offsetof(struct bpf_sock, src_port):
$ case offsetof(struct bpf_sock, rx_queue_mapping):
$ case bpf_ctx_range(struct bpf_sock, src_ip4):
$ case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
$ case bpf_ctx_range(struct bpf_sock, dst_ip4):
$ case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
$ bpf_ctx_record_field_size(info, size_default);
$ return bpf_ctx_narrow_access_ok(off, size, size_default);
$ }
I'm still not sure what should we do now. Should we make all
remote_port and dst_port narrower accessable and endianness
right? For example the remote_port in struct bpf_sock_ops:
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -8414,6 +8414,7 @@ static bool sock_ops_is_valid_access(int off, int size,
return false;
info->reg_type = PTR_TO_PACKET_END;
break;
+ case bpf_ctx_range(struct bpf_sock_ops, remote_port):
case offsetof(struct bpf_sock_ops, skb_tcp_flags):
bpf_ctx_record_field_size(info, size_default);
return bpf_ctx_narrow_access_ok(off, size,
If remote_port/dst_port are made narrower accessable, the
result will be right. Therefore, *((u16*)&sk->remote_port) will
be the port with network byte order. And the port in host byte
order can be get with:
bpf_ntohs(*((u16*)&sk->remote_port))
or
bpf_htonl(sk->remote_port)
Thanks!
Menglong Dong
Powered by blists - more mailing lists