Skip to content

Commit 2c46062

Browse files
borkmanndavem330
authored andcommitted
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 13da9e2 ("Revert "endian: #define __BYTE_ORDER"") and 415586c ("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: 18f3d6b ("selftests/bpf: Add test cases to test narrower ctx field loads") Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent aaf83ae commit 2c46062

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* License as published by the Free Software Foundation.
99
*/
1010

11+
#include <endian.h>
1112
#include <asm/types.h>
1213
#include <linux/types.h>
1314
#include <stdint.h>
@@ -1098,7 +1099,7 @@ static struct bpf_test tests[] = {
10981099
"check skb->hash byte load permitted",
10991100
.insns = {
11001101
BPF_MOV64_IMM(BPF_REG_0, 0),
1101-
#ifdef __LITTLE_ENDIAN
1102+
#if __BYTE_ORDER == __LITTLE_ENDIAN
11021103
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
11031104
offsetof(struct __sk_buff, hash)),
11041105
#else
@@ -1135,7 +1136,7 @@ static struct bpf_test tests[] = {
11351136
"check skb->hash byte load not permitted 3",
11361137
.insns = {
11371138
BPF_MOV64_IMM(BPF_REG_0, 0),
1138-
#ifdef __LITTLE_ENDIAN
1139+
#if __BYTE_ORDER == __LITTLE_ENDIAN
11391140
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
11401141
offsetof(struct __sk_buff, hash) + 3),
11411142
#else
@@ -1244,7 +1245,7 @@ static struct bpf_test tests[] = {
12441245
"check skb->hash half load permitted",
12451246
.insns = {
12461247
BPF_MOV64_IMM(BPF_REG_0, 0),
1247-
#ifdef __LITTLE_ENDIAN
1248+
#if __BYTE_ORDER == __LITTLE_ENDIAN
12481249
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
12491250
offsetof(struct __sk_buff, hash)),
12501251
#else
@@ -1259,7 +1260,7 @@ static struct bpf_test tests[] = {
12591260
"check skb->hash half load not permitted",
12601261
.insns = {
12611262
BPF_MOV64_IMM(BPF_REG_0, 0),
1262-
#ifdef __LITTLE_ENDIAN
1263+
#if __BYTE_ORDER == __LITTLE_ENDIAN
12631264
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
12641265
offsetof(struct __sk_buff, hash) + 2),
12651266
#else
@@ -5422,7 +5423,7 @@ static struct bpf_test tests[] = {
54225423
"check bpf_perf_event_data->sample_period byte load permitted",
54235424
.insns = {
54245425
BPF_MOV64_IMM(BPF_REG_0, 0),
5425-
#ifdef __LITTLE_ENDIAN
5426+
#if __BYTE_ORDER == __LITTLE_ENDIAN
54265427
BPF_LDX_MEM(BPF_B, BPF_REG_0, BPF_REG_1,
54275428
offsetof(struct bpf_perf_event_data, sample_period)),
54285429
#else
@@ -5438,7 +5439,7 @@ static struct bpf_test tests[] = {
54385439
"check bpf_perf_event_data->sample_period half load permitted",
54395440
.insns = {
54405441
BPF_MOV64_IMM(BPF_REG_0, 0),
5441-
#ifdef __LITTLE_ENDIAN
5442+
#if __BYTE_ORDER == __LITTLE_ENDIAN
54425443
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
54435444
offsetof(struct bpf_perf_event_data, sample_period)),
54445445
#else
@@ -5454,7 +5455,7 @@ static struct bpf_test tests[] = {
54545455
"check bpf_perf_event_data->sample_period word load permitted",
54555456
.insns = {
54565457
BPF_MOV64_IMM(BPF_REG_0, 0),
5457-
#ifdef __LITTLE_ENDIAN
5458+
#if __BYTE_ORDER == __LITTLE_ENDIAN
54585459
BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_1,
54595460
offsetof(struct bpf_perf_event_data, sample_period)),
54605461
#else
@@ -5481,7 +5482,7 @@ static struct bpf_test tests[] = {
54815482
"check skb->data half load not permitted",
54825483
.insns = {
54835484
BPF_MOV64_IMM(BPF_REG_0, 0),
5484-
#ifdef __LITTLE_ENDIAN
5485+
#if __BYTE_ORDER == __LITTLE_ENDIAN
54855486
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
54865487
offsetof(struct __sk_buff, data)),
54875488
#else
@@ -5497,7 +5498,7 @@ static struct bpf_test tests[] = {
54975498
"check skb->tc_classid half load not permitted for lwt prog",
54985499
.insns = {
54995500
BPF_MOV64_IMM(BPF_REG_0, 0),
5500-
#ifdef __LITTLE_ENDIAN
5501+
#if __BYTE_ORDER == __LITTLE_ENDIAN
55015502
BPF_LDX_MEM(BPF_H, BPF_REG_0, BPF_REG_1,
55025503
offsetof(struct __sk_buff, tc_classid)),
55035504
#else

0 commit comments

Comments
 (0)