blob: 070fc49e39e2c25568e92498c2b2dc1a3c3c06a7 [file] [log] [blame]
Jakub Kicinski58e2af82016-09-21 11:43:57 +01001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#ifndef _LINUX_BPF_VERIFIER_H
8#define _LINUX_BPF_VERIFIER_H 1
9
10#include <linux/bpf.h> /* for enum bpf_reg_type */
11#include <linux/filter.h> /* for MAX_BPF_STACK */
12
Josef Bacik48461132016-09-28 10:54:32 -040013 /* Just some arbitrary values so we can safely do math without overflowing and
14 * are obviously wrong for any sort of memory access.
15 */
16#define BPF_REGISTER_MAX_RANGE (1024 * 1024 * 1024)
Josef Bacikf23cc642016-11-14 15:45:36 -050017#define BPF_REGISTER_MIN_RANGE -1
Josef Bacik48461132016-09-28 10:54:32 -040018
Jakub Kicinski58e2af82016-09-21 11:43:57 +010019struct bpf_reg_state {
20 enum bpf_reg_type type;
21 union {
22 /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
23 s64 imm;
24
25 /* valid when type == PTR_TO_PACKET* */
26 struct {
Jakub Kicinski58e2af82016-09-21 11:43:57 +010027 u16 off;
28 u16 range;
29 };
30
31 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
32 * PTR_TO_MAP_VALUE_OR_NULL
33 */
34 struct bpf_map *map_ptr;
35 };
Alexei Starovoitovb7f5aa12016-12-07 10:57:59 -080036 u32 id;
37 /* Used to determine if any memory access using this register will
38 * result in a bad access. These two fields must be last.
39 * See states_equal()
40 */
41 s64 min_value;
42 u64 max_value;
Daniel Borkmannbf5b91b2017-07-21 00:00:21 +020043 bool value_from_signed;
Jakub Kicinski58e2af82016-09-21 11:43:57 +010044};
45
46enum bpf_stack_slot_type {
47 STACK_INVALID, /* nothing was stored in this stack slot */
48 STACK_SPILL, /* register spilled into stack */
49 STACK_MISC /* BPF program wrote some data into this slot */
50};
51
52#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
53
54/* state of the program:
55 * type of all registers and stack info
56 */
57struct bpf_verifier_state {
58 struct bpf_reg_state regs[MAX_BPF_REG];
59 u8 stack_slot_type[MAX_BPF_STACK];
60 struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
61};
62
63/* linked list of verifier states used to prune search */
64struct bpf_verifier_state_list {
65 struct bpf_verifier_state state;
66 struct bpf_verifier_state_list *next;
67};
68
69struct bpf_insn_aux_data {
Alexei Starovoitova9bfac142018-01-07 17:33:02 -080070 union {
71 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
72 struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */
73 };
Daniel Borkmann7b5b73e2017-12-22 16:29:03 +010074 bool seen; /* this insn was processed by the verifier */
Jakub Kicinski58e2af82016-09-21 11:43:57 +010075};
76
77#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
78
Jakub Kicinski13a27df2016-09-21 11:43:58 +010079struct bpf_verifier_env;
80struct bpf_ext_analyzer_ops {
81 int (*insn_hook)(struct bpf_verifier_env *env,
82 int insn_idx, int prev_insn_idx);
83};
84
Jakub Kicinski58e2af82016-09-21 11:43:57 +010085/* single container for all structs
86 * one verifier_env per bpf_check() call
87 */
88struct bpf_verifier_env {
89 struct bpf_prog *prog; /* eBPF program being verified */
90 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
91 int stack_size; /* number of states to be processed */
92 struct bpf_verifier_state cur_state; /* current verifier state */
93 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
Jakub Kicinski13a27df2016-09-21 11:43:58 +010094 const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */
95 void *analyzer_priv; /* pointer to external analyzer's private data */
Jakub Kicinski58e2af82016-09-21 11:43:57 +010096 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
97 u32 used_map_cnt; /* number of used maps */
98 u32 id_gen; /* used to generate unique reg IDs */
99 bool allow_ptr_leaks;
100 bool seen_direct_write;
Josef Bacik48461132016-09-28 10:54:32 -0400101 bool varlen_map_value_access;
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100102 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
103};
104
Jakub Kicinski13a27df2016-09-21 11:43:58 +0100105int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
106 void *priv);
107
Jakub Kicinski58e2af82016-09-21 11:43:57 +0100108#endif /* _LINUX_BPF_VERIFIER_H */