All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
@ 2022-03-15 12:39 Liu Jian
  2022-03-15 19:58 ` Martin KaFai Lau
  0 siblings, 1 reply; 7+ messages in thread
From: Liu Jian @ 2022-03-15 12:39 UTC (permalink / raw)
  To: ast, daniel, andrii, kafai, songliubraving, yhs, john.fastabend,
	kpsingh, davem, kuba, sdf, netdev, bpf
  Cc: liujian56

The data length of skb frags + frag_list may be greater than 0xffff,
so here use skb->len to check the validity of the parameters.

And modify bpf_flow_dissector_load_bytes and bpf_skb_load_bytes_relative
in the same way.

Fixes: 05c74e5e53f6 ("bpf: add bpf_skb_load_bytes helper")
Fixes: 4e1ec56cdc59 ("bpf: add skb_load_bytes_relative helper")
Fixes: 089b19a9204f ("flow_dissector: switch kernel context to struct bpf_flow_dissector")
Signed-off-by: Liu Jian <liujian56@huawei.com>
---
 net/core/filter.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index 9eb785842258..61c353caf141 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -1722,7 +1722,7 @@ BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
 {
 	void *ptr;
 
-	if (unlikely(offset > 0xffff))
+	if (unlikely(offset >= skb->len))
 		goto err_clear;
 
 	ptr = skb_header_pointer(skb, offset, len, to);
@@ -1753,10 +1753,10 @@ BPF_CALL_4(bpf_flow_dissector_load_bytes,
 {
 	void *ptr;
 
-	if (unlikely(offset > 0xffff))
+	if (unlikely(!ctx->skb))
 		goto err_clear;
 
-	if (unlikely(!ctx->skb))
+	if (unlikely(offset >= ctx->skb->len))
 		goto err_clear;
 
 	ptr = skb_header_pointer(ctx->skb, offset, len, to);
@@ -1787,7 +1787,7 @@ BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
 	u8 *end = skb_tail_pointer(skb);
 	u8 *start, *ptr;
 
-	if (unlikely(offset > 0xffff))
+	if (unlikely(offset >= skb->len))
 		goto err_clear;
 
 	switch (start_header) {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-15 12:39 [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes Liu Jian
@ 2022-03-15 19:58 ` Martin KaFai Lau
  2022-03-16  1:09   ` liujian (CE)
  0 siblings, 1 reply; 7+ messages in thread
From: Martin KaFai Lau @ 2022-03-15 19:58 UTC (permalink / raw)
  To: Liu Jian
  Cc: ast, daniel, andrii, songliubraving, yhs, john.fastabend,
	kpsingh, davem, kuba, sdf, netdev, bpf

On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> The data length of skb frags + frag_list may be greater than 0xffff,
> so here use skb->len to check the validity of the parameters.
What is the use case that needs to look beyond 0xffff ?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-15 19:58 ` Martin KaFai Lau
@ 2022-03-16  1:09   ` liujian (CE)
  2022-03-16  4:00     ` John Fastabend
  0 siblings, 1 reply; 7+ messages in thread
From: liujian (CE) @ 2022-03-16  1:09 UTC (permalink / raw)
  To: Martin KaFai Lau
  Cc: ast, daniel, andrii, songliubraving, yhs, john.fastabend,
	kpsingh, davem, kuba, sdf, netdev, bpf



> -----Original Message-----
> From: Martin KaFai Lau [mailto:kafai@fb.com]
> Sent: Wednesday, March 16, 2022 3:58 AM
> To: liujian (CE) <liujian56@huawei.com>
> Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> Subject: Re: [PATCH bpf-next] net: Use skb->len to check the validity of the
> parameters in bpf_skb_load_bytes
> 
> On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> > The data length of skb frags + frag_list may be greater than 0xffff,
> > so here use skb->len to check the validity of the parameters.
> What is the use case that needs to look beyond 0xffff ?
I use sockmap with strparser, the stm->strp.offset (the begin of one application layer protocol message) maybe beyond 0xffff, but i need load the message head to do something.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-16  1:09   ` liujian (CE)
@ 2022-03-16  4:00     ` John Fastabend
  2022-03-16 13:08       ` liujian (CE)
  0 siblings, 1 reply; 7+ messages in thread
From: John Fastabend @ 2022-03-16  4:00 UTC (permalink / raw)
  To: liujian (CE), Martin KaFai Lau
  Cc: ast, daniel, andrii, songliubraving, yhs, john.fastabend,
	kpsingh, davem, kuba, sdf, netdev, bpf

liujian (CE) wrote:
> 
> 
> > -----Original Message-----
> > From: Martin KaFai Lau [mailto:kafai@fb.com]
> > Sent: Wednesday, March 16, 2022 3:58 AM
> > To: liujian (CE) <liujian56@huawei.com>
> > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > Subject: Re: [PATCH bpf-next] net: Use skb->len to check the validity of the
> > parameters in bpf_skb_load_bytes
> > 
> > On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> > > The data length of skb frags + frag_list may be greater than 0xffff,
> > > so here use skb->len to check the validity of the parameters.
> > What is the use case that needs to look beyond 0xffff ?

> I use sockmap with strparser, the stm->strp.offset (the begin of one
> application layer protocol message) maybe beyond 0xffff, but i need
> load the message head to do something.

This would explain skb_load_bytes but not the other two right? Also
if we are doing this why not just remove those two checks in
flow_dissector_load() I think skb_header_pointer() does duplicate
checks. Please check.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-16  4:00     ` John Fastabend
@ 2022-03-16 13:08       ` liujian (CE)
  2022-03-16 15:09         ` Stanislav Fomichev
  0 siblings, 1 reply; 7+ messages in thread
