netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 net-next] net: filter: fix length calculation in BPF testsuite
@ 2014-05-29 19:29 Chema Gonzalez
  2014-05-29 20:41 ` Daniel Borkmann
  0 siblings, 1 reply; 7+ messages in thread
From: Chema Gonzalez @ 2014-05-29 19:29 UTC (permalink / raw)
  To: David Miller, Eric Dumazet, Daniel Borkmann, Alexei Starovoitov
  Cc: netdev, Chema Gonzalez

The current probe_filter_length() (the function that calculates the
length of a test BPF filter) behavior is to declare the end of the
filter as soon as it finds {0, *, *, 0}. This is actually a valid
insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
fails (its length is cut short).

We are changing probe_filter_length() so as to start from the end, and
declare the end of the filter as the first instruction which is not
{0, *, *, 0}. This solution produces a simpler patch than the
alternative of using an explicit end-of-filter mark. It is technically
incorrect if your filter ends up with "ld #0", but that should not
happen anyway.

We also add a new test (LD_IMM_0) that includes ld #0 (does not work
without this patch).

Signed-off-by: Chema Gonzalez <chema@google.com>
---
 lib/test_bpf.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index af677cb..e893e3b 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -158,6 +158,18 @@ static struct bpf_test tests[] = {
 		{ { 0, 0x800000ff }, { 1, 0x800000ff } },
 	},
 	{
+		"LD_IMM_0",
+		.u.insns = {
+			BPF_STMT(BPF_LD | BPF_IMM, 0), /* ld #0 */
+			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
+			BPF_STMT(BPF_RET | BPF_K, 0),
+			BPF_STMT(BPF_RET | BPF_K, 1),
+		},
+		CLASSIC,
+		{ },
+		{ { 1, 1 } },
+	},
+	{
 		"LD_IND",
 		.u.insns = {
 			BPF_STMT(BPF_LDX | BPF_LEN, 0),
@@ -1542,12 +1554,11 @@ static int probe_filter_length(struct sock_filter *fp)
 {
 	int len = 0;
 
-	while (fp->code != 0 || fp->k != 0) {
-		fp++;
-		len++;
-	}
+	for (len = MAX_INSNS-1; len > 0; --len)
+		if (fp[len].code != 0 || fp[len].k != 0)
+			break;
 
-	return len;
+	return len+1;
 }
 
 static struct sk_filter *generate_filter(int which, int *err)
-- 
1.9.1.423.g4596e3a

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

* Re: [PATCH v1 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-29 19:29 [PATCH v1 net-next] net: filter: fix length calculation in BPF testsuite Chema Gonzalez
@ 2014-05-29 20:41 ` Daniel Borkmann
  2014-05-30 17:15   ` Chema Gonzalez
  2014-05-30 17:15   ` [PATCH v2 " Chema Gonzalez
  0 siblings, 2 replies; 7+ messages in thread
From: Daniel Borkmann @ 2014-05-29 20:41 UTC (permalink / raw)
  To: Chema Gonzalez; +Cc: David Miller, Eric Dumazet, Alexei Starovoitov, netdev

On 05/29/2014 09:29 PM, Chema Gonzalez wrote:
> The current probe_filter_length() (the function that calculates the
> length of a test BPF filter) behavior is to declare the end of the
> filter as soon as it finds {0, *, *, 0}. This is actually a valid
> insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
> fails (its length is cut short).
>
> We are changing probe_filter_length() so as to start from the end, and
> declare the end of the filter as the first instruction which is not
> {0, *, *, 0}. This solution produces a simpler patch than the
> alternative of using an explicit end-of-filter mark. It is technically
> incorrect if your filter ends up with "ld #0", but that should not
> happen anyway.
>
> We also add a new test (LD_IMM_0) that includes ld #0 (does not work
> without this patch).
>
> Signed-off-by: Chema Gonzalez <chema@google.com>

Looks good to me, thanks a lot Chema!

Acked-by: Daniel Borkmann <dborkman@redhat.com>

> +	for (len = MAX_INSNS-1; len > 0; --len)
> +		if (fp[len].code != 0 || fp[len].k != 0)
> +			break;
>
> -	return len;
> +	return len+1;

Nit: would be great to have a whitespace between MAX_INSNS-1 and len+1 but
that shouldn't matter that much, perhaps.

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

