blob: 04ece14e11d0cf5decc62a765f784312c252d9a4 [file] [log] [blame]
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -07001/* bpf.h
2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * Berkeley Packet Filter functions.
7 */
8
9#ifndef BPF_H
10#define BPF_H
11
12#include <asm/bitsperlong.h> /* for __BITS_PER_LONG */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070013#include <linux/audit.h>
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070014#include <linux/filter.h>
15#include <stddef.h>
16#include <sys/user.h>
17
18#if __BITS_PER_LONG == 32
19#define BITS32
20#elif __BITS_PER_LONG == 64
21#define BITS64
22#endif
23
24/* Constants for comparison operators. */
25#define MIN_OPERATOR 128
26enum operator {
27 EQ = MIN_OPERATOR,
28 NE,
29 LT,
30 LE,
31 GT,
32 GE
33};
34
35/*
36 * BPF return values and data structures,
37 * since they're not yet in the kernel.
38 */
39#define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */
40#define SECCOMP_RET_TRAP 0x00030000U /* return SIGSYS */
41#define SECCOMP_RET_ERRNO 0x00050000U /* return -1 and set errno */
42#define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
43
44#define SECCOMP_RET_DATA 0x0000ffffU /* mask for return value */
45
46struct seccomp_data {
47 int nr;
48 __u32 arch;
49 __u64 instruction_pointer;
50 __u64 args[6];
51};
52
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -070053#define syscall_nr (offsetof(struct seccomp_data, nr))
54#define arch_nr (offsetof(struct seccomp_data, arch))
55
56#if defined(__i386__)
57#define ARCH_NR AUDIT_ARCH_I386
58#elif defined(__x86_64__)
59#define ARCH_NR AUDIT_ARCH_X86_64
60#elif defined(__arm__)
61/*
62 * <linux/audit.h> includes <linux/elf-em.h>, which does not include EM_ARM.
63 * <linux/elf.h> only includes <asm/elf.h> if we're in the kernel.
64 */
65# ifndef EM_ARM
66# define EM_ARM 40
67# endif
68#define ARCH_NR AUDIT_ARCH_ARM
69#else
70#error "AUDIT_ARCH value unavailable"
71#endif
72
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -070073/* Size-dependent defines. */
74#if defined(BITS32)
75/* On 32 bits, comparisons take 2 instructions: 1 load arg, and 1 cmp. */
76#define BPF_LOAD_ARG_LEN 1U
77#define BPF_COMP_LEN 1U
78#define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
79
80#define bpf_comp_jeq bpf_comp_jeq32
81
82#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
83
84#elif __BITS_PER_LONG == 64
85#define BITS64
86/* On 64 bits, comparisons take 7 instructions: 4 load arg, and 3 cmp. */
87#define BPF_LOAD_ARG_LEN 4U
88#define BPF_COMP_LEN 3U
89#define BPF_ARG_COMP_LEN (BPF_LOAD_ARG_LEN + BPF_COMP_LEN)
90
91#define bpf_comp_jeq bpf_comp_jeq64
92
93/* Ensure that we load the logically correct offset. */
94#if defined(__LITTLE_ENDIAN)
95#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
96#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
97#elif defined(__BIG_ENDIAN)
98#define LO_ARG(idx) offsetof(struct seccomp_data, args[(idx)]) + sizeof(__u32)
99#define HI_ARG(idx) offsetof(struct seccomp_data, args[(idx)])
100#else
101#error "Unknown endianness"
102#endif
103
104#endif
105
106/* Common jump targets. */
107#define NEXT 0
108#define SKIP 1
109#define SKIPN(_n) (_n)
110
111/* Support for labels in BPF programs. */
112#define JUMP_JT 0xff
113#define JUMP_JF 0xff
114#define LABEL_JT 0xfe
115#define LABEL_JF 0xfe
116
117#define MAX_BPF_LABEL_LEN 32
118
119#define BPF_LABELS_MAX 256
120struct bpf_labels {
121 int count;
122 struct __bpf_label {
123 const char *label;
124 unsigned int location;
125 } labels[BPF_LABELS_MAX];
126};
127
128/* BPF instruction manipulation functions and macros. */
129inline size_t set_bpf_instr(struct sock_filter *instr,
130 unsigned short code, unsigned int k,
131 unsigned char jt, unsigned char jf);
132
133#define set_bpf_stmt(_block, _code, _k) \
134 set_bpf_instr((_block), (_code), (_k), 0, 0)
135
136#define set_bpf_jump(_block, _code, _k, _jt, _jf) \
137 set_bpf_instr((_block), (_code), (_k), (_jt), (_jf))
138
139#define set_bpf_lbl(_block, _lbl_id) \
140 set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
141 LABEL_JT, LABEL_JF)
142
143#define set_bpf_jump_lbl(_block, _lbl_id) \
144 set_bpf_jump((_block), BPF_JMP+BPF_JA, (_lbl_id), \
145 JUMP_JT, JUMP_JF)
146
147#define set_bpf_ret_kill(_block) \
148 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_KILL)
149
150#define set_bpf_ret_errno(_block, _errno) \
151 set_bpf_stmt((_block), BPF_RET+BPF_K, \
152 SECCOMP_RET_ERRNO | ((_errno) & SECCOMP_RET_DATA))
153
154#define set_bpf_ret_allow(_block) \
155 set_bpf_stmt((_block), BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
156
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700157#define bpf_load_syscall_nr(_filter) \
158 set_bpf_stmt((_filter), BPF_LD+BPF_W+BPF_ABS, syscall_nr)
159
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700160/* BPF label functions. */
161int bpf_resolve_jumps(struct bpf_labels *labels,
162 struct sock_filter *filter, size_t count);
163int bpf_label_id(struct bpf_labels *labels, const char *label);
164void free_label_strings(struct bpf_labels *labels);
165
166/* BPF helper functions. */
167size_t bpf_load_arg(struct sock_filter *filter, int argidx);
168size_t bpf_comp_jeq(struct sock_filter *filter, unsigned long c,
169 unsigned char jt, unsigned char jf);
170
171/* Functions called by syscall_filter.c */
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700172#define ARCH_VALIDATION_LEN 3U
173#define ALLOW_SYSCALL_LEN 2U
174
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700175size_t bpf_arg_comp(struct sock_filter **pfilter,
176 int op, int argidx, unsigned long c, unsigned int label_id);
Jorge Lucangeli Obesd4467262012-03-23 16:19:59 -0700177size_t bpf_validate_arch(struct sock_filter *filter);
178size_t bpf_allow_syscall(struct sock_filter *filter, int nr);
179size_t bpf_allow_syscall_args(struct sock_filter *filter,
180 int nr, unsigned int id);
Jorge Lucangeli Obesfc8ab532012-03-20 10:14:31 -0700181
182/* Debug */
183void dump_bpf_prog(struct sock_fprog *fprog);
184void dump_bpf_filter(struct sock_filter *filter, unsigned short len);
185
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700186#endif /* BPF_H */