From: liujian (CE) @ 2022-03-16 13:08 UTC (permalink / raw)
  To: John Fastabend, Martin KaFai Lau
  Cc: ast, daniel, andrii, songliubraving, yhs, kpsingh, davem, kuba,
	sdf, netdev, bpf



> -----Original Message-----
> From: John Fastabend [mailto:john.fastabend@gmail.com]
> Sent: Wednesday, March 16, 2022 12:00 PM
> To: liujian (CE) <liujian56@huawei.com>; Martin KaFai Lau <kafai@fb.com>
> Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> Subject: RE: [PATCH bpf-next] net: Use skb->len to check the validity of the
> parameters in bpf_skb_load_bytes
> 
> liujian (CE) wrote:
> >
> >
> > > -----Original Message-----
> > > From: Martin KaFai Lau [mailto:kafai@fb.com]
> > > Sent: Wednesday, March 16, 2022 3:58 AM
> > > To: liujian (CE) <liujian56@huawei.com>
> > > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > > Subject: Re: [PATCH bpf-next] net: Use skb->len to check the
> > > validity of the parameters in bpf_skb_load_bytes
> > >
> > > On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> > > > The data length of skb frags + frag_list may be greater than
> > > > 0xffff, so here use skb->len to check the validity of the parameters.
> > > What is the use case that needs to look beyond 0xffff ?
> 
> > I use sockmap with strparser, the stm->strp.offset (the begin of one
> > application layer protocol message) maybe beyond 0xffff, but i need
> > load the message head to do something.
> 
> This would explain skb_load_bytes but not the other two right? Also if we
Yes, I just see that these two functions have the same judgment.
> are doing this why not just remove those two checks in
> flow_dissector_load() I think skb_header_pointer() does duplicate checks.
> Please check.
Yes, skb_header_pointer() have checked as below, and I will send v2 to remove 0xffff check.
----skb_header_pointer
-------- __skb_header_pointer
------------skb_copy_bits
---------------- if (offset > (int)skb->len - len)
--------------------goto fault;

Thank you~

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-16 13:08       ` liujian (CE)
@ 2022-03-16 15:09         ` Stanislav Fomichev
  2022-03-17 14:08           ` liujian (CE)
  0 siblings, 1 reply; 7+ messages in thread
From: Stanislav Fomichev @ 2022-03-16 15:09 UTC (permalink / raw)
  To: liujian (CE)
  Cc: John Fastabend, Martin KaFai Lau, ast, daniel, andrii,
	songliubraving, yhs, kpsingh, davem, kuba, netdev, bpf

On Wed, Mar 16, 2022 at 6:08 AM liujian (CE) <liujian56@huawei.com> wrote:
>
>
>
> > -----Original Message-----
> > From: John Fastabend [mailto:john.fastabend@gmail.com]
> > Sent: Wednesday, March 16, 2022 12:00 PM
> > To: liujian (CE) <liujian56@huawei.com>; Martin KaFai Lau <kafai@fb.com>
> > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > Subject: RE: [PATCH bpf-next] net: Use skb->len to check the validity of the
> > parameters in bpf_skb_load_bytes
> >
> > liujian (CE) wrote:
> > >
> > >
> > > > -----Original Message-----
> > > > From: Martin KaFai Lau [mailto:kafai@fb.com]
> > > > Sent: Wednesday, March 16, 2022 3:58 AM
> > > > To: liujian (CE) <liujian56@huawei.com>
> > > > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > > > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > > > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > > > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > > > Subject: Re: [PATCH bpf-next] net: Use skb->len to check the
> > > > validity of the parameters in bpf_skb_load_bytes
> > > >
> > > > On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> > > > > The data length of skb frags + frag_list may be greater than
> > > > > 0xffff, so here use skb->len to check the validity of the parameters.
> > > > What is the use case that needs to look beyond 0xffff ?
> >
> > > I use sockmap with strparser, the stm->strp.offset (the begin of one
> > > application layer protocol message) maybe beyond 0xffff, but i need
> > > load the message head to do something.
> >
> > This would explain skb_load_bytes but not the other two right? Also if we
> Yes, I just see that these two functions have the same judgment.
> > are doing this why not just remove those two checks in
> > flow_dissector_load() I think skb_header_pointer() does duplicate checks.
> > Please check.
> Yes, skb_header_pointer() have checked as below, and I will send v2 to remove 0xffff check.
> ----skb_header_pointer
> -------- __skb_header_pointer
> ------------skb_copy_bits
> ---------------- if (offset > (int)skb->len - len)
> --------------------goto fault;
>
> Thank you~

Do we need to have at least "offset <= 0x7fffffff" check? IOW, do we
need to enforce the unsignedness of the offset? Or does
skb_header_pointer et all properly work with the negative offsets?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes
  2022-03-16 15:09         ` Stanislav Fomichev