* Re: [PATCH v1 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-29 20:41 ` Daniel Borkmann
@ 2014-05-30 17:15   ` Chema Gonzalez
  2014-05-30 17:15   ` [PATCH v2 " Chema Gonzalez
  1 sibling, 0 replies; 7+ messages in thread
From: Chema Gonzalez @ 2014-05-30 17:15 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: David Miller, Eric Dumazet, Alexei Starovoitov, Network Development

On Thu, May 29, 2014 at 1:41 PM, Daniel Borkmann <dborkman@redhat.com> wrote:
> On 05/29/2014 09:29 PM, Chema Gonzalez wrote:
>>
>> The current probe_filter_length() (the function that calculates the
>> length of a test BPF filter) behavior is to declare the end of the
>> filter as soon as it finds {0, *, *, 0}. This is actually a valid
>> insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM,
>> 0)"
>> fails (its length is cut short).
>>
>> We are changing probe_filter_length() so as to start from the end, and
>> declare the end of the filter as the first instruction which is not
>> {0, *, *, 0}. This solution produces a simpler patch than the
>> alternative of using an explicit end-of-filter mark. It is technically
>> incorrect if your filter ends up with "ld #0", but that should not
>> happen anyway.
>>
>> We also add a new test (LD_IMM_0) that includes ld #0 (does not work
>> without this patch).
>>
>> Signed-off-by: Chema Gonzalez <chema@google.com>
>
>
> Looks good to me, thanks a lot Chema!
>
> Acked-by: Daniel Borkmann <dborkman@redhat.com>
>
>
>> +       for (len = MAX_INSNS-1; len > 0; --len)
>> +               if (fp[len].code != 0 || fp[len].k != 0)
>> +                       break;
>>
>> -       return len;
>> +       return len+1;
>
>
> Nit: would be great to have a whitespace between MAX_INSNS-1 and len+1 but
> that shouldn't matter that much, perhaps.
Done.

-Chema

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

* [PATCH v2 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-29 20:41 ` Daniel Borkmann
  2014-05-30 17:15   ` Chema Gonzalez
@ 2014-05-30 17:15   ` Chema Gonzalez
  2014-05-31 19:21     ` Daniel Borkmann
  2014-06-02 23:33     ` David Miller
  1 sibling, 2 replies; 7+ messages in thread
From: Chema Gonzalez @ 2014-05-30 17:15 UTC (permalink / raw)
  To: David Miller, Eric Dumazet, Daniel Borkmann, Alexei Starovoitov
  Cc: netdev, Chema Gonzalez

The current probe_filter_length() (the function that calculates the
length of a test BPF filter) behavior is to declare the end of the
filter as soon as it finds {0, *, *, 0}. This is actually a valid
insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
fails (its length is cut short).

We are changing probe_filter_length() so as to start from the end, and
declare the end of the filter as the first instruction which is not
{0, *, *, 0}. This solution produces a simpler patch than the
alternative of using an explicit end-of-filter mark. It is technically
incorrect if your filter ends up with "ld #0", but that should not
happen anyway.

We also add a new test (LD_IMM_0) that includes ld #0 (does not work
without this patch).

