eBPF-based security platform
shirudo-build: sign Java agent jars into upatches
John Fastabend4e711f99091b
diff --git a/.agents/skills/upatch/SKILL.md b/.agents/skills/upatch/SKILL.mdindex 542c7412..6a69ab08 100644--- a/.agents/skills/upatch/SKILL.md+++ b/.agents/skills/upatch/SKILL.md@@ -110,6 +110,9 @@ shirudo build --unload <name> --keys-dir <keys> -o <name>.unload.upatch \or the installed one, unless `--vmlinux` names it. Run it from thecheckout, or link `vendor/` and `bpf/` into the package directory asthe publishing guide does.+- A Java agent `.jar` signs the same way, `shirudo build <agent>.jar`,+ with no compiler, and its directive takes `--java` beside+ `--payload-sha256`; in a Beefile it is `type = "java"`.## Build a packagediff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex dd231203..f0a9086f 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -1370,6 +1370,11 @@ a proof covers.through the emitters the proofs cover* - :src:`util/loader.c`- Data only: the embedded BPF objects+ * - :src:`tools/shirudo-build/jar.c`+ - The builder's check of a jar the operator hands it, which reads+ the zip directory and has zlib inflate the manifest. The ``jar``+ unit cases run it on every truncation and every byte flip of a+ jar; no harness is written for it yet* - :src:`builtin-attest.c`, :src:`builtin-build.c`,:src:`builtin-config.c`, :src:`builtin-genkey.c`,:src:`builtin-image.c`, :src:`builtin-init.c`,@@ -1407,7 +1412,10 @@ those parts has no proof. So it is with the package state:and neither that nor reading back what ``bpm_state_emit()`` writes has aproof, as a string of any contents moves all that follows it in thedocument, which costs CBMC more memory than it has. The ``bpm_state``-unit cases check both on fixed entries.+unit cases check both on fixed entries. The zip directory walk of+``jar_premain_class()`` in :src:`tools/shirudo-build/jar.c` has neither+a fuzz target nor a proof; its unit cases cut and flip a jar byte by+byte.Of :src:`util/paths.c` and :src:`util/serializer.c` the proofs cover thefunctions the table names. ``key_paths()``, which callsdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 95b2b34e..8fca7aba 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -206,6 +206,12 @@ signed config image.section without a list, a glob, a name given twice, an emptyelement, a name of ``KSYM_NAME_LEN`` bytes and ``kprobe.session``are refused+ * - ``t3014-java-build``+ - A jar signs into a Java load whose signature covers its data, and+ an unload directive of it, by ``build`` and by ``bpm build``; a file+ that is no jar, a jar without a ``Premain-Class``, ``--java``+ alone, rodata on a Java upatch and a header relabelled BPF are+ refused* - ``t3101-target-fixtures``- Prebuilt fixtures load and unload, foreign and own-keyringfixtures are refused, a map fixture exports@@ -820,7 +826,11 @@ The unit suites in :src:`testing/unit/` cover 32 of the modules in``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and``overflow`` the arithmetic of-:src:`include/shirudo/overflow.h`. ``quote_check`` covers the rows of+:src:`include/shirudo/overflow.h`. ``jar`` covers the+``shirudo-build`` checks of a Java agent's jar in+:src:`tools/shirudo-build/jar.c`: stored and deflated manifests, a+missing or empty ``Premain-Class``, one outside the main section, and+every truncation and byte flip of a jar. ``quote_check`` covers the rows of``shirudo-quote-check`` in :src:`tools/quote-check/row.c`, on quotes asoftware P-256 key signs: every reject reason, the signer's qualifiedname, the empty PCR selection and the base64 of each field. The otherdiff --git a/Documentation/reference/cli/build.rst b/Documentation/reference/cli/build.rstindex 2eccb09a..0e3919f0 100644--- a/Documentation/reference/cli/build.rst+++ b/Documentation/reference/cli/build.rst@@ -8,8 +8,8 @@shirudo build=============-Compile and sign a BPF program into a ``.upatch`` artifact, or sign an-unload directive for a loaded upatch.+Compile and sign a BPF program, or sign a Java agent ``.jar``, into a+``.upatch`` artifact, or sign an unload directive for a loaded upatch.Runs on: operator host, without the image gate. Requires: the``shirudo-build`` helper, the signing key and certificate, and for a@@ -27,7 +27,7 @@ Usage[--vmlinux <vmlinux-h>] [--extra-cflags <flags>][--emit-artifacts]shirudo build --unload <name> [-o <output> | --output <output>]- [--payload-sha256 <digest>]+ [--payload-sha256 <digest> [--java]][--keys-dir <dir> | --keys-dir=<dir>][--key <signing-key>] [--cert <certificate>]shirudo build --help@@ -56,8 +56,9 @@ Building a upatch``/var/lib/shirudo/keys``. The key must be a regular file with mode0600 and the certificate readable.#. It selects the output: ``-o``, else the input path with a trailing- ``.c`` or ``.o`` replaced by ``.upatch``, beside the input- (``fix.bpf.c`` becomes ``fix.bpf.upatch``).+ ``.c``, ``.o`` or ``.jar`` replaced by ``.upatch``, beside the input+ (``fix.bpf.c`` becomes ``fix.bpf.upatch``, ``agent.jar``+ ``agent.upatch``).#. For a ``.bpf.c`` input, it finds ``vmlinux.h``: ``--vmlinux``, else``./vmlinux.h``, else ``./vendor/linux/vmlinux.h``, else the installed``vmlinux.h`` (``/usr/share/shirudo/vmlinux.h`` in the operator@@ -74,10 +75,11 @@ Building a upatch``/usr/lib/llvm-23/bin/clang`` in the Debian package, and ``PATH`` isnot searched. The object goes to a temporary file in``/tmp``, or with ``--emit-artifacts`` beside the output.-#. A ``.bpf.o`` or ``.o`` input is packaged as it is. Any other input is+#. A ``.bpf.o`` or ``.o`` input is packaged as it is. A ``.jar`` input is+ a Java agent, see `Building a Java upatch`_. Any other input isrefused.#. It names the upatch: ``--name``, else the input file name without- ``.bpf.c``, ``.bpf.o`` or ``.o``.+ ``.bpf.c``, ``.bpf.o``, ``.o`` or ``.jar``.#. It generates the loader for the object, signs it with the key andwrites the artifact. The detached CMS signature covers the loaderinstructions and the loader's data, which hold the payload and the@@ -87,6 +89,20 @@ Building a upatchThe helper prints ``wrote <output> (insns=<n> sig=<n> data=<n>sections=<n> maps=<n> progs=<n> decoders=<n> name=<name>)``.+Building a Java upatch+~~~~~~~~~~~~~~~~~~~~~~++A ``.jar`` input is signed as it is, with no compiler. The helper refuses+a file that is no zip archive, and a jar whose manifest names no+``Premain-Class``: a JVM given such a jar as ``-javaagent`` does not+start. It also refuses a zip64 archive and a manifest that is neither+stored nor deflated. The jar and its signature must fit the 16 MiB a+``.upatch`` may have. The+artifact carries no loader: its data holds the signed name, the payload+type and the jar, and the detached CMS signature covers that data alone+(see the Java payloads of :doc:`/reference/formats/upatch-format`).+The helper prints ``wrote <output> (java sig=<n> data=<n> name=<name>)``.+Signing an unload directive~~~~~~~~~~~~~~~~~~~~~~~~~~~@@ -99,7 +115,11 @@ digest:``payload_sha256``).#. Without ``--payload-sha256``, the helper reads the digest from``/var/lib/shirudo/upatches/<name>.upatch`` on the host it runs on,- and refuses with exit status 2 when that file is absent.+ and refuses with exit status 2 when that file is absent. It signs a+ Java directive when that upatch is a Java one.+#. With ``--payload-sha256``, ``--java`` signs the directive of a Java+ upatch: no unload program, the name, the payload type and the digest+ in the signed data.#. The output is ``-o``, else ``<name>.unload.upatch`` in the currentdirectory.@@ -180,6 +200,11 @@ Options- ``<digest>``, 64 hex characters- see above- Payload digest the directive expects. Only with ``--unload``.+ * - ``--java``+ - none+ - off+ - Sign the directive of a Java upatch. Only with ``--unload`` and+ ``--payload-sha256``.* - ``-h``, ``--help``- none-@@ -198,13 +223,15 @@ Exit status- The artifact or directive was written.* - 1- The key or certificate is unusable, ``vmlinux.h`` or clang is- missing, compilation failed, or packaging and signing failed.+ missing, compilation failed, a ``.jar`` input is no zip archive,+ or packaging and signing failed.* - 2- A usage error: an unknown option or one given twice, a valuemissing or empty, a second input, an input that is not- ``.bpf.c``, ``.bpf.o`` or ``.o``, no input, an input together with- ``--unload``, or a ``--payload-sha256`` that is not 64 hexadecimal- characters. Also a usage error of ``image sign-root`` or ``upatch+ ``.bpf.c``, ``.bpf.o``, ``.o`` or ``.jar``, no input, an input+ together with ``--unload``, ``--java`` without ``--unload`` and+ ``--payload-sha256``, or a ``--payload-sha256`` that is not 64+ hexadecimal characters. Also a usage error of ``image sign-root`` or ``upatchrewrite-rodata``.* - 127- ``shirudo`` cannot execute the helper.diff --git a/Documentation/reference/cli/upatch-info.rst b/Documentation/reference/cli/upatch-info.rstindex dffc3051..bd2d584e 100644--- a/Documentation/reference/cli/upatch-info.rst+++ b/Documentation/reference/cli/upatch-info.rst@@ -36,8 +36,9 @@ The human output has these parts:- the loader: offset and length of its instructions, the log buffersize, its metadata and its data- the signature: offset and length-- the signed sections: the name, the length of the attach metadata and,- for a directive, the expected payload digest+- the signed sections: the name, the length of the attach metadata, for+ a Java load the length of its agent jar and, for a directive, the+ expected payload digest- the decoders: target upatch and map, language and API version, inputformat, export signal (``log`` or ``metric``) and source@@ -66,8 +67,8 @@ Options``payload_type``, ``version``, ``payload_sha256``,``loader_insns_len``, ``signature_len``, ``metadata_len``,``data_len``, ``log_buf_sz``, ``file_size``, ``signed_name``,- ``attach_metadata_len``, ``unload_payload_sha256`` and- ``decoders``.+ ``attach_metadata_len``, ``jar_len`` (0 but for a Java load),+ ``unload_payload_sha256`` and ``decoders``.Exit status-----------@@ -132,7 +133,7 @@ The JSON form of another artifact:.. code-block:: consoletarget# shirudo upatch info --json shirudo-test-basic.upatch- {"schema":1,"path":"shirudo-test-basic.upatch","name":"shirudo-test-basic","type":"load","payload_type":"bpf","version":1,"payload_sha256":"8aee7072a1a15e4dddc93ad6b253a0e2e70ad6cdf767442aa8a1c1997bc3069f","loader_insns_len":992,"signature_len":4853,"metadata_len":0,"data_len":2400,"log_buf_sz":1048576,"file_size":8605,"signed_name":"shirudo-test-basic","attach_metadata_len":176,"unload_payload_sha256":null,"decoders":[]}+ {"schema":1,"path":"shirudo-test-basic.upatch","name":"shirudo-test-basic","type":"load","payload_type":"bpf","version":1,"payload_sha256":"8aee7072a1a15e4dddc93ad6b253a0e2e70ad6cdf767442aa8a1c1997bc3069f","loader_insns_len":992,"signature_len":4853,"metadata_len":0,"data_len":2400,"log_buf_sz":1048576,"file_size":8605,"signed_name":"shirudo-test-basic","attach_metadata_len":176,"jar_len":0,"unload_payload_sha256":null,"decoders":[]}Related-------diff --git a/Documentation/reference/formats/beefile.rst b/Documentation/reference/formats/beefile.rstindex 650e2649..d182fe1a 100644--- a/Documentation/reference/formats/beefile.rst+++ b/Documentation/reference/formats/beefile.rst@@ -32,7 +32,7 @@ a package may have:source, target a file shipped as it is[[upatches]] up to 128source the BPF program, names the upatch- type bpf+ type bpf or javaload_target, unload_target where the artifacts go[[upatches.rodata]] up to 16 per upatchsymbol, input, generator, generated .rodata@@ -206,13 +206,17 @@ from the artifact's payload digest, so a package always ships both.- string- yes- A ``.bpf.c``, ``.bpf.o`` or ``.o`` file inside the package- directory, under the same rules as a file ``source``. The upatch- name is its base name without ``.bpf.c``, ``.bpf.o`` or ``.o``.- There is no key to set another name.+ directory, or a ``.jar`` for a Java upatch, under the same rules as+ a file ``source``. The upatch name is its base name without+ ``.bpf.c``, ``.bpf.o``, ``.o`` or ``.jar``. There is no key to set+ another name.* - ``type``- string- no- - ``bpf``, the default and the only value, in any case.+ - ``bpf``, the default, or ``java``, in any case. A ``java`` upatch+ signs its ``.jar`` source as a Java agent (see+ :doc:`/reference/cli/build`) and takes no ``[[upatches.rodata]]``,+ ``[[upatches.policy]]`` or ``[upatches.tree]``.* - ``load_target``- string- nodiff --git a/Makefile.shirudo b/Makefile.shirudoindex 1b21bcf0..f3f10146 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -344,6 +344,7 @@ BUILD_SRCS := \tools/shirudo-build/shirudo-build.c \tools/shirudo-build/compile.c \tools/shirudo-build/package.c \+ tools/shirudo-build/jar.c \tools/shirudo-build/vmlinux.c \tools/shirudo-build/bpm-build.c \tools/shirudo-build/bpm-publish.c \@@ -1110,6 +1111,10 @@ $(UNIT_LIB): $(UTIL_OBJS) $(VERSION_OBJ)build/unit/quote_check: UNIT_OBJS := tools/quote-check/row.obuild/unit/quote_check: tools/quote-check/row.o+# The same for the builder's jar check.+build/unit/jar: UNIT_OBJS := tools/shirudo-build/jar.o+build/unit/jar: tools/shirudo-build/jar.o+build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \$(UNIT_LIB) $(LIBBPF_A)@printf ' CC $@\n'diff --git a/builtin-upatch.c b/builtin-upatch.cindex b499a692..fee93c82 100644--- a/builtin-upatch.c+++ b/builtin-upatch.c@@ -463,7 +463,7 @@ static int cmd_upatch_info(int argc, const char **argv)struct opt_parser p;int opt, err, ret = 1;bool json = false;- __u32 attach_len;+ __u32 attach_len, jar_len;OPT_INIT(&p, "upatch info", upatch_info_opts, &upatch_info_help, argc,argv, 1);@@ -499,6 +499,7 @@ static int cmd_upatch_info(int argc, const char **argv)upatch_header_name(&u, name);upatch_signed_attach(&u, &attach_len);+ upatch_signed_jar(&u, &jar_len);unload_payload = upatch_signed_unload_payload(&u);if (json) {@@ -531,6 +532,7 @@ static int cmd_upatch_info(int argc, const char **argv)json_kv_null(&e, "signed_name");}json_kv_int(&e, "attach_metadata_len", attach_len);+ json_kv_int(&e, "jar_len", jar_len);if (unload_payload) {hex_encode(unload_payload, 32, payload_hex);json_kv_string(&e, "unload_payload_sha256", payload_hex);@@ -611,6 +613,8 @@ static int cmd_upatch_info(int argc, const char **argv)printf(" %-22s %u bytes\n", "Attach metadata:", attach_len);elseprintf(" %-22s (none)\n", "Attach metadata:");+ if (jar_len)+ printf(" %-22s %u bytes\n", "Java agent jar:", jar_len);if (unload_payload) {printf(" %-22s ", "Unload payload:");print_sha256_hex(unload_payload);diff --git a/testing/functional/upatch/t3014-java-build.sh b/testing/functional/upatch/t3014-java-build.shnew file mode 100755index 00000000..05fab9ce--- /dev/null+++ b/testing/functional/upatch/t3014-java-build.sh@@ -0,0 +1,188 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t3014 -- Java agent upatches: shirudo build signs a .jar into a Java+# load and its unload directive, offline (no kernel, no root), and the+# reader refuses a header whose type the signed sections do not carry.+# The jar is a zip that python3 writes; no JDK is needed.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++S=../shirudo+SHB=../tools/shirudo-build/shirudo-build++command -v openssl >/dev/null 2>&1 || test_skip_all "no openssl"+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+[ -x "$S" ] || test_skip_all "shirudo not built"+[ -x "$SHB" ] || test_skip_all "shirudo-build not built (make shirudo-build)"++WORK=$(mktemp -d)+trap 'rm -rf "$WORK"' EXIT++openssl req -x509 -newkey ML-DSA-87 -nodes -keyout "$WORK/k.pem" \+ -outform DER -out "$WORK/c.der" -days 1 -subj "/CN=t3014" \+ >/dev/null 2>&1 || test_skip_all "openssl ML-DSA keygen failed (needs OpenSSL >= 3.5)"+chmod 600 "$WORK/k.pem"++python3 -I - "$WORK/agent.jar" <<'PY'+import sys+import zipfile++with zipfile.ZipFile(sys.argv[1], "w") as z:+ z.writestr("META-INF/MANIFEST.MF",+ "Manifest-Version: 1.0\nPremain-Class: t3014.Agent\n\n")+ z.writestr("t3014/Agent.class", b"\xca\xfe\xba\xbe")+PY++info() {+ "$S" upatch info "$1" --json 2>/dev/null+}++field() {+ python3 -I -c 'import json, sys; print(json.load(sys.stdin)[sys.argv[1]])' "$1"+}++test_expect_success 'a jar becomes a signed Java load' '+ "$SHB" "$WORK/agent.jar" --key "$WORK/k.pem" --cert "$WORK/c.der" \+ >/dev/null 2>&1 &&+ test -f "$WORK/agent.upatch" &&+ info "$WORK/agent.upatch" >"$WORK/load.json" &&+ python3 -I - "$WORK/load.json" "$WORK/agent.jar" <<-PY+ import json, sys+ d = json.load(open(sys.argv[1]))+ jar = open(sys.argv[2], "rb").read()+ assert d["type"] == "load", d+ assert d["payload_type"] == "java", d+ assert d["name"] == "agent" and d["signed_name"] == "agent", d+ assert d["loader_insns_len"] == 0 and d["metadata_len"] == 0, d+ assert d["signature_len"] > 0, d+ assert d["jar_len"] == len(jar), d+ assert d["attach_metadata_len"] == 0 and not d["decoders"], d+ PY+'++test_expect_success 'the payload digest is SHA-256 of the signed data' '+ python3 -I - "$WORK/agent.upatch" "$(field payload_sha256 <"$WORK/load.json")" <<-PY+ import hashlib, struct, sys+ b = open(sys.argv[1], "rb").read()+ off, length = struct.unpack_from("<II", b, 328)+ assert hashlib.sha256(b[off:off + length]).hexdigest() == sys.argv[2]+ PY+'++test_expect_success 'the signature covers the data alone' '+ python3 -I - "$WORK/agent.upatch" "$WORK" <<-PY &&+ import struct, sys+ b = open(sys.argv[1], "rb").read()+ sig_off, sig_len = struct.unpack_from("<II", b, 312)+ data_off, data_len = struct.unpack_from("<II", b, 328)+ open(sys.argv[2] + "/sig.der", "wb").write(b[sig_off:sig_off + sig_len])+ open(sys.argv[2] + "/data.bin", "wb").write(b[data_off:data_off + data_len])+ PY+ openssl x509 -inform DER -in "$WORK/c.der" -out "$WORK/c.pem" &&+ openssl cms -verify -binary -inform DER -in "$WORK/sig.der" \+ -content "$WORK/data.bin" -certfile "$WORK/c.pem" -nointern \+ -noverify -out /dev/null 2>/dev/null+'++test_expect_success 'the unload directive of a Java upatch' '+ payload=$(field payload_sha256 <"$WORK/load.json") &&+ "$SHB" --unload agent --java --payload-sha256 "$payload" \+ --key "$WORK/k.pem" --cert "$WORK/c.der" \+ -o "$WORK/agent.unload.upatch" >/dev/null 2>&1 &&+ info "$WORK/agent.unload.upatch" >"$WORK/unload.json" &&+ python3 -I - "$WORK/unload.json" "$payload" <<-PY+ import json, sys+ d = json.load(open(sys.argv[1]))+ assert d["type"] == "unload" and d["payload_type"] == "java", d+ assert d["unload_payload_sha256"] == sys.argv[2], d+ assert d["jar_len"] == 0, d+ PY+'++test_expect_success '--java takes --unload and --payload-sha256' '+ { "$SHB" "$WORK/agent.jar" --java --key "$WORK/k.pem" \+ --cert "$WORK/c.der" 2>/dev/null; test $? -eq 2; } &&+ { "$SHB" --unload agent --java --key "$WORK/k.pem" \+ --cert "$WORK/c.der" 2>/dev/null; test $? -eq 2; }+'++test_expect_success 'a file that is no jar is refused' '+ printf "not a zip" >"$WORK/bad.jar" &&+ ! "$SHB" "$WORK/bad.jar" --key "$WORK/k.pem" --cert "$WORK/c.der" \+ -o "$WORK/bad.upatch" 2>/dev/null &&+ test ! -e "$WORK/bad.upatch"+'++test_expect_success 'a jar without a Premain-Class is refused' '+ python3 -I -c "+import sys, zipfile+with zipfile.ZipFile(sys.argv[1], \"w\", zipfile.ZIP_DEFLATED) as z:+ z.writestr(\"META-INF/MANIFEST.MF\", \"Manifest-Version: 1.0\\nMain-Class: A\\n\")+" "$WORK/lib.jar" &&+ ! "$SHB" "$WORK/lib.jar" --key "$WORK/k.pem" --cert "$WORK/c.der" \+ -o "$WORK/lib.upatch" 2>"$WORK/lib.err" &&+ grep -q "no Premain-Class" "$WORK/lib.err" &&+ test ! -e "$WORK/lib.upatch"+'++test_expect_success 'a header relabelled BPF is refused' '+ python3 -I - "$WORK/agent.upatch" "$WORK/bpf.upatch" <<-PY &&+ import struct, sys+ b = bytearray(open(sys.argv[1], "rb").read())+ struct.pack_into("<I", b, 352, 1)+ open(sys.argv[2], "wb").write(b)+ PY+ ! "$S" upatch info "$WORK/bpf.upatch" >/dev/null 2>&1+'++command -v tar >/dev/null 2>&1 || test_done+REPO=$(cd .. && pwd)+mkdir -p "$WORK/pkg" "$WORK/keys"+cp "$WORK/agent.jar" "$WORK/pkg/agent.jar"+cp "$WORK/k.pem" "$WORK/keys/signing_key.pem"+cp "$WORK/c.der" "$WORK/keys/cert.der"+cat >"$WORK/pkg/Beefile" <<-TOML+ [package]+ name = "java-agent"+ description = "t3014"+ license = "BSD-2-Clause"++ [[upatches]]+ type = "java"+ source = "agent.jar"+TOML++test_expect_success 'bpm build packages a Java upatch and its unload' '+ ( cd "$WORK" && SHIRUDO_BUILD="$REPO/tools/shirudo-build/shirudo-build" \+ "$REPO/shirudo" bpm build pkg --version 1.0.0 \+ --keys-dir "$WORK/keys" ) >/dev/null 2>&1 &&+ tar -xOf "$WORK/java-agent-1.0.0.bee" \+ java-agent-1.0.0/upatches/agent.upatch >"$WORK/pkg.upatch" &&+ tar -xOf "$WORK/java-agent-1.0.0.bee" \+ java-agent-1.0.0/upatches/agent.unload.upatch \+ >"$WORK/pkg.unload.upatch" &&+ test "$(info "$WORK/pkg.upatch" | field payload_type)" = java &&+ test "$(info "$WORK/pkg.unload.upatch" | field payload_type)" = java &&+ test "$(info "$WORK/pkg.unload.upatch" | field unload_payload_sha256)" = \+ "$(info "$WORK/pkg.upatch" | field payload_sha256)"+'++test_expect_success 'a Java upatch takes no rodata' '+ cat >>"$WORK/pkg/Beefile" <<-TOML &&++ [[upatches.rodata]]+ symbol = "x"+ input = "x"+ generator = ["true"]+ update = ["true"]+ TOML+ ! ( cd "$WORK" && SHIRUDO_BUILD="$REPO/tools/shirudo-build/shirudo-build" \+ "$REPO/shirudo" bpm build pkg --version 1.0.1 \+ --keys-dir "$WORK/keys" ) >/dev/null 2>&1 &&+ test ! -e "$WORK/java-agent-1.0.1.bee"+'++test_donediff --git a/testing/fuzz/fuzz-beefile.c b/testing/fuzz/fuzz-beefile.cindex acaaadec..5435f45e 100644--- a/testing/fuzz/fuzz-beefile.c+++ b/testing/fuzz/fuzz-beefile.c@@ -62,6 +62,21 @@ int build_package_unload(const char *name,return -ENOSYS;}+int build_package_java_load(const char *jar_path, const char *out_upatch,+ const char *upatch_name,+ const char *pkey_path, const char *cert_path)+{+ return -ENOSYS;+}++int build_package_java_unload(const char *name,+ const __u8 payload_sha256[32],+ const char *out_upatch,+ const char *pkey_path, const char *cert_path)+{+ return -ENOSYS;+}+int build_package_patch_rodata(const char *input, const char *output,const char *symbol, const __u8 *value,size_t value_len, const char *pkey_path,diff --git a/testing/unit/jar.c b/testing/unit/jar.cnew file mode 100644index 00000000..26c5eb9c--- /dev/null+++ b/testing/unit/jar.c@@ -0,0 +1,225 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <errno.h>+#include <string.h>++#include "tools/shirudo-build/jar.h"+#include "unit.h"++/* Jars python3's zipfile wrote: a manifest and one class each. */+static const __u8 stored_agent[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0x0d, 0x3e, 0x7c, 0xac, 0x2d, 0x00, 0x00, 0x00, 0x2d, 0x00,+ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0x4d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, 0x2d, 0x56,+ 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0d,+ 0x0a, 0x50, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x2d, 0x43, 0x6c, 0x61,+ 0x73, 0x73, 0x3a, 0x20, 0x61, 0x2e, 0x41, 0x0d, 0x0a, 0x0d, 0x0a, 0x50,+ 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21,+ 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,+ 0x00, 0x09, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61,+ 0x73, 0x73, 0xca, 0xfe, 0xba, 0xbe, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03,+ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x0d, 0x3e,+ 0x7c, 0xac, 0x2d, 0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x14, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01,+ 0x00, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,+ 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46,+ 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00,+ 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x5f, 0x00, 0x00, 0x00, 0x61, 0x2f,+ 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00,+ 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x79, 0x00, 0x00, 0x00, 0x8a,+ 0x00, 0x00, 0x00, 0x00, 0x00,+};++static const __u8 deflated_agent[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0x09, 0xbc, 0x2b, 0x1f, 0x40, 0x00, 0x00, 0x00, 0x03, 0x01,+ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0xf3, 0x4d, 0xcc, 0xcb, 0x4c, 0x4b, 0x2d, 0x2e, 0xd1, 0x0d,+ 0x4b, 0x2d, 0x2a, 0xce, 0xcc, 0xcf, 0xb3, 0x52, 0x30, 0xd4, 0x33, 0xe0,+ 0xe5, 0x72, 0x2e, 0x4a, 0x4d, 0x2c, 0x49, 0x4d, 0xd1, 0x75, 0xaa, 0xb4,+ 0x52, 0xa8, 0x18, 0x26, 0x80, 0x97, 0xab, 0xa0, 0x28, 0x35, 0x37, 0x31,+ 0x33, 0x4f, 0x37, 0x39, 0x27, 0xb1, 0xb8, 0xd8, 0x4a, 0x21, 0x51, 0xcf,+ 0x91, 0x97, 0x8b, 0x97, 0x0b, 0x00, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5,+ 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,+ 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73, 0x73, 0xca, 0xfe, 0xba,+ 0xbe, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x08,+ 0x00, 0x00, 0x00, 0x21, 0x5c, 0x09, 0xbc, 0x2b, 0x1f, 0x40, 0x00, 0x00,+ 0x00, 0x03, 0x01, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x4d,+ 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49,+ 0x46, 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0x50, 0x4b, 0x01, 0x02, 0x14,+ 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d,+ 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,+ 0x01, 0x72, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61,+ 0x73, 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,+ 0x02, 0x00, 0x79, 0x00, 0x00, 0x00, 0x9d, 0x00, 0x00, 0x00, 0x00, 0x00,+};++static const __u8 no_premain[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0xf4, 0xc2, 0xc7, 0xb2, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,+ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0x4d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, 0x2d, 0x56,+ 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a,+ 0x4d, 0x61, 0x69, 0x6e, 0x2d, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x3a, 0x20,+ 0x61, 0x2e, 0x41, 0x0a, 0x0a, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04,+ 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x61,+ 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73, 0x73, 0xca, 0xfe, 0xba, 0xbe,+ 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x21, 0x5c, 0xf4, 0xc2, 0xc7, 0xb2, 0x27, 0x00, 0x00, 0x00,+ 0x27, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x45,+ 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46,+ 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03,+ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57,+ 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01,+ 0x59, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73,+ 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02,+ 0x00, 0x79, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,+};++static const __u8 premain_in_entry_section[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0x39, 0x75, 0xf7, 0x7a, 0x3b, 0x00, 0x00, 0x00, 0x3b, 0x00,+ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0x4d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, 0x2d, 0x56,+ 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a,+ 0x0a, 0x4e, 0x61, 0x6d, 0x65, 0x3a, 0x20, 0x61, 0x2f, 0x41, 0x2e, 0x63,+ 0x6c, 0x61, 0x73, 0x73, 0x0a, 0x50, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e,+ 0x2d, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x3a, 0x20, 0x61, 0x2e, 0x41, 0x0a,+ 0x0a, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04,+ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63,+ 0x6c, 0x61, 0x73, 0x73, 0xca, 0xfe, 0xba, 0xbe, 0x50, 0x4b, 0x01, 0x02,+ 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c,+ 0x39, 0x75, 0xf7, 0x7a, 0x3b, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,+ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04, 0x00,+ 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x6d, 0x00, 0x00, 0x00,+ 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x50, 0x4b, 0x05,+ 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x79, 0x00, 0x00,+ 0x00, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00,+};++static const __u8 empty_premain[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0x92, 0x3a, 0xa4, 0xe4, 0x27, 0x00, 0x00, 0x00, 0x27, 0x00,+ 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49,+ 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e,+ 0x4d, 0x46, 0x4d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, 0x2d, 0x56,+ 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x2e, 0x30, 0x0a,+ 0x50, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x2d, 0x43, 0x6c, 0x61, 0x73,+ 0x73, 0x3a, 0x20, 0x0a, 0x0a, 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04,+ 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x61,+ 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73, 0x73, 0xca, 0xfe, 0xba, 0xbe,+ 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x21, 0x5c, 0x92, 0x3a, 0xa4, 0xe4, 0x27, 0x00, 0x00, 0x00,+ 0x27, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x45,+ 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49, 0x46,+ 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03,+ 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d, 0x57,+ 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09, 0x00,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x01,+ 0x59, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61, 0x73,+ 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02,+ 0x00, 0x79, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,+};++static const __u8 no_manifest[] = {+ 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,+ 0x21, 0x5c, 0x1d, 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00,+ 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c,+ 0x61, 0x73, 0x73, 0xca, 0xfe, 0xba, 0xbe, 0x50, 0x4b, 0x01, 0x02, 0x14,+ 0x03, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x5c, 0x1d,+ 0x57, 0x1d, 0xb5, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x09,+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,+ 0x01, 0x00, 0x00, 0x00, 0x00, 0x61, 0x2f, 0x41, 0x2e, 0x63, 0x6c, 0x61,+ 0x73, 0x73, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,+ 0x01, 0x00, 0x37, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00,+};++static void agent_jar_is_accepted_stored_or_deflated(void)+{+ EXPECT_EQ(jar_premain_class(stored_agent, sizeof(stored_agent)), 0);+ EXPECT_EQ(jar_premain_class(deflated_agent, sizeof(deflated_agent)),+ 0);+}++static void jar_without_premain_is_no_agent(void)+{+ EXPECT_EQ(jar_premain_class(no_premain, sizeof(no_premain)), -ENOENT);+ EXPECT_EQ(jar_premain_class(empty_premain, sizeof(empty_premain)),+ -ENOENT);+ EXPECT_EQ(jar_premain_class(no_manifest, sizeof(no_manifest)),+ -ENOENT);+}++/* A JVM reads the attribute from the main section only. */+static void premain_of_an_entry_section_does_not_count(void)+{+ EXPECT_EQ(jar_premain_class(premain_in_entry_section,+ sizeof(premain_in_entry_section)),+ -ENOENT);+}++static void file_that_is_no_zip_is_refused(void)+{+ static const __u8 text[] = "not a zip, though long enough to hold one";++ EXPECT_EQ(jar_premain_class(text, sizeof(text)), -EINVAL);+ EXPECT_EQ(jar_premain_class(text, 3), -EINVAL);+}++/* Every cut of a jar is refused or read within its bounds. */+static void truncated_jar_is_never_an_agent(void)+{+ __u8 cut[sizeof(deflated_agent)];+ size_t len;++ for (len = 0; len < sizeof(deflated_agent); len++) {+ memcpy(cut, deflated_agent, len);+ EXPECT_TRUE(jar_premain_class(cut, len) < 0);+ }+}++static void damaged_directory_is_refused(void)+{+ __u8 jar[sizeof(stored_agent)];+ size_t i;++ for (i = 0; i < sizeof(jar); i++) {+ memcpy(jar, stored_agent, sizeof(jar));+ jar[i] ^= 0xff;+ EXPECT_TRUE(jar_premain_class(jar, sizeof(jar)) <= 0);+ }+}++static const struct unit_case cases[] = {+ UNIT_CASE(agent_jar_is_accepted_stored_or_deflated),+ UNIT_CASE(jar_without_premain_is_no_agent),+ UNIT_CASE(premain_of_an_entry_section_does_not_count),+ UNIT_CASE(file_that_is_no_zip_is_refused),+ UNIT_CASE(truncated_jar_is_never_an_agent),+ UNIT_CASE(damaged_directory_is_refused),+ {}+};++UNIT_SUITE(jar, cases);diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 9ca148fc..d827a08e 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -379,6 +379,19 @@ static bool build_version_ok(const char *s)return s && strlen(s) < 128 && bpm_semver_valid(s);}+/*+ * A BPF upatch, or a Java agent jar, which has no maps to rewrite or+ * label and no programs for the tree's device classes.+ */+static bool build_upatch_type_ok(const struct bpm_build_upatch *u)+{+ if (!strcmp(u->type, "bpf"))+ return true;+ return !strcmp(u->type, "java") &&+ name_has_suffix(u->source, strlen(u->source), ".jar") &&+ !u->n_rodata && !u->n_policies && !u->tree.present;+}+static int build_default_targets(struct bpm_build_upatch *u){char base[PATH_MAX];@@ -720,7 +733,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)strscpy(u->type, "bpf", sizeof(u->type));for (p = u->type; *p; p++)*p = (char)tolower((__u8)*p);- if (strcmp(u->type, "bpf") || build_default_targets(u))+ if (!build_upatch_type_ok(u) || build_default_targets(u))return -EINVAL;for (size_t j = 0; j < u->n_rodata; j++) {struct bpm_build_rodata *r = &u->rodata[j];@@ -1221,6 +1234,8 @@ int build_upatch_name(const char *source, char *out, size_t cap)out[n - 6] = '\0';else if (name_has_suffix(out, n, ".o"))out[n - 2] = '\0';+ else if (name_has_suffix(out, n, ".jar"))+ out[n - 4] = '\0';return 0;}@@ -1341,6 +1356,7 @@ static int build_upatch_unload(struct bpm_build_upatch *u, const char *load,{__u8 payload[CRYPTO_SHA256_LEN];struct upatch parsed = {};+ bool java;int ret;if (upatch_open(load, &parsed))@@ -1354,11 +1370,32 @@ static int build_upatch_unload(struct bpm_build_upatch *u, const char *load,u->payload_sha256);memcpy(payload, parsed.hdr.payload_sha256, sizeof(payload));strscpy(u->type, upatch_type_str(&parsed), sizeof(u->type));+ java = parsed.hdr.type == UPATCH_TYPE_JAVA;upatch_close(&parsed);- ret = build_package_unload(u->name, payload, unload, pkey, cert);+ if (java)+ ret = build_package_java_unload(u->name, payload, unload, pkey,+ cert);+ else+ ret = build_package_unload(u->name, payload, unload, pkey,+ cert);return ret < 0 ? ret : ret ? -EIO : 0;}+static int build_java_upatch(struct bpm_build_upatch *u, const char *source,+ const char *load, const char *unload,+ const char *pkey, const char *cert)+{+ char name[UPATCH_NAME_MAX];+ int ret;++ if (build_upatch_name(source, name, sizeof(name)))+ return -ENAMETOOLONG;+ ret = build_package_java_load(source, load, name, pkey, cert);+ if (ret)+ return ret < 0 ? ret : -EIO;+ return build_upatch_unload(u, load, unload, pkey, cert);+}+/** The unload directive is bound to the payload digest the load artifact* ended up with.@@ -1390,6 +1427,13 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,return -ENAMETOOLONG;strscpy(u->load_path, load, sizeof(u->load_path));strscpy(u->unload_path, unload, sizeof(u->unload_path));+ if (!strcmp(u->type, "java")) {+ ret = build_java_upatch(u, source, load, unload, pkey,+ cert);+ if (ret)+ return ret;+ continue;+ }if (name_has_suffix(source, strlen(source), ".bpf.c")) {if (!vmlinux_h) {vmlinux_h = build_locate_vmlinux(NULL);@@ -2369,7 +2413,6 @@ static int __build_bpm_config_update(int argc, const char **argv,char load[PATH_MAX], unload[PATH_MAX];char expected_name[UPATCH_NAME_MAX];struct upatch parsed = {};- __u8 payload[CRYPTO_SHA256_LEN];if (strsprintf(load, sizeof(load), "%s/update-%zu.upatch",spec->stage, i) < 0 ||@@ -2404,24 +2447,10 @@ static int __build_bpm_config_update(int argc, const char **argv,overrides, n_overrides, true);if (ret)break;- ret = upatch_open(load, &parsed);- if (ret)- break;- if (upatch_is_unload(&parsed)) {- ret = -EINVAL;- upatch_close(&parsed);- break;- }- upatch_header_name(&parsed, upatch->name);- memcpy(payload, parsed.hdr.payload_sha256, sizeof(payload));- hex_encode(payload, sizeof(payload), upatch->payload_sha256);- strscpy(upatch->type, upatch_type_str(&parsed),- sizeof(upatch->type));- upatch_close(&parsed);strscpy(upatch->load_path, load, sizeof(upatch->load_path));strscpy(upatch->unload_path, unload, sizeof(upatch->unload_path));- ret = build_package_unload(upatch->name, payload, unload,- pkey, cert_path);+ ret = build_upatch_unload(upatch, load, unload, pkey,+ cert_path);}if (!ret)ret = build_archive("bpm config update", spec, version,diff --git a/tools/shirudo-build/jar.c b/tools/shirudo-build/jar.cnew file mode 100644index 00000000..dd61dc8b--- /dev/null+++ b/tools/shirudo-build/jar.c@@ -0,0 +1,237 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The manifest of a jar, read from its zip central directory, and the+ * Premain-Class attribute a Java agent needs in it.+ */++#include <errno.h>+#include <string.h>+#include <zlib.h>++#include <shirudo/byteorder.h>+#include <shirudo/misc.h>+#include <shirudo/string.h>++#include "jar.h"+#include "util/malloc.h"++#define ZIP_END_SIG 0x06054b50u+#define ZIP_END_LEN 22u+#define ZIP_COMMENT_MAX 65535u+#define ZIP_ENTRY_SIG 0x02014b50u+#define ZIP_ENTRY_LEN 46u+#define ZIP_LOCAL_SIG 0x04034b50u+#define ZIP_LOCAL_LEN 30u+#define ZIP_STORED 0+#define ZIP_DEFLATED 8+#define JAR_MANIFEST_MAX (1u << 20)++static const char manifest_name[] = "META-INF/MANIFEST.MF";+static const char premain_attr[] = "Premain-Class:";++struct zip_entry {+ __u16 method;+ __u32 packed_len;+ __u32 len;+ __u32 local_off;+};++static bool in_bounds(size_t off, size_t need, size_t len)+{+ return off <= len && need <= len - off;+}++static char ascii_lower(char c)+{+ return c >= 'A' && c <= 'Z' ? (char)(c - 'A' + 'a') : c;+}++/* A JVM finds the manifest and its attributes whatever their case. */+static bool same_ignoring_case(const __u8 *a, const char *b, size_t len)+{+ size_t i;++ for (i = 0; i < len; i++) {+ if (ascii_lower((char)a[i]) != ascii_lower(b[i]))+ return false;+ }+ return true;+}++/* The end record is last but for a comment, so the search runs backward. */+static int zip_directory(const __u8 *zip, size_t len, size_t *off,+ size_t *entries)+{+ size_t at, low;+ __u32 dir_off;++ if (len < ZIP_END_LEN)+ return -EINVAL;+ at = len - ZIP_END_LEN;+ low = at > ZIP_COMMENT_MAX ? at - ZIP_COMMENT_MAX : 0;+ while (get_le32(zip + at) != ZIP_END_SIG) {+ if (at == low)+ return -EINVAL;+ at--;+ }+ *entries = get_le16(zip + at + 10);+ dir_off = get_le32(zip + at + 16);+ if (*entries == 0xffff || dir_off == 0xffffffffu)+ return -ENOTSUP;+ if (dir_off > at)+ return -EINVAL;+ *off = dir_off;+ return 0;+}++/* 0 with *e the entry named name, -ENOENT for none. */+static int zip_find(const __u8 *zip, size_t len, const char *name,+ struct zip_entry *e)+{+ size_t off, entries, name_len, skip, i;+ int ret;++ ret = zip_directory(zip, len, &off, &entries);+ if (ret)+ return ret;+ for (i = 0; i < entries; i++) {+ if (!in_bounds(off, ZIP_ENTRY_LEN, len) ||+ get_le32(zip + off) != ZIP_ENTRY_SIG)+ return -EINVAL;+ name_len = get_le16(zip + off + 28);+ skip = (size_t)get_le16(zip + off + 30) ++ get_le16(zip + off + 32);+ if (!in_bounds(off + ZIP_ENTRY_LEN, name_len + skip, len))+ return -EINVAL;+ if (name_len == strlen(name) &&+ same_ignoring_case(zip + off + ZIP_ENTRY_LEN, name,+ name_len)) {+ e->method = get_le16(zip + off + 10);+ e->packed_len = get_le32(zip + off + 20);+ e->len = get_le32(zip + off + 24);+ e->local_off = get_le32(zip + off + 42);+ return 0;+ }+ off += ZIP_ENTRY_LEN + name_len + skip;+ }+ return -ENOENT;+}++/* Where the entry's bytes start, past its local header. */+static int zip_entry_data(const __u8 *zip, size_t len,+ const struct zip_entry *e, const __u8 **data)+{+ size_t off = e->local_off, skip;++ if (!in_bounds(off, ZIP_LOCAL_LEN, len) ||+ get_le32(zip + off) != ZIP_LOCAL_SIG)+ return -EINVAL;+ skip = (size_t)get_le16(zip + off + 26) + get_le16(zip + off + 28);+ off += ZIP_LOCAL_LEN;+ if (!in_bounds(off, skip, len) ||+ !in_bounds(off + skip, e->packed_len, len))+ return -EINVAL;+ *data = zip + off + skip;+ return 0;+}++static int inflate_entry(const __u8 *packed, const struct zip_entry *e,+ __u8 *out)+{+ z_stream z = {};+ int ret;++ /* Negative window bits: a zip entry is a raw deflate stream. */+ if (inflateInit2(&z, -MAX_WBITS) != Z_OK)+ return -ENOMEM;+ z.next_in = (Bytef *)packed;+ z.avail_in = e->packed_len;+ z.next_out = out;+ z.avail_out = e->len;+ ret = inflate(&z, Z_FINISH);+ inflateEnd(&z);+ return ret == Z_STREAM_END && !z.avail_out ? 0 : -EINVAL;+}++/* The entry's bytes, xmalloc'd, e->len of them. */+static int zip_entry_read(const __u8 *zip, size_t len,+ const struct zip_entry *e, __u8 **out)+{+ const __u8 *data;+ __u8 *buf;+ int ret;++ if (e->len > JAR_MANIFEST_MAX)+ return -EFBIG;+ if (e->method != ZIP_STORED && e->method != ZIP_DEFLATED)+ return -ENOTSUP;+ if (e->method == ZIP_STORED && e->packed_len != e->len)+ return -EINVAL;+ ret = zip_entry_data(zip, len, e, &data);+ if (ret)+ return ret;+ buf = xmalloc((size_t)e->len + 1);+ if (!buf)+ return -ENOMEM;+ if (e->method == ZIP_STORED)+ memcpy(buf, data, e->len);+ else+ ret = inflate_entry(data, e, buf);+ if (ret) {+ xfree(buf, (size_t)e->len + 1);+ return ret;+ }+ *out = buf;+ return 0;+}++static bool line_end(__u8 c)+{+ return c == '\n' || c == '\r';+}++/*+ * Whether the main section, which the first empty line ends, has a+ * Premain-Class with a value.+ */+static bool manifest_has_premain(const __u8 *m, size_t len)+{+ size_t attr = strlen(premain_attr), at = 0, end;++ while (at < len && !line_end(m[at])) {+ for (end = at; end < len && !line_end(m[end]); end++)+ ;+ if (end - at > attr &&+ same_ignoring_case(m + at, premain_attr, attr)) {+ for (at += attr; at < end; at++) {+ if (m[at] != ' ')+ return true;+ }+ return false;+ }+ at = end;+ if (at < len && m[at] == '\r')+ at++;+ if (at < len && m[at] == '\n')+ at++;+ }+ return false;+}++int jar_premain_class(const __u8 *jar, size_t len)+{+ struct zip_entry e;+ __u8 *manifest;+ int ret;++ ret = zip_find(jar, len, manifest_name, &e);+ if (!ret)+ ret = zip_entry_read(jar, len, &e, &manifest);+ if (ret)+ return ret;+ ret = manifest_has_premain(manifest, e.len) ? 0 : -ENOENT;+ xfree(manifest, (size_t)e.len + 1);+ return ret;+}diff --git a/tools/shirudo-build/jar.h b/tools/shirudo-build/jar.hnew file mode 100644index 00000000..06d06627--- /dev/null+++ b/tools/shirudo-build/jar.h@@ -0,0 +1,24 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * What the builder checks of a jar before it signs it as a Java agent.+ */++#ifndef SHIRUDO_BUILD_JAR_H+#define SHIRUDO_BUILD_JAR_H++#include <stddef.h>++#include <shirudo/misc.h>++/*+ * Whether the jar's manifest names a Premain-Class, without which a JVM+ * given the jar as -javaagent does not start: 0 when it does, -ENOENT+ * for no manifest or no such attribute, -ENOTSUP for a zip64 archive or+ * a manifest neither stored nor deflated, -EFBIG for a manifest past+ * 1 MiB, -EINVAL for a file that is no zip archive or a damaged one.+ */+int jar_premain_class(const __u8 *jar, size_t len) __must_check;++#endif /* SHIRUDO_BUILD_JAR_H */diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex 89eb1580..1764fa50 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -31,6 +31,7 @@#include "bpm_package.h"#include "elf_target.h"#include "io.h"+#include "jar.h"#include "loader.h"#include "help.h"#include "log.h"@@ -2369,3 +2370,141 @@ int build_package_unload(const char *upatch_name,DIRECTIVE_UNLOAD, payload_sha256,NULL, NULL, 0, pkey_path, cert_path);}++/*+ * A Java upatch has no loader: its data is the signed sections alone,+ * NAME and PTYP, then the jar of a load or the digest an unload expects.+ * The target verifies the signature over that data in userspace.+ */+static int finalize_java(const char *out_path, const char *upatch_name,+ __u32 flags, const struct signed_section *payload,+ const char *pkey_path, const char *cert_path)+{+ __le32 type = cpu_to_le32(UPATCH_TYPE_JAVA);+ char signed_name[UPATCH_NAME_MAX] = {};+ __u32 signed_data_len = 0, data_off;+ struct signed_section sections[3];+ struct upatch_header hdr;+ void *signed_data = NULL;+ struct sign_io sio = {};+ void *sig_buf = NULL;+ int err;++ if (strscpy(signed_name, upatch_name, sizeof(signed_name)) < 0)+ return -ENAMETOOLONG;+ sections[0] = (struct signed_section){+ UPATCH_SECTION_NAME, signed_name, sizeof(signed_name),+ };+ sections[1] = (struct signed_section){+ UPATCH_SECTION_TYPE, &type, sizeof(type),+ };+ sections[2] = *payload;+ err = compose_signed_data(NULL, 0, sections, ARRAY_SIZE(sections),+ &signed_data, &signed_data_len);+ if (err)+ return err;+ sig_buf = xmalloc(MAX_SIG_SIZE);+ if (!sig_buf) {+ err = -ENOMEM;+ goto out;+ }+ sio.insns = signed_data;+ sio.insns_sz = 0;+ sio.data = signed_data;+ sio.data_sz = signed_data_len;+ sio.signature = sig_buf;+ sio.signature_sz = MAX_SIG_SIZE;+ err = cms_sign(&sio, pkey_path, cert_path);+ if (err) {+ pr_err("cms_sign: %d", err);+ goto out;+ }+ if (check_add_overflow(sizeof(hdr), sio.signature_sz, &data_off) ||+ data_off > UPATCH_FILE_MAX ||+ signed_data_len > UPATCH_FILE_MAX - data_off) {+ pr_err("%s: the jar and its signature do not fit a upatch of %u bytes",+ upatch_name, UPATCH_FILE_MAX);+ err = -E2BIG;+ goto out;+ }+ pack_header(&hdr, upatch_name, UPATCH_TYPE_JAVA, flags,+ sio.payload_sha256, 0, 0,+ (__u32)sizeof(hdr), (__u32)sio.signature_sz, 0, 0,+ data_off, signed_data_len, 0, 0, 0, 0);+ err = write_artifact(out_path, &hdr, NULL, 0, sig_buf,+ sio.signature_sz, signed_data, signed_data_len);+ if (!err)+ pr_info("wrote %s (java sig=%zu data=%u name=%s%s)", out_path,+ sio.signature_sz, signed_data_len, upatch_name,+ (flags & DIRECTIVE_UNLOAD) ? " UNLOAD" : "");+out:+ xfree(sig_buf, MAX_SIG_SIZE);+ xfree(signed_data, signed_data_len);+ return err;+}++/*+ * Refused here, where the operator sees it: on the target a jar a JVM+ * does not take as an agent stops every JVM the options reach.+ */+static int check_agent_jar(const char *path, const __u8 *buf, size_t len)+{+ int err = jar_premain_class(buf, len);++ if (err == -ENOENT)+ pr_err("%s has no Premain-Class in its manifest: a JVM does not start with it as -javaagent",+ path);+ else if (err == -ENOTSUP)+ pr_err("%s: a zip64 jar or a manifest neither stored nor deflated is not supported",+ path);+ else if (err == -EFBIG)+ pr_err("%s: the manifest is too large", path);+ else if (err)+ pr_err("%s is not a jar", path);+ return err;+}++int build_package_java_load(const char *jar_path, const char *out_path,+ const char *upatch_name,+ const char *pkey_path, const char *cert_path)+{+ struct signed_section jar;+ size_t len = 0;+ __u8 *buf;+ int err;++ err = check_upatch_name(upatch_name);+ if (err)+ return err;+ err = read_file(jar_path, UPATCH_FILE_MAX, &buf, &len);+ if (err) {+ pr_err("read %s: %s", jar_path, strerror(-err));+ return err;+ }+ err = check_agent_jar(jar_path, buf, len);+ if (err)+ goto out;+ jar = (struct signed_section){ UPATCH_SECTION_JAR, buf, (__u32)len };+ err = finalize_java(out_path, upatch_name, 0, &jar, pkey_path,+ cert_path);+out:+ xfree(buf, len + 1);+ return err;+}++int build_package_java_unload(const char *upatch_name,+ const __u8 payload_sha256[32],+ const char *out_path,+ const char *pkey_path, const char *cert_path)+{+ struct signed_section unload = {+ UPATCH_SECTION_UNLOAD, payload_sha256, UPATCH_PAYLOAD_HASH_LEN,+ };+ int err;++ err = check_upatch_name(upatch_name);+ if (err)+ return err;+ return finalize_java(out_path, upatch_name, DIRECTIVE_UNLOAD, &unload,+ pkey_path, cert_path);+}diff --git a/tools/shirudo-build/package.h b/tools/shirudo-build/package.hindex 134bd582..da5e3fea 100644--- a/tools/shirudo-build/package.h+++ b/tools/shirudo-build/package.h@@ -24,6 +24,15 @@ int build_package_unload(const char *name,const char *pkey_path,const char *cert_path);+/* A Java agent jar, and the unload directive of a Java upatch. */+int build_package_java_load(const char *jar_path, const char *out_upatch,+ const char *upatch_name,+ const char *pkey_path, const char *cert_path);+int build_package_java_unload(const char *name,+ const __u8 payload_sha256[32],+ const char *out_upatch,+ const char *pkey_path, const char *cert_path);+int build_package_patch_rodata(const char *input, const char *output,const char *symbol, const __u8 *value,size_t value_len, const char *pkey_path,diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 1d060038..ccab3393 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -45,6 +45,7 @@ enum build_opt {BUILD_EXTRA_CFLAGS,BUILD_UNLOAD,BUILD_PAYLOAD_SHA256,+ BUILD_JAVA,BUILD_EMIT_ARTIFACTS,};@@ -72,6 +73,9 @@ static const struct opt_spec build_opts[] = {[BUILD_PAYLOAD_SHA256] = { "--payload-sha256", 0, 0, "<64-hex>","with --unload, the payload digest, which the archive ""/var/lib/shirudo/upatches/<name>.upatch gives otherwise" },+ [BUILD_JAVA] = { "--java", 0, 0, NULL,+ "with --unload and --payload-sha256, sign the directive of a "+ "Java upatch, which the archive tells otherwise" },[BUILD_EMIT_ARTIFACTS] = { "--emit-artifacts", 0, 0, NULL,"keep the compiled .bpf.o beside the .upatch and print each ""path written" },@@ -82,13 +86,14 @@ static const struct help_section build_sections[] = {};static const struct help build_help = {- .usage = "shirudo build <input.bpf.c|.bpf.o> [-o <output.upatch>] "- "[options]\n"+ .usage = "shirudo build <input.bpf.c|.bpf.o|.jar> "+ "[-o <output.upatch>] [options]\n"" shirudo build --unload <upatch-name> ""[-o <output.upatch>] [options]",- .summary = "Compile a .bpf.c, or take a .bpf.o, and sign it into a "- ".upatch, or sign\nthe UNLOAD directive of a loaded "- "upatch. foo/bar.bpf.c becomes\nfoo/bar.bpf.upatch.",+ .summary = "Compile a .bpf.c, or take a .bpf.o or a Java agent "+ ".jar, and sign it into a\n.upatch, or sign the UNLOAD "+ "directive of a loaded upatch.\nfoo/bar.bpf.c becomes "+ "foo/bar.bpf.upatch, foo/agent.jar foo/agent.upatch.",.sections = build_sections,.n_sections = ARRAY_SIZE(build_sections),.footer = "The helper also serves shirudo image build and verify, "@@ -104,6 +109,8 @@ static int derive_output(const char *input, char *buf, size_t buf_sz)if (name_has_suffix(input, stem, ".c") ||name_has_suffix(input, stem, ".o"))stem -= 2;+ else if (name_has_suffix(input, stem, ".jar"))+ stem -= 4;if (strsprintf(buf, buf_sz, "%.*s.upatch", (int)stem, input) < 0)return -ENAMETOOLONG;@@ -193,6 +200,7 @@ int main(int argc, char **argv)char name_buf[256];const char *bpf_o;int emit_artifacts = 0;+ bool java = false;int pkey_explicit = 0;int cert_explicit = 0;struct opt_parser p;@@ -245,6 +253,9 @@ int main(int argc, char **argv)case BUILD_PAYLOAD_SHA256:payload_sha256_hex = p.value;break;+ case BUILD_JAVA:+ java = true;+ break;case BUILD_EMIT_ARTIFACTS:emit_artifacts = 1;break;@@ -267,7 +278,11 @@ int main(int argc, char **argv)return EXIT_USAGE;}if (!unload_name && !input) {- opt_usage(&p, "takes <input.bpf.c|.bpf.o> or --unload <upatch-name>");+ opt_usage(&p, "takes <input.bpf.c|.bpf.o|.jar> or --unload <upatch-name>");+ return EXIT_USAGE;+ }+ if (java && (!unload_name || !payload_sha256_hex)) {+ opt_usage(&p, "--java takes --unload and --payload-sha256");return EXIT_USAGE;}@@ -325,6 +340,7 @@ int main(int argc, char **argv)if (upatch_open(archive, &u) == 0) {memcpy(payload_sha256, u.hdr.payload_sha256,sizeof(payload_sha256));+ java = u.hdr.type == UPATCH_TYPE_JAVA;upatch_close(&u);pr_info("--payload-sha256 derived from archive %s",archive);@@ -346,8 +362,13 @@ int main(int argc, char **argv)pr_info("writing %s (no -o given)", output);}- ret = build_package_unload(unload_name, payload_sha256,- output, pkey, cert);+ if (java)+ ret = build_package_java_unload(unload_name,+ payload_sha256, output,+ pkey, cert);+ else+ ret = build_package_unload(unload_name, payload_sha256,+ output, pkey, cert);if (ret == 0 && emit_artifacts) {printf("emitted:\n");printf(" upatch: %s\n", output);@@ -374,6 +395,21 @@ int main(int argc, char **argv)pr_info("writing %s (no -o given)", output);}+ if (!name) {+ if (build_upatch_name(input, name_buf, sizeof(name_buf))) {+ pr_err("upatch name from %s too long", input);+ return 2;+ }+ name = name_buf;+ }++ if (name_has_suffix(input, strlen(input), ".jar")) {+ ret = build_package_java_load(input, output, name, pkey, cert);+ if (ret == 0 && emit_artifacts)+ printf("emitted:\n upatch: %s\n", output);+ return ret ? 1 : 0;+ }+if (name_has_suffix(input, strlen(input), ".bpf.c")) {const char *vmlinux_h =build_locate_vmlinux(vmlinux_override);@@ -413,18 +449,10 @@ int main(int argc, char **argv)} else if (name_has_suffix(input, strlen(input), ".o")) {bpf_o = input; /* decoder paths resolve from the cwd */} else {- pr_err("input must be .bpf.c or .bpf.o");+ pr_err("input must be .bpf.c, .bpf.o or .jar");return 2;}- if (!name) {- if (build_upatch_name(input, name_buf, sizeof(name_buf))) {- pr_err("upatch name from %s too long", input);- return 2;- }- name = name_buf;- }-ret = build_package_load(bpf_o, src_path, output, name,NULL, 0, pkey, cert);