Commit 2c460621 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by David S. Miller

bpf: fix byte order test in test_verifier

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 13da9e20 ("Revert "endian: #define
__BYTE_ORDER"") and 415586c9 ("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: 18f3d6be ("selftests/bpf: Add test cases to test narrower ctx field loads")
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarYonghong <yhs@fb.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent aaf83aec
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
* License as published by the Free Software Foundation. * License as published by the Free Software Foundation.
*/ */
#include <endian.h>
#include <asm/types.h> #include <asm/types.h>
#include <linux/types.h> #include <linux/types.h>
#include <stdint.h> #include <stdint.h>
...@@ -1098,7 +1099,7 @@ static struct bpf_test tests[] = { ...@@ -1098,7 +1099,7 @@ static struct bpf_test tests[] = {
"check skb->hash byte load permitted", "check skb->hash byte load permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash)), offsetof(struct __sk_buff, hash)),
#else #else
...@@ -1135,7 +1136,7 @@ static struct bpf_test tests[] = { ...@@ -1135,7 +1136,7 @@ static struct bpf_test tests[] = {
"check skb->hash byte load not permitted 3", "check skb->hash byte load not permitted 3",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash) + 3), offsetof(struct __sk_buff, hash) + 3),
#else #else
...@@ -1244,7 +1245,7 @@ static struct bpf_test tests[] = { ...@@ -1244,7 +1245,7 @@ static struct bpf_test tests[] = {
"check skb->hash half load permitted", "check skb->hash half load permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash)), offsetof(struct __sk_buff, hash)),
#else #else
...@@ -1259,7 +1260,7 @@ static struct bpf_test tests[] = { ...@@ -1259,7 +1260,7 @@ static struct bpf_test tests[] = {
"check skb->hash half load not permitted", "check skb->hash half load not permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, hash) + 2), offsetof(struct __sk_buff, hash) + 2),
#else #else
...@@ -5422,7 +5423,7 @@ static struct bpf_test tests[] = { ...@@ -5422,7 +5423,7 @@ static struct bpf_test tests[] = {
"check bpf_perf_event_data->sample_period byte load permitted", "check bpf_perf_event_data->sample_period byte load permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)), offsetof(struct bpf_perf_event_data, sample_period)),
#else #else
...@@ -5438,7 +5439,7 @@ static struct bpf_test tests[] = { ...@@ -5438,7 +5439,7 @@ static struct bpf_test tests[] = {
"check bpf_perf_event_data->sample_period half load permitted", "check bpf_perf_event_data->sample_period half load permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)), offsetof(struct bpf_perf_event_data, sample_period)),
#else #else
...@@ -5454,7 +5455,7 @@ static struct bpf_test tests[] = { ...@@ -5454,7 +5455,7 @@ static struct bpf_test tests[] = {
"check bpf_perf_event_data->sample_period word load permitted", "check bpf_perf_event_data->sample_period word load permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
offsetof(struct bpf_perf_event_data, sample_period)), offsetof(struct bpf_perf_event_data, sample_period)),
#else #else
...@@ -5481,7 +5482,7 @@ static struct bpf_test tests[] = { ...@@ -5481,7 +5482,7 @@ static struct bpf_test tests[] = {
"check skb->data half load not permitted", "check skb->data half load not permitted",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, data)), offsetof(struct __sk_buff, data)),
#else #else
...@@ -5497,7 +5498,7 @@ static struct bpf_test tests[] = { ...@@ -5497,7 +5498,7 @@ static struct bpf_test tests[] = {
"check skb->tc_classid half load not permitted for lwt prog", "check skb->tc_classid half load not permitted for lwt prog",
.insns = { .insns = {
BPF_MOV64_IMM(BPF_REG_0, 0), 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, BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
offsetof(struct __sk_buff, tc_classid)), offsetof(struct __sk_buff, tc_classid)),
#else #else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment