eBPF-based security platform
bpm: declare upatch policy labels in Beefile
John Fastabend817eb8c40daf
diff --git a/Documentation/reference/formats/upatch-format.rst b/Documentation/reference/formats/upatch-format.rstindex c5ba8838..25e1c357 100644--- a/Documentation/reference/formats/upatch-format.rst+++ b/Documentation/reference/formats/upatch-format.rst@@ -295,9 +295,14 @@ Section tags- variable- Signed decoders, see `DCDB section`_. Present in a load artifactwhen its programs declare decoders.+ * - ``SHPL``+ - ``0x4c504853``+ - variable+ - Signed policy labels, each naming a policy ID, one-hot bit and+ absolute file path.The writer emits ``NAME``, then ``UNLD`` for an unload directive, or-``UPGM`` and an optional ``DCDB`` for a load artifact.+``UPGM`` and optional ``DCDB`` and ``SHPL`` sections for a load artifact.UPGM section~~~~~~~~~~~~@@ -653,7 +658,7 @@ unload later refuses for the section it lacks, andand the trailer.#. Each entry has ``reserved`` 0 and a section that ends at or before``dir_off``.-#. Each tag is ``NAME``, ``UPGM``, ``UNLD`` or ``DCDB``, and appears at most+#. Each tag is ``NAME``, ``UPGM``, ``UNLD``, ``DCDB`` or ``SHPL``, and appears at mostonce.#. ``NAME`` is 256 bytes and contains a NUL. ``UPGM`` is not empty.``UNLD`` is 32 bytes.diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rstindex e1737978..35840456 100644--- a/Documentation/upatches/publish/beefile.rst+++ b/Documentation/upatches/publish/beefile.rst@@ -29,9 +29,10 @@ A Beefile is a small subset of TOML, read line by line.line. A ``"..."`` string takes the escapes ``\n``, ``\r``, ``\t``,``\\`` and ``\"``; a ``'...'`` string takes none.- ``#`` starts a comment outside a string.-- There are no numbers, booleans, inline tables or values that span- lines.-- ``[[upatches.rodata]]`` and the ``[upatches.tree]`` tables belong to the+- Numeric values are accepted for policy bit masks; booleans, inline+ tables and values that span lines are not supported.+- ``[[upatches.rodata]]``, ``[[upatches.policy]]`` and the+ ``[upatches.tree]`` tables belong to the``[[upatches]]`` entry above them. Write an entry's own keys before itsfirst sub-table: a key after ``[upatches.tree]`` belongs to that table.@@ -202,6 +203,25 @@ is ``watch``. ``--rodata-input <name>=<path>`` replaces the input of everyentry with that name for one build. An override for a name that no entryhas fails. Entries that share a name must name the same input.+``[[upatches.policy]]``+~~~~~~~~~~~~~~~~~~~~~~~++A policy table declares a named bit and the absolute paths whose+``security.bpf.shirudo.policy`` xattrs this upatch labels. The builder+embeds the entries in the signed upatch metadata.++.. code-block:: toml++ [[upatches.policy]]+ id = "apache"+ bit = 0x1+ paths = ["/usr/local/apache", "/usr/bin/apache"]++``id`` maps to one one-hot 64-bit mask. Repeating the same ID and mask in+other upatches extends that policy; an ID cannot change masks, and two+different IDs cannot use the same mask in one package. Paths may name+non-executable files.+The generator and the updater run on the operator host with theoperator's rights. The target receives only the signed result. Without``-f``, ``bpm config update`` takes the Beefile and the updater from thediff --git a/testing/fuzz/fuzz-beefile.c b/testing/fuzz/fuzz-beefile.cindex c714ab0c..4757a20e 100644--- a/testing/fuzz/fuzz-beefile.c+++ b/testing/fuzz/fuzz-beefile.c@@ -45,6 +45,8 @@ const char *build_locate_vmlinux(const char *override)int build_package_load(const char *bpf_o, const char *src_path,const char *out_upatch, const char *upatch_name,+ const struct shirudo_upatch_policy_label_entry *policies,+ __u32 nr_policies,const char *pkey_path, const char *cert_path){return -ENOSYS;diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 6eff06f1..9fafd7d5 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -363,6 +363,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)struct bpm_build_upatch *upatch = NULL, *tree_owner = NULL;struct bpm_build_rodata *rodata = NULL;struct tree_selector *sel = NULL;+ struct bpm_build_policy *policy = NULL;struct bpm_build_file *file = NULL;bool tree_section = false;char *save = NULL, *line;@@ -394,6 +395,7 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)file = NULL;upatch = NULL;rodata = NULL;+ policy = NULL;tree_section = !array &&(!strcmp(section, "upatches.tree") ||!strcmp(section, "upatches.tree.selector") ||@@ -420,6 +422,18 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)return -ENOMEM;}rodata = &tree_owner->rodata[tree_owner->n_rodata++];+ } else if (array && !strcmp(section, "upatches.policy")) {+ if (!tree_owner ||+ tree_owner->n_policies >= BPM_BUILD_MAX_POLICIES)+ return -E2BIG;+ if (!tree_owner->policies) {+ tree_owner->policies =+ xcalloc(BPM_BUILD_MAX_POLICIES,+ sizeof(*tree_owner->policies));+ if (!tree_owner->policies)+ return -ENOMEM;+ }+ policy = &tree_owner->policies[tree_owner->n_policies++];} else if (array) {return -EINVAL;} else if (tree_section) {@@ -498,6 +512,33 @@ static int build_parse_spec(struct bpm_build_spec *spec, char *body)}if (build_parse_string(value, dst, cap))return -EINVAL;+ } else if (policy) {+ if (!strcmp(key, "id")) {+ if (build_parse_string(value, policy->id,+ sizeof(policy->id)))+ return -EINVAL;+ } else if (!strcmp(key, "bit")) {+ const char *number = value;+ size_t len = strlen(value);++ if (len > 2 && value[0] == '0' &&+ (value[1] == 'x' || value[1] == 'X')) {+ number += 2;+ len -= 2;+ }+ if (!len || parse_x64(number, len, UINT64_MAX, &policy->bit) ||+ !policy->bit ||+ (policy->bit & (policy->bit - 1)))+ return -EINVAL;+ } else if (!strcmp(key, "paths")) {+ if (policy->n_paths ||+ build_parse_string_array(value, policy->paths,+ ARRAY_SIZE(policy->paths),+ &policy->n_paths))+ return -EINVAL;+ } else {+ return -EINVAL;+ }} else if (rodata) {if (!strcmp(key, "symbol")) {if (build_parse_string(value, rodata->symbol,@@ -651,11 +692,17 @@ void bpm_build_spec_free(struct bpm_build_spec *spec)if (!spec)return;for (size_t i = 0; i < spec->n_upatches; i++) {- xfree(spec->upatches[i].tree.selector,- sizeof(*spec->upatches[i].tree.selector));- xfree(spec->upatches[i].rodata,- BPM_BUILD_MAX_RODATA *- sizeof(*spec->upatches[i].rodata));+ if (spec->upatches[i].tree.selector)+ xfree(spec->upatches[i].tree.selector,+ sizeof(*spec->upatches[i].tree.selector));+ if (spec->upatches[i].rodata)+ xfree(spec->upatches[i].rodata,+ BPM_BUILD_MAX_RODATA *+ sizeof(*spec->upatches[i].rodata));+ if (spec->upatches[i].policies)+ xfree(spec->upatches[i].policies,+ BPM_BUILD_MAX_POLICIES *+ sizeof(*spec->upatches[i].policies));}xfree(spec->build_file, spec->build_file_len + 1);xfree(spec, sizeof(*spec));@@ -996,6 +1043,28 @@ static int __build_validate(struct bpm_build_spec *spec,return -EINVAL;}}+ for (size_t j = 0; j < u->n_policies; j++) {+ struct bpm_build_policy *p = &u->policies[j];++ if (!p->id[0] || !p->bit || !p->n_paths ||+ (p->bit & (p->bit - 1)))+ return -EINVAL;+ for (int path = 0; path < p->n_paths; path++)+ if (p->paths[path][0] != '/')+ return -EINVAL;+ for (size_t x = 0; x <= i; x++) {+ struct bpm_build_upatch *other = &spec->upatches[x];+ size_t limit = x == i ? j : other->n_policies;++ for (size_t y = 0; y < limit; y++) {+ struct bpm_build_policy *q = &other->policies[y];++ if ((!strcmp(p->id, q->id) && p->bit != q->bit) ||+ (strcmp(p->id, q->id) && p->bit == q->bit))+ return -EEXIST;+ }+ }+ }}return 0;}@@ -1182,6 +1251,8 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,__u8 payload[CRYPTO_SHA256_LEN];struct upatch parsed = {};int ret;+ struct shirudo_upatch_policy_label_entry *labels = NULL;+ __u32 nr_labels = 0;if (build_resolve_source(spec, u->source, source, sizeof(source),&(struct stat){}))@@ -1219,8 +1290,30 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,return -EINVAL;}build_upatch_name(source, name, sizeof(name));- ret = build_package_load(bpf_o, src_path, load, name, pkey,- cert);+ for (size_t j = 0; j < u->n_policies; j++)+ nr_labels += u->policies[j].n_paths;+ if (nr_labels) {+ labels = xcalloc(nr_labels, sizeof(*labels));+ if (!labels)+ return -ENOMEM;+ nr_labels = 0;+ for (size_t j = 0; j < u->n_policies; j++) {+ struct bpm_build_policy *p = &u->policies[j];++ for (int k = 0; k < p->n_paths; k++) {+ struct shirudo_upatch_policy_label_entry *e =+ &labels[nr_labels++];++ e->bit = p->bit;+ strscpy(e->policy, p->id, sizeof(e->policy));+ strscpy(e->path, p->paths[k], sizeof(e->path));+ }+ }+ }+ ret = build_package_load(bpf_o, src_path, load, name,+ labels, nr_labels, pkey, cert);+ if (labels)+ xfree(labels, (size_t)nr_labels * sizeof(*labels));if (bpf_o == object)unlink(object);if (ret)diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.hindex 7ca03f10..aba694d6 100644--- a/tools/shirudo-build/bpm.h+++ b/tools/shirudo-build/bpm.h@@ -21,6 +21,14 @@#define BPM_BUILD_MAX_ENTRIES 128#define BPM_BUILD_MAX_RODATA 16#define BPM_BUILD_MAX_COMMAND_ARGS 16+#define BPM_BUILD_MAX_POLICIES 64++struct bpm_build_policy {+ char id[SHIRUDO_UPATCH_POLICY_NAME_MAX];+ char paths[16][TREE_STRING_MAX];+ int n_paths;+ __u64 bit;+};struct bpm_build_rodata {char symbol[128];@@ -67,6 +75,8 @@ struct bpm_build_upatch {struct bpm_build_tree tree;struct bpm_build_rodata *rodata;size_t n_rodata;+ struct bpm_build_policy *policies;+ size_t n_policies;};struct bpm_build_spec {diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.cindex e1228ae2..cf1effc0 100644--- a/tools/shirudo-build/package.c+++ b/tools/shirudo-build/package.c@@ -1523,6 +1523,7 @@ static void unregister_target_attach_types(int handler_ids[2])static int finalize_object(struct bpf_object *obj, const char *out_path,const char *upatch_name, __u32 flags,const __u8 *unload_payload, const char *src_path,+ const void *policy_labels, __u32 policy_labels_len,const char *pkey_path, const char *cert_path){LIBBPF_OPTS(gen_loader_opts, opts);@@ -1623,6 +1624,11 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,UPATCH_SECTION_DECODERS, decoder_blob,decoder_blob_len,};+ if (policy_labels_len)+ sections[nr_sections++] = (struct signed_section){+ SHIRUDO_UPATCH_SECTION_POLICY_LABELS,+ policy_labels, policy_labels_len,+ };}err = compose_signed_data(opts.data, (__u32)opts.data_sz, sections,nr_sections, &signed_data, &signed_data_len);@@ -1745,9 +1751,14 @@ static int check_upatch_name(const char *name)int build_package_load(const char *bpf_o, const char *src_path,const char *out_path, const char *upatch_name,- const char *pkey_path, const char *cert_path)+ const struct shirudo_upatch_policy_label_entry *policies,+ __u32 nr_policies,+ const char *pkey_path, const char *cert_path){struct bpf_object *obj;+ struct shirudo_upatch_policy_label_header *label_blob = NULL;+ struct shirudo_upatch_policy_label_entry *label_entries;+ __u32 label_len = 0;int handler_ids[2] = {-1, -1};int err;@@ -1768,8 +1779,27 @@ int build_package_load(const char *bpf_o, const char *src_path,unregister_target_attach_types(handler_ids);return err;}+ if (nr_policies) {+ label_len = sizeof(*label_blob) + nr_policies * sizeof(*label_entries);+ label_blob = xcalloc(1, label_len);+ if (!label_blob) {+ bpf_object__close(obj);+ unregister_target_attach_types(handler_ids);+ return -ENOMEM;+ }+ label_blob->magic = htole32(SHIRUDO_UPATCH_POLICY_LABELS_MAGIC);+ label_blob->version = htole32(SHIRUDO_UPATCH_POLICY_LABELS_VERSION);+ label_blob->count = htole32(nr_policies);+ label_entries = (void *)(label_blob + 1);+ for (__u32 i = 0; i < nr_policies; i++) {+ label_entries[i] = policies[i];+ label_entries[i].bit = htole64(policies[i].bit);+ }+ }err = finalize_object(obj, out_path, upatch_name, 0, NULL, src_path,- pkey_path, cert_path);+ label_blob, label_len, pkey_path, cert_path);+ if (label_blob)+ xfree(label_blob, label_len);unregister_target_attach_types(handler_ids);return err;}@@ -1843,5 +1873,5 @@ int build_package_unload(const char *upatch_name,*/return finalize_object(obj, out_upatch, upatch_name,DIRECTIVE_UNLOAD, payload_sha256,- NULL, pkey_path, cert_path);+ NULL, NULL, 0, pkey_path, cert_path);}diff --git a/tools/shirudo-build/package.h b/tools/shirudo-build/package.hindex 59883cbf..53e53c6d 100644--- a/tools/shirudo-build/package.h+++ b/tools/shirudo-build/package.h@@ -10,8 +10,12 @@#include <shirudo/misc.h>+struct shirudo_upatch_policy_label_entry;+int build_package_load(const char *bpf_o, const char *src_path,const char *out_upatch, const char *upatch_name,+ const struct shirudo_upatch_policy_label_entry *policies,+ __u32 nr_policies,const char *pkey_path, const char *cert_path);int build_package_unload(const char *name,diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 4e9c3f29..a28f5733 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -426,7 +426,7 @@ int main(int argc, char **argv)name = derive_name(input, name_buf, sizeof(name_buf));ret = build_package_load(bpf_o, src_path, output, name,- pkey, cert);+ NULL, 0, pkey, cert);if (temp_bpf_o) {unlink(temp_bpf_o);