Skip to content

eBPF-based security platform

6.7 KB
Raw Blame
..
   SPDX-License-Identifier: BSD-2-Clause
   Copyright (c) 2026 Authors of shirudo

:page-type: Explanation
:audience: All readers

CO-RE and BTF
=============

A BPF program that reads kernel data structures depends on their layout,
and the layout changes between kernel versions and configurations. CO-RE
("compile once, run everywhere") lets one compiled program run on kernels
whose layouts differ. It rests on BTF, the type information the kernel
carries about itself. This page explains both, and why every upatch is
built this way.

Why struct offsets are a problem
--------------------------------

Consider an LSM program that reads ``file->f_inode``. The compiler turns
that expression into a load from a fixed offset into ``struct file``. The
offset is right for the kernel whose headers the program was compiled
against. Another kernel version, or the same version built with a
different configuration, may add, remove or reorder fields, and the same
instruction then reads some other field.

The obvious fix is to compile the program on each target, against that
target's headers. That needs a compiler and kernel headers on every
target. It also defeats signing: the operator signs bytes, and bytes
compiled on the target were never signed.

BTF
---

BTF (BPF Type Format) is a compact encoding of C types: structs and
unions with their members, offsets and sizes, enums, typedefs and
function prototypes. clang emits BTF for a BPF object compiled with
``-g`` into the ``.BTF`` section. It also writes ``.BTF.ext``, which holds
per-instruction information: function and line records and the CO-RE
relocations described below.

A kernel built with ``CONFIG_DEBUG_INFO_BTF`` carries BTF for all of its
own types and exposes it at ``/sys/kernel/btf/vmlinux``. With
``CONFIG_DEBUG_INFO_BTF_MODULES``, each module's types appear under
``/sys/kernel/btf/`` too. The verifier uses this vmlinux BTF to type-check
accesses through kernel pointers in tracing and LSM programs, and CO-RE
resolves against it.

A header generated from vmlinux BTF, ``vmlinux.h``, declares every kernel
type, so a BPF program needs no kernel headers. The generator marks every
struct and union with clang's ``preserve_access_index`` attribute, which
makes each field access in the program relocatable.

Relocations
-----------

For each access to a field of such a type, or each one wrapped in
``BPF_CORE_READ()``, clang emits the instruction with the offset it knows
from the local header. It also records a CO-RE relocation: which
instruction, which type, and the access path by member name, such as
member ``f_inode`` of ``struct file``.

At load time, a type of the same name is looked up in the target
kernel's BTF, the same path is followed by member name, and the
instruction is patched with the offset found there. Other relocation
kinds answer other questions: whether a field or type exists, how large a
field is, what value an enumerator has. A type name with a ``___suffix``
matches the kernel type without the suffix, which lets one program carry
definitions for several kernel variants.

A relocation that cannot be resolved, because the field does not exist
on the target, does not fail the load at once. The instruction is
replaced by an invalid call. If the program can reach that instruction,
the verifier rejects the program with this message:

.. code-block:: text

   invalid func unknown#195896080

If the instruction sits behind a ``bpf_core_field_exists()`` check that
is false on this kernel, the verifier never reaches it and the program
loads.

Who applies the relocations
---------------------------

Usually libbpf applies the relocations in userspace before it calls
``BPF_PROG_LOAD``, reading the target's BTF from
``/sys/kernel/btf/vmlinux``.

A loader generated by gen_loader (see :doc:`bpf`) cannot do that, because
it runs inside the kernel. It passes the relocation records to the kernel
together with each program it loads, and the kernel applies them itself,
against its own BTF, before the verifier analyzes the program.

The signed loader itself carries no CO-RE relocations. The kernel refuses
a signed program that does, because the relocations would rewrite signed
instructions with values the caller chooses, and logs:

.. code-block:: text

   signed program cannot carry CO-RE relocations

The records the payload needs travel in the loader's data, which the
signature covers (see :doc:`bpf-signing`).

How shirudo uses CO-RE
----------------------

``shirudo build`` compiles a ``.bpf.c`` file with
``clang -target bpf -mcpu=v4 -fno-jump-tables -O2 -g`` against a ``vmlinux.h`` and
hands the object
to gen_loader. Unless
the operator names a header, the build uses ``./vmlinux.h``, then
``./vendor/linux/vmlinux.h``, then the copy installed with shirudo
(``shirudo/vmlinux.h`` under the data directory, ``/usr/local/share`` by
default). The relocation records end up in the loader's data and are
signed with it.

On the target, the kernel resolves the records against its own BTF when
the loader loads the payload. One signed upatch therefore loads on every
kernel whose BTF has the types and fields it uses. A kernel without
``CONFIG_DEBUG_INFO_BTF`` has no BTF to resolve against, and shirudo's own
LSM programs need it as well.

shirudo's own enforcement objects are CO-RE as well. libbpf relocates
them in userspace when ``shirudo init`` loads them. They use
``bpf_core_field_offset()`` to find the ``struct mount`` that contains a
``struct vfsmount``, and ``bpf_core_field_exists()`` to handle a kernel
built without ``CONFIG_SECCOMP``, where ``struct seccomp`` is empty.

The ``vmlinux.h`` still has to declare every type a program names. The
admission program reads the signature verdict from
``struct bpf_prog_aux``, so shirudo itself needs a ``vmlinux.h`` from a
kernel with BPF signing.

What a mismatch looks like
--------------------------

A upatch that touches a field the target kernel lacks fails when the
loader loads the payload. ``shirudo upatch load`` reports category
``verifier_rejected`` in phase ``payload_load`` and includes the loader's
log, where the verifier's message for the payload appears. See
:doc:`/upatches/test/debugging` for reading that log and
:doc:`/reference/failure-categories` for the categories.

A program that hard-codes an offset carries no relocation, so nothing
adjusts it. Where the verifier can check the access against BTF, it may
reject the program. Otherwise the program loads and reads whatever lies
at that offset on the target kernel.

Limits
------

CO-RE matches types and fields by name and layout, not by meaning. A
field that keeps its name but changes what it holds relocates without
complaint, and a renamed field fails to relocate even if its meaning is
unchanged. A upatch built for one kernel release still needs testing on
every kernel it ships to; see :doc:`/upatches/test/testing`.