ebpfkit 0.1.0

Kernel-Space eBPF Just-In-Time Pipeline Filter Compiler
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Native Linux syscall bridge for injecting JIT'd eBPF filters.
//!
//! Uses bare-metal libc `bpf()` syscall constraints without relying on heavyweight
//! libraries like `libbpf` or `aya`. This forces ultimate alignment and allows zero-dependency
//! kernel injections.

use crate::assembler::BpfInsn;
use std::mem::MaybeUninit;
use std::os::unix::io::RawFd;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};

/// BPF syscall number — use libc constant for portability across architectures.
#[cfg(target_os = "linux")]
const SYS_BPF: libc::c_long = libc::SYS_bpf;
#[cfg(not(target_os = "linux"))]
const SYS_BPF: libc::c_long = 321; // fallback for non-Linux (will fail at syscall)

const BPF_PROG_LOAD: u32 = 5;
const BPF_MAP_CREATE: u32 = 0;
const BPF_OBJ_GET_INFO_BY_FD: u32 = 15;
const BPF_PROG_TYPE_SOCKET_FILTER: u32 = 1;
const BPF_MAP_TYPE_RINGBUF: u32 = 27;
const BPF_RINGBUF_BUSY_BIT: u32 = 1 << 31;
const BPF_RINGBUF_DISCARD_BIT: u32 = 1 << 30;
const BPF_RINGBUF_HDR_SZ: usize = 8;

/// Maximum BPF program instructions the verifier accepts.
const MAX_BPF_INSNS: usize = 4096;

#[repr(C)]
#[derive(Default)]
struct BpfAttrProgLoad {
    prog_type: u32,
    insn_cnt: u32,
    insns: u64,
    license: u64,
    log_level: u32,
    log_size: u32,
    log_buf: u64,
    kern_version: u32,
    prog_flags: u32,
    prog_name: [u8; 16],
    prog_ifindex: u32,
    expected_attach_type: u32,
    prog_btf_fd: u32,
    func_info_rec_size: u32,
    func_info: u64,
    func_info_cnt: u32,
    line_info_rec_size: u32,
    line_info: u64,
    line_info_cnt: u32,
    attach_btf_id: u32,
}

#[repr(C)]
#[derive(Default)]
struct BpfAttrMapCreate {
    map_type: u32,
    key_size: u32,
    value_size: u32,
    max_entries: u32,
    map_flags: u32,
    inner_map_fd: u32,
    numa_node: u32,
    map_name: [u8; 16],
    map_ifindex: u32,
    btf_fd: u32,
    btf_key_type_id: u32,
    btf_value_type_id: u32,
    btf_vmlinux_value_type_id: u32,
    map_extra: u64,
}

#[repr(C)]
#[derive(Default)]
struct BpfAttrObjInfoByFd {
    bpf_fd: u32,
    info_len: u32,
    info: u64,
}

#[repr(C)]
#[derive(Default)]
struct BpfMapInfo {
    map_type: u32,
    id: u32,
    key_size: u32,
    value_size: u32,
    max_entries: u32,
    map_flags: u32,
    name: [u8; 16],
    ifindex: u32,
    btf_vmlinux_value_type_id: u32,
    netns_dev: u64,
    netns_ino: u64,
    btf_id: u32,
    btf_key_type_id: u32,
    btf_value_type_id: u32,
    map_extra: u64,
}

#[repr(C)]
struct RingbufHeader {
    len: AtomicU32,
    pad: u32,
}

/// Injects a dynamically compiled JIT bytecode array deep into the kernel.
///
/// Returns the Raw File Descriptor representing the active verified filter program.
/// This FD can then be locked onto sockets or natively bound to `io_uring` drops.
pub fn load_filter(insns: &[BpfInsn]) -> Result<RawFd, std::io::Error> {
    if insns.is_empty() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "BPF program is empty. Fix: provide at least one instruction.",
        ));
    }
    if insns.len() > MAX_BPF_INSNS {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!(
                "BPF program has {} instructions, exceeding the {MAX_BPF_INSNS}-instruction verifier limit. Fix: simplify the pattern or split into multiple programs.",
                insns.len()
            ),
        ));
    }
    let insn_cnt = u32::try_from(insns.len()).map_err(|_| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "BPF instruction count exceeds u32. Fix: reduce program size.",
        )
    })?;
    let license = b"GPL\0";

    let attr = BpfAttrProgLoad {
        prog_type: BPF_PROG_TYPE_SOCKET_FILTER,
        insn_cnt,
        insns: insns.as_ptr() as u64,
        license: license.as_ptr() as u64,
        ..Default::default()
    };

    // SAFETY: The syscall transfers boundaries correctly to the kernel BPF verifier.
    let fd = unsafe {
        libc::syscall(
            SYS_BPF,
            BPF_PROG_LOAD,
            &attr as *const BpfAttrProgLoad,
            std::mem::size_of::<BpfAttrProgLoad>(),
        )
    };

    if fd < 0 {
        return Err(std::io::Error::last_os_error());
    }

    Ok(fd as RawFd)
}

