-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
370 lines (330 loc) · 9.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use bs_system::{consts::*, Level, Name, Result, SetSocketOption, SocketOption, SystemError};
use libc::socklen_t;
use libc::{EOVERFLOW, SOL_SOCKET};
use log::debug;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive as FromVal;
use std::mem::size_of_val;
use std::os::unix::io::RawFd;
pub const OPTION_LEVEL: i32 = SOL_SOCKET;
pub const OPTION_NAME: i32 = 50; // SO_ATTACH_BPF;
/// `bpf_insn`
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct Instruction {
code: u8,
// Rust bitflags are no fun
regs: u8,
off: i16,
imm: i32,
}
#[doc(hidden)]
#[repr(u8)]
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Register {
Ret = 0,
Context = 1,
Arg1 = 2,
Arg2 = 3,
Arg3 = 4,
Arg4 = 5,
SocketBuffer = 6,
Gen1 = 7,
Gen2 = 8,
Gen3 = 9,
FramePointer = 10,
}
impl Register {
#[doc(hidden)]
#[allow(non_upper_case_globals)]
pub const None: Self = Self::Ret;
}
impl Instruction {
/// Creates a new `Instruction` with the given parameters
pub const fn new(code: u8, dst_reg: Register, src_reg: Register, off: i16, imm: i32) -> Self {
Self {
code,
regs: ((dst_reg as u8) << 4) | src_reg as u8,
off,
imm,
}
}
/// Helper function, creates a new `Instruction` with given `code`
/// other parameters (registers, `off`, `imm`) are set to 0
pub const fn from_code(code: u8) -> Self {
Self {
code,
regs: 0,
off: 0,
imm: 0,
}
}
}
/// File descriptor referring to a loaded and verified (e)BPF socket filter
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct SocketFilterFd {
fd: RawFd,
}
impl SocketOption for SocketFilterFd {
fn level() -> Level {
Level::Socket
}
fn name() -> Name {
Name::AttachBpf
}
fn optlen(&self) -> socklen_t {
size_of_val(self) as socklen_t
}
}
impl SetSocketOption for SocketFilterFd {}
/// `sock_fprog`
#[repr(C)]
#[derive(Debug, Clone)]
pub struct SocketFilterBpfAttribute {
program_type: u32,
instructions_count: u32,
instructions: Box<[Instruction]>,
license: CString,
log_level: u32,
log_size: u32,
log_buffer: Vec<u8>,
kernel_version: u32,
program_flags: u32,
}
const BPF_PROG_TYPE_SOCKET_FILTER: u32 = 1;
use std::ffi::CString;
use std::mem::forget;
use std::ptr::null_mut;
use syscall::syscall;
impl SocketFilterBpfAttribute {
/// Creates a new `SocketFilterBpfAttribute` from the given `Instruction` vector
pub fn new(v: Vec<Instruction>) -> Self {
let program_type = BPF_PROG_TYPE_SOCKET_FILTER;
let instructions_count = v.len() as u32;
let instructions = v.into_boxed_slice();
let license = CString::new("GPL").unwrap();
let log_level = if cfg!(debug_assertions) { 1 } else { 0 };
let log_size: u32 = if cfg!(debug_assertions) { 4096 } else { 0 };
let log_buffer = Vec::with_capacity(log_size as usize);
let kernel_version = 0;
let program_flags = 0;
Self {
program_type,
instructions_count,
instructions,
license,
log_level,
log_size,
log_buffer,
kernel_version,
program_flags,
}
}
/// XXX
pub fn load(mut self) -> Result<SocketFilterFd> {
#[repr(C)]
struct Attr {
program_type: u32,
instructions_count: u32,
instructions: u64,
license: u64,
log_level: u32,
log_size: u32,
log_buffer: u64,
kernel_version: u32,
prog_flags: u32,
}
let log_ptr = if cfg!(debug_assertions) {
self.log_buffer.as_mut_ptr()
} else {
null_mut()
};
let mut attr = Attr {
program_type: self.program_type,
instructions_count: self.instructions_count,
instructions: self.instructions.as_ptr() as u64,
license: self.license.as_ptr() as u64,
log_level: self.log_level,
log_size: self.log_size,
log_buffer: log_ptr as u64,
kernel_version: self.kernel_version,
prog_flags: self.program_flags,
};
let ptr: *mut Attr = &mut attr;
let fd = unsafe { syscall!(BPF, 5, ptr, size_of_val(&attr)) as i32 };
if cfg!(debug_assertions) {
unsafe {
forget(self.log_buffer);
let log =
Vec::from_raw_parts(log_ptr, self.log_size as usize, self.log_size as usize);
debug!("BPF_PROG_LOAD log: {:}", String::from_utf8(log).unwrap());
}
}
if fd > 0 {
Ok(SocketFilterFd { fd })
} else {
Err(SystemError(-fd))
}
}
}
#[repr(u8)]
#[derive(Copy, Clone, Debug, Ord, Eq, Hash, PartialEq, PartialOrd, FromPrimitive, ToPrimitive)]
pub enum Comparison {
// TODO - 32 bit comparisons
Always = 0x00,
Equal = 0x10,
GreaterThan = 0x20,
GreaterEqual = 0x30,
AndMask = 0x40,
NotEqual = 0x50,
SignedGreaterThan = 0x60,
SignedGreaterEqual = 0x70,
LesserThan = 0xa0,
LesserEqual = 0xb0,
SignedLesserThan = 0xc0,
SignedLesserEqual = 0xd0,
Unknown,
}
impl From<u8> for Comparison {
fn from(u: u8) -> Self {
FromVal::from_u8(u).unwrap()
}
}
#[derive(Copy, Clone, Debug, Ord, Eq, Hash, PartialEq, PartialOrd)]
pub enum Operand {
RegAndImm(Register, i32),
// XXX - remove the allow deriviate when no longer needed
#[allow(dead_code)]
DstAndSrc(Register, Register),
}
impl From<u32> for Operand {
fn from(u: u32) -> Self {
Operand::RegAndImm(Register::Ret, u as i32)
}
}
const OFFSET_SK_BUFF_LEN: i16 = 0;
const EXIT: Instruction = Instruction::from_code((BPF_JMP | BPF_EXIT) as u8);
const fn load_packet_length(dst: Register) -> Instruction {
Instruction::new(
(BPF_LDX | BPF_W | BPF_MEM) as u8,
Register::SocketBuffer,
dst,
OFFSET_SK_BUFF_LEN,
0,
)
}
const fn copy_imm(dst: Register, imm: i32) -> Instruction {
Instruction::new(
(BPF_ALU64 | BPF_MOV | BPF_W) as u8,
dst,
Register::None,
0,
imm,
)
}
const fn copy(dst: Register, src: Register) -> Instruction {
Instruction::new((BPF_ALU64 | BPF_MOV | BPF_X) as u8, dst, src, 0, 0)
}
/// # logic
/// * set R6 a pointer to the processed packet, necessery for eBPF direct packet access
pub fn initialization_sequence() -> Vec<Instruction> {
vec![copy(Register::Context, Register::SocketBuffer)]
}
pub fn return_sequence() -> (Vec<Instruction>, usize, usize) {
let res = vec![
EXIT,
copy_imm(Register::Ret, 0),
jump_always(1),
load_packet_length(Register::Ret),
];
(res, 0, 2)
}
pub fn teotology() -> Vec<Instruction> {
vec![EXIT, load_packet_length(Register::Ret)]
}
pub fn contradiction() -> Vec<Instruction> {
vec![copy_imm(Register::Ret, 0)]
}
pub fn into_socket_option(instructions: Vec<Instruction>) -> Result<SocketFilterFd> {
let len = instructions.len();
if len > u16::max_value() as usize {
return Err(SystemError(EOVERFLOW));
}
Ok(SocketFilterBpfAttribute::new(instructions).load()?)
}
const fn jump_always(offset: i16) -> Instruction {
Instruction::new(
(BPF_JMP as u8) | (BPF_JA as u8),
Register::None,
Register::None,
offset,
0,
)
}
const fn jump_imm(comp: Comparison, reg: Register, imm: i32, offset: i16) -> Instruction {
Instruction::new(
(BPF_JMP as u8) | comp as u8 | (BPF_K as u8),
reg,
Register::None,
offset,
imm,
)
}
const fn jump_reg(comp: Comparison, dst: Register, src: Register, offset: i16) -> Instruction {
Instruction::new(
(BPF_JMP as u8) | comp as u8 | (BPF_X as u8),
dst,
src,
offset,
0,
)
}
/// jump sequence
pub fn jump(comparison: Comparison, operand: Operand, jt: usize, jf: usize) -> Vec<Instruction> {
let distance_to_true_label: i16 = jt as i16 + 1;
match operand {
Operand::DstAndSrc(dst, src) => vec![
jump_always(jf as i16),
jump_reg(comparison, dst, src, distance_to_true_label),
],
Operand::RegAndImm(reg, imm) => vec![
jump_always(jf as i16),
jump_imm(comparison, reg, imm, distance_to_true_label),
],
}
}
pub fn load_u8_at(offset: i32) -> Vec<Instruction> {
vec![Instruction::new(
(BPF_ABS | BPF_LD | BPF_B) as _,
Register::None,
Register::None,
0,
offset,
)]
}
pub fn load_u16_at(offset: i32) -> Vec<Instruction> {
vec![Instruction::new(
(BPF_ABS | BPF_LD | BPF_H) as _,
Register::None,
Register::None,
0,
offset,
)]
}
pub fn load_u32_at(offset: i32) -> Vec<Instruction> {
vec![Instruction::new(
(BPF_ABS | BPF_LD | BPF_W) as _,
Register::None,
Register::None,
0,
offset,
)]
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}