Signed-off-by: Chema Gonzalez <chema@google.com>
---
 lib/test_bpf.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index af677cb..e60f766 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -158,6 +158,18 @@ static struct bpf_test tests[] = {
 		{ { 0, 0x800000ff }, { 1, 0x800000ff } },
 	},
 	{
+		"LD_IMM_0",
+		.u.insns = {
+			BPF_STMT(BPF_LD | BPF_IMM, 0), /* ld #0 */
+			BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
+			BPF_STMT(BPF_RET | BPF_K, 0),
+			BPF_STMT(BPF_RET | BPF_K, 1),
+		},
+		CLASSIC,
+		{ },
+		{ { 1, 1 } },
+	},
+	{
 		"LD_IND",
 		.u.insns = {
 			BPF_STMT(BPF_LDX | BPF_LEN, 0),
@@ -1542,12 +1554,11 @@ static int probe_filter_length(struct sock_filter *fp)
 {
 	int len = 0;
 
-	while (fp->code != 0 || fp->k != 0) {
-		fp++;
-		len++;
-	}
+	for (len = MAX_INSNS - 1; len > 0; --len)
+		if (fp[len].code != 0 || fp[len].k != 0)
+			break;
 
-	return len;
+	return len + 1;
 }
 
 static struct sk_filter *generate_filter(int which, int *err)
-- 
1.9.1.423.g4596e3a

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

* Re: [PATCH v2 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-30 17:15   ` [PATCH v2 " Chema Gonzalez
@ 2014-05-31 19:21     ` Daniel Borkmann
  2014-06-02  6:33       ` Alexei Starovoitov
  2014-06-02 23:33     ` David Miller
  1 sibling, 1 reply; 7+ messages in thread
From: Daniel Borkmann @ 2014-05-31 19:21 UTC (permalink / raw)
  To: Chema Gonzalez; +Cc: David Miller, Eric Dumazet, Alexei Starovoitov, netdev

On 05/30/2014 07:15 PM, Chema Gonzalez wrote:
> The current probe_filter_length() (the function that calculates the
> length of a test BPF filter) behavior is to declare the end of the
> filter as soon as it finds {0, *, *, 0}. This is actually a valid
> insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
> fails (its length is cut short).
>
> We are changing probe_filter_length() so as to start from the end, and
> declare the end of the filter as the first instruction which is not
> {0, *, *, 0}. This solution produces a simpler patch than the
> alternative of using an explicit end-of-filter mark. It is technically
> incorrect if your filter ends up with "ld #0", but that should not
> happen anyway.
>
> We also add a new test (LD_IMM_0) that includes ld #0 (does not work
> without this patch).
>
> Signed-off-by: Chema Gonzalez <chema@google.com>

Thanks (sorry for the delay as I'm currently on travel).

Acked-by: Daniel Borkmann <dborkman@redhat.com>

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

* Re: [PATCH v2 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-31 19:21     ` Daniel Borkmann
@ 2014-06-02  6:33       ` Alexei Starovoitov
  0 siblings, 0 replies; 7+ messages in thread
From: Alexei Starovoitov @ 2014-06-02  6:33 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Chema Gonzalez, David Miller, Eric Dumazet, Network Development

On Sat, May 31, 2014 at 12:21 PM, Daniel Borkmann <dborkman@redhat.com> wrote:
> On 05/30/2014 07:15 PM, Chema Gonzalez wrote:
>>
>> The current probe_filter_length() (the function that calculates the
>> length of a test BPF filter) behavior is to declare the end of the
>> filter as soon as it finds {0, *, *, 0}. This is actually a valid
>> insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM,
>> 0)"
>> fails (its length is cut short).
>>
>> We are changing probe_filter_length() so as to start from the end, and
>> declare the end of the filter as the first instruction which is not
>> {0, *, *, 0}. This solution produces a simpler patch than the
>> alternative of using an explicit end-of-filter mark. It is technically
>> incorrect if your filter ends up with "ld #0", but that should not
>> happen anyway.
>>
>> We also add a new test (LD_IMM_0) that includes ld #0 (does not work
>> without this patch).
>>
>> Signed-off-by: Chema Gonzalez <chema@google.com>

Thank you for addressing all our comments.

Acked-by: Alexei Starovoitov <ast@plumgrid.com>

>
> Thanks (sorry for the delay as I'm currently on travel).
>
> Acked-by: Daniel Borkmann <dborkman@redhat.com>

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

* Re: [PATCH v2 net-next] net: filter: fix length calculation in BPF testsuite
  2014-05-30 17:15   ` [PATCH v2 " Chema Gonzalez
  2014-05-31 19:21     ` Daniel Borkmann
@ 2014-06-02 23:33     ` David Miller
  1 sibling, 0 replies; 7+ messages in thread
From: David Miller @ 2014-06-02 23:33 UTC (permalink / raw)
  To: chema; +Cc: edumazet, dborkman, ast, netdev

From: Chema Gonzalez <chema@google.com>
Date: Fri, 30 May 2014 10:15:12 -0700

> The current probe_filter_length() (the function that calculates the
> length of a test BPF filter) behavior is to declare the end of the
> filter as soon as it finds {0, *, *, 0}. This is actually a valid
> insn ("ld #0"), so any filter with includes "BPF_STMT(BPF_LD | BPF_IMM, 0)"
> fails (its length is cut short).
> 
> We are changing probe_filter_length() so as to start from the end, and
> declare the end of the filter as the first instruction which is not
> {0, *, *, 0}. This solution produces a simpler patch than the
> alternative of using an explicit end-of-filter mark. It is technically
> incorrect if your filter ends up with "ld #0", but that should not
> happen anyway.
> 
> We also add a new test (LD_IMM_0) that includes ld #0 (does not work
> without this patch).
> 
> Signed-off-by: Chema Gonzalez <chema@google.com>

Applied, thank you.

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

end of thread, other threads:[~2014-06-02 23:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-29 19:29 [PATCH v1 net-next] net: filter: fix length calculation in BPF testsuite Chema Gonzalez
2014-05-29 20:41 ` Daniel Borkmann
2014-05-30 17:15   ` Chema Gonzalez
2014-05-30 17:15   ` [PATCH v2 " Chema Gonzalez
2014-05-31 19:21     ` Daniel Borkmann
2014-06-02  6:33       ` Alexei Starovoitov
2014-06-02 23:33     ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).