eBPF-based security platform
Documentation: list what an upatch program misses or cannot rely on
Daniel Borkmann1a7480798f6d
diff --git a/AGENTS.md b/AGENTS.mdindex 0fe99e45..a2069416 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -417,6 +417,26 @@ one usually still compiles and passes tests.review and blitmus.`Documentation/upatches/write/memory-consistency/hazards.rst` sayswhere each hazard applies.+- **Plan for what an upatch program does not see.** The kernel skips+ `kprobe` and `kretprobe` runs while another one runs on the CPU or the+ CPU is in a `bpf()` map call, runs of an `fentry`, `fexit`, `fmod_ret`+ or `tp_btf` program already running there, and `kretprobe` returns+ beyond its instances, so a detector can miss what it looks for. Refuse+ an operation in an `lsm` program wherever a hook covers it, never in+ `fmod_ret`: a skipped `fmod_ret` run lets the call through, and a task+ can arrange the skip. An `lsm` program does not see what an LSM before+ it refused, and its hook runs before the operation, which can still+ fail. In a softirq the current task is whichever one was interrupted. A+ sleepable program walks RCU pointers under `bpf_rcu_read_lock()`. Check+ map update and delete results where a lost update matters, size LRU+ maps with room to spare, and read values userspace rewrites under+ `BPF_F_LOCK`. Prefer `lsm`, `tp_btf` and `fentry` to `kprobe`, which+ misses calls the compiler inlined.+ `Documentation/upatches/write/pitfalls.rst` explains each. When+ `shirudo build` starts accepting another attach kind, the same commit+ adds its run context to the table in `hazards.rst` and what it misses+ or cannot rely on to `pitfalls.rst`, so that agents writing upatches+ for it meet its hazards there.## Memory, JSON and loggingdiff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex eb031783..501a0dd8 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -119,6 +119,11 @@ error-injection marks take effect only with``CONFIG_FUNCTION_ERROR_INJECTION``. On any other function the load failsin 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. :doc:`pitfalls` says when, and+why an ``lsm`` program is the place to refuse an operation where a hook+covers it.+lsm---diff --git a/Documentation/upatches/write/index.rst b/Documentation/upatches/write/index.rstindex 6e084683..3277a05e 100644--- a/Documentation/upatches/write/index.rst+++ b/Documentation/upatches/write/index.rst@@ -7,8 +7,8 @@ WriteWhere a program can attach, how an upatch keeps and exports its state inmaps, arenas and serializers, how a Lua decoder turns that state into-telemetry, and how to test the memory ordering that programs on several-CPUs rely on.+telemetry, what a program can miss or cannot rely on, and how to test+the memory ordering that programs on several CPUs rely on... toctree:::titlesonly:@@ -16,4 +16,5 @@ CPUs rely on.attach-kindsmaps-arenas-serializersdecoders+ pitfallsmemory-consistency/indexdiff --git a/Documentation/upatches/write/memory-consistency/hazards.rst b/Documentation/upatches/write/memory-consistency/hazards.rstindex f53935a9..baf61f15 100644--- a/Documentation/upatches/write/memory-consistency/hazards.rst+++ b/Documentation/upatches/write/memory-consistency/hazards.rst@@ -153,7 +153,10 @@ of a map that is not per-CPU, and for inode, socket and cgroup storage,which hooks on different CPUs reach for the same inode, socket orcgroup. Update such a value with ``__sync_fetch_and_add()``,``__sync_fetch_and_or()`` and their kin, which do it in one instruction.-``bpf_map_rmw`` refuses the plain forms.+``bpf_map_rmw`` refuses the plain forms. A global variable is a value+too, of the one array map that holds the object's ``.bss`` or ``.data``,+which every CPU shares; ``bpf_map_rmw`` does not see it, so update it+the same way.A per-CPU counter needs an atomic only where a run can come in betweenon the same CPU: in a program the kernel does not guard, as listed@@ -185,6 +188,14 @@ after a delete from the same map; it cannot tell whether the two keysare equal and takes any delete. Testing the pointer against ``NULL``reads nothing and is left alone.+An update of a key the hash map already holds does the same to the old+element: the map links a new element in its place and hands the old one+to the next update on that CPU. A program that holds a pointer from a+lookup and updates the value through it changes the entry in place; one+that replaces the entry with ``bpf_map_update_elem()`` while other runs+hold pointers to it lets them read or write a stranger's entry. No rule+refuses this.+Compare-and-swap----------------@@ -259,6 +270,8 @@ Related-------- :doc:`blitmus` to test an ordering on each architecture+- :doc:`/upatches/write/pitfalls` for what a program misses or cannot+ rely on- :doc:`/upatches/write/maps-arenas-serializers` for the arena table andits serializer- :doc:`/contribute/code/developer-guide` for the Coccinelle rulesdiff --git a/Documentation/upatches/write/pitfalls.rst b/Documentation/upatches/write/pitfalls.rstnew file mode 100644index 00000000..474a5382--- /dev/null+++ b/Documentation/upatches/write/pitfalls.rst@@ -0,0 +1,137 @@+..+ SPDX-License-Identifier: BSD-2-Clause+ Copyright (c) 2026 Authors of shirudo++:page-type: Explanation+:audience: upatch authors++Pitfalls+========++A program that loads and runs can still miss the events it is meant to+see, decide on data that is not what it looks like, or let through what+it is meant to refuse. Neither the verifier nor a test in a quiet VM+shows any of it. This page lists what the kernel does that an upatch has+to plan for, for the attach kinds ``shirudo build`` accepts.+:doc:`memory-consistency/hazards` covers the runs that interleave.++Runs the kernel skips+---------------------++The kernel skips some runs of a program and counts them only in the+program's ``recursion_misses``, which ``struct bpf_prog_info`` reports:++- A ``kprobe`` or ``kretprobe`` run, while another ``kprobe`` or+ ``kretprobe`` program runs on the CPU, and while the CPU is in a+ ``bpf()`` call that looks up, updates or deletes a map element from+ userspace, as shirudo makes when it reads an upatch's maps. A+ ``kprobe`` hit inside the handler of another ``kprobe`` is missed by+ the kprobe itself.+- An ``fentry``, ``fexit``, ``fmod_ret`` or ``tp_btf`` run of a program+ that is already running on the CPU: one a preempted task left there+ with preemptible RCU, or one an NMI interrupted.+- The return of a function a ``kretprobe`` watches, when more calls of+ it are in flight than the probe has instances for: the larger of 10+ and twice the number of CPUs.++A detector built on these kinds can therefore miss the event it looks+for, and a count it keeps is a lower bound. ``lsm`` programs are never+skipped.++Enforce in lsm programs+-----------------------++``fmod_ret`` refuses a call when its program returns non-zero. A run the+kernel skips returns nothing, and the call goes through as if the+program had returned 0. Another task can make that happen on purpose: it+calls the function on a CPU where a run of the program waits, which an+``fmod_ret.s`` run that sleeps leaves on any kernel and a preempted+``fmod_ret`` run with preemptible RCU. Refuse an operation in an ``lsm``+program, which the kernel runs every time its hook is reached, and keep+``fmod_ret`` for functions no LSM hook covers, knowing that it can be+bypassed.++LSM hook order and outcome+--------------------------++``security_<hook>()`` calls the LSMs in the order ``lsm=`` or+``CONFIG_LSM`` gives and stops at the first one that refuses. An ``lsm``+program never sees an operation that an LSM before the BPF LSM refused,+such as SELinux, AppArmor or Landlock, and a refusal by the program+keeps the LSMs after it from seeing it. Hooks that return nothing run+every LSM.++A hook runs before the operation it guards, which can still fail after+it: an LSM later in the order refuses it, memory runs out, the network+does not answer. A record taken in ``socket_connect`` or ``file_open``+is of an attempt. Where the outcome matters, take it from a hook the+kernel calls afterwards, such as ``socket_post_create`` or+``bprm_committed_creds``.++The current task in a softirq+-----------------------------++``cgroup_skb`` ingress, ``netfilter`` and the socket hooks the network+stack calls on receive run in a softirq, on top of whatever task the CPU+was running. There ``bpf_get_current_pid_tgid()``,+``bpf_get_current_task_btf()``, ``bpf_get_current_comm()`` and+``bpf_get_current_cgroup_id()`` describe that task, not the packet.+Attribute a packet by its socket, with ``bpf_skb_cgroup_id()`` or+socket storage, and keep such programs off task storage.++RCU pointers in sleepable programs+----------------------------------++In a sleepable program, a pointer read from a field the kernel protects+with RCU, such as ``task->real_parent``, is trusted only between+``bpf_rcu_read_lock()`` and ``bpf_rcu_read_unlock()``. Outside them the+verifier marks it untrusted: a load through it becomes a read that+tolerates a fault and can return memory the kernel freed and reused, and+a kfunc that needs a trusted argument refuses it. Walk such pointers+inside the lock and copy out what the program needs.++Map updates that fail+---------------------++``bpf_map_update_elem()`` and ``bpf_map_delete_elem()`` on a hash map+take a lock on the key's bucket and fail with ``-EDEADLK`` or+``-ETIMEDOUT`` when a run nests on a CPU that holds it, as a ``kprobe``+or an NMI inside another update can. An update of a new key fails with+``-E2BIG`` when the map is full. Check the result and count the+failures, or the program loses records without a trace.++An LRU hash map never fails for lack of room, but it keeps part of its+free elements on each CPU's own list and evicts from the shared lists+once these run short, so an insert can evict an entry before the map is+full. Size it with room to spare, and expect a lookup to miss a key that+was inserted earlier.++Values userspace writes+-----------------------++An update of an array map element from userspace copies the value+without a lock, so a program that reads it meanwhile can see part of the+old value and part of the new one. Give the value a ``struct+bpf_spin_lock`` and update it with ``BPF_F_LOCK``, read it under+``bpf_spin_lock()``, or have userspace write a fresh element and switch+an index to it.++Attach points that miss calls+-----------------------------++A ``kprobe`` on a function the compiler inlined into some of its+callers fires only for the calls that reach the out-of-line copy, and a+kernel built with another configuration or compiler inlines differently.+An ``fentry`` on a function without a BTF entry fails to attach, which+is the better failure. A probe on a system call's entry point misses the+same operation started through ``io_uring`` or from inside the kernel;+an LSM hook sits on the operation itself. Prefer ``lsm``, ``tp_btf`` and+``fentry`` to ``kprobe``, and test that a detector fires on the kernel it+ships to: a probe that never fires looks the same as no attack.++Related+-------++- :doc:`attach-kinds` for each attach kind and what the kernel needs for it+- :doc:`memory-consistency/hazards` for runs that interleave+- :doc:`maps-arenas-serializers` for the maps an upatch keeps state in