Attach kinds#
The section name of a program decides where it attaches. shirudo build
reads it, refuses the sections below the table, and records the attach
kind and target in the signed attach metadata. After the signed loader
has created the programs, shirudo upatch load attaches each one, checks
the link against the metadata and pins it.
Section |
Program type |
Attached by |
Target kernel needs |
|---|---|---|---|
|
tracing |
|
the base requirements |
|
tracing |
|
the base requirements |
|
tracing |
|
|
|
LSM |
|
the base requirements |
|
tracing |
|
the base requirements |
|
kprobe |
|
|
|
kprobe |
as |
|
|
kprobe |
|
|
|
kprobe |
as |
|
|
kprobe |
|
|
|
kprobe |
as |
|
|
kprobe |
|
|
|
kprobe |
as |
|
|
cgroup skb |
|
|
|
netfilter |
|
|
|
XDP |
|
native XDP and multi-buffer support on selected devices |
|
syscall |
not attached, run by |
the base requirements |
|
syscall |
not attached, run once at load |
the base requirements |
The base requirements are the kernel options shirudo itself needs, listed in System requirements. They include BPF trampolines, BTF and the BPF LSM, which is why the BTF-based kinds need nothing more.
Every attached program becomes one link pinned at
/run/shirudo/bpffs/upatches/<name>/link when the upatch has one attach
program, and at link_0, link_1 and so on, in the order of the
object, when it has several. <name> has every . written as ~d.
A upatch containing XDP always uses link_0, link_1 and so on,
because one program can attach to several interfaces.
fentry and fexit#
fentry runs at the entry of a kernel function with its arguments,
fexit at its return with the arguments and the return value. Write
the program with BPF_PROG() and the function’s parameters. The
verifier checks the prototype against the kernel’s BTF.
The signed loader looks the function up in the target’s BTF and loads the
program with that BTF id as its attach target. shirudo then calls
BPF_RAW_TRACEPOINT_OPEN with no name, so the kernel attaches at the
BTF id the program carries. A function that is not in the target’s BTF,
often because the compiler inlined it, fails inside the loader, as
verifier_rejected in phase payload_load, and never reaches the
attach step.
fentry.s and fexit.s declare sleepable programs. The kernel admits
them only on functions it allows to sleep in, such as those on its
error-injection list.
fmod_ret#
fmod_ret runs at the entry of a function. A non-zero return skips the
function body, and the caller receives that value. This is the kind for a
shield that refuses a call before the vulnerable code runs.
shirudo attaches with BPF_LINK_CREATE and attach type
BPF_MODIFY_RETURN, without a fallback. The kernel admits fmod_ret
only on security_* hooks, on system call entry points and on
functions marked for error injection or return modification. The
error-injection marks take effect only with
CONFIG_FUNCTION_ERROR_INJECTION. On any other function the load fails
in phase payload_load. fmod_ret.s declares the sleepable variant.
The kernel skips a run of an fmod_ret program that is already running
on the CPU, and the call then goes through. Pitfalls says when, and
why an lsm program is the place to refuse an operation where a hook
covers it.
lsm#
lsm/<hook> attaches a program to the LSM hook security_<hook>
calls, for example lsm/file_open or lsm/socket_connect. The
program receives the hook’s arguments and the return value of the BPF LSM
programs that ran before it on the same hook. A non-zero return on a hook
that returns int refuses the operation. lsm.s/<hook> declares a
sleepable program, which the kernel admits only on hooks it lists as
sleepable.
shirudo attaches with BPF_LINK_CREATE and BPF_LSM_MAC. Every
target that runs shirudo has the BPF LSM active, because shirudo’s own
enforcement programs are LSM programs.
tp_btf#
tp_btf/<tracepoint> attaches to a kernel tracepoint, for example
tp_btf/sched_process_exec, and receives the tracepoint’s raw
arguments with their BTF types. Tracepoints are stable observation points
that the kernel maintains on purpose.
The loader resolves the tracepoint by BTF, and shirudo attaches with
BPF_RAW_TRACEPOINT_OPEN as for fentry.
kprobe and kretprobe#
kprobe/<symbol> and kretprobe/<symbol> attach at the entry or the
return of any kernel function that kallsyms lists, whether or not it is in
BTF. kprobe/<symbol>+<offset> probes an instruction inside the
function. The offset is decimal or 0x hexadecimal. A leading zero is
refused rather than read as octal, and a kretprobe takes no offset.
The program reads its arguments from struct pt_regs, so the source
defines the __TARGET_ARCH_* macro for bpf_tracing.h.
shirudo reads the kprobe PMU’s type from
/sys/bus/event_source/devices/kprobe/type and, for a return probe, the
return-probe bit from .../kprobe/format/retprobe, opens a perf event
for the symbol and offset, and attaches the program to it with
BPF_LINK_CREATE and BPF_PERF_EVENT.
A kprobe may call bpf_override_return() to refuse a call. The kernel
allows that only at the entry of a function on its error-injection list
and with CONFIG_BPF_KPROBE_OVERRIDE. Elsewhere the kernel refuses
the link, and the load fails in phase link_create.
kprobe.multi and kretprobe.multi#
kprobe.multi/<symbol>[,<symbol>...] and
kretprobe.multi/<symbol>[,<symbol>...] attach one program to the
entry or return of each named kernel function. The builder records the
distinct symbol names in sorted order in signed attach metadata. Globs
and empty list elements are unsupported. The target creates a
BPF_TRACE_KPROBE_MULTI link; return probes set
BPF_F_KPROBE_MULTI_RETURN. Programs receive struct pt_regs as
regular kprobes do.
uprobe and uretprobe#
uprobe/<path>:<function> and uretprobe/<path>:<function> attach to
the entry or the return of one function in a userspace binary, in every
process, for example SEC("uprobe//usr/bin/example:fooFunc").
<path> is absolute. uprobe/<path>:<function>+<offset> probes an
instruction inside the function; the offset is decimal or 0x
hexadecimal, a leading zero is refused rather than read as octal, and a
uretprobe takes none. <path>:0x<offset> in place of a function
probes that file offset in the binary, for code no symbol names. The
program reads its arguments and, in a uretprobe, the return value from
struct pt_regs, as a kprobe does.
The builder resolves the function as libbpf does, refusing a name that
matches no symbol or several at different addresses, adds the offset and
records the one file offset it gets with the binary’s GNU build ID in the
signed metadata, as for uprobe.multi. It refuses an offset that lies
outside the file, and one beyond 4 GiB, because the kernel reports a
uprobe link’s offset in 32 bits.
On the target, shirudo opens and checks the binary as for
uprobe.multi, then reads the uprobe PMU’s type from
/sys/bus/event_source/devices/uprobe/type and, for a return probe, the
return-probe bit from .../uprobe/format/retprobe, and opens a perf
event on every CPU for /proc/self/fd/<n>, the descriptor it checked,
at the recorded offset. The kernel resolves that path to the inode
shirudo checked, not to whatever <path> names later. shirudo attaches
the program to the event with BPF_LINK_CREATE and BPF_PERF_EVENT.
uprobe.s and uretprobe.s declare sleepable programs, as the
.s forms of uprobe.multi do, with the same attach kinds, 10 and 11.
uprobe.multi and uretprobe.multi#
uprobe.multi/<path>:<glob> and uretprobe.multi/<path>:<glob>
attach to the entry or the return of every function in a userspace binary
whose symbol matches the glob, in every process, for example
SEC("uprobe.multi//usr/bin/example:*Func"). <path> is absolute.
The glob has libbpf’s syntax: * matches any sequence and ? one
character. A return program reads the return value with
PT_REGS_RC(); by then the registers no longer hold the arguments.
These kinds are resolved on the operator host. The builder opens the binary
at <path>, matches the glob against its function symbols, converts the
matches to sorted file offsets and records them with the binary’s GNU
build ID in the signed metadata. It refuses a glob that matches nothing
and a binary without a build ID.
On the target, shirudo opens <path>, requires a regular file, opens it
again through /proc/self/fd and compares device and inode, checks that
every offset lies inside the file, and compares the build ID. A different
binary fails with binary_mismatch in phase target_verify, before
any BPF is loaded. shirudo keeps that descriptor open and attaches with
BPF_LINK_CREATE, BPF_TRACE_UPROBE_MULTI and
BPF_F_UPROBE_MULTI_PATH_FD, and BPF_F_UPROBE_MULTI_RETURN for
uretprobe.multi, so the probes land in the inode it checked, not in
whatever the path names later. There is no path fallback.
uprobe.multi.s and uretprobe.multi.s declare sleepable programs,
which may call bpf_copy_from_user() and fault in the user memory they
read. They take the same attach kinds, 7 and 9: BPF_F_SLEEPABLE is a
load flag the signed loader sets, so the signature covers it, and the
target attaches them as it does the others.
Limit |
Value |
|---|---|
path |
absolute, shorter than 4096 bytes |
build ID |
1 to 64 bytes |
offsets per program |
1 to 1,048,576 |
glob |
one pattern, without |
cgroup_skb#
cgroup_skb/ingress and cgroup_skb/egress see every IP packet that
a socket on the host receives or sends, as a struct __sk_buff. The
program returns 1 to pass the packet and 0 to drop it.
shirudo opens /sys/fs/cgroup and attaches with BPF_LINK_CREATE
and BPF_CGROUP_INET_INGRESS or BPF_CGROUP_INET_EGRESS, so the link
sits on the root of the unified cgroup hierarchy. The kernel runs every
program attached there and drops a packet if one of them says so.
netfilter#
netfilter attaches a program as a netfilter hook for IPv6 at
NF_INET_PRE_ROUTING with priority NF_IP6_PRI_RAW_BEFORE_DEFRAG
(-450). The program runs before connection tracking and before IPv6
defragmentation, so it sees fragments as they arrive. It receives a
struct bpf_nf_ctx and returns NF_ACCEPT or NF_DROP.
shirudo fixes the family, hook and priority. The section name carries
none of them. A shield that needs another family or hook is written as a
cgroup_skb or lsm program.
XDP#
xdp/<decimal-mask> signs a selection of device classes, not a device
name or an ifindex. The target attaches the program to every device of
those classes in the initial network namespace, at load, at boot restore
and whenever a device appears.
Mask and classes#
The mask is mandatory, nonzero and contains only the bits below. For
example, SEC("xdp/3") selects network ports and virtual devices;
SEC("xdp/27") selects all classes. Bare xdp, xdp.frags,
devmap and cpumap sections are refused; xdp/<decimal-mask> already
means multi-buffer.
Bit |
Class |
Devices |
|---|---|---|
1 |
Network-facing |
Hardware-backed NICs and physical devlink ports. |
2 |
Virtual |
Software interfaces, host tap devices, devlink representors and Xen netback interfaces in dom0. |
8 |
VM |
Guest adapters using virtio-net, vmxnet3, Hyper-V netvsc or Xen netfront. Emulated hardware and passthrough NICs use their hardware class. |
16 |
Unknown |
Devices without sufficient classification evidence. This bit is a catchall class, not a wildcard; 27 selects every class. |
Bit 4 is not a class, and a mask with it is refused. Loopback has no native XDP, so it has no class and no mask selects it. Every other interface has exactly one class.
How a device is classified#
The first rule that matches decides:
Loopback has no class.
A device whose parent bus is
xen-backend, Xen’s netback in dom0, is virtual: it is the host end of a guest’s link, though its driver is namedvifas netfront’s is.A device whose driver is
virtio_net,vmxnet3,hv_netvsc,xen-netfrontorvifis a VM adapter.A device whose driver is
netdevsimis virtual, whatever its devlink port says.A device with a devlink port takes its class from the port’s flavour: physical is network-facing; CPU, DSA, PCI PF, PCI VF, PCI SF and virtual are virtual; any other flavour is unknown.
A device with a parent bus, a hardware bus, is network-facing.
A device with a link kind, a kernel software link, is virtual.
Any other device is unknown.
The parent bus and the link kind come from rtnetlink, the driver name from ethtool and the port flavour from devlink, without interface-name or sysfs heuristics.
Device discovery#
Load and boot restore each take one snapshot of the interfaces in the initial network namespace, select the matching classes and attach in increasing ifindex order. A veth pair gets one attachment: the endpoint in the initial namespace when its peer is elsewhere, or the lower ifindex when both ends are in that namespace. Peer namespace information prevents equal ifindexes in separate namespaces from being mistaken for a pair. Pairs entirely outside the initial namespace are excluded.
Discovery tolerates interfaces changing under it: a snapshot the kernel
reports as interrupted is retried, and an interface renamed or removed
during the snapshot, or a veth whose peer is not in it, is left out
rather than failing the load. So is a device whose driver name or
devlink port cannot be read, with a warning naming it: it need not be
one the upatch selects, so it does not fail the load or the boot
restore, and status lists it under XdpCovered as not
classified until a later read succeeds, unless it runs the upatch. A
lookup that cannot be made at all, out of memory or descriptors, timed
out, refused with EPERM or EACCES, still fails the load: it would
leave every device out. A kernel that does not answer a netlink request
within 5 seconds fails the load with ETIMEDOUT, and is not asked
again.
Attachment#
shirudo attaches in the driver only, never replaces an XDP program a device runs, and checks each link before it pins it. Outcomes gives what each condition does; this section adds what the table cannot.
Links use XDP_FLAGS_DRV_MODE. Generic XDP, which runs in the stack
after the socket buffer is built, is not supported, as it filters too
late to be of use, so a driver without native XDP is not covered. A
device that runs a generic or offloaded program, which shirudo never
attaches, holds its XDP slot: it is occupied like one running another
driver-mode program.
A device without native XDP refuses with EOPNOTSUPP and reports no
XDP features, as a bridge does. Any other refusal is the driver ruling
XDP out in its current settings, xdp_driver_refused: EOPNOTSUPP
from a device that reports native XDP, as virtio_net gives while the
host does GRO_HW or checksum offload and bnxt with header-data
split, EINVAL from mlx5 with LRO or hardware GRO on, ENOSPC
from a veth with fewer rx queues than its peer has tx queues, and
ERANGE from a driver whose MTU is too large. The journal names the
driver’s own errno.
An XDP program with no device to attach to still loads: a pin of its
own, xdp_prog_<i>, keeps it. Boot restore attaches the same way:
it skips what a load skips and fails where a load fails, and init
then stops with the register unbound. A device that a upatch restored
before this one holds fails the restore like any other, also one that
reconcile only warned about before the reboot.
A setting root makes after boot is the reconcile pass’s to meet, which
leaves the device unattached, warns, and lets status list it.
Device classification and peer identity are rechecked around attachment, and the returned link’s ifindex is checked before pinning. The link reports no namespace, so the load checks its own before it reads the devices.
Multi-buffer drivers#
Every XDP program is loaded with BPF_F_XDP_HAS_FRAGS, so it can run on
a device whose MTU does not fit one buffer, and a selected device with
native XDP must report multi-buffer support, NETDEV_XDP_ACT_RX_SG.
One that does not fails upatch load with
xdp_multibuf_unsupported, and the load is undone, so a single such
device under the signed mask stops it: narrow the mask. Boot restore
fails on such a device too. Reconcile leaves it unattached, logs a
warning, and status lists it under XdpCovered. In a tree, a
failed add stops bpm tree sync at that operation, so scope it
with a node label.
These drivers report multi-buffer XDP in Linux bpf-next at 53435562a725:
Driver |
Hardware or use |
|---|---|
|
Virtual Ethernet pairs |
|
Virtio guest adapters, with mergeable receive buffers |
|
Intel |
|
NVIDIA ConnectX, with LRO and hardware GRO off |
|
Broadcom NetXtreme |
|
AMD Pensando |
|
Meta |
|
Marvell (Aquantia) AQtion |
|
Marvell Armada, without hardware buffer management |
|
NXP |
|
When every slave supports it |
The list is a guide. What counts is what the device reports when it is
attached, and that can depend on its configuration: a veth with fewer
rx queues than its peer has tx queues reports nothing. ena, gve,
mana, netvsc, vmxnet3, xen-netfront, igb, igc,
ixgbe and mlx4 have native XDP without multi-buffer.
Reconcile#
The signed artifact and the TPM measurement cover the class policy, not
a fixed device inventory, so the devices a upatch runs on change after
its load. shirudo upatch reconcile repeats the discovery
of load and restore later: it attaches a loaded upatch to any selected
device it does not run on yet and removes the pin of a link whose device
has gone. Removing a device detaches its XDP link, so init and
status do not count XDP link pins as they do other links: they
require the xdp_prog_<i> pin of each XDP program and a link for each
other attach program of the upatch. The location status and
upatch get report names the first device still attached and how
many more there are, as in xdp/eth0 (+1), or xdp (waiting for a
device) when there is none.
Outcomes#
The table sums up what each condition does at load and boot restore, at
reconcile and in status. Skipped means the program is not attached
to that device and the operation carries on with the others; reconcile
tries the device again on its next pass. Failed means the operation
stops: a load is undone, init stops with the register unbound, and
a reconcile pass exits 1.
Condition |
Load category |
Journal |
Load and boot restore |
Reconcile |
|
|---|---|---|---|---|---|
No native XDP, such as a bridge |
none |
|
Skipped |
Skipped |
|
No multi-buffer support |
|
|
Failed |
Skipped, with a warning on every pass |
|
The driver refuses in its current settings |
|
|
Failed |
Skipped, with a warning on every pass |
|
Another XDP program on the device, in any mode |
|
|
Failed |
Skipped, without a warning |
|
A link on another device, or a run outside the initial network namespace |
|
|
Failed |
Failed |
No condition of its own |
The device changed or went away during the attach |
none |
|
Skipped |
Skipped |
None |
The driver name or devlink port cannot be read |
none |
|
Skipped |
Skipped |
|
No device matches the mask |
none |
|
Loads and waits |
Attaches when a device appears |
|
The archive differs from the loaded payload |
not a load failure |
|
Not checked |
Failed, |
|
An |
not a load failure |
|
Not checked |
Failed, |
|
An |
not a load failure |
The same line |
Not checked |
Failed, |
None: |
The guards refuse the pin or the link |
not a load failure |
The same line, or the report message |
Not checked |
Failed, |
The verdict does not change. The tamper counters count a refusal by shirudo’s own guards. |
The pin cannot be opened for lack of descriptors or memory |
not a load failure |
The same line |
Not checked |
Failed, |
None |
status reports what it finds itself, not the result of a reconcile
pass, and the keepalive carries the verdict of status. A load that
fails is undone, so it does not change the verdict; the journal, the
report and the tree sync report carry the failure. A boot restore that
fails leaves the host reboot-pending. Failure categories and exit status
has the errno and the remedy of each category, and
Read shirudo status each reason XdpCovered gives.
Programs that attach nowhere#
Two roles are recognized by the program’s name, and both must be in
SEC("syscall"). Any other syscall program is refused.
shirudo_serialize__<map>Renders
<map>into records for export. shirudo pins it atserializers/<map>under the upatch’s pin directory, andshirudo upatch mapruns it withBPF_PROG_TEST_RUN. The upatch must also define theshirudo_seroutput map. One serializer per map. Maps, arenas and serializers describes the interface.shirudo_test__<id>Pinned as
testand run once withBPF_PROG_TEST_RUNand a zeroed 16-byte context, after all attachments. A negative return fails the load. At most one per upatch.
The LSM allows BPF_PROG_TEST_RUN from userspace only to tasks with
shirudo’s runtime authority, so root cannot run a pinned serializer or
test-run program itself.
Refused sections#
shirudo build refuses these sections with the message shown, before
it generates a loader.
Section |
Message |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
every other section, |
|
|
|
Attach-scope check#
The site a kprobe, uprobe, cgroup or netfilter program attaches at depends
on state that root can arrange on the target: the mount of
/sys/fs/cgroup, the network namespace of the loading process, the
sysfs files that name the kprobe and uprobe PMUs. Before shirudo pins a
link, it asks the kernel about the link and compares the answer with the
signed metadata:
Kind |
The kernel must report |
|---|---|
|
a cgroup link on cgroup id 1, the root of the unified hierarchy |
|
a netfilter link for IPv6, pre-routing, priority -450, created by a task in the initial network namespace |
|
a perf event link of the kprobe or kretprobe type, with the same symbol and offset |
|
a kprobe multi link with the same symbol count and direction |
|
a perf event link of the uprobe or uretprobe type, with the recorded file offset and no reference counter. Not the file name: the descriptor the event was opened through fixes the inode, and the name the kernel reports can differ from the signed path by a symlink |
|
a uprobe multi link with as many offsets as recorded and the same direction |
|
a tracing link with attach type |
|
a tracing or raw tracepoint link |
On a mismatch shirudo closes the link, which detaches it, removes the pins
this load made and fails with attach_scope_mismatch in phase
link_create.
Missing attach targets#
attach_target_missing reports a site that does not exist on this
target: a kprobe symbol that kallsyms does not list, often because the
module that holds it is not loaded, a kernel without the kprobe PMU, a
missing /sys/fs/cgroup, or the binary of a uprobe, uretprobe or
their multi forms that is not there. shirudo upatch load exits with status 2 for
it, not 1.
The BTF-based kinds never report it. fentry, fexit, fmod_ret,
lsm and tp_btf targets are looked up inside the signed loader, and
a missing one fails the load as verifier_rejected in phase
payload_load with errno ENOENT.
bpm tree sync treats an add that fails with
attach_target_missing as applied: it logs bpm tree: skipped <id>:
module not loaded, measures the operation and moves past it, and does
not retry it when the module loads later. bpm install and bpm
upgrade fail and roll back instead. Narrow tree selectors keep an entry
away from targets that lack its site.