@ 2022-03-17 14:08           ` liujian (CE)
  0 siblings, 0 replies; 7+ messages in thread
From: liujian (CE) @ 2022-03-17 14:08 UTC (permalink / raw)
  To: Stanislav Fomichev
  Cc: John Fastabend, Martin KaFai Lau, ast, daniel, andrii,
	songliubraving, yhs, kpsingh, davem, kuba, netdev, bpf



> -----Original Message-----
> From: Stanislav Fomichev [mailto:sdf@google.com]
> Sent: Wednesday, March 16, 2022 11:09 PM
> To: liujian (CE) <liujian56@huawei.com>
> Cc: John Fastabend <john.fastabend@gmail.com>; Martin KaFai Lau
> <kafai@fb.com>; ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> songliubraving@fb.com; yhs@fb.com; kpsingh@kernel.org;
> davem@davemloft.net; kuba@kernel.org; netdev@vger.kernel.org;
> bpf@vger.kernel.org
> Subject: Re: [PATCH bpf-next] net: Use skb->len to check the validity of the
> parameters in bpf_skb_load_bytes
> 
> On Wed, Mar 16, 2022 at 6:08 AM liujian (CE) <liujian56@huawei.com> wrote:
> >
> >
> >
> > > -----Original Message-----
> > > From: John Fastabend [mailto:john.fastabend@gmail.com]
> > > Sent: Wednesday, March 16, 2022 12:00 PM
> > > To: liujian (CE) <liujian56@huawei.com>; Martin KaFai Lau
> > > <kafai@fb.com>
> > > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > > Subject: RE: [PATCH bpf-next] net: Use skb->len to check the
> > > validity of the parameters in bpf_skb_load_bytes
> > >
> > > liujian (CE) wrote:
> > > >
> > > >
> > > > > -----Original Message-----
> > > > > From: Martin KaFai Lau [mailto:kafai@fb.com]
> > > > > Sent: Wednesday, March 16, 2022 3:58 AM
> > > > > To: liujian (CE) <liujian56@huawei.com>
> > > > > Cc: ast@kernel.org; daniel@iogearbox.net; andrii@kernel.org;
> > > > > songliubraving@fb.com; yhs@fb.com; john.fastabend@gmail.com;
> > > > > kpsingh@kernel.org; davem@davemloft.net; kuba@kernel.org;
> > > > > sdf@google.com; netdev@vger.kernel.org; bpf@vger.kernel.org
> > > > > Subject: Re: [PATCH bpf-next] net: Use skb->len to check the
> > > > > validity of the parameters in bpf_skb_load_bytes
> > > > >
> > > > > On Tue, Mar 15, 2022 at 08:39:16PM +0800, Liu Jian wrote:
> > > > > > The data length of skb frags + frag_list may be greater than
> > > > > > 0xffff, so here use skb->len to check the validity of the parameters.
> > > > > What is the use case that needs to look beyond 0xffff ?
> > >
> > > > I use sockmap with strparser, the stm->strp.offset (the begin of
> > > > one application layer protocol message) maybe beyond 0xffff, but i
> > > > need load the message head to do something.
> > >
> > > This would explain skb_load_bytes but not the other two right? Also
> > > if we
> > Yes, I just see that these two functions have the same judgment.
> > > are doing this why not just remove those two checks in
> > > flow_dissector_load() I think skb_header_pointer() does duplicate
> checks.
> > > Please check.
> > Yes, skb_header_pointer() have checked as below, and I will send v2 to
> remove 0xffff check.
> > ----skb_header_pointer
> > -------- __skb_header_pointer
> > ------------skb_copy_bits
> > ---------------- if (offset > (int)skb->len - len)
> > --------------------goto fault;
> >
> > Thank you~
> 
> Do we need to have at least "offset <= 0x7fffffff" check? IOW, do we need
> to enforce the unsignedness of the offset? Or does skb_header_pointer et
> all properly work with the negative offsets?
Yes, skb_header_pointer can not handle the negative offset.
I sent a new patch. Please help review it again. Thank you.
https://patchwork.kernel.org/project/netdevbpf/patch/20220317135940.358774-1-liujian56@huawei.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-03-17 14:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-15 12:39 [PATCH bpf-next] net: Use skb->len to check the validity of the parameters in bpf_skb_load_bytes Liu Jian
2022-03-15 19:58 ` Martin KaFai Lau
2022-03-16  1:09   ` liujian (CE)
2022-03-16  4:00     ` John Fastabend
2022-03-16 13:08       ` liujian (CE)
2022-03-16 15:09         ` Stanislav Fomichev
2022-03-17 14:08           ` liujian (CE)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.