BPF: Regression test for 64bit arithmetic
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
---
V3: Rebased on master and included line numbers in instructions
Capability patch has not been applied to master at time of rebase.
include/lapi/bpf.h | 27 +++ runtest/syscalls | 1 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/bpf_prog02.c | 182 +++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog02.c
include/lapi/bpf.h +27 -0
@@ -18,7 +18,9 @@/* Start copy from linux/bpf_(common).h */ #define BPF_CLASS (code) ((code) & 0x07)#define BPF_LD 0x00 +#define BPF_LDX 0x01#define BPF_ST 0x02 +#define BPF_STX 0x03#define BPF_JMP 0x05#define BPF_SIZE (code) ((code) & 0x18)
@@ -30,6 +32,7 @@#define BPF_OP (code) ((code) & 0xf0)#define BPF_ADD 0x00 +#define BPF_SUB 0x10#define BPF_JEQ 0x10
@@ -432,6 +435,14 @@ enum bpf_func_id {/* Start copy from tools/include/filter.h */ +#define BPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) +#define BPF_ALU64_IMM (OP ,DST ,IMM ) \ ((struct bpf_insn) { \ .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
@@ -477,6 +488,22 @@ enum bpf_func_id { .off = OFF, \ .imm = IMM }) +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ ((struct bpf_insn) { \ .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
runtest/syscalls +1 -0
@@ -34,6 +34,7 @@ bind03 bind03 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01 +bpf_prog02 bpf_prog02 brk01 brk01
testcases/kernel/syscalls/bpf/.gitignore +1 -0
@@ -1,2 +1,3 @@ bpf_map01 bpf_prog01 +bpf_prog02
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +182 -0
182 lines not on this page · raw message
Signature · 1 line
2.22.1
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
Hi!
A few minor points below, I guess I can fix these before pushing as well.
Quoted 161 lines
Signed-off-by Richard Palethorpe <[email protected]>Reviewed-by Jan Stancek <[email protected]>Reviewed-by Cyril Hrubis <[email protected]>---
V3: Rebased on master and included line numbers in instructions
Capability patch has not been applied to master at time of rebase.
include/lapi/bpf.h | 27 +++ runtest/syscalls | 1 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/bpf_prog02.c | 182 +++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog02.cinclude/lapi/bpf.h +27
-0@@ -18,7 +18,9 @@/* Start copy from linux/bpf_(common).h */ #define BPF_CLASS (code) ((code) & 0x07)#define BPF_LD 0x00 +#define BPF_LDX 0x01#define BPF_ST 0x02 +#define BPF_STX 0x03#define BPF_JMP 0x05#define BPF_SIZE (code) ((code) & 0x18)@@ -30,6 +32,7 @@#define BPF_OP (code) ((code) & 0xf0)#define BPF_ADD 0x00 +#define BPF_SUB 0x10#define BPF_JEQ 0x10@@ -432,6 +435,14 @@ enum bpf_func_id {/* Start copy from tools/include/filter.h */ +#define BPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) +#define BPF_ALU64_IMM (OP ,DST ,IMM ) \ ((struct bpf_insn) { \ .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \@@ -477,6 +488,22 @@ enum bpf_func_id { .off = OFF, \ .imm = IMM }) +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ ((struct bpf_insn) { \ .code = BPF_JMP | BPF_OP(OP) | BPF_K, \runtest/syscalls +1
-0@@ -34,6 +34,7 @@ bind03 bind03 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01 +bpf_prog02 bpf_prog02 brk01 brk01testcases/kernel/syscalls/bpf/.gitignore +1
-0@@ -1,2 +1,3 @@ bpf_map01 bpf_prog01 +bpf_prog02testcases/kernel/syscalls/bpf/bpf_prog02.c new file +59
-0@@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Check if eBPF can do arithmetic with 64bits. This targets a specific +* regression which only effects unprivileged users who are subject to extra +* pointer arithmetic checks during verification. +* +* Fixed by commit 3612af783cf52c74a031a2f11b82247b2599d3cd. +* https://new.blog.cloudflare.com/ebpf-cant-count/ +* +* This test is very similar in structure to bpf_prog01 which is better +* annotated. +*/ + +#include <limits.h> +#include <string.h> +#include <stdio.h> + +#include "config.h" +#include "tst_test.h" +#include "tst_capability.h" +#include "lapi/socket.h" +#include "lapi/bpf.h" + +#define A64INT (((uint64_t)1) << 60) + +const char MSG [] ="Ahoj!" ; +static char *msg; + +static char *log; +static uint32_t *key; +static uint64_t *val; +static union bpf_attr *attr; + +static int load_prog (int fd) +{ +struct bpf_insn *prog; +struct bpf_insn insn[] = { +BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */ + +BPF_LD_MAP_FD (BPF_REG_1 , fd),/* 1: r1 = &fd */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),/* 3: r2 = fp */ +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* 4: r2 = r2 - 8 */ +BPF_ST_MEM (BPF_DW ,BPF_REG_2 ,0 ,0 ),/* 5: *r2 = 0 */ +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem),/* 6: map_lookup_elem */ +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,17 ),/* 7: if(!r0) goto 25 */ +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_0 ),/* 8: r3 = r0 */ +BPF_LD_IMM64 (BPF_REG_4 ,A64INT ),/* 9: r4 = 2^61 */ +BPF_ALU64_REG (BPF_ADD ,BPF_REG_4 ,BPF_REG_6 ),/* 11: r4 += r6 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_4 ,0 ),/* 12: *r3 = r4 */ + +BPF_LD_MAP_FD (BPF_REG_1 , fd),/* 13: r1 = &fd */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),/* 15: r2 = fp */ +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* 16: r2 = r2 - 8 */ +BPF_ST_MEM (BPF_DW ,BPF_REG_2 ,0 ,1 ),/* 17: *r2 = 1 */ +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem),/* 18: map_lookup_elem */ +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,5 ),/* 19: if(!r0) goto 25 */ +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_0 ),/* 20: r3 = r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c line 69
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+BPF_LD_IMM64 (BPF_REG_4 ,A64INT ),/* 21: r4 = 2^61 */ +BPF_ALU64_REG (BPF_SUB ,BPF_REG_4 ,BPF_REG_6 ),/* 23: r4 -= r6 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_4 ,0 ),/* 24: *r3 = r4 */ + +BPF_MOV64_IMM (BPF_REG_0 ,0 ),/* 25: r0 = 0 */ + BPF_EXIT_INSN(), /* 26: return r0 */ + }; + +/* Leaks memory when -i is specified */ + prog =tst_alloc (sizeof (insn));
Why not just declare the prog static? Then we could do:
if (!prog) prog = tst_alloc(sizeof(insn));
Quoted 6 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +6
-0+memcpy (prog, insn,sizeof (insn)); + +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (prog); + attr->insn_cnt =ARRAY_SIZE (insn);
testcases/kernel/syscalls/bpf/bpf_prog02.c line 85
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+ attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log); + attr->log_size =BUFSIZ ; + attr->log_level =1 ; + +TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (log[0 ] !=0 ) { +tst_res (TINFO ,"Verification log:" ); +fputs (log, stderr);
I guess that we can do tst_res(TINFO, "Verification log:\n%s", log); instead.
Quoted 50 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +50
-0+tst_brk (TBROK |TTERRNO ,"Failed verification" ); + }else { +tst_brk (TBROK |TTERRNO ,"Failed to load program" ); + } + } + +return TST_RET ; +} + +static void setup (void ) +{ +memcpy (msg,MSG ,sizeof (MSG )); +} + +static void run (void ) +{ +int map_fd, prog_fd; +int sk[2 ]; + +memset (attr,0 ,sizeof (*attr)); + attr->map_type =BPF_MAP_TYPE_ARRAY ; + attr->key_size =4 ; + attr->value_size =8 ; + attr->max_entries =2 ; + +TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (TST_ERR ==EPERM ) { +tst_brk (TCONF |TTERRNO , +"bpf() requires CAP_SYS_ADMIN on this system" ); + }else { +tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); + } + } + map_fd =TST_RET ; + + prog_fd =load_prog (map_fd); + +SAFE_SOCKETPAIR (AF_UNIX ,SOCK_DGRAM ,0 , sk); +SAFE_SETSOCKOPT (sk[1 ],SOL_SOCKET ,SO_ATTACH_BPF , + &prog_fd,sizeof (prog_fd)); + +SAFE_WRITE (1 , sk[0 ], msg,sizeof (MSG )); + +memset (attr,0 ,sizeof (*attr)); + attr->map_fd = map_fd; + attr->key =ptr_to_u64 (key); + attr->value =ptr_to_u64 (val); + *key =0 ; +
testcases/kernel/syscalls/bpf/bpf_prog02.c line 145
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT +1 ) { +tst_res (TFAIL , +"val = %lu, but should be val = %lu + 1" , + *val,A64INT ); + }else { +tst_res (TPASS ,"val = %lu + 1" ,A64INT ); + }
Wrong indentation, also I do not fancy this if else maze. I guess that we can can safely do goto exit; if map lookup fails that would point right before the SAFE_CLOSE() block.
testcases/kernel/syscalls/bpf/bpf_prog02.c line 158
Quoted 12 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +12
-0+ *key =1 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT -1 ) { +tst_res (TFAIL , +"val = %lu, but should be val = %lu - 1" , + *val,A64INT ); + }else { +tst_res (TPASS ,"val = %lu - 1" ,A64INT ); + }
Here as well.
Quoted 26 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +23
-0+SAFE_CLOSE (prog_fd); +SAFE_CLOSE (map_fd); +SAFE_CLOSE (sk[0 ]); +SAFE_CLOSE (sk[1 ]); +} + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .min_kver ="3.18" , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {} + }, + .bufs = (struct tst_buffers []) { + {&key, .size =sizeof (*key)}, + {&val, .size =sizeof (*val)}, + {&log, .size =BUFSIZ }, + {&attr, .size =sizeof (*attr)}, + {&msg, .size =sizeof (MSG )}, + {}, + } +};Signature · 1 line
2.22.1
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
Hello,
Quoted 178 lines · Cyril Hrubis
Hi!
A few minor points below, I guess I can fix these before pushing as well.Quoted 161 lines
Signed-off-by Richard Palethorpe <[email protected]>Reviewed-by Jan Stancek <[email protected]>Reviewed-by Cyril Hrubis <[email protected]>---
V3: Rebased on master and included line numbers in instructions
Capability patch has not been applied to master at time of rebase.
include/lapi/bpf.h | 27 +++ runtest/syscalls | 1 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/bpf_prog02.c | 182 +++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog02.cinclude/lapi/bpf.h +27
-0@@ -18,7 +18,9 @@/* Start copy from linux/bpf_(common).h */ #define BPF_CLASS (code) ((code) & 0x07)#define BPF_LD 0x00 +#define BPF_LDX 0x01#define BPF_ST 0x02 +#define BPF_STX 0x03#define BPF_JMP 0x05#define BPF_SIZE (code) ((code) & 0x18)@@ -30,6 +32,7 @@#define BPF_OP (code) ((code) & 0xf0)#define BPF_ADD 0x00 +#define BPF_SUB 0x10#define BPF_JEQ 0x10@@ -432,6 +435,14 @@ enum bpf_func_id {/* Start copy from tools/include/filter.h */ +#define BPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) +#define BPF_ALU64_IMM (OP ,DST ,IMM ) \ ((struct bpf_insn) { \ .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \@@ -477,6 +488,22 @@ enum bpf_func_id { .off = OFF, \ .imm = IMM }) +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF ) \ ((struct bpf_insn) { \ .code = BPF_JMP | BPF_OP(OP) | BPF_K, \runtest/syscalls +1
-0@@ -34,6 +34,7 @@ bind03 bind03 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01 +bpf_prog02 bpf_prog02 brk01 brk01testcases/kernel/syscalls/bpf/.gitignore +1
-0@@ -1,2 +1,3 @@ bpf_map01 bpf_prog01 +bpf_prog02testcases/kernel/syscalls/bpf/bpf_prog02.c new file +59
-0@@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Check if eBPF can do arithmetic with 64bits. This targets a specific +* regression which only effects unprivileged users who are subject to extra +* pointer arithmetic checks during verification. +* +* Fixed by commit 3612af783cf52c74a031a2f11b82247b2599d3cd. +* https://new.blog.cloudflare.com/ebpf-cant-count/ +* +* This test is very similar in structure to bpf_prog01 which is better +* annotated. +*/ + +#include <limits.h> +#include <string.h> +#include <stdio.h> + +#include "config.h" +#include "tst_test.h" +#include "tst_capability.h" +#include "lapi/socket.h" +#include "lapi/bpf.h" + +#define A64INT (((uint64_t)1) << 60) + +const char MSG [] ="Ahoj!" ; +static char *msg; + +static char *log; +static uint32_t *key; +static uint64_t *val; +static union bpf_attr *attr; + +static int load_prog (int fd) +{ +struct bpf_insn *prog; +struct bpf_insn insn[] = { +BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */ + +BPF_LD_MAP_FD (BPF_REG_1 , fd),/* 1: r1 = &fd */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),/* 3: r2 = fp */ +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* 4: r2 = r2 - 8 */ +BPF_ST_MEM (BPF_DW ,BPF_REG_2 ,0 ,0 ),/* 5: *r2 = 0 */ +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem),/* 6: map_lookup_elem */ +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,17 ),/* 7: if(!r0) goto 25 */ +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_0 ),/* 8: r3 = r0 */ +BPF_LD_IMM64 (BPF_REG_4 ,A64INT ),/* 9: r4 = 2^61 */ +BPF_ALU64_REG (BPF_ADD ,BPF_REG_4 ,BPF_REG_6 ),/* 11: r4 += r6 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_4 ,0 ),/* 12: *r3 = r4 */ + +BPF_LD_MAP_FD (BPF_REG_1 , fd),/* 13: r1 = &fd */ +BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ),/* 15: r2 = fp */ +BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ),/* 16: r2 = r2 - 8 */ +BPF_ST_MEM (BPF_DW ,BPF_REG_2 ,0 ,1 ),/* 17: *r2 = 1 */ +BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem),/* 18: map_lookup_elem */ +BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,5 ),/* 19: if(!r0) goto 25 */ +BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_0 ),/* 20: r3 = r0 */ testcases/kernel/syscalls/bpf/bpf_prog02.c line 69
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+BPF_LD_IMM64 (BPF_REG_4 ,A64INT ),/* 21: r4 = 2^61 */ +BPF_ALU64_REG (BPF_SUB ,BPF_REG_4 ,BPF_REG_6 ),/* 23: r4 -= r6 */ +BPF_STX_MEM (BPF_DW ,BPF_REG_3 ,BPF_REG_4 ,0 ),/* 24: *r3 = r4 */ + +BPF_MOV64_IMM (BPF_REG_0 ,0 ),/* 25: r0 = 0 */ + BPF_EXIT_INSN(), /* 26: return r0 */ + }; + +/* Leaks memory when -i is specified */ + prog =tst_alloc (sizeof (insn));Why not just declare the prog static? Then we could do:
Quoted 2 lines
if (!prog) prog = tst_alloc(sizeof(insn));
OK!
Quoted 12 lines
Quoted 12 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +11
-0+memcpy (prog, insn,sizeof (insn)); + +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (prog); + attr->insn_cnt =ARRAY_SIZE (insn); + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log); + attr->log_size =BUFSIZ ; + attr->log_level =1 ; +
Quoted 8 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c line 85
Quoted 5 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +5
-0+TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (log[0 ] !=0 ) { +tst_res (TINFO ,"Verification log:" ); +fputs (log, stderr);I guess that we can do
tst_res(TINFO, "Verification log:\n%s", log); instead.
Nope, the log is often too long for tst_res and is truncated.
Quoted 57 lines
Quoted 57 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +56
-0+tst_brk (TBROK |TTERRNO ,"Failed verification" ); + }else { +tst_brk (TBROK |TTERRNO ,"Failed to load program" ); + } + } + +return TST_RET ; +} + +static void setup (void ) +{ +memcpy (msg,MSG ,sizeof (MSG )); +} + +static void run (void ) +{ +int map_fd, prog_fd; +int sk[2 ]; + +memset (attr,0 ,sizeof (*attr)); + attr->map_type =BPF_MAP_TYPE_ARRAY ; + attr->key_size =4 ; + attr->value_size =8 ; + attr->max_entries =2 ; + +TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (TST_ERR ==EPERM ) { +tst_brk (TCONF |TTERRNO , +"bpf() requires CAP_SYS_ADMIN on this system" ); + }else { +tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); + } + } + map_fd =TST_RET ; + + prog_fd =load_prog (map_fd); + +SAFE_SOCKETPAIR (AF_UNIX ,SOCK_DGRAM ,0 , sk); +SAFE_SETSOCKOPT (sk[1 ],SOL_SOCKET ,SO_ATTACH_BPF , + &prog_fd,sizeof (prog_fd)); + +SAFE_WRITE (1 , sk[0 ], msg,sizeof (MSG )); + +memset (attr,0 ,sizeof (*attr)); + attr->map_fd = map_fd; + attr->key =ptr_to_u64 (key); + attr->value =ptr_to_u64 (val); + *key =0 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT +1 ) { + tst_res(TFAIL, + "val = %lu, but should be val = %lu + 1",
Quoted 8 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c line 145
Quoted 4 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +4
-0+ *val, A64INT); + }else { +tst_res (TPASS ,"val = %lu + 1" ,A64INT ); + }Wrong indentation, also I do not fancy this if else maze. I guess that we can can safely do goto exit; if map lookup fails that would point right before the
SAFE_CLOSE()block.
OK!
Quoted 16 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c line 158
Quoted 13 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +12
-0+ *key =1 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT -1 ) { +tst_res (TFAIL , +"val = %lu, but should be val = %lu - 1" , + *val,A64INT ); + }else { +tst_res (TPASS ,"val = %lu - 1" ,A64INT ); + }Here as well.
OK!
Quoted 27 lines
Quoted 27 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +23
-0+SAFE_CLOSE (prog_fd); +SAFE_CLOSE (map_fd); +SAFE_CLOSE (sk[0 ]); +SAFE_CLOSE (sk[1 ]); +} + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .min_kver ="3.18" , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {} + }, + .bufs = (struct tst_buffers []) { + {&key, .size =sizeof (*key)}, + {&val, .size =sizeof (*val)}, + {&log, .size =BUFSIZ }, + {&attr, .size =sizeof (*attr)}, + {&msg, .size =sizeof (MSG )}, + {}, + } +};Signature · 1 line
2.22.1
Signature · 2 lines
Thank you, Richard.
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
In reply to Richard PalethorpeHi,
agree with comments from Cyril. And add two tiny one.
Quoted 204 lines · Richard Palethorpe
Signed-off-by Richard Palethorpe <[email protected]>Reviewed-by Jan Stancek <[email protected]>Reviewed-by Cyril Hrubis <[email protected]>---
V3: Rebased on master and included line numbers in instructions
Capability patch has not been applied to master at time of rebase.
include/lapi/bpf.h | 27 +++ runtest/syscalls | 1 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/bpf_prog02.c | 182 +++++++++++++++++++++ 4 files changed, 211 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog02.cinclude/lapi/bpf.h +11
-0@@ -18,7 +18,9 @@/* Start copy from linux/bpf_(common).h */ #define BPF_CLASS (code) ((code) & 0x07)#define BPF_LD 0x00 +#define BPF_LDX 0x01#define BPF_ST 0x02 +#define BPF_STX 0x03#define BPF_JMP 0x05#define BPF_SIZE (code) ((code) & 0x18)@@ -30,6 +32,7 @@#define BPF_OP (code) ((code) & 0xf0)#define BPF_ADD 0x00 +#define BPF_SUB 0x10#define BPF_JEQ 0x10@@ -432,6 +435,14 @@ enum bpf_func_id {/* Start copy from tools/include/filter.h */ +#define BPF_ALU64_REG (OP ,DST ,SRC ) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) +#define
BPF_ALU64_IMM(OP, DST, IMM)\
((structbpf_insn) {
\include/lapi/bpf.h +4
-0.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \@@ -477,6 +488,22 @@ enum bpf_func_id { .off = OFF, \ .imm = IMM }) +#define BPF_LDX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST,\
include/lapi/bpf.h +1
-0+ .src_reg = SRC,\
include/lapi/bpf.h +1
-0+ .off = OFF,\
include/lapi/bpf.h +4
-0+ .imm = 0 }) + +#define BPF_STX_MEM (SIZE ,DST ,SRC ,OFF ) \ + ((struct bpf_insn) {\
include/lapi/bpf.h +1
-0+ .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM,\
include/lapi/bpf.h +1
-0+ .dst_reg = DST,\
include/lapi/bpf.h +1
-0+ .src_reg = SRC,\
include/lapi/bpf.h +1
-0+ .off = OFF,\
include/lapi/bpf.h +2
-0+ .imm = 0 }) +#define BPF_JMP_IMM (OP ,DST ,IMM ,OFF )\
((structbpf_insn) {
\
.code =BPF_JMP|BPF_OP(OP)|BPF_K,
\runtest/syscalls +1
-0@@ -34,6 +34,7 @@ bind03 bind03 bpf_map01 bpf_map01 bpf_prog01 bpf_prog01 +bpf_prog02 bpf_prog02 brk01 brk01testcases/kernel/syscalls/bpf/.gitignore +1
-0@@ -1,2 +1,3 @@ bpf_map01 bpf_prog01 +bpf_prog02testcases/kernel/syscalls/bpf/bpf_prog02.c new file +5
-0@@ -0,0 +1,182 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2019 Richard Palethorpe <[email protected]> +* +* Check if eBPF can do arithmetic with 64bits. This targets a specific
+ * regression which only effects unprivileged users who are subject to extra
+ * pointer arithmetic checks during verification.
+ *
+ * Fixed by commit3612af783cf52c74a031a2f11b82247b2599d3cd.
+ * https://new.blog.cloudflare.com/ebpf-cant-count/
+ *
+ * This test is very similar in structure tobpf_prog01which is better
+ * annotated.
+ */testcases/kernel/syscalls/bpf/bpf_prog02.c new file +26
-0+ +#include <limits.h> +#include <string.h> +#include <stdio.h> + +#include "config.h" +#include "tst_test.h" +#include "tst_capability.h" +#include "lapi/socket.h" +#include "lapi/bpf.h" + +#define A64INT (((uint64_t)1) << 60) + +const char MSG [] ="Ahoj!" ; +static char *msg; + +static char *log; +static uint32_t *key; +static uint64_t *val; +static union bpf_attr *attr; + +static int load_prog (int fd) +{ +struct bpf_insn *prog; +struct bpf_insn insn[] = { +BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +2
-0+ + BPF_LD_MAP_FD(BPF_REG_1, fd), /* 1:r1 = &fd */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 3: r2 =fp */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), /* 4: r2 =r2 - 8 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 0), /* 5: *r2 =0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),/* 6:
map_lookup_elem*/testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 17), /* 7:if(!r0) goto 25 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_MOV64_REG(BPF_REG_3, BPF_REG_0), /* 8: r3 =r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_LD_IMM64(BPF_REG_4, A64INT), /* 9: r4 =2^61 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_6), /* 11:r4 += r6 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 12:*r3 = r4 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +2
-0+ + BPF_LD_MAP_FD(BPF_REG_1, fd), /* 13:r1 = &fd */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* 15: r2 =fp */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), /* 16: r2 =r2 - 8 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ST_MEM(BPF_DW, BPF_REG_2, 0, 1), /* 17: *r2 =1 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),/* 18:
map_lookup_elem*/testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 5), /* 19:if(!r0) goto 25 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_MOV64_REG(BPF_REG_3, BPF_REG_0), /* 20: r3 =r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_LD_IMM64(BPF_REG_4, A64INT), /* 21: r4 =
Quoted 1 line
2^61 */
^
I think 2^61 is different to 1<<60
Quoted 79 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_ALU64_REG(BPF_SUB, BPF_REG_4, BPF_REG_6), /* 23:r4 -= r6 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 24:*r3 = r4 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +2
-0+ + BPF_MOV64_IMM(BPF_REG_0, 0), /* 25: r0 =0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +1
-0+ BPF_EXIT_INSN(), /* 26:return r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +21
-0+ }; + +/* Leaks memory when -i is specified */ + prog =tst_alloc (sizeof (insn)); +memcpy (prog, insn,sizeof (insn)); + +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (prog); + attr->insn_cnt =ARRAY_SIZE (insn); + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log); + attr->log_size =BUFSIZ ; + attr->log_level =1 ; + +TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (log[0 ] !=0 ) { +tst_res (TINFO ,"Verification log:" ); +fputs (log, stderr); + tst_brk(TBROK | TTERRNO, "Failedverification");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +2
-0+ }else { + tst_brk(TBROK | TTERRNO, "Failed to loadprogram");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +27
-0+ } + } + +return TST_RET ; +} + +static void setup (void ) +{ +memcpy (msg,MSG ,sizeof (MSG )); +} + +static void run (void ) +{ +int map_fd, prog_fd; +int sk[2 ]; + +memset (attr,0 ,sizeof (*attr)); + attr->map_type =BPF_MAP_TYPE_ARRAY ; + attr->key_size =4 ; + attr->value_size =8 ; + attr->max_entries =2 ; + +TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (TST_ERR ==EPERM ) { + tst_brk(TCONF | TTERRNO, + "bpf() requires CAP_SYS_ADMIN onthis system");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +2
-0+ }else { + tst_brk(TBROK | TTERRNO, "Failed to createarray map");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +14
-0+ } + } + map_fd =TST_RET ; + + prog_fd =load_prog (map_fd); + +SAFE_SOCKETPAIR (AF_UNIX ,SOCK_DGRAM ,0 , sk); +SAFE_SETSOCKOPT (sk[1 ],SOL_SOCKET ,SO_ATTACH_BPF , + &prog_fd,sizeof (prog_fd)); + +SAFE_WRITE (1 , sk[0 ], msg,sizeof (MSG )); + +memset (attr,0 ,sizeof (*attr)); + attr->map_fd = map_fd;
testcases/kernel/syscalls/bpf/bpf_prog02.c line 141
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+ attr->key =ptr_to_u64 (key); + attr->value =ptr_to_u64 (val); + *key =0 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT +1 ) { + tst_res(TFAIL, + "val = %lu, but should be val = %lu + 1",
^
Not sure if it is really needed but I would use %llu here. If so, other places as well.
Quoted 45 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +41
-0+ *val, A64INT); + }else { +tst_res (TPASS ,"val = %lu + 1" ,A64INT ); + } + + *key =1 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT -1 ) { +tst_res (TFAIL , +"val = %lu, but should be val = %lu - 1" , + *val,A64INT ); + }else { +tst_res (TPASS ,"val = %lu - 1" ,A64INT ); + } + +SAFE_CLOSE (prog_fd); +SAFE_CLOSE (map_fd); +SAFE_CLOSE (sk[0 ]); +SAFE_CLOSE (sk[1 ]); +} + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .min_kver ="3.18" , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {} + }, + .bufs = (struct tst_buffers []) { + {&key, .size =sizeof (*key)}, + {&val, .size =sizeof (*val)}, + {&log, .size =BUFSIZ }, + {&attr, .size =sizeof (*attr)}, + {&msg, .size =sizeof (MSG )}, + {}, + } +};Signature · 1 line
2.22.1
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
Hi!
Quoted 4 lines
Quoted 2 lines
(quoted diff) +1
-0+ BPF_LD_IMM64(BPF_REG_4, A64INT), /* 21: r4 =2^61 */
^
I think 2^61 is different to 1<<60
Agreed, 1<<60 is 2^60
Quoted 90 lines
Quoted 79 lines
(quoted diff) +1
-0+ BPF_ALU64_REG(BPF_SUB, BPF_REG_4, BPF_REG_6), /* 23:r4 -= r6 */
(quoted diff) +1
-0+ BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 24:*r3 = r4 */
(quoted diff) +2
-0+ + BPF_MOV64_IMM(BPF_REG_0, 0), /* 25: r0 =0 */
(quoted diff) +1
-0+ BPF_EXIT_INSN(), /* 26:return r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +21
-0+ }; + +/* Leaks memory when -i is specified */ + prog =tst_alloc (sizeof (insn)); +memcpy (prog, insn,sizeof (insn)); + +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (prog); + attr->insn_cnt =ARRAY_SIZE (insn); + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log); + attr->log_size =BUFSIZ ; + attr->log_level =1 ; + +TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (log[0 ] !=0 ) { +tst_res (TINFO ,"Verification log:" ); +fputs (log, stderr); + tst_brk(TBROK | TTERRNO, "Failedverification");
(quoted diff) +2
-0+ } else { + tst_brk(TBROK | TTERRNO, "Failed to loadprogram");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +27
-0+ } + } + +return TST_RET ; +} + +static void setup (void ) +{ +memcpy (msg,MSG ,sizeof (MSG )); +} + +static void run (void ) +{ +int map_fd, prog_fd; +int sk[2 ]; + +memset (attr,0 ,sizeof (*attr)); + attr->map_type =BPF_MAP_TYPE_ARRAY ; + attr->key_size =4 ; + attr->value_size =8 ; + attr->max_entries =2 ; + +TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (TST_ERR ==EPERM ) { + tst_brk(TCONF | TTERRNO, + "bpf() requires CAP_SYS_ADMIN onthis system");
(quoted diff) +2
-0+ } else { + tst_brk(TBROK | TTERRNO, "Failed to createarray map");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +14
-0+ } + } + map_fd =TST_RET ; + + prog_fd =load_prog (map_fd); + +SAFE_SOCKETPAIR (AF_UNIX ,SOCK_DGRAM ,0 , sk); +SAFE_SETSOCKOPT (sk[1 ],SOL_SOCKET ,SO_ATTACH_BPF , + &prog_fd,sizeof (prog_fd)); + +SAFE_WRITE (1 , sk[0 ], msg,sizeof (MSG )); + +memset (attr,0 ,sizeof (*attr)); + attr->map_fd = map_fd;testcases/kernel/syscalls/bpf/bpf_prog02.c line 141
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+ attr->key =ptr_to_u64 (key); + attr->value =ptr_to_u64 (val); + *key =0 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT +1 ) { + tst_res(TFAIL, + "val = %lu, but should be val = %lu + 1",^
Quoted 2 lines
Not sure if it is really needed but I would use %llu here. If so, other places as well.
Technically we should be using PRIu64 which expands to %lu on 64 bit and to %llu on 32 bit. But given that sizeof(long long) == sizeof(long) on 64 bit on Linux %llu should work as well.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
Hello,
Quoted 101 lines · Cyril Hrubis
Hi!
Quoted 4 lines
Quoted 2 lines
(quoted diff) +1
-0+ BPF_LD_IMM64(BPF_REG_4, A64INT), /* 21: r4 =2^61 */
^
I think 2^61 is different to 1<<60Agreed, 1<<60 is 2^60
Quoted 90 lines
Quoted 79 lines
(quoted diff) +1
-0+ BPF_ALU64_REG(BPF_SUB, BPF_REG_4, BPF_REG_6), /* 23:r4 -= r6 */
(quoted diff) +1
-0+ BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_4, 0), /* 24:*r3 = r4 */
(quoted diff) +2
-0+ + BPF_MOV64_IMM(BPF_REG_0, 0), /* 25: r0 =0 */
(quoted diff) +1
-0+ BPF_EXIT_INSN(), /* 26:return r0 */
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +21
-0+ }; + +/* Leaks memory when -i is specified */ + prog =tst_alloc (sizeof (insn)); +memcpy (prog, insn,sizeof (insn)); + +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (prog); + attr->insn_cnt =ARRAY_SIZE (insn); + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log); + attr->log_size =BUFSIZ ; + attr->log_level =1 ; + +TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (log[0 ] !=0 ) { +tst_res (TINFO ,"Verification log:" ); +fputs (log, stderr); + tst_brk(TBROK | TTERRNO, "Failedverification");
(quoted diff) +2
-0+ } else { + tst_brk(TBROK | TTERRNO, "Failed to loadprogram");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +27
-0+ } + } + +return TST_RET ; +} + +static void setup (void ) +{ +memcpy (msg,MSG ,sizeof (MSG )); +} + +static void run (void ) +{ +int map_fd, prog_fd; +int sk[2 ]; + +memset (attr,0 ,sizeof (*attr)); + attr->map_type =BPF_MAP_TYPE_ARRAY ; + attr->key_size =4 ; + attr->value_size =8 ; + attr->max_entries =2 ; + +TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +if (TST_ERR ==EPERM ) { + tst_brk(TCONF | TTERRNO, + "bpf() requires CAP_SYS_ADMIN onthis system");
(quoted diff) +2
-0+ } else { + tst_brk(TBROK | TTERRNO, "Failed to createarray map");
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +14
-0+ } + } + map_fd =TST_RET ; + + prog_fd =load_prog (map_fd); + +SAFE_SOCKETPAIR (AF_UNIX ,SOCK_DGRAM ,0 , sk); +SAFE_SETSOCKOPT (sk[1 ],SOL_SOCKET ,SO_ATTACH_BPF , + &prog_fd,sizeof (prog_fd)); + +SAFE_WRITE (1 , sk[0 ], msg,sizeof (MSG )); + +memset (attr,0 ,sizeof (*attr)); + attr->map_fd = map_fd;testcases/kernel/syscalls/bpf/bpf_prog02.c line 141
Quoted 10 lines
testcases/kernel/syscalls/bpf/bpf_prog02.c new file +10
-0+ attr->key =ptr_to_u64 (key); + attr->value =ptr_to_u64 (val); + *key =0 ; + +TEST (bpf (BPF_MAP_LOOKUP_ELEM , attr,sizeof (*attr))); +if (TST_RET ==-1 ) { +tst_res (TFAIL |TTERRNO ,"array map lookup" ); + }else if (*val !=A64INT +1 ) { + tst_res(TFAIL, + "val = %lu, but should be val = %lu + 1",^Quoted 2 lines
Not sure if it is really needed but I would use %llu here. If so, other places as well.
Quoted 3 lines
Technically we should be using PRIu64 which expands to %lu on 64 bit and to %llu on 32 bit. But given that sizeof(long long) == sizeof(long) on 64 bit on Linux %llu should work as well.
Ack.
Signature · 2 lines
Thank you, Richard.
PATCH v3 1/1 BPF: Regression test for 64bit arithmetic
Hi!
Quoted 5 lines
Quoted 3 lines
Technically we should be using PRIu64 which expands to %lu on 64 bit and to %llu on 32 bit. But given that sizeof(long long) == sizeof(long) on 64 bit on Linux %llu should work as well.
Ack.
In the end I've pushed this with the PRIu64 since the compiler was throwing warnings for %llu on 64 bit.
Signature · 2 lines
Cyril Hrubis [email protected]