.upatch format#
A .upatch file is the signed container that shirudo build
writes and that shirudo upatch load and
shirudo upatch unload consume. One layout serves both a load
artifact and an unload directive. The structures are defined in
include/shirudo/upatch.h. The writer is tools/shirudo-build/package.c
and the reader is util/upatch.c.
Conventions#
Every multi-byte integer is little-endian on every architecture.
Header offsets count from the start of the file. Offsets inside
datacount from the start ofdata.Fixed-size string fields are NUL-padded and must contain a NUL.
A file is at most 16 MiB (
UPATCH_FILE_MAX).
Layout#
The writer emits the regions in the order below. A reader locates each
region through the header only. Figure 1 shows the regions in file
order and what data holds.
Region |
Located by |
Content |
|---|---|---|
header |
offset 0, 360 bytes |
|
|
|
The |
|
|
Detached CMS signature over |
|
|
The loader’s data blob ( |
|
|
Absent. |
Figure 1. A .upatch file in file order, not to scale, with
data drawn wider below. The signed bytes are two regions with the
signature between them.#
Header#
struct upatch_header, 360 bytes, no padding.
Offset |
Size |
Field |
Value |
|---|---|---|---|
0 |
8 |
|
|
8 |
4 |
|
|
12 |
4 |
|
Bit 0, |
16 |
256 |
|
The upatch name, NUL-padded. Writers accept 1 to 255 characters of
|
272 |
32 |
|
SHA-256 of |
304 |
4 |
|
At least 360 and a multiple of 8. |
308 |
4 |
|
Non-zero multiple of 8. |
312 |
4 |
|
File offset of the signature. |
316 |
4 |
|
Non-zero. |
320 |
4 |
|
|
324 |
4 |
|
|
328 |
4 |
|
File offset of |
332 |
4 |
|
Length of |
336 |
4 |
|
Writers set 1048576. The target ignores the field and sizes its verifier log at 16 MiB. |
340 |
4 |
|
Number of maps in the BPF object, the count of map descriptors the loader writes behind its context. |
344 |
4 |
|
Number of programs in the BPF object, the count of program descriptors behind the map descriptors. |
348 |
4 |
|
Offset in |
352 |
4 |
|
|
356 |
4 |
|
Signature#
Property |
Value |
|---|---|
Signed bytes |
|
Encoding |
DER CMS |
Algorithm |
ML-DSA-87. The writer refuses any other key. |
Signed attributes |
Built against OpenSSL 3.x: signed attributes with a SHA-512 message digest and no signing time. Built against OpenSSL 4: no signed attributes, the signature covers the content directly. |
Size |
At most 16 KiB from the writer. The kernel accepts at most 64 KiB. |
Verifier |
For a Java payload, the target in userspace, see Java payloads.
For a BPF payload, the kernel, during |
Signed loader path describes the load sequence around this check.
Signed sections#
What the target needs beyond the loader rides inside data, behind the
loader’s own bytes, so the signature covers it, as Figure 1 shows. The
loader addresses only its own prefix of data. The writer aligns each
section and the directory to 8 bytes from the start of data and fills
the padding with zeros. The trailer is the last 16 bytes of data, and
the directory ends where the trailer begins.
The signature and payload_sha256 cover loader_insns followed by
data with nothing between the two, and the header that gives their
lengths is not signed. The trailer is the last 16 of the signed bytes
wherever a header ends the loader, and dir_off + 16 * nr_sections + 16
is the length of data. A header that divides the same signed bytes
elsewhere therefore names a directory that does not end at the trailer.
The reader treats that directory as absent, and the load refuses for
the signed name it lacks.
Trailer#
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
Offset of the directory in |
+12 |
4 |
|
Number of directory entries, 1 to 8. |
Directory entry#
nr_sections entries of 16 bytes each.
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
Section tag, one of the table below. |
+4 |
4 |
|
|
+8 |
4 |
|
Offset of the section in |
+12 |
4 |
|
Length of the section in bytes. |
Java payloads#
A Java payload (type 2) carries a Java agent .jar instead of a BPF
loader. It has no loader instructions and no metadata, and
loader_insns_off, log_buf_sz, nr_maps, nr_progs and
rodata_data_off are 0. Its data holds only signed sections, so the
signature covers data alone and payload_sha256 is SHA-256 of
data. The kernel never sees a Java payload. The target verifies its
signature in userspace with OpenSSL, against the unrevoked certificates of
the authenticated runtime’s set, and then requires the signer to be a key
the sealed .bpf keyring holds. That lookup compares the description the
kernel gives the key, not the key material.
The header is not signed, so the signed PTYP section carries the type
too, and the reader refuses a file whose header type differs from it. A
BPF payload’s header cannot be relabelled 2, since its signed sections
carry no PTYP. A Java payload cannot pass for a BPF one either: its
data has no loader, and any split of its signed bytes into a loader and
data leaves a directory that does not end at the trailer, so the reader
finds no NAME and the load refuses.
UPGM section#
A 16-byte header followed by nr_progs program entries, in libbpf’s
program iteration order, which is the order of the loader’s program
descriptors.
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
Number of program entries. Equal to the header |
+12 |
4 |
|
Number of maps in the BPF object. Equal to the header |
Each program entry is 160 bytes:
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
Program role, see the table below. |
+4 |
4 |
|
Attach kind, see the table below. 0 for the serializer and test-run roles. |
+8 |
8 |
|
Byte offset into the function for |
+16 |
128 |
|
NUL-terminated. The kernel symbol for |
+144 |
16 |
|
NUL-terminated name of the map a serializer reads. Empty for every other role. |
|
Name |
Program |
|---|---|---|
0 |
attach |
A program the target attaches and pins as a link. |
1 |
serializer |
A |
2 |
test-run |
The one |
|
Name |
Sections that produce it |
|---|---|---|
0 |
raw tracepoint (BTF target) |
|
1 |
kprobe |
|
2 |
kretprobe |
|
3 |
cgroup_skb ingress |
|
4 |
cgroup_skb egress |
|
5 |
fmod_ret |
|
6 |
netfilter IPv6 pre-routing |
|
7 |
uprobe.multi |
|
8 |
native XDP device classes |
|
9 |
uretprobe.multi |
|
10 |
uprobe |
|
11 |
uretprobe |
|
Attach kinds describes how the target attaches each kind.
XDP retains the 160-byte entry and UPGM version 1; readers that do not
know kind 8 refuse it. Its target and map are empty. A reader
refuses a device-class mask that sets a bit outside 27, which bit 4 is,
and a table whose XDP entries share a class.
Attach kinds says how a device is classified.
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.
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
Number of target records, equal to the number of kind 7, 9, 10 and 11 entries. |
+12 |
4 |
|
Each target record is a 16-byte head followed, without padding, by its variable parts:
Offset |
Field |
Value |
|---|---|---|
+0 |
|
Index of a kind 7, 9, 10 or 11 program entry. Each such entry has exactly one record. |
+4 |
|
Length of the path, 1 to 4095. |
+8 |
|
Length of the GNU build ID, 1 to 64. |
+12 |
|
Number of offsets, 1 to 1048576; exactly 1 for kinds 10 and 11. |
+16 |
path |
|
then |
build ID |
|
then |
offsets |
|
DCDB section#
A 16-byte header, nr_decoders entries of 200 bytes, then the decoder
sources, concatenated without terminators.
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
At least 1. |
+12 |
4 |
|
Each decoder entry:
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
|
+12 |
4 |
|
|
+16 |
64 |
|
NUL-terminated, empty or the artifact’s name. The writer refuses
another name. A reader ignores the field: the decoder belongs to
the upatch whose signed data carries it, and the reader picks it
by |
+80 |
16 |
|
NUL-terminated name of a map in the object. |
+96 |
64 |
|
NUL-terminated relative path of the decoder source, resolved
against the directory of the |
+160 |
32 |
|
NUL-terminated name of the Lua function. |
+192 |
4 |
|
Offset of the source in the section, behind the entries. |
+196 |
4 |
|
Length of the source, non-zero. |
The writer takes each entry from a struct decoder_manifest that
the BPF program declares in .rodata (bpf/lib/decoder.h,
192 bytes, magic 0x44434544, the bytes DECD, u16 fields
version, language, api_version, input_format,
export_signal and reserved, then the same four strings). It scans
.rodata at 4-byte steps, accepts one decoder per map, and reads each
source file, at most 16 MiB. A decoder whose language,
api_version, input_format or export_signal differs from the
values above fails the build. Write a Lua decoder covers the decoder
API.
SHPL section#
SHPL starts with a 20-byte version 1 header, followed by count
fixed-size entries. policy_map_idx and socket_policy_map_idx are
the zero-based indices of the shared maps in the loader’s map descriptor
array. The builder accepts this section only when the BPF object declares
compatible maps named shirudo_task_policy and shirudo_sock_policy.
At load time, shirudo supplies the pinned task and socket storage maps at
those descriptors; the loader does not create or pin private copies.
upatches without SHPL use their normal maps.
Offset |
Size |
Field |
Value |
|---|---|---|---|
+0 |
4 |
|
|
+4 |
4 |
|
|
+8 |
4 |
|
Number of policy/path entries; non-zero. |
+12 |
4 |
|
Map descriptor index, less than |
+16 |
4 |
|
Distinct map descriptor index, less than |
Each entry is 4168 bytes: a little-endian one-hot u64 policy bit,
64 bytes of NUL-terminated policy ID, then 4096 bytes that hold a
NUL-terminated absolute path of at most 255 bytes. The path does not end
in /, and * and ? appear in its last component alone, where
they make it a pattern (Beefile); the target refuses a section
with any other path. After a successful load, shirudo applies each entry
by ORing the policy bit into the existing policy xattr of the inode the
path names, or of every regular file in the path’s directory whose name
the pattern matches, without following a symbolic link there. It takes
the entries from the copy of the artifact the kernel verified, not from
the file again.
Header fields outside the signature#
The header is not signed. The target checks some fields against the signed bytes and trusts others as they are.
Field |
Treatment |
|---|---|
|
Must equal the signed |
|
Recomputed as SHA-256 of |
|
Bit 0 selects the load or the unload path. Each path refuses an
artifact without the signed section it requires ( |
|
Must equal the |
|
Must equal the |
|
Must equal the offset in |
|
Ignored. |
|
Bounds-checked. Not read on the target. |
|
Checked by the reader, see Reader validation. |
Reader validation#
util/upatch.c applies these checks in order. Any failure rejects the
file.
The file is a regular file, not empty and at most 16 MiB.
The file is at least 360 bytes long.
magicequals0x53485244554e4150.versionequals 1.namecontains a NUL.reserved1is 0.signature,metadataanddataeach lie inside the file, and each non-empty one starts at or after offset 360.signature_lenis not 0.typeis 1 or 2.For
type1:loader_insns_offis at least 360 and a multiple of 8,loader_insns_lenis a multiple of 8 and not 0, the region lies inside the file, androdata_data_offis at mostdata_len.For
type2:loader_insns_off,loader_insns_len,metadata_len,log_buf_sz,nr_maps,nr_progsandrodata_data_offare 0, anddata_lenis not.
The reader then parses the signed sections. A failure here does not reject the file. The reader treats the whole directory as absent, so a load or an unload later refuses for the section it lacks, and shirudo upatch info shows no signed sections.
datais at least 16 bytes long and its last 16 bytes are a trailer with magicSGTRand version 1.nr_sectionsis 1 to 8, and the directory’s entries start atdir_offand end where the trailer begins.Each entry has
reserved0 and a section that ends at or beforedir_off.Each tag is
NAME,UPGM,UNLD,DCDB,SHPL,PTYPorJAVA, and appears at most once.NAMEis 256 bytes and contains a NUL.UPGMis not empty.UNLDis 32 bytes.PTYPis 4 bytes.JAVAis not empty.DCDBhas magicDCDB, version 1, at least one entry, all entries inside the section, NUL-terminated strings, non-zerolanguage,api_version,input_formatandexport_signal, and each source inside the section behind the entries.
Last, the reader rejects the file unless the header type equals the
PTYP value, or is 1 where the sections carry no PTYP. A Java file
whose directory is malformed is therefore rejected rather than read with
no sections.
CBMC proves these checks of the reader for every header, of a file of any size, and for the signed sections, decoders, policy labels and program table within the bounds Proof coverage lists: what the reader accepts lies inside the file, and what it refuses leaves nothing behind. Within the bounds listed there it also proves that the reader takes what the writer lays out, and reads back the header, the signed sections, the decoders and the program table as they were written.
Target admission#
A load artifact passes the reader, then these checks in order. A failure
of checks 1 to 7 is reported as parser_rejected, see
Failure categories and exit status.
flagsbit 0 is clear.typeis 1.loader_insns_lenis a multiple of 8.The header
nameis not empty, does not start with., contains only[A-Za-z0-9._-]and has at most 255 characters with each.counted twice, so that its pin directory name fits.A
NAMEsection is present and equals the headername.payload_sha256equals SHA-256 ofloader_insns || data.A
UPGMsection is present and parses: version 1 (any other version isEOPNOTSUPP),nr_mapsandnr_progsequal to the header’s, every entry valid for its role and kind, and aUPUTblock that is present exactly when a kind 7, 9, 10 or 11 entry exists and ends the section.Each
uprobe,uretprobe,uprobe.multianduretprobe.multitarget file is a regular file whose GNU build ID equals the recorded one.The kernel verifies the signature in
BPF_PROG_LOAD.
A Java load artifact passes the reader, then, in place of the checks
above: flags bit 0 is clear, the name is as in check 4, NAME,
PTYP and JAVA are present, NAME equals the header name,
PTYP is 2, payload_sha256 equals SHA-256 of data
(parser_rejected), and the signature verifies over data under a
certificate init enrolled in the .bpf keyring
(signature_rejected). A Java unload directive takes the same checks
with UNLD in place of JAVA and bit 0 set. See
Java agent upatches.
An unload directive passes the reader, then these checks:
flagsbit 0 is set.NAMEandUNLDsections are present andNAMEequals the headername.Load checks 2 to 6 above, then the kernel verifies the signature in
BPF_PROG_LOAD. The target never runs the directive’s loader.The
UNLDvalue equals thesecurity.bpf.shirudo.payloadxattr of the loaded upatch, or no upatch of that name is pinned.
Versioning#
Structure |
Version |
Reader on another value |
|---|---|---|
Container header |
1 |
Rejects the file. |
Trailer |
1 |
Treats the directory as absent. |
|
1 |
Load refused ( |
|
1 |
Load refused. |
|
1 |
Treats the directory as absent. |
|
1 |
Treats the directory as absent. |
Decoder manifest in |
1 |
The writer skips the candidate. |
A section tag the reader does not know makes it treat the directory as
absent. A new section therefore needs readers that know its tag before
writers emit it. Reserved fields are 0 in every file a writer emits. The
reader checks reserved1, the directory entries’ reserved and the
UPUT reserved field. It does not check flags
bits 1 to 31 or the DCDB reserved field.
Limits#
Item |
Limit |
|---|---|
File size |
16 MiB |
Signature |
16 KiB (writer), 64 KiB (kernel) |
Signed sections |
1 to 8 |
Name of a upatch |
255 characters of |
|
127 bytes |
Serializer map name |
15 bytes |
|
4095 bytes |
GNU build ID |
1 to 64 bytes |
|
1 to 1048576 |
Decoder source |
16 MiB per file |
Target verifier log |
16 MiB |