* [PATCH net] bpf: fix byte order test in test_verifier
@ 2017-08-04 20:24 Daniel Borkmann
2017-08-04 22:26 ` Yonghong Song
2017-08-04 23:09 ` David Miller
0 siblings, 2 replies; 3+ messages in thread
From: Daniel Borkmann @ 2017-08-04 20:24 UTC (permalink / raw)
To: davem; +Cc: yhs, alexei.starovoitov, netdev, Daniel Borkmann
We really must check with #if __BYTE_ORDER == XYZ instead of
just presence of #ifdef __LITTLE_ENDIAN. I noticed that when
actually running this on big endian machine, the latter test
resolves to true for user space, same for #ifdef __BIG_ENDIAN.
E.g., looking at endian.h from libc, both are also defined
there, so we really must test this against __BYTE_ORDER instead
for proper insns selection. For the kernel, such checks are
fine though e.g. see 13da9e200fe4 ("Revert "endian: #define
__BYTE_ORDER"") and 415586c9e6d3 ("UAPI: fix endianness conditionals
in M32R's asm/stat.h") for some more context, but not for
user space. Lets also make sure to properly include endian.h.
After that, suite passes for me:
./test_verifier: ELF 64-bit MSB executable, [...]
Linux foo 4.13.0-rc3+ #4 SMP Fri Aug 4 06:59:30 EDT 2017 s390x s390x s390x GNU/Linux
Before fix: Summary: 505 PASSED, 11 FAILED
After fix: Summary: 516 PASSED, 0 FAILED
Fixes: 18f3d6be6be1 ("selftests/bpf: Add test cases to test narrower ctx field loads")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
---
tools/testing/selftests/bpf/test_verifier.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
index addea82..d3ed732 100644
--- a/tools/testing/selftests/bpf/test_verifier.c
+++ b/tools/testing/selftests/bpf/test_verifier.c
@@ -8,6 +8,7 @@
* License as published by the Free Software Foundation.
*/
+#include <endian.h>
#include <asm/types.h>
#include <linux/types.h>
#include <stdint.h>
@@ -1098,7 +1099,7 @@ struct test_val {
"check skb->hash byte load permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash)),
#else
@@ -1135,7 +1136,7 @@ struct test_val {
"check skb->hash byte load not permitted 3",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash) + 3),
#else
@@ -1244,7 +1245,7 @@ struct test_val {
"check skb->hash half load permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash)),
#else
@@ -1259,7 +1260,7 @@ struct test_val {
"check skb->hash half load not permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash) + 2),
#else
@@ -5422,7 +5423,7 @@ struct test_val {
"check bpf_perf_event_data->sample_period byte load permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)),
#else
@@ -5438,7 +5439,7 @@ struct test_val {
"check bpf_perf_event_data->sample_period half load permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)),
#else
@@ -5454,7 +5455,7 @@ struct test_val {
"check bpf_perf_event_data->sample_period word load permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)),
#else
@@ -5481,7 +5482,7 @@ struct test_val {
"check skb->data half load not permitted",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, data)),
#else
@@ -5497,7 +5498,7 @@ struct test_val {
"check skb->tc_classid half load not permitted for lwt prog",
.insns = {
BPF_MOV64_IMM(BPF_REG_0, 0),
-#ifdef __LITTLE_ENDIAN
+#if __BYTE_ORDER == __LITTLE_ENDIAN
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, tc_classid)),
#else
--
1.9.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net] bpf: fix byte order test in test_verifier
2017-08-04 20:24 [PATCH net] bpf: fix byte order test in test_verifier Daniel Borkmann
@ 2017-08-04 22:26 ` Yonghong Song
2017-08-04 23:09 ` David Miller
1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2017-08-04 22:26 UTC (permalink / raw)
To: Daniel Borkmann, davem; +Cc: alexei.starovoitov, netdev
On 8/4/17 1:24 PM, Daniel Borkmann wrote:
> We really must check with #if __BYTE_ORDER == XYZ instead of
> just presence of #ifdef __LITTLE_ENDIAN. I noticed that when
> actually running this on big endian machine, the latter test
> resolves to true for user space, same for #ifdef __BIG_ENDIAN.
>
> E.g., looking at endian.h from libc, both are also defined
> there, so we really must test this against __BYTE_ORDER instead
> for proper insns selection. For the kernel, such checks are
> fine though e.g. see 13da9e200fe4 ("Revert "endian: #define
> __BYTE_ORDER"") and 415586c9e6d3 ("UAPI: fix endianness conditionals
> in M32R's asm/stat.h") for some more context, but not for
> user space. Lets also make sure to properly include endian.h.
> After that, suite passes for me:
>
> ./test_verifier: ELF 64-bit MSB executable, [...]
>
> Linux foo 4.13.0-rc3+ #4 SMP Fri Aug 4 06:59:30 EDT 2017 s390x s390x s390x GNU/Linux
>
> Before fix: Summary: 505 PASSED, 11 FAILED
> After fix: Summary: 516 PASSED, 0 FAILED
>
> Fixes: 18f3d6be6be1 ("selftests/bpf: Add test cases to test narrower ctx field loads")
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> ---
> tools/testing/selftests/bpf/test_verifier.c | 19 ++++++++++---------
> 1 file changed, 10 insertions(+), 9 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
> index addea82..d3ed732 100644
> --- a/tools/testing/selftests/bpf/test_verifier.c
> +++ b/tools/testing/selftests/bpf/test_verifier.c
> @@ -8,6 +8,7 @@
> * License as published by the Free Software Foundation.
> */
>
> +#include <endian.h>
> #include <asm/types.h>
> #include <linux/types.h>
> #include <stdint.h>
> @@ -1098,7 +1099,7 @@ struct test_val {
> "check skb->hash byte load permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, hash)),
> #else
> @@ -1135,7 +1136,7 @@ struct test_val {
> "check skb->hash byte load not permitted 3",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, hash) + 3),
> #else
> @@ -1244,7 +1245,7 @@ struct test_val {
> "check skb->hash half load permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, hash)),
> #else
> @@ -1259,7 +1260,7 @@ struct test_val {
> "check skb->hash half load not permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, hash) + 2),
> #else
> @@ -5422,7 +5423,7 @@ struct test_val {
> "check bpf_perf_event_data->sample_period byte load permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
> offsetof(struct bpf_perf_event_data, sample_period)),
> #else
> @@ -5438,7 +5439,7 @@ struct test_val {
> "check bpf_perf_event_data->sample_period half load permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
> offsetof(struct bpf_perf_event_data, sample_period)),
> #else
> @@ -5454,7 +5455,7 @@ struct test_val {
> "check bpf_perf_event_data->sample_period word load permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
> offsetof(struct bpf_perf_event_data, sample_period)),
> #else
> @@ -5481,7 +5482,7 @@ struct test_val {
> "check skb->data half load not permitted",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, data)),
> #else
> @@ -5497,7 +5498,7 @@ struct test_val {
> "check skb->tc_classid half load not permitted for lwt prog",
> .insns = {
> BPF_MOV64_IMM(BPF_REG_0, 0),
> -#ifdef __LITTLE_ENDIAN
> +#if __BYTE_ORDER == __LITTLE_ENDIAN
> BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
> offsetof(struct __sk_buff, tc_classid)),
> #else
>
The change looks good to me.
Acked-by: Yonghong <yhs@fb.com>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net] bpf: fix byte order test in test_verifier
2017-08-04 20:24 [PATCH net] bpf: fix byte order test in test_verifier Daniel Borkmann
2017-08-04 22:26 ` Yonghong Song
@ 2017-08-04 23:09 ` David Miller
1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2017-08-04 23:09 UTC (permalink / raw)
To: daniel; +Cc: yhs, alexei.starovoitov, netdev
From: Daniel Borkmann <daniel@iogearbox.net>
Date: Fri, 4 Aug 2017 22:24:41 +0200
> We really must check with #if __BYTE_ORDER == XYZ instead of
> just presence of #ifdef __LITTLE_ENDIAN. I noticed that when
> actually running this on big endian machine, the latter test
> resolves to true for user space, same for #ifdef __BIG_ENDIAN.
>
> E.g., looking at endian.h from libc, both are also defined
> there, so we really must test this against __BYTE_ORDER instead
> for proper insns selection. For the kernel, such checks are
> fine though e.g. see 13da9e200fe4 ("Revert "endian: #define
> __BYTE_ORDER"") and 415586c9e6d3 ("UAPI: fix endianness conditionals
> in M32R's asm/stat.h") for some more context, but not for
> user space. Lets also make sure to properly include endian.h.
> After that, suite passes for me:
>
> ./test_verifier: ELF 64-bit MSB executable, [...]
>
> Linux foo 4.13.0-rc3+ #4 SMP Fri Aug 4 06:59:30 EDT 2017 s390x s390x s390x GNU/Linux
>
> Before fix: Summary: 505 PASSED, 11 FAILED
> After fix: Summary: 516 PASSED, 0 FAILED
>
> Fixes: 18f3d6be6be1 ("selftests/bpf: Add test cases to test narrower ctx field loads")
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Applied and queued up for -stable, thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-08-04 23:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-04 20:24 [PATCH net] bpf: fix byte order test in test_verifier Daniel Borkmann
2017-08-04 22:26 ` Yonghong Song
2017-08-04 23:09 ` 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).