# Understanding eBPF: How Linux Became Programmable Without Changing a Single Line of Kernel Code

So recently I was exploring Shirudo (<https://shirudo.dev/>) which is basically an Open Source Linux runtime which allows AI agents to keep system secure without downtime.

What it does basically - It lets agents write dynamic eBPF controls to shield systems from exploits, giving defenders more time to test and roll out patches.

But I didn't have any idea what this eBPF was, so I researched on it, to know what it is and what it does in short. but after learning about it I got suprised and interested about it and what are the possibilities it can open. So Like me if u also didn't knew what eBPF was, here's what I understood.

## What is eBPF ?

eBPF (extended Berkeley Packet Filter) is a technology that allows us to run sandboxed, custom programs inside the Linux kernel dynamically, without changing kernel source code or loading risky kernel modules.

To understand why this is a big deal, we first need to look at how Linux is divided and why modifying the kernel used to be so painful.

## The Divide: User Space vs. Kernel Space

Every Linux system splits its memory and execution into two distinct privileges:

1. **User Space:** Where our everyday applications run - web servers, databases, Python scripts, and CLI tools. Code here is isolated; if an application crashes, the rest of the system survives. To talk to hardware, read a file, or open a network socket, user-space programs must ask the kernel for permission via **System Calls (syscalls)**.
2. **Kernel Space:** The privileged core of the operating system. It manages CPU scheduling, memory, storage, and networking. Because it has unrestricted access to hardware, a single bug, infinite loop, or null-pointer dereference in kernel space brings down the entire machine with a **Kernel Panic**.

![User Vs Kernel Space (gemini made)](https://cdn.hashnode.com/uploads/covers/6462434e7ffd2bd3573f4e1d/eda48c4d-ab42-4adb-b183-0789f536a9c5.png align="center")

Earlier, if we wanted to add anything in kernel like deep observability, custom packet filtering, or security enforcement, we had only two choices:

- **Modify the kernel source code directly** and wait years for our changes to be reviewed, merged upstream, and adopted by enterprise Linux distributions.
- **Write a Loadable Kernel Module (LKM)**, which runs with full kernel privileges meaning a single memory bug in our module could crash the whole server or introduce a severe privilege-escalation vulnerability.

eBPF solves this problem by giving us/developers a safe, fast, and dynamic way to extend kernel behavior on the fly.

### How eBPF Works Under the Hood

eBPF programs are **event-driven**. Rather than running continuously like a background daemon, an eBPF program sits dormant until a specific event occurs in the kernel - such as a network packet arriving at the network interface card (NIC), a process invoking `execve()` to launch a binary, or a function executing inside a user-space application.

![](https://cdn.hashnode.com/uploads/covers/6462434e7ffd2bd3573f4e1d/e758bdbd-60e0-4d8c-83d8-70e37849cd04.png align="center")

Here is the lifecycle of an eBPF program from code to execution:

1. **Write and Compile to Bytecode:** we as devs write our logic in a restricted subset of C (or Rust), and compile it using LLVM/Clang into **eBPF bytecode** - a universal, architecture-independent instruction set.
2. **Load via the** `bpf()` **Syscall:** A user-space loader program (using libraries like `libbpf`, Cilium's `ebpf-go`, or Python's `BCC`) passes this bytecode into the kernel using the `bpf()` system call.
3. **The eBPF Verifier (The Safety Gatekeeper):** Before the kernel accepts a single instruction, the **Verifier** performs static analysis on the entire program. It walks every possible execution path to guarantee that:
   - The program will always terminate (no unbounded loops that could hang the CPU).
   - It never dereferences invalid pointers or reads out-of-bounds kernel memory.
   - It does not exceed stack limits (512 bytes) or maximum instruction counts. If any check fails, the kernel flatly rejects the program.
4. **JIT Compilation:** Once verified, the **Just-In-Time (JIT) compiler** translates the generic eBPF bytecode into native machine instructions (x86_64, ARM64, etc.). This ensures eBPF runs at near-native kernel speed, with virtually zero interpretation overhead.
5. **Attach to a Hook:** The compiled program is attached to a specific kernel **hook** which is an event-driven insertion point inside the kernel execution path. When the kernel encounters a hook, it pauses its normal execution briefly, runs our attached eBPF program, and then resumes.
6. **Communicate via eBPF Maps:** Because eBPF programs are stateless between invocations, they store state and share data with user space using **eBPF Maps**. eBPF Maps are efficient key-value data structures (hash tables, ring buffers, LRU caches) residing in kernel memory that both the eBPF program and user-space applications can read and write concurrently.

So this was the basic explaination about eBPF, if u want to explore more, u can visit their website (<https://ebpf.io/>).

If you liked the explaination, please leave a comment. Follow for more !!
