]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/linux/bpf_verifier.h
Merge tag 'staging-4.9-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[mirror_ubuntu-bionic-kernel.git] / include / linux / bpf_verifier.h
1 /* 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
13 /* 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)
17 #define BPF_REGISTER_MIN_RANGE -(1024 * 1024 * 1024)
18
19 struct bpf_reg_state {
20 enum bpf_reg_type type;
21 /*
22 * Used to determine if any memory access using this register will
23 * result in a bad access.
24 */
25 u64 min_value, max_value;
26 union {
27 /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
28 s64 imm;
29
30 /* valid when type == PTR_TO_PACKET* */
31 struct {
32 u32 id;
33 u16 off;
34 u16 range;
35 };
36
37 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
38 * PTR_TO_MAP_VALUE_OR_NULL
39 */
40 struct bpf_map *map_ptr;
41 };
42 };
43
44 enum bpf_stack_slot_type {
45 STACK_INVALID, /* nothing was stored in this stack slot */
46 STACK_SPILL, /* register spilled into stack */
47 STACK_MISC /* BPF program wrote some data into this slot */
48 };
49
50 #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
51
52 /* state of the program:
53 * type of all registers and stack info
54 */
55 struct bpf_verifier_state {
56 struct bpf_reg_state regs[MAX_BPF_REG];
57 u8 stack_slot_type[MAX_BPF_STACK];
58 struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
59 };
60
61 /* linked list of verifier states used to prune search */
62 struct bpf_verifier_state_list {
63 struct bpf_verifier_state state;
64 struct bpf_verifier_state_list *next;
65 };
66
67 struct bpf_insn_aux_data {
68 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
69 };
70
71 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
72
73 struct bpf_verifier_env;
74 struct bpf_ext_analyzer_ops {
75 int (*insn_hook)(struct bpf_verifier_env *env,
76 int insn_idx, int prev_insn_idx);
77 };
78
79 /* single container for all structs
80 * one verifier_env per bpf_check() call
81 */
82 struct bpf_verifier_env {
83 struct bpf_prog *prog; /* eBPF program being verified */
84 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
85 int stack_size; /* number of states to be processed */
86 struct bpf_verifier_state cur_state; /* current verifier state */
87 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
88 const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */
89 void *analyzer_priv; /* pointer to external analyzer's private data */
90 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
91 u32 used_map_cnt; /* number of used maps */
92 u32 id_gen; /* used to generate unique reg IDs */
93 bool allow_ptr_leaks;
94 bool seen_direct_write;
95 bool varlen_map_value_access;
96 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
97 };
98
99 int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
100 void *priv);
101
102 #endif /* _LINUX_BPF_VERIFIER_H */