eBPF-based security platform
Documentation: add docs for standard uprobe attachment
Jiri Olsa75172f665cf9
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 1e6b2cf7..509d145b 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -161,9 +161,10 @@ signed config image.* - ``t3006-cgroup-skb-metadata``- ``cgroup_skb`` ingress and egress attach metadata* - ``t3007-uprobe-multi-metadata``- - ``uprobe.multi`` and ``uretprobe.multi`` globs, sleepable ``.s``- forms included, to file offsets and the target's build ID, and the- uprobe forms the builder refuses+ - ``uprobe`` and ``uretprobe`` functions, function offsets and raw+ file offsets, ``uprobe.multi`` and ``uretprobe.multi`` globs,+ sleepable ``.s`` forms included, to file offsets and the target's+ build ID, and the uprobe forms the builder refuses* - ``t3008-override-metadata``- kprobe override and ``fmod_ret`` attach metadata* - ``t3009-netfilter-metadata``@@ -191,8 +192,10 @@ signed config image.- ``upatch get`` source lines and JSON* - ``t3107-uprobe-multi``- A live ``uprobe.multi`` load, refusal after a build ID change, a- live ``uretprobe.multi`` load that runs and unloads, and a live- sleepable ``uprobe.multi.s`` load that reads user memory+ live ``uretprobe.multi`` load that runs and unloads, a live+ sleepable ``uprobe.multi.s`` load that reads user memory, and live+ ``uprobe`` and ``uretprobe`` loads that run once, report their+ checked ``/proc/self/fd/<n>`` target paths and unload* - ``t3108-status-many``- ``status --json`` with eighty loaded upatches lists all of them* - ``t3109-policy-labels``@@ -664,8 +667,8 @@ Coverage by area- Offline metadata: ``t3006``, ``t3007``, ``t3008``, ``t3009``,``t3010``. Loaded on a target: fentry (fixtures), ``lsm``(``t9101``), ``cgroup_skb`` and netfilter (``t6116``),- ``uprobe.multi``, ``uretprobe.multi`` and ``uprobe.multi.s``- (``t3107``)+ ``uprobe``, ``uretprobe``, ``uprobe.multi``, ``uretprobe.multi`` and+ ``uprobe.multi.s`` (``t3107``)* - bpffs pins and mounts- ``t4101``, ``t6105``, ``t6106``, ``t6108``, ``t6120``,``t6121``diff --git a/Documentation/internals/kernel-interfaces.rst b/Documentation/internals/kernel-interfaces.rstindex f0a320c8..7e3ad0b5 100644--- a/Documentation/internals/kernel-interfaces.rst+++ b/Documentation/internals/kernel-interfaces.rst@@ -199,7 +199,8 @@ where the caller is the loader.``attach_type`` ``BPF_MODIFY_RETURN``. Kinds 3 and 4,``cgroup_skb``: ``target_fd`` an ``O_DIRECTORY`` descriptor of``/sys/fs/cgroup``, ``attach_type`` ``BPF_CGROUP_INET_INGRESS`` or- ``BPF_CGROUP_INET_EGRESS``. Kinds 1 and 2, kprobe and kretprobe:+ ``BPF_CGROUP_INET_EGRESS``. Kinds 1 and 2, kprobe and kretprobe,+ and kinds 10 and 11, uprobe and uretprobe with their ``.s`` forms:``target_fd`` a perf event, ``attach_type`` ``BPF_PERF_EVENT``.Kind 6, netfilter: ``attach_type`` ``BPF_NETFILTER``, ``pf`` 10(``NFPROTO_IPV6``), ``hooknum`` 0 (``NF_INET_PRE_ROUTING``),@@ -217,8 +218,9 @@ where the caller is the loader.- On each link before it is pinned. Cgroup links: ``cgroup_id`` 1.Netfilter: ``pf``, ``hooknum``, ``priority`` as set, and thecaller's network namespace cookie (``SO_NETNS_COOKIE``) 7. Kprobe:- perf event type, function name and offset as signed. Uprobe- multi: offset count and the return flag. ``fmod_ret``:+ perf event type, function name and offset as signed. Uprobe:+ perf event type, the file offset as signed and ``ref_ctr_offset``+ 0. Uprobe multi: offset count and the return flag. ``fmod_ret``:``BPF_MODIFY_RETURN``.XDP: ``BPF_LINK_TYPE_XDP`` and ``xdp.ifindex`` matching the selecteddevice; the initial namespace, device class and peer scope are@@ -232,7 +234,10 @@ A kprobe link's perf event comes from ``perf_event_open(2)`` with``type`` read from ``/sys/bus/event_source/devices/kprobe/type``,``config1`` the symbol, ``config2`` the offset, the bit named in``.../kprobe/format/retprobe`` set in ``config`` for a kretprobe, ``pid``--1, ``cpu`` 0 and ``PERF_FLAG_FD_CLOEXEC``.+-1, ``cpu`` 0 and ``PERF_FLAG_FD_CLOEXEC``. A uprobe link's comes from+the same call on the uprobe PMU, ``/sys/bus/event_source/devices/uprobe``,+with ``config1`` the path ``/proc/self/fd/<n>`` of the target binary's+checked descriptor and ``config2`` the recorded file offset.An unload directive makes the first four calls and no others.diff --git a/Documentation/internals/signed-loader.rst b/Documentation/internals/signed-loader.rstindex 89121213..114822bf 100644--- a/Documentation/internals/signed-loader.rst+++ b/Documentation/internals/signed-loader.rst@@ -97,9 +97,9 @@ loader inside the kernel, which ``lsm/bpf`` sees with ``kernel`` true.- The file is read once into ``/var/lib/shirudo/incoming``. Thesigned name must equal the header name, ``payload_sha256`` mustequal SHA-256 of ``loader_insns || data``, and the signed attach- metadata must parse. Each ``uprobe.multi`` and ``uretprobe.multi``- target is opened and its GNU build ID compared before any BPF- loads.+ metadata must parse. Each ``uprobe``, ``uretprobe``,+ ``uprobe.multi`` and ``uretprobe.multi`` target is opened and its+ GNU build ID compared before any BPF loads.* - userspace- ``BPF_MAP_CREATE`` of ``__loader.map``- ``lsm/bpf`` allows it to a task with runtime authority, elsediff --git a/Documentation/operate/plan/scalability.rst b/Documentation/operate/plan/scalability.rstindex a9f3264d..cb3d9285 100644--- a/Documentation/operate/plan/scalability.rst+++ b/Documentation/operate/plan/scalability.rst@@ -183,8 +183,8 @@ without ``CAP_BPF``. What bounds a target is the attach point.* - ``tp_btf``- No limit per tracepoint-- * - ``kprobe``, ``kretprobe``- - No limit per symbol; every attachment is a perf event of its own+ * - ``kprobe``, ``kretprobe``, ``uprobe``, ``uretprobe``+ - No limit per site; every attachment is a perf event of its own-* - ``uprobe.multi``, ``uretprobe.multi``- 1,048,576 offsets per link, which shirudo also enforcesdiff --git a/Documentation/operate/plan/system-requirements.rst b/Documentation/operate/plan/system-requirements.rstindex 3f0ab7a8..d80a14c6 100644--- a/Documentation/operate/plan/system-requirements.rst+++ b/Documentation/operate/plan/system-requirements.rst@@ -253,7 +253,8 @@ point is missing fails to load.* - ``CONFIG_BPF_KPROBE_OVERRIDE``- ``bpf_override_return()`` from a ``kprobe`` upatch.* - ``CONFIG_UPROBE_EVENTS``- - ``uprobe.multi`` and ``uretprobe.multi`` upatches.+ - ``uprobe``, ``uretprobe``, ``uprobe.multi`` and+ ``uretprobe.multi`` upatches.* - ``CONFIG_CGROUP_BPF``- ``cgroup_skb`` upatches.* - ``CONFIG_NETFILTER``, ``CONFIG_NETFILTER_BPF_LINK``diff --git a/Documentation/reference/failure-categories.rst b/Documentation/reference/failure-categories.rstindex 89bab562..153dd1de 100644--- a/Documentation/reference/failure-categories.rst+++ b/Documentation/reference/failure-categories.rst@@ -40,8 +40,9 @@ measures in ``tpm_extend`` and removes the pins in ``pin``.name, the signed name, ``payload_sha256`` against the loader anddata, and the signed attach metadata.* - ``target_verify``- - Opening each ``uprobe.multi`` and ``uretprobe.multi`` target binary- and comparing its GNU build ID with the one the artifact records.+ - Opening each ``uprobe``, ``uretprobe``, ``uprobe.multi`` and+ ``uretprobe.multi`` target binary and comparing its GNU build ID+ with the one the artifact records.* - ``signed_loader_load``- Creating, filling and freezing ``__loader.map`` and the``BPF_PROG_LOAD`` of the signed loader, where the kernel verifiesdiff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex 3f6cc64c..38a35135 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -361,7 +361,9 @@ Each program entry is 160 bytes:* - +8- 8- ``offset`` / ``xdp``- - Byte offset into the function for ``kprobe``. For XDP, a+ - Byte offset into the function for ``kprobe`` and for ``uprobe`` at a+ function; the file offset the target uses is in the ``UPUT`` record.+ For XDP, alittle-endian ``u32 device_classes`` followed by ``u32 reserved``(0). The mask is nonzero and uses only bits 1, 2, 4, 8 and 16.0 for every other kind and role.@@ -369,9 +371,10 @@ Each program entry is 160 bytes:- 128- ``target``- NUL-terminated. The kernel symbol for ``kprobe`` and- ``kretprobe``, the function glob for ``uprobe.multi`` and- ``uretprobe.multi`` (no ``,`` or ``|``). Empty for every other kind- and role.+ ``kretprobe``, the function or the ``0x`` file offset as written for+ ``uprobe`` and ``uretprobe``, the function glob for ``uprobe.multi``+ and ``uretprobe.multi`` (no ``,`` or ``|``). Empty for every other+ kind and role.* - +144- 16- ``map``@@ -435,6 +438,16 @@ Each program entry is 160 bytes:- uretprobe.multi- ``uretprobe.multi/<absolute-path>:<function-glob>``,``uretprobe.multi.s/<absolute-path>:<function-glob>``+ * - 10+ - uprobe+ - ``uprobe/<absolute-path>:<function>[+<offset>]``,+ ``uprobe/<absolute-path>:0x<file-offset>``, and their ``uprobe.s``+ forms+ * - 11+ - uretprobe+ - ``uretprobe/<absolute-path>:<function>``,+ ``uretprobe/<absolute-path>:0x<file-offset>``, and their+ ``uretprobe.s`` forms:doc:`/upatches/write/attach-kinds` describes how the target attaches each kind.XDP retains the 160-byte entry and UPGM version 1; readers that do not@@ -444,9 +457,9 @@ selects all classes. Bit 4 is not a class, and a reader refuses a maskthat sets it or any bit above 16, and a table whose XDP entries share aclass.-When any entry has kind 7 or 9, one ``UPUT`` block follows the program-entries and ends the section. Without a kind 7 or 9 entry the section ends-after the program entries.+When any entry has kind 7, 9, 10 or 11, one ``UPUT`` block follows the+program entries and ends the section. Without such an entry the section+ends after the program entries... list-table:::header-rows: 1@@ -467,8 +480,8 @@ after the program entries.* - +8- 4- ``nr_targets``- - Number of target records, equal to the number of kind 7 and 9- entries.+ - Number of target records, equal to the number of kind 7, 9, 10 and+ 11 entries.* - +12- 4- ``reserved``@@ -486,8 +499,8 @@ variable parts:- Value* - +0- ``prog_idx``- - Index of a kind 7 or 9 program entry. Each such entry has exactly one- record.+ - Index of a kind 7, 9, 10 or 11 program entry. Each such entry has+ exactly one record.* - +4- ``path_len``- Length of the path, 1 to 4095.@@ -496,7 +509,7 @@ variable parts:- Length of the GNU build ID, 1 to 64.* - +12- ``nr_offsets``- - Number of offsets, 1 to 1048576.+ - Number of offsets, 1 to 1048576; exactly 1 for kinds 10 and 11.* - +16- path- ``path_len`` bytes, an absolute path without a NUL.@@ -763,9 +776,11 @@ of checks 1 to 6 is reported as ``parser_rejected``, see#. A ``UPGM`` section is present and parses: version 1 (any other versionis ``EOPNOTSUPP``), ``nr_maps`` and ``nr_progs`` equal to the header's,every entry valid for its role and kind, and a ``UPUT`` block that is- present exactly when a kind 7 or 9 entry exists and ends the section.-#. Each ``uprobe.multi`` and ``uretprobe.multi`` target file is a regular- file whose GNU build ID equals the recorded one.+ present exactly when a kind 7, 9, 10 or 11 entry exists and ends the+ section.+#. Each ``uprobe``, ``uretprobe``, ``uprobe.multi`` and+ ``uretprobe.multi`` target file is a regular file whose GNU build ID+ equals the recorded one.#. The kernel verifies the signature in ``BPF_PROG_LOAD``.An unload directive passes the reader, then these checks:@@ -835,12 +850,13 @@ Limits* - Name of an upatch- 255 characters of ``[A-Za-z0-9._-]``, each ``.`` counting as two,not starting with ``.``- * - ``kprobe``, ``kretprobe``, ``uprobe.multi`` or ``uretprobe.multi``- target+ * - ``kprobe``, ``kretprobe``, ``uprobe``, ``uretprobe``,+ ``uprobe.multi`` or ``uretprobe.multi`` target- 127 bytes* - Serializer map name- 15 bytes- * - ``uprobe.multi`` or ``uretprobe.multi`` path+ * - ``uprobe``, ``uretprobe``, ``uprobe.multi`` or ``uretprobe.multi``+ path- 4095 bytes* - GNU build ID- 1 to 64 bytesdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex 70a76434..1098e3c3 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1545,7 +1545,8 @@ R20 The TPM owner password is not a secret on the targetR21 upatches can be neutralized by changing their targetsAn upatch stays loaded and measured while root makes it ineffective:by unloading the module a kprobe targets, by replacing the binary a- ``uprobe.multi`` or ``uretprobe.multi`` target names with a new inode,+ ``uprobe``, ``uretprobe``, ``uprobe.multi`` or ``uretprobe.multi``+ target names with a new inode,or, without lockdown (A4), by disabling kprobes through debugfs. Theseare readings of the kernel source and are not tested.diff --git a/Documentation/upatches/learn/concepts.rst b/Documentation/upatches/learn/concepts.rstindex 15b41031..62d847eb 100644--- a/Documentation/upatches/learn/concepts.rst+++ b/Documentation/upatches/learn/concepts.rst@@ -256,8 +256,9 @@ target's before it ships.It cannot attach anywhere other than where its signed metadata says.Before shirudo pins a link, it compares what the kernel reports about thelink with the metadata: the link type and attach type, the kprobe symbol-and offset, the cgroup, the netfilter hook and network namespace, the-number of offsets and the direction of a uprobe multi link. A mismatch+and offset, the uprobe file offset, the cgroup, the netfilter hook and+network namespace, the number of offsets and the direction of a uprobe+multi link. A mismatchunpins what the load made and fails with ``attach_scope_mismatch``. Forfentry, fexit, ``fmod_ret``, LSM and ``tp_btf`` programs the site is theBTF target the verifier bound at load.diff --git a/Documentation/upatches/test/debugging.rst b/Documentation/upatches/test/debugging.rstindex 7d88b606..94653365 100644--- a/Documentation/upatches/test/debugging.rst+++ b/Documentation/upatches/test/debugging.rst@@ -98,7 +98,8 @@ Find the cause* - ``target_verify``- ``attach_target_missing``, ``binary_mismatch``,``parser_rejected``, ``lsm_denied``- - A ``uprobe.multi`` or ``uretprobe.multi`` binary is missing,+ - The binary of a ``uprobe``, ``uretprobe``, ``uprobe.multi`` or+ ``uretprobe.multi`` upatch is missing,differs from the one the upatch was built against, is shorterthan a recorded offset, or could not be read. Rebuild againstthe binary the target runs.diff --git a/Documentation/upatches/test/testing.rst b/Documentation/upatches/test/testing.rstindex 49af8ca9..aa5460ce 100644--- a/Documentation/upatches/test/testing.rst+++ b/Documentation/upatches/test/testing.rst@@ -193,6 +193,9 @@ one.* - ``shirudo-test-uretprobe-multi``- ``uretprobe.multi`` on the same binary and functions, counting inthe map ``hits``+ * - ``shirudo-test-uprobe``, ``shirudo-test-uretprobe``+ - ``uprobe`` and ``uretprobe`` on ``fooFunc`` of the same binary,+ counting in ``hits``* - ``shirudo-test-uprobe-multi-sleepable``- sleepable ``uprobe.multi.s`` on the same binary and functions,counting in ``hits`` each call whose ``bpf_copy_from_user()`` of thediff --git a/Documentation/upatches/write/attach-kinds.rst b/Documentation/upatches/write/attach-kinds.rstindex 3c4f6959..6e2dd12d 100644--- a/Documentation/upatches/write/attach-kinds.rst+++ b/Documentation/upatches/write/attach-kinds.rst@@ -52,6 +52,17 @@ the link against the metadata and pins it.- kprobe- as ``kprobe``, with the return-probe bit set- ``CONFIG_KPROBES``, ``CONFIG_KPROBE_EVENTS``+ * - ``uprobe/<path>:<function>[+<offset>]``, ``uprobe/<path>:0x<offset>``,+ their ``uprobe.s`` forms+ - kprobe+ - ``perf_event_open`` on the uprobe PMU, ``BPF_LINK_CREATE``,+ ``BPF_PERF_EVENT``+ - ``CONFIG_UPROBE_EVENTS``+ * - ``uretprobe/<path>:<function>``, ``uretprobe/<path>:0x<offset>``,+ their ``uretprobe.s`` forms+ - kprobe+ - as ``uprobe``, with the return-probe bit set+ - ``CONFIG_UPROBE_EVENTS``* - ``uprobe.multi/<path>:<glob>``, ``uprobe.multi.s/<path>:<glob>``- kprobe- ``BPF_LINK_CREATE``, ``BPF_TRACE_UPROBE_MULTI``,@@ -183,6 +194,39 @@ allows that only at the entry of a function on its error-injection listand with ``CONFIG_BPF_KPROBE_OVERRIDE``. Elsewhere the kernel refusesthe link, and the load fails in phase ``link_create``.+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--------------------------------@@ -608,12 +652,22 @@ it generates a loader.``kretprobe.multi*``- ``unsupported kprobe section '<section>'; use kprobe/<symbol> orkretprobe/<symbol>``- * - ``uprobe``, ``uprobe/*``, ``uretprobe``, ``uretprobe/*``, other- ``uprobe.*`` and ``uretprobe.*`` forms, ``uprobe.session`` and+ * - ``uprobe`` and ``uretprobe`` without a binary, other ``uprobe.*``+ and ``uretprobe.*`` forms, ``uprobe.session`` and``uprobe.session.s`` among them- ``unsupported uprobe section '<section>'; use+ uprobe[.s]//absolute/path:function[+offset],+ uretprobe[.s]//absolute/path:function,uprobe.multi[.s]//absolute/path:function_glob oruretprobe.multi[.s]//absolute/path:function_glob``+ * - ``uprobe`` or ``uretprobe`` with a relative path, no function or a+ malformed offset+ - ``invalid uprobe section '<section>'; expected+ uprobe[.s]//absolute/path:function[+offset],+ uretprobe[.s]//absolute/path:function or a 0x file offset in place+ of the function``+ * - ``uretprobe`` or ``uretprobe.s`` with ``+<offset>``+ - ``uretprobe offsets are unsupported in section '<section>'``* - ``sk_skb``, ``sk_skb/*``- ``unsupported sk_skb section '<section>'; supported network packetsections are cgroup_skb/ingress, cgroup_skb/egress, and netfilter``@@ -629,8 +683,9 @@ it generates a loader.* - every other section, ``tp/``, ``raw_tp/``, ``xdp`` and ``tc`` amongthem- ``unsupported section '<section>'; supported are fentry, fexit,- fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe.multi[.s],- uretprobe.multi[.s], cgroup_skb, netfilter and xdp/<mask>``+ fmod_ret, lsm, tp_btf, kprobe, kretprobe, uprobe[.s],+ uretprobe[.s], uprobe.multi[.s], uretprobe.multi[.s], cgroup_skb,+ netfilter and xdp/<mask>``* - ``syscall`` with another program name- ``SEC("syscall") program '<name>' must be named shirudo_test__<id>``@@ -640,9 +695,9 @@ Attach-scope checkThe site a kprobe, uprobe, cgroup or netfilter program attaches at dependson state that root can arrange on the target: the mount of``/sys/fs/cgroup``, the network namespace of the loading process, the-sysfs file that names the kprobe PMU. Before shirudo pins a link, it asks-the kernel about the link and compares the answer with the signed-metadata:+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:.. list-table:::header-rows: 1@@ -658,6 +713,12 @@ metadata:* - ``kprobe``, ``kretprobe``- a perf event link of the kprobe or kretprobe type, with the samesymbol and offset+ * - ``uprobe``, ``uretprobe``+ - 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* - ``uprobe.multi``, ``uretprobe.multi``- a uprobe multi link with as many offsets as recorded and the samedirection@@ -676,8 +737,8 @@ Missing attach targets``attach_target_missing`` reports a site that does not exist on thistarget: a ``kprobe`` symbol that kallsyms does not list, often because themodule that holds it is not loaded, a kernel without the kprobe PMU, a-missing ``/sys/fs/cgroup``, or a ``uprobe.multi`` or ``uretprobe.multi``-binary that is not there. ``shirudo upatch load`` exits with status 2 for+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 forit, not 1.The BTF-based kinds never report it. ``fentry``, ``fexit``, ``fmod_ret``,