linux-stable/tools/testing/selftests/bpf
Jiong Wang e434b8cdf7 bpf: relax verifier restriction on BPF_MOV | BPF_ALU
Currently, the destination register is marked as unknown for 32-bit
sub-register move (BPF_MOV | BPF_ALU) whenever the source register type is
SCALAR_VALUE.

This is too conservative that some valid cases will be rejected.
Especially, this may turn a constant scalar value into unknown value that
could break some assumptions of verifier.

For example, test_l4lb_noinline.c has the following C code:

    struct real_definition *dst

1:  if (!get_packet_dst(&dst, &pckt, vip_info, is_ipv6))
2:    return TC_ACT_SHOT;
3:
4:  if (dst->flags & F_IPV6) {

get_packet_dst is responsible for initializing "dst" into valid pointer and
return true (1), otherwise return false (0). The compiled instruction
sequence using alu32 will be:

  412: (54) (u32) r7 &= (u32) 1
  413: (bc) (u32) r0 = (u32) r7
  414: (95) exit

insn 413, a BPF_MOV | BPF_ALU, however will turn r0 into unknown value even
r7 contains SCALAR_VALUE 1.

This causes trouble when verifier is walking the code path that hasn't
initialized "dst" inside get_packet_dst, for which case 0 is returned and
we would then expect verifier concluding line 1 in the above C code pass
the "if" check, therefore would skip fall through path starting at line 4.
Now, because r0 returned from callee has became unknown value, so verifier
won't skip analyzing path starting at line 4 and "dst->flags" requires
dereferencing the pointer "dst" which actually hasn't be initialized for
this path.

This patch relaxed the code marking sub-register move destination. For a
SCALAR_VALUE, it is safe to just copy the value from source then truncate
it into 32-bit.

A unit test also included to demonstrate this issue. This test will fail
before this patch.

This relaxation could let verifier skipping more paths for conditional
comparison against immediate. It also let verifier recording a more
accurate/strict value for one register at one state, if this state end up
with going through exit without rejection and it is used for state
comparison later, then it is possible an inaccurate/permissive value is
better. So the real impact on verifier processed insn number is complex.
But in all, without this fix, valid program could be rejected.

>From real benchmarking on kernel selftests and Cilium bpf tests, there is
no impact on processed instruction number when tests ares compiled with
default compilation options. There is slightly improvements when they are
compiled with -mattr=+alu32 after this patch.

Also, test_xdp_noinline/-mattr=+alu32 now passed verification. It is
rejected before this fix.

Insn processed before/after this patch:

                        default     -mattr=+alu32

Kernel selftest

===
test_xdp.o              371/371      369/369
test_l4lb.o             6345/6345    5623/5623
test_xdp_noinline.o     2971/2971    rejected/2727
test_tcp_estates.o      429/429      430/430

Cilium bpf
===
bpf_lb-DLB_L3.o:        2085/2085     1685/1687
bpf_lb-DLB_L4.o:        2287/2287     1986/1982
bpf_lb-DUNKNOWN.o:      690/690       622/622
bpf_lxc.o:              95033/95033   N/A
bpf_netdev.o:           7245/7245     N/A
bpf_overlay.o:          2898/2898     3085/2947

NOTE:
  - bpf_lxc.o and bpf_netdev.o compiled by -mattr=+alu32 are rejected by
    verifier due to another issue inside verifier on supporting alu32
    binary.
  - Each cilium bpf program could generate several processed insn number,
    above number is sum of them.

v1->v2:
 - Restrict the change on SCALAR_VALUE.
 - Update benchmark numbers on Cilium bpf tests.

Signed-off-by: Jiong Wang <jiong.wang@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-12-10 09:23:33 -08:00
..
gnu selftests/bpf: get rid of -D__x86_64__ 2017-05-03 09:51:25 -04:00
include/uapi/linux License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
.gitignore selftests/bpf: add missing executables to .gitignore 2018-10-19 13:31:27 -07:00
Makefile Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next 2018-11-26 13:08:17 -08:00
bpf_endian.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
bpf_flow.c selftests/bpf: rename flow dissector section to flow_dissector 2018-11-10 15:56:10 -08:00
bpf_helpers.h media: bpf: add bpf function to report mouse movement 2018-12-09 14:37:18 -08:00
bpf_rand.h bpf: add ld64 imm test cases 2018-05-14 19:11:45 -07:00
bpf_rlimit.h bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
bpf_util.h bpf: Refactor ARRAY_SIZE macro to bpf_util.h 2018-08-11 01:58:46 +02:00
cgroup_helpers.c selftests/bpf: Fix const'ness in cgroup_helpers 2018-07-15 00:08:41 +02:00
cgroup_helpers.h selftests/bpf: Fix const'ness in cgroup_helpers 2018-07-15 00:08:41 +02:00
config selftests/bpf: add config fragment CONFIG_FTRACE_SYSCALLS 2018-11-28 22:13:06 +01:00
connect4_prog.c selftest/bpf: Use bpf_sk_lookup_{tcp, udp} in test_sock_addr 2018-11-16 17:54:29 -08:00
connect6_prog.c selftest/bpf: Use bpf_sk_lookup_{tcp, udp} in test_sock_addr 2018-11-16 17:54:29 -08:00
dev_cgroup.c selftests/bpf: add a test for device cgroup controller 2017-11-05 23:26:51 +09:00
flow_dissector_load.c tools/bpf: add unlimited rlimit for flow_dissector_load 2018-10-30 23:31:21 +01:00
get_cgroup_id_kern.c tools/bpf: fix selftest get_cgroup_id_user 2018-06-08 00:10:07 +02:00
get_cgroup_id_user.c tools/bpf: fix selftest get_cgroup_id_user 2018-06-08 00:10:07 +02:00
netcnt_common.h selftests/bpf: cgroup local storage-based network counters 2018-10-01 16:18:33 +02:00
netcnt_prog.c selftests/bpf: cgroup local storage-based network counters 2018-10-01 16:18:33 +02:00
sample_map_ret0.c selftest/bpf: extend the offload test with map checks 2018-01-18 22:54:26 +01:00
sample_ret0.c selftests/bpf: add offload test based on netdevsim 2017-12-03 00:27:58 +01:00
sendmsg4_prog.c selftests/bpf: Selftest for sys_sendmsg hooks 2018-05-28 17:41:05 +02:00
sendmsg6_prog.c selftests/bpf: Selftest for sys_sendmsg hooks 2018-05-28 17:41:05 +02:00
socket_cookie_prog.c selftests/bpf: Test for get_socket_cookie 2018-07-31 09:33:48 +02:00
sockmap_parse_prog.c bpf: add map tests for BPF_PROG_TYPE_SK_MSG 2018-03-19 21:14:39 +01:00
sockmap_tcp_msg_prog.c bpf: add map tests for BPF_PROG_TYPE_SK_MSG 2018-03-19 21:14:39 +01:00
sockmap_verdict_prog.c bpf: add map tests for BPF_PROG_TYPE_SK_MSG 2018-03-19 21:14:39 +01:00
tcp_client.py bpf: Add Python 3 support to selftests scripts for bpf 2018-07-25 07:46:48 +02:00
tcp_server.py bpf: Add Python 3 support to selftests scripts for bpf 2018-07-25 07:46:48 +02:00
test_adjust_tail.c bpf: adding tests for bpf_xdp_adjust_tail 2018-04-18 23:34:17 +02:00
test_align.c bpf: Add BPF_F_ANY_ALIGNMENT. 2018-11-30 21:38:48 -08:00
test_btf.c bpf: Add unit tests for bpf_line_info 2018-12-09 13:54:38 -08:00
test_btf_haskv.c tools/bpf: enhance test_btf file testing to test func info 2018-11-20 10:54:39 -08:00
test_btf_nokv.c tools/bpf: enhance test_btf file testing to test func info 2018-11-20 10:54:39 -08:00
test_cgroup_storage.c selftests/bpf: extend the storage test to test per-cpu cgroup storage 2018-10-01 16:18:33 +02:00
test_dev_cgroup.c bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
test_flow_dissector.c selftests/bpf: test bpf flow dissection 2018-09-14 12:04:33 -07:00
test_flow_dissector.sh selftests/bpf: rename flow dissector section to flow_dissector 2018-11-10 15:56:10 -08:00
test_get_stack_rawtp.c tools/bpf: add a test for bpf_get_stack with raw tracepoint prog 2018-04-29 08:45:54 -07:00
test_iptunnel_common.h
test_kmod.sh selftests: bpf: notification about privilege required to run test_kmod.sh testing script 2018-06-22 00:30:02 +02:00
test_l4lb.c bpf: Move endianness BPF helpers out of bpf_util.h 2017-05-01 12:43:49 -07:00
test_l4lb_noinline.c selftests/bpf: add bpf_call test 2017-12-17 20:34:36 +01:00
test_libbpf.sh selftests/bpf: enable (uncomment) all tests in test_libbpf.sh 2018-11-07 22:20:56 +01:00
test_libbpf_open.c selftests/bpf: add test program for loading BPF ELF files 2018-02-09 00:24:38 +01:00
test_lirc_mode2.sh media: bpf: add bpf function to report mouse movement 2018-12-09 14:37:18 -08:00
test_lirc_mode2_kern.c media: bpf: add bpf function to report mouse movement 2018-12-09 14:37:18 -08:00
test_lirc_mode2_user.c media: bpf: add bpf function to report mouse movement 2018-12-09 14:37:18 -08:00
test_lpm_map.c bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
test_lru_map.c bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
test_lwt_seg6local.c selftests/bpf: test for seg6local End.BPF action 2018-05-24 11:57:36 +02:00
test_lwt_seg6local.sh selftests/bpf: update test_lwt_seg6local.sh according to iproute2 2018-08-03 10:19:33 +02:00
test_map_in_map.c bpf: adding tests for map_in_map helpber in libbpf 2018-11-21 23:33:22 +01:00
test_maps.c bpf: adding tests for map_in_map helpber in libbpf 2018-11-21 23:33:22 +01:00
test_netcnt.c kselftests/bpf: use ping6 as the default ipv6 ping binary when it exists 2018-11-09 10:55:09 +01:00
test_obj_id.c bpf: Fix test_obj_id.c for llvm 5.0 2017-06-09 15:15:11 -04:00
test_offload.py selftests/bpf: add test for sharing objects between netdevs 2018-07-18 15:10:34 +02:00
test_pkt_access.c selftests/bpf: fix broken build due to types.h 2017-05-17 18:45:14 -04:00
test_pkt_md_access.c bpf: fix selftest/bpf/test_pkt_md_access on s390x 2017-08-07 10:06:27 -07:00
test_progs.c selftests: add a test for bpf_prog_test_run_xattr 2018-12-04 08:18:13 -08:00
test_queue_map.c selftests/bpf: add test cases for queue and stack maps 2018-10-19 13:24:31 -07:00
test_queue_stack_map.h selftests/bpf: add test cases for queue and stack maps 2018-10-19 13:24:31 -07:00
test_section_names.c selftests/bpf: Test libbpf_{prog,attach}_type_by_name 2018-09-27 21:14:59 +02:00
test_select_reuseport.c bpf: Test BPF_PROG_TYPE_SK_REUSEPORT 2018-08-11 01:58:46 +02:00
test_select_reuseport_common.h bpf: Test BPF_PROG_TYPE_SK_REUSEPORT 2018-08-11 01:58:46 +02:00
test_select_reuseport_kern.c bpf: Test BPF_PROG_TYPE_SK_REUSEPORT 2018-08-11 01:58:46 +02:00
test_sk_lookup_kern.c selftests/bpf: Add C tests for reference tracking 2018-10-03 02:53:48 +02:00
test_skb_cgroup_id.sh kselftests/bpf: use ping6 as the default ipv6 ping binary if it exists 2018-10-31 23:05:30 +01:00
test_skb_cgroup_id_kern.c selftests/bpf: Selftest for bpf_skb_ancestor_cgroup_id 2018-08-13 01:02:40 +02:00
test_skb_cgroup_id_user.c selftests/bpf: Selftest for bpf_skb_ancestor_cgroup_id 2018-08-13 01:02:40 +02:00
test_sock.c bpf: Refactor ARRAY_SIZE macro to bpf_util.h 2018-08-11 01:58:46 +02:00
test_sock_addr.c selftests/bpf: Test narrow loads with off > 0 for bpf_sock_addr 2018-11-10 22:29:59 -08:00
test_sock_addr.sh kselftests/bpf: use ping6 as the default ipv6 ping binary if it exists 2018-10-31 23:05:30 +01:00
test_socket_cookie.c selftests/bpf: Use libbpf_attach_type_by_name in test_socket_cookie 2018-09-27 21:14:59 +02:00
test_sockhash_kern.c bpf: selftest additions for SOCKHASH 2018-05-16 22:00:12 +02:00
test_sockmap.c bpf: test_sockmap, add options for msg_pop_data() helper 2018-11-28 22:07:57 +01:00
test_sockmap_kern.c bpf: selftest additions for SOCKHASH 2018-05-16 22:00:12 +02:00
test_sockmap_kern.h bpf: test_sockmap, add options for msg_pop_data() helper 2018-11-28 22:07:57 +01:00
test_stack_map.c selftests/bpf: add test cases for queue and stack maps 2018-10-19 13:24:31 -07:00
test_stacktrace_build_id.c tools/bpf: add a test for bpf_get_stack with tracepoint prog 2018-04-29 08:45:54 -07:00
test_stacktrace_map.c tools/bpf: add a test for bpf_get_stack with tracepoint prog 2018-04-29 08:45:54 -07:00
test_tag.c bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
test_tcp_estats.c selftests/bpf: add a test case to check verifier pointer arithmetic 2017-05-03 09:51:25 -04:00
test_tcpbpf.h selftests/bpf: Test case for BPF_SOCK_OPS_TCP_LISTEN_CB 2018-07-15 00:08:41 +02:00
test_tcpbpf_kern.c bpf: add selftest for bpf's (set|get)_sockopt for SAVE_SYN 2018-09-01 01:37:00 +02:00
test_tcpbpf_user.c bpf: add selftest for bpf's (set|get)_sockopt for SAVE_SYN 2018-09-01 01:37:00 +02:00
test_tcpnotify.h selftests/bpf: add a test case for sock_ops perf-event notification 2018-11-09 09:40:17 +01:00
test_tcpnotify_kern.c selftests/bpf: add a test case for sock_ops perf-event notification 2018-11-09 09:40:17 +01:00
test_tcpnotify_user.c selftests/bpf: add a test case for sock_ops perf-event notification 2018-11-09 09:40:17 +01:00
test_tracepoint.c bpf/tracing: add a bpf test for new ioctl query interface 2017-12-12 08:46:40 -08:00
test_tunnel.sh bpf, selftests: delete xfrm tunnel when test exits. 2018-06-15 03:31:52 +02:00
test_tunnel_kern.c selftests/bpf: bpf tunnel test. 2018-04-27 00:11:14 +02:00
test_verifier.c bpf: relax verifier restriction on BPF_MOV | BPF_ALU 2018-12-10 09:23:33 -08:00
test_verifier_log.c bpf: unify rlimit handling in selftests 2018-02-26 20:11:23 -08:00
test_xdp.c selftests: bpf: Use bpf_endian.h in test_xdp.c 2017-05-02 07:52:01 -07:00
test_xdp_meta.c bpf: improve selftests and add tests for meta pointer 2017-09-26 13:36:44 -07:00
test_xdp_meta.sh tools/bpf: fix batch-mode test failure of test_xdp_redirect.sh 2018-02-06 11:34:42 +01:00
test_xdp_noinline.c selftests/bpf: add xdp noinline test 2017-12-17 20:34:36 +01:00
test_xdp_redirect.c selftests: bpf: add a test for XDP redirect 2017-08-08 18:12:50 -07:00
test_xdp_redirect.sh tools/bpf: fix batch-mode test failure of test_xdp_redirect.sh 2018-02-06 11:34:42 +01:00
test_xdp_vlan.c selftests/bpf: add XDP selftests for modifying and popping VLAN headers 2018-10-09 21:59:09 -07:00
test_xdp_vlan.sh selftests/bpf: add XDP selftests for modifying and popping VLAN headers 2018-10-09 21:59:09 -07:00
trace_helpers.c bpf, libbpf: simplify and cleanup perf ring buffer walk 2018-10-20 23:13:32 -07:00
trace_helpers.h trace_helpers.c: Add helpers to poll multiple perf FDs for events 2018-06-27 11:01:03 +02:00
urandom_read.c bpf: add selftest for stackmap with build_id in NMI context 2018-05-14 23:29:45 +02:00
with_addr.sh selftests/bpf: test bpf flow dissection 2018-09-14 12:04:33 -07:00
with_tunnels.sh selftests/bpf: test bpf flow dissection 2018-09-14 12:04:33 -07:00
xdp_dummy.c selftests: add dummy xdp test helper 2018-11-07 16:23:05 -08:00