/// Dynamically attaches the loaded eBPF prog FD to an active raw socket file descriptor.
pub fn attach_to_socket(prog_fd: RawFd, socket_fd: RawFd) -> Result<(), std::io::Error> {
    const SO_ATTACH_BPF: libc::c_int = 50;

    // SAFETY: Both file descriptors are valid (caller contract). The prog_fd
    // pointer is a stack reference valid for the duration of the syscall.
    let res = unsafe {
        libc::setsockopt(
            socket_fd,
            libc::SOL_SOCKET,
            SO_ATTACH_BPF,
            &prog_fd as *const _ as *const libc::c_void,
            std::mem::size_of::<RawFd>() as libc::socklen_t,
        )
    };

    if res < 0 {
        return Err(std::io::Error::last_os_error());
    }

    Ok(())
}

/// Creates a `BPF_MAP_TYPE_RINGBUF` map with the requested capacity.
///
/// `size_bytes` must be a non-zero power of two because the kernel uses it as
/// the ring size directly.
pub fn create_ringbuf(size_bytes: u32) -> Result<RawFd, std::io::Error> {
    if size_bytes == 0 || !size_bytes.is_power_of_two() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "ring buffer size must be a non-zero power of two. Fix: pass 4096, 8192, 16384, or another power of two.",
        ));
    }

    let attr = BpfAttrMapCreate {
        map_type: BPF_MAP_TYPE_RINGBUF,
        max_entries: size_bytes,
        ..Default::default()
    };

    // SAFETY: `attr` is a valid `BPF_MAP_CREATE` payload for the duration of
    // the syscall and the kernel copies it before returning.
    let fd = unsafe {
        libc::syscall(
            SYS_BPF,
            BPF_MAP_CREATE,
            &attr as *const BpfAttrMapCreate,
            std::mem::size_of::<BpfAttrMapCreate>(),
        )
    };

    if fd < 0 {
        return Err(std::io::Error::last_os_error());
    }

    Ok(fd as RawFd)
}

/// Blocks until the kernel ring buffer contains events and invokes `callback`
/// for each record available at that wake-up.
pub fn poll_ringbuf(map_fd: RawFd, callback: &mut dyn FnMut(&[u8])) -> Result<(), std::io::Error> {
    let info = map_info(map_fd)?;
    if info.map_type != BPF_MAP_TYPE_RINGBUF {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "fd does not reference a BPF ring buffer map. Fix: pass the fd returned by create_ringbuf or a BPF_MAP_TYPE_RINGBUF map.",
        ));
    }

    let page_size = page_size()?;
    let ring_size = usize::try_from(info.max_entries).map_err(|_| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "ring buffer max_entries does not fit in usize. Fix: use a smaller ring size.",
        )
    })?;
    let consumer = RingbufConsumer::new(map_fd, ring_size, page_size)?;
    let epoll_fd = EpollFd::new()?;
    epoll_fd.add(map_fd)?;

    epoll_fd.wait()?;
    consume_ring_records(&consumer, callback);
    Ok(())
}

fn page_size() -> Result<usize, std::io::Error> {
    let value = unsafe { libc::sysconf(libc::_SC_PAGESIZE) };
    if value <= 0 {
        return Err(std::io::Error::last_os_error());
    }
    usize::try_from(value).map_err(|_| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "system page size does not fit in usize. Fix: run on a supported userspace architecture.",
        )
    })
}

fn map_info(map_fd: RawFd) -> Result<BpfMapInfo, std::io::Error> {
    let mut info = MaybeUninit::<BpfMapInfo>::zeroed();
    let attr = BpfAttrObjInfoByFd {
        bpf_fd: map_fd as u32,
        info_len: std::mem::size_of::<BpfMapInfo>() as u32,
        info: info.as_mut_ptr() as u64,
    };

    // SAFETY: the kernel writes at most `info_len` bytes into `info`.
    let rc = unsafe {
        libc::syscall(
            SYS_BPF,
            BPF_OBJ_GET_INFO_BY_FD,
            &attr as *const BpfAttrObjInfoByFd,
            std::mem::size_of::<BpfAttrObjInfoByFd>(),
        )
    };

    if rc < 0 {
        return Err(std::io::Error::last_os_error());
    }

    // SAFETY: successful syscall fully initializes the bytes we requested.
    Ok(unsafe { info.assume_init() })
}

