blob: c5cb661712c926bab5c4470de4919d754629a2b7 [file] [log] [blame]
Jakub Kicinski58e2af8b2016-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
13struct bpf_reg_state {
14 enum bpf_reg_type type;
15 union {
16 /* valid when type == CONST_IMM | PTR_TO_STACK | UNKNOWN_VALUE */
17 s64 imm;
18
19 /* valid when type == PTR_TO_PACKET* */
20 struct {
21 u32 id;
22 u16 off;
23 u16 range;
24 };
25
26 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
27 * PTR_TO_MAP_VALUE_OR_NULL
28 */
29 struct bpf_map *map_ptr;
30 };
31};
32
33enum bpf_stack_slot_type {
34 STACK_INVALID, /* nothing was stored in this stack slot */
35 STACK_SPILL, /* register spilled into stack */
36 STACK_MISC /* BPF program wrote some data into this slot */
37};
38
39#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
40
41/* state of the program:
42 * type of all registers and stack info
43 */
44struct bpf_verifier_state {
45 struct bpf_reg_state regs[MAX_BPF_REG];
46 u8 stack_slot_type[MAX_BPF_STACK];
47 struct bpf_reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE];
48};
49
50/* linked list of verifier states used to prune search */
51struct bpf_verifier_state_list {
52 struct bpf_verifier_state state;
53 struct bpf_verifier_state_list *next;
54};
55
56struct bpf_insn_aux_data {
57 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
58};
59
60#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
61
Jakub Kicinski13a27df2016-09-21 11:43:58 +010062struct bpf_verifier_env;
63struct bpf_ext_analyzer_ops {
64 int (*insn_hook)(struct bpf_verifier_env *env,
65 int insn_idx, int prev_insn_idx);
66};
67
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010068/* single container for all structs
69 * one verifier_env per bpf_check() call
70 */
71struct bpf_verifier_env {
72 struct bpf_prog *prog; /* eBPF program being verified */
73 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
74 int stack_size; /* number of states to be processed */
75 struct bpf_verifier_state cur_state; /* current verifier state */
76 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
Jakub Kicinski13a27df2016-09-21 11:43:58 +010077 const struct bpf_ext_analyzer_ops *analyzer_ops; /* external analyzer ops */
78 void *analyzer_priv; /* pointer to external analyzer's private data */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010079 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
80 u32 used_map_cnt; /* number of used maps */
81 u32 id_gen; /* used to generate unique reg IDs */
82 bool allow_ptr_leaks;
83 bool seen_direct_write;
84 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
85};
86
Jakub Kicinski13a27df2016-09-21 11:43:58 +010087int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
88 void *priv);
89
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010090#endif /* _LINUX_BPF_VERIFIER_H */