Skip to content

eBPF-based security platform

upatch: parse Java agent payloads and their signed type

John Fastabend1ee454b4026c
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 064f4e79..ca911bc4 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -510,10 +510,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Every file: the header is read from a buffer of exactly its size
for files of any size
- Reads nothing past the header; an accepted header has its magic,
- version, zero reserved field and a terminated name, a BPF
- payload, a loader and a signature, whole aligned instructions,
- every section inside the file and behind the header, and a
- ``.rodata`` offset inside the data
+ version, zero reserved field and a terminated name, a BPF or Java
+ payload, a signature, every section inside the file and behind
+ the header; a BPF payload has a loader of whole aligned
+ instructions and a ``.rodata`` offset inside the data, a Java
+ payload data and none of the loader's fields
* - ``upatch-signed-sections``
- ``parse_signed_sections()``
- Data of 15, 16, 40 and 64 bytes
diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rst
index 32650f2c..50450080 100644
--- a/Documentation/reference/formats/upatch-format.rst
+++ b/Documentation/reference/formats/upatch-format.rst
@@ -165,7 +165,9 @@ Header
* - 352
- 4
- ``type``
- - ``1`` (``UPATCH_TYPE_BPF``).
+ - ``1`` (``UPATCH_TYPE_BPF``) or ``2`` (``UPATCH_TYPE_JAVA``), see
+ `Java payloads`_. Equal to the signed ``PTYP`` section, or 1 where
+ there is none.
* - 356
- 4
- ``reserved1``
@@ -196,7 +198,8 @@ Signature
* - Size
- At most 16 KiB from the writer. The kernel accepts at most 64 KiB.
* - Verifier
- - The kernel, during ``BPF_PROG_LOAD`` of the loader, with
+ - For a Java payload, the target in userspace, see `Java payloads`_.
+ For a BPF payload, the kernel, during ``BPF_PROG_LOAD`` of the loader, with
``attr.keyring_id`` set to ``KEY_SPEC_BPF_KEYRING`` (the ``.bpf``
keyring). The target passes ``data`` as the frozen value of a
one-element array map, ``__loader.map``, made exclusive to the
@@ -311,9 +314,41 @@ Section tags
absolute file path. Its presence opts into the shared task
``shirudo_task_policy`` and socket ``shirudo_sock_policy``
bindings.
-
-The writer emits ``NAME``, then ``UNLD`` for an unload directive, or
-``UPGM`` and optional ``DCDB`` and ``SHPL`` sections for a load artifact.
+ * - ``PTYP``
+ - ``0x50595450``
+ - 4
+ - The payload type, a little-endian ``__u32`` equal to the header
+ ``type``. Required in a Java payload, absent from a BPF one.
+ * - ``JAVA``
+ - ``0x4156414a``
+ - variable, non-zero
+ - The Java agent ``.jar``, byte for byte. Required in a Java load
+ artifact.
+
+For a BPF payload the writer emits ``NAME``, then ``UNLD`` for an unload
+directive, or ``UPGM`` and optional ``DCDB`` and ``SHPL`` sections for a
+load artifact. For a Java payload it emits ``NAME`` and ``PTYP``, then
+``UNLD`` for an unload directive or ``JAVA`` for a load artifact.
+
+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 against the certificates the sealed ``.bpf`` keyring
+was enrolled from.
+
+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 moves the directory away from where the trailer points, so the reader
+finds no ``NAME`` and the load refuses.
UPGM section
~~~~~~~~~~~~
@@ -732,14 +767,16 @@ file.
#. ``version`` equals 1.
#. ``name`` contains a NUL.
#. ``reserved1`` is 0.
-#. ``type`` equals 1.
-#. ``loader_insns_off`` is at least 360 and a multiple of 8,
- ``loader_insns_len`` is a multiple of 8, and the region lies inside the
- file.
#. ``signature``, ``metadata`` and ``data`` each lie inside the file, and
each non-empty one starts at or after offset 360.
-#. ``rodata_data_off`` is at most ``data_len``.
-#. ``loader_insns_len`` and ``signature_len`` are not 0.
+#. ``signature_len`` is not 0.
+#. ``type`` is 1 or 2.
+#. For ``type`` 1: ``loader_insns_off`` is at least 360 and a multiple of
+ 8, ``loader_insns_len`` is a multiple of 8 and not 0, the region lies
+ inside the file, and ``rodata_data_off`` is at most ``data_len``.
+#. For ``type`` 2: ``loader_insns_off``, ``loader_insns_len``,
+ ``metadata_len``, ``log_buf_sz``, ``nr_maps``, ``nr_progs`` and
+ ``rodata_data_off`` are 0, and ``data_len`` is 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
@@ -752,15 +789,20 @@ unload later refuses for the section it lacks, and
and the trailer.
#. Each entry has ``reserved`` 0 and a section that ends at or before
``dir_off``.
-#. Each tag is ``NAME``, ``UPGM``, ``UNLD``, ``DCDB`` or ``SHPL``, and appears at most
- once.
+#. Each tag is ``NAME``, ``UPGM``, ``UNLD``, ``DCDB``, ``SHPL``, ``PTYP``
+ or ``JAVA``, and appears at most once.
#. ``NAME`` is 256 bytes and contains a NUL. ``UPGM`` is not empty.
- ``UNLD`` is 32 bytes.
+ ``UNLD`` is 32 bytes. ``PTYP`` is 4 bytes. ``JAVA`` is not empty.
#. ``DCDB`` has magic ``DCDB``, version 1, at least one entry, all entries
inside the section, NUL-terminated strings, non-zero ``language``,
``api_version``, ``input_format`` and ``export_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
@@ -774,10 +816,11 @@ Target admission
----------------
A load artifact passes the reader, then these checks in order. A failure
-of checks 1 to 6 is reported as ``parser_rejected``, see
+of checks 1 to 7 is reported as ``parser_rejected``, see
:doc:`/reference/failure-categories`.
#. ``flags`` bit 0 is clear.
+#. ``type`` is 1.
#. ``loader_insns_len`` is a multiple of 8.
#. The header ``name`` is not empty, does not start with ``.``, contains
only ``[A-Za-z0-9._-]`` and has at most 255 characters with each ``.``
@@ -799,7 +842,7 @@ An unload directive passes the reader, then these checks:
#. ``flags`` bit 0 is set.
#. ``NAME`` and ``UNLD`` sections are present and ``NAME`` equals the
header ``name``.
-#. Load checks 2 to 5 above, then the kernel verifies the signature in
+#. 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 ``UNLD`` value equals the ``security.bpf.shirudo.payload`` xattr of
the loaded upatch, or no upatch of that name is pinned.
diff --git a/builtin-upatch.c b/builtin-upatch.c
index e8eaf0ed..b499a692 100644
--- a/builtin-upatch.c
+++ b/builtin-upatch.c
@@ -2167,6 +2167,18 @@ static int refuse_unload_directive(const struct signed_load *sl)
return 0;
}
+/* Only a BPF payload has a loader for the kernel to verify and run. */
+static int refuse_foreign_payload(const struct signed_load *sl)
+{
+ if (sl->u->hdr.type == UPATCH_TYPE_BPF)
+ return 0;
+ pr_err("upatch load: %s carries a %s payload, not BPF", sl->label,
+ upatch_type_str(sl->u));
+ rep_fail(sl->rep, "parse", "parser_rejected", 0,
+ "not a BPF payload");
+ return 1;
+}
+
/*
* The pin directory, the archive and unload share the raw name. Boot
* restore reaches here without the command-level check, so validate it
@@ -2541,7 +2553,7 @@ static int signed_load_run(struct signed_load *sl)
const struct upatch *u = sl->u;
int ret;
- if (refuse_unload_directive(sl) ||
+ if (refuse_unload_directive(sl) || refuse_foreign_payload(sl) ||
trust_anchor_keyring(&sl->keyring, &sl->serial, sl->rep) ||
upatch_signed_name_check(sl) || upatch_payload_check(sl) ||
upatch_attach_prepare(sl) || loader_map_prepare(sl) ||
diff --git a/include/shirudo/upatch.h b/include/shirudo/upatch.h
index c770178c..294b8fc9 100644
--- a/include/shirudo/upatch.h
+++ b/include/shirudo/upatch.h
@@ -17,8 +17,10 @@
/*
* The PKCS#7 signature covers insns || data (the gen_loader metadata
* blob), verified by the kernel at load time; payload_sha256 is SHA-256
- * of that same concatenation. See the kernel's Documentation/bpf/signing.rst
- * and Documentation/reference/formats/upatch-format.rst in this tree.
+ * of that same concatenation. A Java payload has no insns, and the target
+ * verifies its signature over the data against the sealed certificates.
+ * See the kernel's Documentation/bpf/signing.rst and
+ * Documentation/reference/formats/upatch-format.rst in this tree.
*/
#define UPATCH_VERSION 1u
#define UPATCH_HEADER_SIZE 360u
@@ -27,6 +29,7 @@
enum upatch_type {
UPATCH_TYPE_BPF = 1,
+ UPATCH_TYPE_JAVA = 2,
};
#define UPATCH_NAME_MAX 256
@@ -70,7 +73,9 @@ BUILD_BUG_ON(sizeof(struct upatch_header) != UPATCH_HEADER_SIZE);
* data's last 16 bytes. The loader ignores the bytes behind its own.
* A LOAD carries its name and its attach metadata, an UNLOAD directive
* the name of its target and the payload digest it expects, and a LOAD
- * carries decoders when its programs declare any.
+ * carries decoders when its programs declare any. A Java upatch carries
+ * its payload type, which the header's type must match, and a Java LOAD
+ * its agent .jar.
*/
#define UPATCH_SIGNED_TRAILER_MAGIC 0x52544753u /* "SGTR" le */
#define UPATCH_SIGNED_VERSION 1u
@@ -82,6 +87,8 @@ enum upatch_section_tag {
UPATCH_SECTION_UNLOAD = 0x444c4e55u, /* "UNLD" le */
UPATCH_SECTION_DECODERS = 0x42444344u, /* "DCDB" le */
UPATCH_SECTION_POLICY_LABELS = 0x4c504853u, /* "SHPL" le */
+ UPATCH_SECTION_TYPE = 0x50595450u, /* "PTYP" le */
+ UPATCH_SECTION_JAR = 0x4156414au, /* "JAVA" le */
};
#define UPATCH_POLICY_LABELS_MAGIC 0x4c504853u /* "SHPL" le */
diff --git a/testing/fuzz/corpus/upatch/java b/testing/fuzz/corpus/upatch/java
new file mode 100644
index 00000000..98fb728b
Binary files /dev/null and b/testing/fuzz/corpus/upatch/java differ
diff --git a/testing/fuzz/fuzz-upatch.c b/testing/fuzz/fuzz-upatch.c
index 0fe28c51..d710f916 100644
--- a/testing/fuzz/fuzz-upatch.c
+++ b/testing/fuzz/fuzz-upatch.c
@@ -13,7 +13,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
struct prog_spec *specs;
struct upatch u;
const void *attach;
- __u32 attach_len;
+ __u32 attach_len, jar_len;
char path[64];
int fd;
@@ -29,6 +29,8 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)
upatch_signed_name(&u);
upatch_signed_unload_payload(&u);
upatch_is_unload(&u);
+ upatch_signed_type(&u);
+ upatch_signed_jar(&u, &jar_len);
if (upatch_has_decoders(&u)) {
struct upatch_decoder_view d;
diff --git a/testing/proofs/cbmc/upatch-header.c b/testing/proofs/cbmc/upatch-header.c
index 87f9ec70..c83a994f 100644
--- a/testing/proofs/cbmc/upatch-header.c
+++ b/testing/proofs/cbmc/upatch-header.c
@@ -5,7 +5,8 @@
* The header of a .upatch, the target's first look at a file from disk or
* a registry, for files of every size: util/upatch.c reads nothing past
* the header, and the sections of a header it accepts lie inside the file
- * and behind the header.
+ * and behind the header, a BPF payload with its loader and a Java payload
+ * with none of the loader's fields.
*/
#include "proof.h"
@@ -34,14 +35,23 @@ void harness(void)
"a terminated name");
if (validate_bounds(&h, n))
return;
- __CPROVER_assert(h.type == UPATCH_TYPE_BPF, "a BPF payload");
+ __CPROVER_assert(h.type == UPATCH_TYPE_BPF ||
+ h.type == UPATCH_TYPE_JAVA, "a known payload");
__CPROVER_assert(inside(h.loader_insns_off, h.loader_insns_len, n) &&
inside(h.signature_off, h.signature_len, n) &&
inside(h.metadata_off, h.metadata_len, n) &&
inside(h.data_off, h.data_len, n),
"every section inside the file, behind the header");
- __CPROVER_assert(h.loader_insns_len && h.signature_len,
- "a loader and a signature");
+ __CPROVER_assert(h.signature_len, "a signature");
+ if (h.type == UPATCH_TYPE_JAVA) {
+ __CPROVER_assert(!h.loader_insns_off && !h.loader_insns_len &&
+ !h.metadata_len && !h.log_buf_sz &&
+ !h.nr_maps && !h.nr_progs &&
+ !h.rodata_data_off && h.data_len,
+ "data alone, without a loader's fields");
+ return;
+ }
+ __CPROVER_assert(h.loader_insns_len, "a loader");
__CPROVER_assert(!(h.loader_insns_off % 8) &&
!(h.loader_insns_len % 8),
"whole, aligned instructions");
diff --git a/testing/proofs/cbmc/upatch-signed-sections.c b/testing/proofs/cbmc/upatch-signed-sections.c
index e1980168..d88b4d64 100644
--- a/testing/proofs/cbmc/upatch-signed-sections.c
+++ b/testing/proofs/cbmc/upatch-signed-sections.c
@@ -30,7 +30,7 @@ static bool kept_any(const struct upatch *u)
{
return u->signed_name || u->signed_attach ||
u->signed_unload_payload || u->signed_policy_labels ||
- u->decoder_blob;
+ u->decoder_blob || u->signed_type || u->signed_jar;
}
static void forgotten(const struct upatch *u)
@@ -39,7 +39,8 @@ static void forgotten(const struct upatch *u)
!u->signed_policy_labels_len &&
!u->decoder_blob_len && !u->nr_decoders &&
!u->signed_policy_map_idx &&
- !u->signed_socket_policy_map_idx,
+ !u->signed_socket_policy_map_idx &&
+ !u->signed_jar_len,
"no length, count or map without a section");
}
@@ -90,6 +91,13 @@ void harness(void)
below(&u, u.signed_unload_payload,
UPATCH_PAYLOAD_HASH_LEN, dir_off),
"an unload digest below the directory");
+ __CPROVER_assert(!u.signed_type ||
+ below(&u, u.signed_type, sizeof(__le32), dir_off),
+ "a payload type below the directory");
+ __CPROVER_assert(!u.signed_jar ||
+ (u.signed_jar_len &&
+ below(&u, u.signed_jar, u.signed_jar_len, dir_off)),
+ "a jar below the directory");
__CPROVER_assert(!u.signed_policy_labels, "no policy labels");
__CPROVER_assert(!u.decoder_blob ||
(u.nr_decoders &&
diff --git a/testing/unit/upatch.c b/testing/unit/upatch.c
index 97d36812..c326c428 100644
--- a/testing/unit/upatch.c
+++ b/testing/unit/upatch.c
@@ -3,6 +3,7 @@
#include <errno.h>
#include <limits.h>
+#include <stddef.h>
#include <string.h>
#include "unit.h"
@@ -345,6 +346,105 @@ static void xdp_programs_select_disjoint_classes(void)
EXPECT_FALSE(specs);
}
+struct java_fixture {
+ struct upatch_header header;
+ __u8 signature[8];
+ __u8 jar[8];
+ char name[UPATCH_NAME_MAX];
+ __le32 type;
+ __u8 pad[4];
+ struct upatch_signed_section dir[3];
+ struct upatch_signed_trailer trailer;
+};
+
+#define JAVA_DATA_OFF offsetof(struct java_fixture, jar)
+
+static void java_section(struct java_fixture *f, __u32 i, __u32 tag,
+ size_t off, __u32 len)
+{
+ f->dir[i].tag = cpu_to_le32(tag);
+ f->dir[i].off = cpu_to_le32(off - JAVA_DATA_OFF);
+ f->dir[i].len = cpu_to_le32(len);
+}
+
+static void java_load(struct java_fixture *f, __u32 signed_type)
+{
+ memset(f, 0, sizeof(*f));
+ f->header.magic = cpu_to_le64(UPATCH_MAGIC);
+ f->header.version = cpu_to_le32(UPATCH_VERSION);
+ f->header.type = cpu_to_le32(UPATCH_TYPE_JAVA);
+ memcpy(f->header.name, "agent", sizeof("agent"));
+ f->header.signature_off =
+ cpu_to_le32(offsetof(struct java_fixture, signature));
+ f->header.signature_len = cpu_to_le32(sizeof(f->signature));
+ f->header.data_off = cpu_to_le32(JAVA_DATA_OFF);
+ f->header.data_len = cpu_to_le32(sizeof(*f) - JAVA_DATA_OFF);
+ memcpy(f->jar, "PK\x03\x04jar", sizeof(f->jar));
+ memcpy(f->name, "agent", sizeof("agent"));
+ f->type = cpu_to_le32(signed_type);
+ java_section(f, 0, UPATCH_SECTION_NAME,
+ offsetof(struct java_fixture, name), sizeof(f->name));
+ java_section(f, 1, UPATCH_SECTION_JAR,
+ offsetof(struct java_fixture, jar), sizeof(f->jar));
+ java_section(f, 2, UPATCH_SECTION_TYPE,
+ offsetof(struct java_fixture, type), sizeof(f->type));
+ f->trailer.magic = cpu_to_le32(UPATCH_SIGNED_TRAILER_MAGIC);
+ f->trailer.version = cpu_to_le32(UPATCH_SIGNED_VERSION);
+ f->trailer.dir_off =
+ cpu_to_le32(offsetof(struct java_fixture, dir) - JAVA_DATA_OFF);
+ f->trailer.nr_sections = cpu_to_le32(3);
+}
+
+static void java_load_opens_with_its_jar(void)
+{
+ struct java_fixture f;
+ struct upatch u;
+ const void *jar;
+ __u32 len;
+
+ java_load(&f, UPATCH_TYPE_JAVA);
+ ASSERT_EQ(upatch_open_memory(&f, sizeof(f), &u), 0);
+ EXPECT_STREQ(upatch_type_str(&u), "java");
+ EXPECT_UEQ(upatch_signed_type(&u), UPATCH_TYPE_JAVA);
+ EXPECT_TRUE(upatch_name_signed(&u));
+ jar = upatch_signed_jar(&u, &len);
+ EXPECT_UEQ(len, sizeof(f.jar));
+ EXPECT_TRUE(jar && !memcmp(jar, f.jar, sizeof(f.jar)));
+ upatch_close(&u);
+}
+
+static void java_header_needs_the_signed_type(void)
+{
+ struct java_fixture f;
+ struct upatch u;
+
+ java_load(&f, UPATCH_TYPE_BPF);
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+ java_load(&f, UPATCH_TYPE_JAVA);
+ f.trailer.nr_sections = cpu_to_le32(2);
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+ java_load(&f, UPATCH_TYPE_JAVA);
+ f.header.type = cpu_to_le32(3);
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+}
+
+static void java_header_carries_no_loader(void)
+{
+ struct java_fixture f;
+ struct upatch u;
+
+ java_load(&f, UPATCH_TYPE_JAVA);
+ f.header.loader_insns_off = f.header.signature_off;
+ f.header.loader_insns_len = cpu_to_le32(8);
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+ java_load(&f, UPATCH_TYPE_JAVA);
+ f.header.nr_progs = cpu_to_le32(1);
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+ java_load(&f, UPATCH_TYPE_JAVA);
+ f.header.signature_len = 0;
+ EXPECT_EQ(upatch_open_memory(&f, sizeof(f), &u), -EINVAL);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(prog_specs_take_the_signed_counts),
UNIT_CASE(prog_specs_refuse_another_header_count),
@@ -360,6 +460,9 @@ static const struct unit_case cases[] = {
UNIT_CASE(xdp_classes_come_from_xdp_programs_alone),
UNIT_CASE(xdp_masks_are_signed_and_bounded),
UNIT_CASE(xdp_programs_select_disjoint_classes),
+ UNIT_CASE(java_load_opens_with_its_jar),
+ UNIT_CASE(java_header_needs_the_signed_type),
+ UNIT_CASE(java_header_carries_no_loader),
{}
};
diff --git a/util/upatch.c b/util/upatch.c
index 276f0bd5..59386f28 100644
--- a/util/upatch.c
+++ b/util/upatch.c
@@ -94,26 +94,47 @@ static int section_inside(__u32 off, __u32 len, size_t n)
return 1;
}
-static int validate_bounds(const struct upatch_host_header *h,
- size_t n)
+static int validate_bpf_bounds(const struct upatch_host_header *h,
+ size_t n)
{
- if (h->type != UPATCH_TYPE_BPF)
- return -EINVAL;
if (h->loader_insns_off < UPATCH_HEADER_SIZE ||
h->loader_insns_off % 8 || h->loader_insns_len % 8 ||
!section_inside(h->loader_insns_off, h->loader_insns_len, n))
return -EINVAL;
+ if (h->rodata_data_off > h->data_len || h->loader_insns_len == 0)
+ return -EINVAL;
+ return 0;
+}
+
+/* A Java payload is all data: none of the loader's fields apply. */
+static int validate_java_bounds(const struct upatch_host_header *h)
+{
+ if (h->loader_insns_off || h->loader_insns_len || h->metadata_len ||
+ h->log_buf_sz || h->nr_maps || h->nr_progs ||
+ h->rodata_data_off || h->data_len == 0)
+ return -EINVAL;
+ return 0;
+}
+
+static int validate_bounds(const struct upatch_host_header *h,
+ size_t n)
+{
if (!section_inside(h->signature_off, h->signature_len, n))
return -EINVAL;
if (!section_inside(h->metadata_off, h->metadata_len, n))
return -EINVAL;
if (!section_inside(h->data_off, h->data_len, n))
return -EINVAL;
- if (h->rodata_data_off > h->data_len)
+ if (h->signature_len == 0)
return -EINVAL;
- if (h->loader_insns_len == 0 || h->signature_len == 0)
+ switch (h->type) {
+ case UPATCH_TYPE_BPF:
+ return validate_bpf_bounds(h, n);
+ case UPATCH_TYPE_JAVA:
+ return validate_java_bounds(h);
+ default:
return -EINVAL;
- return 0;
+ }
}
static bool has_nul(const char *s, size_t n)
@@ -224,6 +245,9 @@ static void forget_signed_sections(struct upatch *u)
u->signed_policy_labels_len = 0;
u->signed_policy_map_idx = 0;
u->signed_socket_policy_map_idx = 0;
+ u->signed_type = NULL;
+ u->signed_jar = NULL;
+ u->signed_jar_len = 0;
}
/*
@@ -301,6 +325,19 @@ static void parse_signed_sections(struct upatch *u)
u->signed_policy_labels = data + off;
u->signed_policy_labels_len = len;
break;
+ case UPATCH_SECTION_TYPE:
+ bit = 32;
+ if (len != sizeof(__le32))
+ goto reject;
+ u->signed_type = data + off;
+ break;
+ case UPATCH_SECTION_JAR:
+ bit = 64;
+ if (len == 0)
+ goto reject;
+ u->signed_jar = data + off;
+ u->signed_jar_len = len;
+ break;
default:
goto reject;
}
@@ -331,6 +368,14 @@ static int upatch_parse(const void *data, size_t size,
if (ret)
goto err;
parse_signed_sections(out);
+ /*
+ * The header is not signed: a type it names that the signed
+ * sections do not would route the bytes to the wrong verifier.
+ */
+ if (upatch_signed_type(out) != out->hdr.type) {
+ ret = -EINVAL;
+ goto err;
+ }
return 0;
err:
memset(out, 0, sizeof(*out));
@@ -503,7 +548,20 @@ bool upatch_is_unload(const struct upatch *u)
const char *upatch_type_str(const struct upatch *u)
{
- return "bpf";
+ return u->hdr.type == UPATCH_TYPE_JAVA ? "java" : "bpf";
+}
+
+__u32 upatch_signed_type(const struct upatch *u)
+{
+ if (!u->map || !u->signed_type)
+ return UPATCH_TYPE_BPF;
+ return get_le32(u->signed_type);
+}
+
+const void *upatch_signed_jar(const struct upatch *u, __u32 *len)
+{
+ *len = u->map ? u->signed_jar_len : 0;
+ return u->map ? u->signed_jar : NULL;
}
bool upatch_has_decoders(const struct upatch *u)
diff --git a/util/upatch.h b/util/upatch.h
index 94bb673c..fb446f8e 100644
--- a/util/upatch.h
+++ b/util/upatch.h
@@ -58,6 +58,9 @@ struct upatch {
__u32 signed_policy_labels_len;
__u32 signed_policy_map_idx;
__u32 signed_socket_policy_map_idx;
+ const __u8 *signed_type; /* __le32 */
+ const __u8 *signed_jar;
+ __u32 signed_jar_len;
};
struct upatch_decoder_view {
@@ -134,6 +137,13 @@ const __u8 *upatch_signed_unload_payload(const struct upatch *u);
const void *upatch_signed_policy_labels(const struct upatch *u,
__u32 *len);
+/*
+ * The payload type the signed sections carry, UPATCH_TYPE_BPF where they
+ * carry none. A file opens only when it equals the header's type.
+ */
+__u32 upatch_signed_type(const struct upatch *u);
+/* The agent .jar of a Java load, NULL where there is none. */
+const void *upatch_signed_jar(const struct upatch *u, __u32 *len);
/*
* An upatch name: [A-Za-z0-9._-]+ not starting with '.', so nothing that