fn consume_ring_records(consumer: &RingbufConsumer, callback: &mut dyn FnMut(&[u8])) {
    let mut consumer_pos = consumer.consumer_pos().load(Ordering::Acquire);

    loop {
        let producer_pos = consumer.producer_pos().load(Ordering::Acquire);
        let mut consumed_any = false;

        while consumer_pos < producer_pos {
            let offset = (consumer_pos as usize) & consumer.mask;
            let header = consumer.header(offset);
            let raw_len = header.len.load(Ordering::Acquire);
            if raw_len & BPF_RINGBUF_BUSY_BIT != 0 {
                consumer
                    .consumer_pos()
                    .store(consumer_pos, Ordering::Release);
                return;
            }

            let data_len = (raw_len & !(BPF_RINGBUF_BUSY_BIT | BPF_RINGBUF_DISCARD_BIT)) as usize;
            let record_len = round_record_len(data_len);
            if raw_len & BPF_RINGBUF_DISCARD_BIT == 0 {
                let data_offset = offset + BPF_RINGBUF_HDR_SZ;
                callback(consumer.data_slice(data_offset, data_len));
            }

            consumer_pos += record_len as u64;
            consumer
                .consumer_pos()
                .store(consumer_pos, Ordering::Release);
            consumed_any = true;
        }

        if !consumed_any {
            return;
        }
    }
}

fn round_record_len(data_len: usize) -> usize {
    let total = data_len.saturating_add(BPF_RINGBUF_HDR_SZ);
    (total + 7) & !7
}

struct EpollFd(RawFd);

impl EpollFd {
    fn new() -> Result<Self, std::io::Error> {
        let fd = unsafe { libc::epoll_create1(libc::EPOLL_CLOEXEC) };
        if fd < 0 {
            return Err(std::io::Error::last_os_error());
        }
        Ok(Self(fd))
    }

    fn add(&self, map_fd: RawFd) -> Result<(), std::io::Error> {
        let mut event = libc::epoll_event {
            events: libc::EPOLLIN as u32,
            u64: map_fd as u64,
        };
        let rc = unsafe { libc::epoll_ctl(self.0, libc::EPOLL_CTL_ADD, map_fd, &mut event) };
        if rc < 0 {
            return Err(std::io::Error::last_os_error());
        }
        Ok(())
    }

    fn wait(&self) -> Result<(), std::io::Error> {
        let mut event = libc::epoll_event { events: 0, u64: 0 };
        let rc = unsafe { libc::epoll_wait(self.0, &mut event, 1, -1) };
        if rc < 0 {
            return Err(std::io::Error::last_os_error());
        }
        Ok(())
    }
}

impl Drop for EpollFd {
    fn drop(&mut self) {
        let _ = unsafe { libc::close(self.0) };
    }
}

struct RingbufConsumer {
    consumer_mapping: *mut libc::c_void,
    producer_mapping: *mut libc::c_void,
    data_ptr: *const u8,
    mapping_len: usize,
    mask: usize,
    page_size: usize,
}

impl RingbufConsumer {
    fn new(map_fd: RawFd, ring_size: usize, page_size: usize) -> Result<Self, std::io::Error> {
        let consumer_mapping = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                page_size,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_SHARED,
                map_fd,
                0,
            )
        };
        if consumer_mapping == libc::MAP_FAILED {
            return Err(std::io::Error::last_os_error());
        }

        let mapping_len = page_size
            .checked_add(ring_size.checked_mul(2).ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "ring buffer mapping length overflowed. Fix: use a smaller ring size.",
                )
            })?)
            .ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "ring buffer mapping length overflowed. Fix: use a smaller ring size.",
                )
            })?;

        let producer_mapping = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                mapping_len,
                libc::PROT_READ,
                libc::MAP_SHARED,
                map_fd,
                page_size as libc::off_t,
            )
        };
        if producer_mapping == libc::MAP_FAILED {
            let error = std::io::Error::last_os_error();
            let _ = unsafe { libc::munmap(consumer_mapping, page_size) };
            return Err(error);
        }

        let data_ptr = unsafe { (producer_mapping as *const u8).add(page_size) };
        Ok(Self {
            consumer_mapping,
            producer_mapping,
            data_ptr,
            mapping_len,
            mask: ring_size - 1,
            page_size,
        })
    }

    fn consumer_pos(&self) -> &AtomicU64 {
        unsafe { &*(self.consumer_mapping as *const AtomicU64) }
    }

    fn producer_pos(&self) -> &AtomicU64 {
        unsafe { &*(self.producer_mapping as *const AtomicU64) }
    }

    fn header(&self, offset: usize) -> &RingbufHeader {
        unsafe { &*(self.data_ptr.add(offset) as *const RingbufHeader) }
    }

    fn data_slice(&self, offset: usize, len: usize) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.data_ptr.add(offset), len) }
    }
}

impl Drop for RingbufConsumer {
    fn drop(&mut self) {
        let _ = unsafe { libc::munmap(self.consumer_mapping, self.page_size) };
        let _ = unsafe { libc::munmap(self.producer_mapping, self.mapping_len) };
    }
}

/// CO-RE (Compile Once — Run Everywhere) capability detection.
///
/// BPF CO-RE requires BTF (BPF Type Format) support in the kernel. This
/// allows eBPF programs to reference kernel types by name rather than
/// hard-coded offsets, making programs portable across kernel versions.
pub mod core_detect {
    use std::path::Path;

    /// Check if the kernel supports BTF-based CO-RE.
    ///
    /// Returns `true` if `/sys/kernel/btf/vmlinux` exists, indicating
    /// the kernel was built with `CONFIG_DEBUG_INFO_BTF=y`.
    #[must_use]
    pub fn has_btf() -> bool {
        Path::new("/sys/kernel/btf/vmlinux").exists()
    }

    /// Check if the kernel supports BPF ring buffers (Linux 5.8+).
    #[must_use]
    pub fn has_ringbuf() -> bool {
        kernel_version() >= (5, 8, 0)
    }

    /// Check if the kernel supports fentry/fexit tracing programs (Linux 5.5+).
    #[must_use]
    pub fn has_fentry() -> bool {
        kernel_version() >= (5, 5, 0)
    }

    /// Get a diagnostic summary of BPF CO-RE capabilities.
    #[must_use]
    pub fn diagnostics() -> Vec<String> {
        let mut diags = Vec::new();
        let version = kernel_version();
        diags.push(format!("kernel: {}.{}.{}", version.0, version.1, version.2));
        diags.push(format!("BTF available: {}", has_btf()));
        diags.push(format!("fentry support: {}", has_fentry()));
        diags.push(format!("ring buffer support: {}", has_ringbuf()));

        #[cfg(target_os = "linux")]
        {
            let euid = unsafe { libc::geteuid() };
            diags.push(format!("running as root: {}", euid == 0));
        }

        diags
    }

    /// Parse the kernel version from /proc/sys/kernel/osrelease.
    fn kernel_version() -> (u32, u32, u32) {
        #[cfg(target_os = "linux")]
        {
            if let Ok(release) = std::fs::read_to_string("/proc/sys/kernel/osrelease") {
                let trimmed = release.trim();
                let mut parts = trimmed.split(|c: char| !c.is_ascii_digit());
                let major = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
                let minor = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
                let patch = parts.next().and_then(|s| s.parse().ok()).unwrap_or(0);
                return (major, minor, patch);
            }
        }
        (0, 0, 0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(target_os = "linux")]
    #[test]
    fn create_ringbuf_rejects_non_power_of_two_sizes() {
        let error = match create_ringbuf(3) {
            Ok(fd) => {
                let _ = unsafe { libc::close(fd) };
                std::io::Error::other(
                    "create_ringbuf unexpectedly accepted a non-power-of-two size",
                )
            }
            Err(error) => error,
        };
        assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn create_ringbuf_gracefully_handles_kernel_support() {
        match create_ringbuf(4096) {
            Ok(fd) => {
                let close_rc = unsafe { libc::close(fd) };
                assert_eq!(close_rc, 0);
            }
            Err(error) => {
                assert!(
                    matches!(error.raw_os_error(), Some(code) if [
                        libc::EINVAL,
                        libc::EPERM,
                        libc::ENOSYS,
                        libc::EOPNOTSUPP
                    ]
                    .contains(&code)),
                    "unexpected ringbuf create failure: {error}",
                );
            }
        }
    }
}